mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-06-06 23:46:28 +02:00
Add more UserInfo test cases
This commit is contained in:
parent
2549b722d3
commit
7407fbd3a7
@ -1124,24 +1124,67 @@ func NewUserInfoEndpointTest() (*ProcessCookieTest, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUserInfoEndpointAccepted(t *testing.T) {
|
func TestUserInfoEndpointAccepted(t *testing.T) {
|
||||||
test, err := NewUserInfoEndpointTest()
|
testCases := []struct {
|
||||||
if err != nil {
|
name string
|
||||||
t.Fatal(err)
|
session *sessions.SessionState
|
||||||
|
expectedResponse string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Full session",
|
||||||
|
session: &sessions.SessionState{
|
||||||
|
User: "john.doe",
|
||||||
|
Email: "john.doe@example.com",
|
||||||
|
Groups: []string{"example", "groups"},
|
||||||
|
AccessToken: "my_access_token",
|
||||||
|
},
|
||||||
|
expectedResponse: "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"]}\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Minimal session",
|
||||||
|
session: &sessions.SessionState{
|
||||||
|
User: "john.doe",
|
||||||
|
Email: "john.doe@example.com",
|
||||||
|
Groups: []string{"example", "groups"},
|
||||||
|
},
|
||||||
|
expectedResponse: "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"]}\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No groups",
|
||||||
|
session: &sessions.SessionState{
|
||||||
|
User: "john.doe",
|
||||||
|
Email: "john.doe@example.com",
|
||||||
|
AccessToken: "my_access_token",
|
||||||
|
},
|
||||||
|
expectedResponse: "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\"}\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "With Preferred Username",
|
||||||
|
session: &sessions.SessionState{
|
||||||
|
User: "john.doe",
|
||||||
|
PreferredUsername: "john",
|
||||||
|
Email: "john.doe@example.com",
|
||||||
|
Groups: []string{"example", "groups"},
|
||||||
|
AccessToken: "my_access_token",
|
||||||
|
},
|
||||||
|
expectedResponse: "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"],\"preferredUsername\":\"john\"}\n",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
startSession := &sessions.SessionState{
|
for _, tc := range testCases {
|
||||||
User: "john.doe",
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
Email: "john.doe@example.com",
|
test, err := NewUserInfoEndpointTest()
|
||||||
Groups: []string{"example", "groups"},
|
if err != nil {
|
||||||
AccessToken: "my_access_token",
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
err = test.SaveSession(startSession)
|
err = test.SaveSession(tc.session)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
test.proxy.ServeHTTP(test.rw, test.req)
|
test.proxy.ServeHTTP(test.rw, test.req)
|
||||||
assert.Equal(t, http.StatusOK, test.rw.Code)
|
assert.Equal(t, http.StatusOK, test.rw.Code)
|
||||||
bodyBytes, _ := ioutil.ReadAll(test.rw.Body)
|
bodyBytes, _ := ioutil.ReadAll(test.rw.Body)
|
||||||
assert.Equal(t, "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"]}\n", string(bodyBytes))
|
assert.Equal(t, tc.expectedResponse, string(bodyBytes))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUserInfoEndpointUnauthorizedOnNoCookieSetError(t *testing.T) {
|
func TestUserInfoEndpointUnauthorizedOnNoCookieSetError(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user