forked from oauth2-proxy/mockoidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_test.go
165 lines (148 loc) · 4.09 KB
/
user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mockoidc_test
import (
"encoding/json"
"testing"
"github.com/golang-jwt/jwt"
"github.com/oauth2-proxy/mockoidc"
"github.com/stretchr/testify/assert"
)
func TestMockUser_Userinfo(t *testing.T) {
testUser := mockoidc.DefaultUser()
testCases := map[string]struct {
Scope []string
ExpectedEmail string
ExpectedPhone string
ExpectedGroups []string
}{
"all scopes": {
Scope: []string{"openid", "email", "profile", "groups"},
ExpectedEmail: testUser.Email,
ExpectedPhone: testUser.Phone,
ExpectedGroups: testUser.Groups,
},
"missing groups scope": {
Scope: []string{"openid", "email", "profile"},
ExpectedEmail: testUser.Email,
ExpectedPhone: testUser.Phone,
ExpectedGroups: nil,
},
"missing profile scope": {
Scope: []string{"openid", "email", "groups"},
ExpectedEmail: testUser.Email,
ExpectedPhone: "",
ExpectedGroups: testUser.Groups,
},
"missing email scope": {
Scope: []string{"openid", "profile", "groups"},
ExpectedEmail: "",
ExpectedPhone: testUser.Phone,
ExpectedGroups: testUser.Groups,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
payload, err := testUser.Userinfo(tc.Scope)
assert.NoError(t, err)
data := make(map[string]interface{})
err = json.Unmarshal(payload, &data)
assert.NoError(t, err)
if tc.ExpectedEmail == "" {
assert.Nil(t, data["email"])
} else {
assert.Equal(t, tc.ExpectedEmail, data["email"])
}
if tc.ExpectedPhone == "" {
assert.Nil(t, data["phone_number"])
} else {
assert.Equal(t, tc.ExpectedPhone, data["phone_number"])
}
var groups []string
if data["groups"] != nil {
for _, group := range data["groups"].([]interface{}) {
groups = append(groups, group.(string))
}
}
assert.Equal(t, tc.ExpectedGroups, groups)
})
}
}
func TestMockUser_Claims(t *testing.T) {
keypair, err := mockoidc.RandomKeypair(1024)
assert.NoError(t, err)
testUser := mockoidc.DefaultUser()
testCases := map[string]struct {
Scope []string
Nonce string
ExpectedEmail string
ExpectedPhone string
ExpectedGroups []string
}{
"all scopes": {
Scope: []string{"openid", "email", "profile", "groups"},
Nonce: "1234987",
ExpectedEmail: testUser.Email,
ExpectedPhone: testUser.Phone,
ExpectedGroups: testUser.Groups,
},
"missing groups scope": {
Scope: []string{"openid", "email", "profile"},
Nonce: "3948y2tiugiu",
ExpectedEmail: testUser.Email,
ExpectedPhone: testUser.Phone,
ExpectedGroups: nil,
},
"missing profile scope": {
Scope: []string{"openid", "email", "groups"},
Nonce: "",
ExpectedEmail: testUser.Email,
ExpectedPhone: "",
ExpectedGroups: testUser.Groups,
},
"missing email scope": {
Scope: []string{"openid", "profile", "groups"},
Nonce: "",
ExpectedEmail: "",
ExpectedPhone: testUser.Phone,
ExpectedGroups: testUser.Groups,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
base := &mockoidc.IDTokenClaims{
Nonce: tc.Nonce,
}
claims, err := testUser.Claims(tc.Scope, base)
assert.NoError(t, err)
tokenStr, err := keypair.SignJWT(claims)
assert.NoError(t, err)
token, err := keypair.VerifyJWT(tokenStr)
assert.NoError(t, err)
assert.True(t, token.Valid)
data, ok := token.Claims.(jwt.MapClaims)
assert.True(t, ok)
assert.NotNil(t, claims)
if tc.Nonce == "" {
assert.Nil(t, data["nonce"])
} else {
assert.Equal(t, tc.Nonce, data["nonce"])
}
if tc.ExpectedEmail == "" {
assert.Nil(t, data["email"])
} else {
assert.Equal(t, tc.ExpectedEmail, data["email"])
}
if tc.ExpectedPhone == "" {
assert.Nil(t, data["phone_number"])
} else {
assert.Equal(t, tc.ExpectedPhone, data["phone_number"])
}
var groups []string
if data["groups"] != nil {
for _, group := range data["groups"].([]interface{}) {
groups = append(groups, group.(string))
}
}
assert.Equal(t, tc.ExpectedGroups, groups)
})
}
}