|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIdentity_String(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + testCases := []struct { |
| 15 | + name string |
| 16 | + identity *Identity |
| 17 | + contains []string |
| 18 | + notContains []string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "redacts_token", |
| 22 | + identity: &Identity{ |
| 23 | + |
| 24 | + Name: "Test User", |
| 25 | + |
| 26 | + Groups: []string{"admin", "users"}, |
| 27 | + Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.sensitive-token-data", |
| 28 | + TokenType: "Bearer", |
| 29 | + }, |
| 30 | + contains: []string{ |
| 31 | + |
| 32 | + "Name:\"Test User\"", |
| 33 | + "Token:REDACTED", |
| 34 | + "TokenType:\"Bearer\"", |
| 35 | + }, |
| 36 | + notContains: []string{ |
| 37 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9", |
| 38 | + "sensitive-token-data", |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "shows_empty_token", |
| 43 | + identity: &Identity{ |
| 44 | + |
| 45 | + Token: "", |
| 46 | + }, |
| 47 | + contains: []string{"Token:<empty>"}, |
| 48 | + notContains: []string{"REDACTED"}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "handles_nil_identity", |
| 52 | + identity: nil, |
| 53 | + contains: []string{"<nil>"}, |
| 54 | + notContains: []string{}, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tc := range testCases { |
| 59 | + t.Run(tc.name, func(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + |
| 62 | + result := tc.identity.String() |
| 63 | + |
| 64 | + for _, expected := range tc.contains { |
| 65 | + assert.Contains(t, result, expected) |
| 66 | + } |
| 67 | + |
| 68 | + for _, forbidden := range tc.notContains { |
| 69 | + assert.NotContains(t, result, forbidden) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestIdentity_MarshalJSON(t *testing.T) { |
| 76 | + t.Parallel() |
| 77 | + |
| 78 | + testCases := []struct { |
| 79 | + name string |
| 80 | + identity *Identity |
| 81 | + contains []string |
| 82 | + notContains []string |
| 83 | + }{ |
| 84 | + { |
| 85 | + name: "redacts_token", |
| 86 | + identity: &Identity{ |
| 87 | + |
| 88 | + Name: "Test User", |
| 89 | + |
| 90 | + Groups: []string{"admin", "users"}, |
| 91 | + Token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.sensitive-token-data", |
| 92 | + TokenType: "Bearer", |
| 93 | + }, |
| 94 | + contains: []string{ |
| 95 | + |
| 96 | + `"name":"Test User"`, |
| 97 | + |
| 98 | + `"token":"REDACTED"`, |
| 99 | + `"tokenType":"Bearer"`, |
| 100 | + }, |
| 101 | + notContains: []string{ |
| 102 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9", |
| 103 | + "sensitive-token-data", |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "preserves_empty_token", |
| 108 | + identity: &Identity{ |
| 109 | + |
| 110 | + Token: "", |
| 111 | + }, |
| 112 | + contains: [] string{ `"subject":"[email protected]"`}, |
| 113 | + notContains: []string{`"token":"REDACTED"`}, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "handles_nil_identity", |
| 117 | + identity: nil, |
| 118 | + contains: []string{"null"}, |
| 119 | + notContains: []string{}, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tc := range testCases { |
| 124 | + t.Run(tc.name, func(t *testing.T) { |
| 125 | + t.Parallel() |
| 126 | + |
| 127 | + data, err := json.Marshal(tc.identity) |
| 128 | + require.NoError(t, err) |
| 129 | + |
| 130 | + result := string(data) |
| 131 | + |
| 132 | + for _, expected := range tc.contains { |
| 133 | + assert.Contains(t, result, expected) |
| 134 | + } |
| 135 | + |
| 136 | + for _, forbidden := range tc.notContains { |
| 137 | + assert.NotContains(t, result, forbidden) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments