This repository was archived by the owner on Apr 13, 2025. It is now read-only.
forked from oauth2-proxy/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider_default_test.go
121 lines (104 loc) · 2.84 KB
/
provider_default_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
package providers
import (
"context"
"net/url"
"testing"
"time"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
)
func TestRefresh(t *testing.T) {
p := &ProviderData{}
now := time.Unix(1234567890, 10)
expires := time.Unix(1234567890, 0)
ss := &sessions.SessionState{}
ss.Clock.Set(now)
ss.SetExpiresOn(expires)
refreshed, err := p.RefreshSession(context.Background(), ss)
assert.False(t, refreshed)
assert.Equal(t, ErrNotImplemented, err)
refreshed, err = p.RefreshSession(context.Background(), nil)
assert.False(t, refreshed)
assert.Equal(t, ErrNotImplemented, err)
}
func TestCodeChallengeConfigured(t *testing.T) {
p := &ProviderData{
LoginURL: &url.URL{
Scheme: "http",
Host: "my.test.idp",
Path: "/oauth/authorize",
},
}
extraValues := url.Values{}
extraValues["code_challenge"] = []string{"challenge"}
extraValues["code_challenge_method"] = []string{"method"}
result := p.GetLoginURL("https://my.test.app/oauth", "", "", extraValues)
assert.Contains(t, result, "code_challenge=challenge")
assert.Contains(t, result, "code_challenge_method=method")
}
func TestCodeChallengeNotConfigured(t *testing.T) {
p := &ProviderData{
LoginURL: &url.URL{
Scheme: "http",
Host: "my.test.idp",
Path: "/oauth/authorize",
},
}
result := p.GetLoginURL("https://my.test.app/oauth", "", "", url.Values{})
assert.NotContains(t, result, "code_challenge")
assert.NotContains(t, result, "code_challenge_method")
}
func TestProviderDataEnrichSession(t *testing.T) {
g := NewWithT(t)
p := &ProviderData{}
s := &sessions.SessionState{}
err := p.EnrichSession(context.Background(), s)
g.Expect(err).ToNot(HaveOccurred())
}
func TestProviderDataAuthorize(t *testing.T) {
testCases := []struct {
name string
allowedGroups []string
groups []string
expectedAuthZ bool
}{
{
name: "NoAllowedGroups",
allowedGroups: []string{},
groups: []string{},
expectedAuthZ: true,
},
{
name: "NoAllowedGroupsUserHasGroups",
allowedGroups: []string{},
groups: []string{"foo", "bar"},
expectedAuthZ: true,
},
{
name: "UserInAllowedGroup",
allowedGroups: []string{"foo"},
groups: []string{"foo", "bar"},
expectedAuthZ: true,
},
{
name: "UserNotInAllowedGroup",
allowedGroups: []string{"bar"},
groups: []string{"baz", "foo"},
expectedAuthZ: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
session := &sessions.SessionState{
Groups: tc.groups,
}
p := &ProviderData{}
p.setAllowedGroups(tc.allowedGroups)
authorized, err := p.Authorize(context.Background(), session)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(authorized).To(Equal(tc.expectedAuthZ))
})
}
}