mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-03-25 22:00:56 +02:00
Adds tests for redeemCode and enrichSession
This commit is contained in:
parent
0bd8eb3191
commit
b6061f0803
@ -404,6 +404,78 @@ func (tp *TestProvider) ValidateSessionState(ctx context.Context, session *sessi
|
|||||||
return tp.ValidToken
|
return tp.ValidToken
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_redeemCode(t *testing.T) {
|
||||||
|
opts := baseTestOptions()
|
||||||
|
err := validation.Validate(opts)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
proxy, err := NewOAuthProxy(opts, func(string) bool { return true })
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = proxy.redeemCode(context.Background(), "www.example.com", "")
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_enrichSession(t *testing.T) {
|
||||||
|
const (
|
||||||
|
sessionUser = "Mr Session"
|
||||||
|
sessionEmail = "session@example.com"
|
||||||
|
providerEmail = "provider@example.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
testCases := map[string]struct {
|
||||||
|
session *sessions.SessionState
|
||||||
|
expectedUser string
|
||||||
|
expectedEmail string
|
||||||
|
}{
|
||||||
|
"Session already has enrichable fields": {
|
||||||
|
session: &sessions.SessionState{
|
||||||
|
User: sessionUser,
|
||||||
|
Email: sessionEmail,
|
||||||
|
},
|
||||||
|
expectedUser: sessionUser,
|
||||||
|
expectedEmail: sessionEmail,
|
||||||
|
},
|
||||||
|
"Session is missing Email and GetEmailAddress is implemented": {
|
||||||
|
session: &sessions.SessionState{
|
||||||
|
User: sessionUser,
|
||||||
|
},
|
||||||
|
expectedUser: sessionUser,
|
||||||
|
expectedEmail: providerEmail,
|
||||||
|
},
|
||||||
|
"Session is missing User and GetUserName is not implemented": {
|
||||||
|
session: &sessions.SessionState{
|
||||||
|
Email: sessionEmail,
|
||||||
|
},
|
||||||
|
expectedUser: "",
|
||||||
|
expectedEmail: sessionEmail,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range testCases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
opts := baseTestOptions()
|
||||||
|
err := validation.Validate(opts)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// intentionally set after validation.Validate(opts) since it will clobber
|
||||||
|
// our TestProvider and call `providers.New` defaulting to `providers.GoogleProvider`
|
||||||
|
opts.SetProvider(NewTestProvider(&url.URL{Host: "www.example.com"}, providerEmail))
|
||||||
|
proxy, err := NewOAuthProxy(opts, func(string) bool { return true })
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = proxy.enrichSession(context.Background(), tc.session)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tc.expectedUser, tc.session.User)
|
||||||
|
assert.Equal(t, tc.expectedEmail, tc.session.Email)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBasicAuthPassword(t *testing.T) {
|
func TestBasicAuthPassword(t *testing.T) {
|
||||||
providerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
providerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
logger.Printf("%#v", r)
|
logger.Printf("%#v", r)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user