|
| 1 | +package gotado |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "golang.org/x/oauth2" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCopyToken(t *testing.T) { |
| 11 | + t.Run("NilToken", func(t *testing.T) { |
| 12 | + copied := copyToken(nil) |
| 13 | + if copied != nil { |
| 14 | + t.Error("Expected nil for nil input") |
| 15 | + } |
| 16 | + }) |
| 17 | + |
| 18 | + t.Run("BasicFields", func(t *testing.T) { |
| 19 | + expiry := time.Now().Add(1 * time.Hour) |
| 20 | + original := &oauth2.Token{ |
| 21 | + AccessToken: "test_access_token", |
| 22 | + TokenType: "Bearer", |
| 23 | + RefreshToken: "test_refresh_token", |
| 24 | + Expiry: expiry, |
| 25 | + } |
| 26 | + |
| 27 | + copied := copyToken(original) |
| 28 | + |
| 29 | + // Verify it's a different object |
| 30 | + if original == copied { |
| 31 | + t.Error("Expected different pointer, got same object") |
| 32 | + } |
| 33 | + |
| 34 | + // Verify all fields are copied |
| 35 | + if copied.AccessToken != original.AccessToken { |
| 36 | + t.Errorf("AccessToken mismatch: got %v, want %v", copied.AccessToken, original.AccessToken) |
| 37 | + } |
| 38 | + if copied.TokenType != original.TokenType { |
| 39 | + t.Errorf("TokenType mismatch: got %v, want %v", copied.TokenType, original.TokenType) |
| 40 | + } |
| 41 | + if copied.RefreshToken != original.RefreshToken { |
| 42 | + t.Errorf("RefreshToken mismatch: got %v, want %v", copied.RefreshToken, original.RefreshToken) |
| 43 | + } |
| 44 | + if !copied.Expiry.Equal(original.Expiry) { |
| 45 | + t.Errorf("Expiry mismatch: got %v, want %v", copied.Expiry, original.Expiry) |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("ExpiresInField", func(t *testing.T) { |
| 50 | + original := &oauth2.Token{ |
| 51 | + AccessToken: "test_access_token", |
| 52 | + ExpiresIn: 600, |
| 53 | + } |
| 54 | + |
| 55 | + copied := copyToken(original) |
| 56 | + |
| 57 | + if copied.ExpiresIn != original.ExpiresIn { |
| 58 | + t.Errorf("ExpiresIn mismatch: got %v, want %v", copied.ExpiresIn, original.ExpiresIn) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("WithExtraFields", func(t *testing.T) { |
| 63 | + extraMap := map[string]interface{}{ |
| 64 | + "scope": "read write", |
| 65 | + "custom_field": "custom_value", |
| 66 | + "number": float64(123), |
| 67 | + } |
| 68 | + |
| 69 | + original := (&oauth2.Token{ |
| 70 | + AccessToken: "test_access_token", |
| 71 | + TokenType: "Bearer", |
| 72 | + RefreshToken: "test_refresh_token", |
| 73 | + }).WithExtra(extraMap) |
| 74 | + |
| 75 | + copied := copyToken(original) |
| 76 | + |
| 77 | + // Verify extra fields are NOT copied (limitation of simple copy) |
| 78 | + if copied.Extra("scope") != nil { |
| 79 | + t.Errorf("Extra 'scope' should be nil (not copied), got %v", copied.Extra("scope")) |
| 80 | + } |
| 81 | + if copied.Extra("custom_field") != nil { |
| 82 | + t.Errorf("Extra 'custom_field' should be nil (not copied), got %v", copied.Extra("custom_field")) |
| 83 | + } |
| 84 | + if copied.Extra("number") != nil { |
| 85 | + t.Errorf("Extra 'number' should be nil (not copied), got %v", copied.Extra("number")) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("IndependentCopy", func(t *testing.T) { |
| 90 | + original := &oauth2.Token{ |
| 91 | + AccessToken: "original_access", |
| 92 | + TokenType: "Bearer", |
| 93 | + RefreshToken: "original_refresh", |
| 94 | + } |
| 95 | + |
| 96 | + copied := copyToken(original) |
| 97 | + |
| 98 | + // Modify the original |
| 99 | + original.AccessToken = "modified_access" |
| 100 | + original.RefreshToken = "modified_refresh" |
| 101 | + |
| 102 | + // Verify the copy is not affected |
| 103 | + if copied.AccessToken != "original_access" { |
| 104 | + t.Errorf("Copy was affected by original modification: got %v, want %v", copied.AccessToken, "original_access") |
| 105 | + } |
| 106 | + if copied.RefreshToken != "original_refresh" { |
| 107 | + t.Errorf("Copy was affected by original modification: got %v, want %v", copied.RefreshToken, "original_refresh") |
| 108 | + } |
| 109 | + }) |
| 110 | +} |
0 commit comments