10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
11 . "github.com/onsi/ginkgo"
12 . "github.com/onsi/ginkgo/extensions/table"
13 . "github.com/onsi/gomega"
16func testGitLabProvider(hostname string) *GitLabProvider {
17 p := NewGitLabProvider(
21 RedeemURL: &url.URL{},
22 ProfileURL: &url.URL{},
23 ValidateURL: &url.URL{},
26 updateURL(p.Data().LoginURL, hostname)
27 updateURL(p.Data().RedeemURL, hostname)
28 updateURL(p.Data().ProfileURL, hostname)
29 updateURL(p.Data().ValidateURL, hostname)
35func testGitLabBackend() *httptest.Server {
39 "email": "foo@bar.com",
40 "email_verified": false,
41 "groups": ["foo", "bar"]
48 "path_with_namespace": "my_group/my_project",
50 "project_access": null,
53 "notification_level": 3
59 personalProjectInfo := `
61 "name": "MyPersonalProject",
63 "path_with_namespace": "my_profile/my_personal_project",
67 "notification_level": 3
74 archivedProjectInfo := `
76 "name": "MyArchivedProject",
78 "path_with_namespace": "my_group/my_archived_project",
82 "notification_level": 3
89 authHeader := "Bearer gitlab_access_token"
91 return httptest.NewServer(http.HandlerFunc(
92 func(w http.ResponseWriter, r *http.Request) {
94 case "/oauth/userinfo":
95 if r.Header["Authorization"][0] == authHeader {
97 w.Write([]byte(userInfo))
101 case "/api/v4/projects/my_group/my_project":
102 if r.Header["Authorization"][0] == authHeader {
104 w.Write([]byte(projectInfo))
108 case "/api/v4/projects/my_group/my_archived_project":
109 if r.Header["Authorization"][0] == authHeader {
111 w.Write([]byte(archivedProjectInfo))
115 case "/api/v4/projects/my_profile/my_personal_project":
116 if r.Header["Authorization"][0] == authHeader {
118 w.Write([]byte(personalProjectInfo))
122 case "/api/v4/projects/my_group/my_bad_project":
130var _ = Describe("Gitlab Provider Tests", func() {
131 var p *GitLabProvider
132 var b *httptest.Server
135 b = testGitLabBackend()
137 bURL, err := url.Parse(b.URL)
138 Expect(err).To(BeNil())
140 p = testGitLabProvider(bURL.Host)
147 Context("with bad token", func() {
148 It("should trigger an error", func() {
149 p.AllowUnverifiedEmail = false
150 session := &sessions.SessionState{AccessToken: "unexpected_gitlab_access_token"}
151 err := p.EnrichSession(context.Background(), session)
152 Expect(err).To(MatchError(errors.New("failed to retrieve user info: error getting user info: unexpected status \"401\": ")))
156 Context("when filtering on email", func() {
157 type emailsTableInput struct {
160 allowUnverifiedEmail bool
163 DescribeTable("should return expected results",
164 func(in emailsTableInput) {
165 p.AllowUnverifiedEmail = in.allowUnverifiedEmail
166 session := &sessions.SessionState{AccessToken: "gitlab_access_token"}
168 err := p.EnrichSession(context.Background(), session)
170 if in.expectedError != nil {
171 Expect(err).To(MatchError(err))
173 Expect(err).To(BeNil())
174 Expect(session.Email).To(Equal(in.expectedValue))
177 Entry("unverified email denied", emailsTableInput{
178 expectedError: errors.New("user email is not verified"),
179 allowUnverifiedEmail: false,
181 Entry("unverified email allowed", emailsTableInput{
183 expectedValue: "foo@bar.com",
184 allowUnverifiedEmail: true,
189 Context("when filtering on gitlab entities (groups and projects)", func() {
190 type entitiesTableInput struct {
191 expectedValue []string
196 DescribeTable("should return expected results",
197 func(in entitiesTableInput) {
198 p.AllowUnverifiedEmail = true
199 session := &sessions.SessionState{AccessToken: "gitlab_access_token"}
201 err := p.AddProjects(in.projects)
202 Expect(err).To(BeNil())
205 if len(in.groups) > 0 {
209 err = p.EnrichSession(context.Background(), session)
211 Expect(err).To(BeNil())
212 Expect(session.Groups).To(Equal(in.expectedValue))
214 Entry("project membership valid on group project", entitiesTableInput{
215 expectedValue: []string{"project:my_group/my_project"},
216 projects: []string{"my_group/my_project"},
218 Entry("project membership invalid on group project, insufficient access level level", entitiesTableInput{
220 projects: []string{"my_group/my_project=40"},
222 Entry("project membership valid on personnal project", entitiesTableInput{
223 expectedValue: []string{"project:my_profile/my_personal_project"},
224 projects: []string{"my_profile/my_personal_project"},
226 Entry("project membership invalid on personnal project, insufficient access level", entitiesTableInput{
228 projects: []string{"my_profile/my_personal_project=40"},
230 Entry("project membership invalid", entitiesTableInput{
232 projects: []string{"my_group/my_bad_project"},
234 Entry("group membership valid", entitiesTableInput{
235 expectedValue: []string{"group:foo"},
236 groups: []string{"foo"},
238 Entry("groups and projects", entitiesTableInput{
239 expectedValue: []string{"group:foo", "group:baz", "project:my_group/my_project", "project:my_profile/my_personal_project"},
240 groups: []string{"foo", "baz"},
241 projects: []string{"my_group/my_project", "my_profile/my_personal_project"},
243 Entry("archived projects", entitiesTableInput{
246 projects: []string{"my_group/my_archived_project"},
252 Context("when generating group list from multiple kind", func() {
253 type entitiesTableInput struct {
258 DescribeTable("should prefix entities with group kind", func(in entitiesTableInput) {
260 err := p.AddProjects(in.projects)
261 Expect(err).To(BeNil())
263 all := p.PrefixAllowedGroups()
265 Expect(len(all)).To(Equal(len(in.projects) + len(in.groups)))
267 Entry("simple test case", entitiesTableInput{
268 projects: []string{"my_group/my_project", "my_group/my_other_project"},
269 groups: []string{"mygroup", "myothergroup"},
271 Entry("projects only", entitiesTableInput{
272 projects: []string{"my_group/my_project", "my_group/my_other_project"},
275 Entry("groups only", entitiesTableInput{
276 projects: []string{},
277 groups: []string{"mygroup", "myothergroup"},
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list