git.iko.soy/typeable/oauth2-proxy
on master help sign in
typeable/oauth2-proxy/providers/provider_default_test.go
161 lines 3.9 kB history raw
1package providers
3import (
4 "context"
5 "net/url"
6 "testing"
7 "time"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
10 . "github.com/onsi/gomega"
11 "github.com/stretchr/testify/assert"
14func TestRefresh(t *testing.T) {
15 p := &ProviderData{}
17 now := time.Unix(1234567890, 10)
18 expires := time.Unix(1234567890, 0)
20 ss := &sessions.SessionState{
21 Clock: func() time.Time { return now },
22 }
23 ss.SetExpiresOn(expires)
25 refreshed, err := p.RefreshSession(context.Background(), ss)
26 assert.False(t, refreshed)
27 assert.Equal(t, ErrNotImplemented, err)
29 refreshed, err = p.RefreshSession(context.Background(), nil)
30 assert.False(t, refreshed)
31 assert.Equal(t, ErrNotImplemented, err)
34func TestCodeChallengeConfigured(t *testing.T) {
35 p := &ProviderData{
36 LoginURL: &url.URL{
37 Scheme: "http",
38 Host: "my.test.idp",
39 Path: "/oauth/authorize",
40 },
41 }
43 extraValues := url.Values{}
44 extraValues["code_challenge"] = []string{"challenge"}
45 extraValues["code_challenge_method"] = []string{"method"}
46 result := p.GetLoginURL("https://my.test.app/oauth", "", "", extraValues)
47 assert.Contains(t, result, "code_challenge=challenge")
48 assert.Contains(t, result, "code_challenge_method=method")
51func TestCodeChallengeNotConfigured(t *testing.T) {
52 p := &ProviderData{
53 LoginURL: &url.URL{
54 Scheme: "http",
55 Host: "my.test.idp",
56 Path: "/oauth/authorize",
57 },
58 }
60 result := p.GetLoginURL("https://my.test.app/oauth", "", "", url.Values{})
61 assert.NotContains(t, result, "code_challenge")
62 assert.NotContains(t, result, "code_challenge_method")
65func TestProviderDataEnrichSession(t *testing.T) {
66 g := NewWithT(t)
67 p := &ProviderData{}
68 s := &sessions.SessionState{}
70 err := p.EnrichSession(context.Background(), s)
71 g.Expect(err).ToNot(HaveOccurred())
74func TestProviderDataAuthorize(t *testing.T) {
75 testCases := []struct {
76 name string
77 allowedGroups []string
78 groups []string
79 expectedAuthZ bool
80 }{
81 {
82 name: "NoAllowedGroups",
83 allowedGroups: []string{},
84 groups: []string{},
85 expectedAuthZ: true,
86 },
87 {
88 name: "NoAllowedGroupsUserHasGroups",
89 allowedGroups: []string{},
90 groups: []string{"foo", "bar"},
91 expectedAuthZ: true,
92 },
93 {
94 name: "UserInAllowedGroup",
95 allowedGroups: []string{"foo"},
96 groups: []string{"foo", "bar"},
97 expectedAuthZ: true,
98 },
99 {
100 name: "UserNotInAllowedGroup",
101 allowedGroups: []string{"bar"},
102 groups: []string{"baz", "foo"},
103 expectedAuthZ: false,
104 },
105 {
106 name: "AllowedGroupsWithEmptyString",
107 allowedGroups: []string{"group2", ""},
108 groups: []string{"group1", "group2"},
109 expectedAuthZ: true,
110 },
111 {
112 name: "AllowedGroupsOnlyEmptyString",
113 allowedGroups: []string{""},
114 groups: []string{"group1", "group2"},
115 expectedAuthZ: true,
116 },
117 }
119 for _, tc := range testCases {
120 t.Run(tc.name, func(t *testing.T) {
121 g := NewWithT(t)
123 session := &sessions.SessionState{
124 Groups: tc.groups,
125 }
126 p := &ProviderData{}
127 p.setAllowedGroups(tc.allowedGroups)
129 authorized, err := p.Authorize(context.Background(), session)
130 g.Expect(err).ToNot(HaveOccurred())
131 g.Expect(authorized).To(Equal(tc.expectedAuthZ))
132 })
133 }
136func TestResponseModeConfigured(t *testing.T) {
137 p := &ProviderData{
138 LoginURL: &url.URL{
139 Scheme: "http",
140 Host: "my.test.idp",
141 Path: "/oauth/authorize",
142 },
143 AuthRequestResponseMode: "form_post",
144 }
146 result := p.GetLoginURL("https://my.test.app/oauth", "", "", url.Values{})
147 assert.Contains(t, result, "response_mode=form_post")
150func TestResponseModeNotConfigured(t *testing.T) {
151 p := &ProviderData{
152 LoginURL: &url.URL{
153 Scheme: "http",
154 Host: "my.test.idp",
155 Path: "/oauth/authorize",
156 },
157 }
159 result := p.GetLoginURL("https://my.test.app/oauth", "", "", url.Values{})
160 assert.NotContains(t, result, "response_mode")