Debian Codegraph @

pkg/oidc/userinfo_test.go

raw
1 package oidc
2
3 import (
4 "encoding/json"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestUserInfo_AppendClaims(t *testing.T) {
11 u := new(UserInfo)
12 u.AppendClaims("a", "b")
13 want := map[string]any{"a": "b"}
14 assert.Equal(t, want, u.Claims)
15
16 u.AppendClaims("d", "e")
17 want["d"] = "e"
18 assert.Equal(t, want, u.Claims)
19 }
20
21 func TestUserInfo_GetAddress(t *testing.T) {
22 // nil address
23 u := new(UserInfo)
24 assert.Equal(t, &UserInfoAddress{}, u.GetAddress())
25
26 u.Address = &UserInfoAddress{PostalCode: "1234"}
27 assert.Equal(t, u.Address, u.GetAddress())
28 }
29
30 func TestUserInfoMarshal(t *testing.T) {
31 userinfo := &UserInfo{
32 Subject: "test",
33 Address: &UserInfoAddress{
34 StreetAddress: "Test 789\nPostfach 2",
35 },
36 UserInfoEmail: UserInfoEmail{
37 Email: "test",
38 EmailVerified: true,
39 },
40 UserInfoPhone: UserInfoPhone{
41 PhoneNumber: "0791234567",
42 PhoneNumberVerified: true,
43 },
44 UserInfoProfile: UserInfoProfile{
45 Name: "Test",
46 },
47 Claims: map[string]any{"private_claim": "test"},
48 }
49
50 marshal, err := json.Marshal(userinfo)
51 assert.NoError(t, err)
52
53 out := new(UserInfo)
54 assert.NoError(t, json.Unmarshal(marshal, out))
55 expected, err := json.Marshal(out)
56
57 assert.NoError(t, err)
58 assert.Equal(t, expected, marshal)
59
60 out2 := new(UserInfo)
61 assert.NoError(t, json.Unmarshal(expected, out2))
62 assert.Equal(t, out, out2)
63 }
64
65 // TestUserInfoVerifiedFieldsUnmarshal ensures email_verified and phone_number_verified
66 // handle both standard booleans and AWS Cognito's non-compliant strings.
67 func TestUserInfoVerifiedFieldsUnmarshal(t *testing.T) {
68 t.Parallel()
69
70 tests := []struct {
71 name string
72 json string
73 wantEmailVerified Bool
74 wantPhoneVerified Bool
75 }{
76 {
77 name: "booleans true",
78 json: `{"email_verified": true, "phone_number_verified": true}`,
79 wantEmailVerified: true,
80 wantPhoneVerified: true,
81 },
82 {
83 name: "booleans false",
84 json: `{"email_verified": false, "phone_number_verified": false}`,
85 wantEmailVerified: false,
86 wantPhoneVerified: false,
87 },
88 {
89 name: "strings true",
90 json: `{"email_verified": "true", "phone_number_verified": "true"}`,
91 wantEmailVerified: true,
92 wantPhoneVerified: true,
93 },
94 {
95 name: "strings false",
96 json: `{"email_verified": "false", "phone_number_verified": "false"}`,
97 wantEmailVerified: false,
98 wantPhoneVerified: false,
99 },
100 {
101 name: "mixed bool/string",
102 json: `{"email_verified": true, "phone_number_verified": "false"}`,
103 wantEmailVerified: true,
104 wantPhoneVerified: false,
105 },
106 }
107
108 for _, tt := range tests {
109 t.Run(tt.name, func(t *testing.T) {
110 var got UserInfo
111 err := json.Unmarshal([]byte(tt.json), &got)
112 assert.NoError(t, err)
113 assert.Equal(t, tt.wantEmailVerified, got.EmailVerified)
114 assert.Equal(t, tt.wantPhoneVerified, got.PhoneNumberVerified)
115 })
116 }
117 }
118
119 // TestBoolUnmarshal verifies the Bool type handles various inputs correctly.
120 func TestBoolUnmarshal(t *testing.T) {
121 t.Parallel()
122
123 tests := []struct {
124 name string
125 input string
126 want Bool
127 wantErr bool
128 }{
129 {
130 name: "bool true",
131 input: `true`,
132 want: true,
133 },
134 {
135 name: "bool false",
136 input: `false`,
137 want: false,
138 },
139 {
140 name: "string true",
141 input: `"true"`,
142 want: true,
143 },
144 {
145 name: "string false",
146 input: `"false"`,
147 want: false,
148 },
149 {
150 name: "invalid string",
151 input: `"yes"`,
152 wantErr: true,
153 },
154 {
155 name: "number",
156 input: `1`,
157 wantErr: true,
158 },
159 }
160
161 for _, tt := range tests {
162 t.Run(tt.name, func(t *testing.T) {
163 var got Bool
164 err := json.Unmarshal([]byte(tt.input), &got)
165 if tt.wantErr {
166 assert.Error(t, err)
167 return
168 }
169 assert.NoError(t, err)
170 assert.Equal(t, tt.want, got)
171 })
172 }
173 }