git.iko.soy/typeable/oauth2-proxy
on master help sign in
typeable/oauth2-proxy/providers/atlassian-cloud_test.go
93 lines 3.2 kB history raw
1package providers
3import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "testing"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
11 . "github.com/onsi/gomega"
14func testAtlassianBackend(status int, body string) *httptest.Server {
15 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16 if r.URL.Path != "/me" || r.Header.Get("Authorization") != "Bearer imaginary_access_token" {
17 w.WriteHeader(http.StatusUnauthorized)
18 return
19 }
20 w.WriteHeader(status)
21 _, _ = w.Write([]byte(body))
22 }))
25func testAtlassianProvider(backend *httptest.Server) *AtlassianProvider {
26 p := NewAtlassianProvider(&ProviderData{})
27 u, _ := url.Parse(backend.URL)
28 updateURL(p.ProfileURL, u.Host)
29 updateURL(p.ValidateURL, u.Host)
30 return p
33func TestNewAtlassianProvider(t *testing.T) {
34 g := NewWithT(t)
36 providerData := NewAtlassianProvider(&ProviderData{}).Data()
37 g.Expect(providerData.ProviderName).To(Equal("Atlassian"))
38 g.Expect(providerData.LoginURL.String()).To(Equal("https://auth.atlassian.com/authorize"))
39 g.Expect(providerData.RedeemURL.String()).To(Equal("https://auth.atlassian.com/oauth/token"))
40 g.Expect(providerData.ProfileURL.String()).To(Equal("https://api.atlassian.com/me"))
41 g.Expect(providerData.ValidateURL.String()).To(Equal("https://api.atlassian.com/me"))
42 g.Expect(providerData.Scope).To(Equal("read:me"))
45func TestAtlassianProviderGetLoginURL(t *testing.T) {
46 g := NewWithT(t)
47 p := NewAtlassianProvider(&ProviderData{ClientID: "client"})
49 loginURL, err := url.Parse(p.GetLoginURL("https://proxy/oauth2/callback", "state", "",
50 url.Values{"approval_prompt": {"force"}}))
51 g.Expect(err).ToNot(HaveOccurred())
53 q := loginURL.Query()
54 g.Expect(q.Get("audience")).To(Equal("api.atlassian.com"))
55 g.Expect(q.Get("prompt")).To(Equal("consent"))
56 g.Expect(q.Has("approval_prompt")).To(BeFalse())
57 g.Expect(q.Get("scope")).To(Equal("read:me"))
59 loginURL, err = url.Parse(p.GetLoginURL("https://proxy/oauth2/callback", "state", "",
60 url.Values{"prompt": {"login"}}))
61 g.Expect(err).ToNot(HaveOccurred())
62 g.Expect(loginURL.Query().Get("prompt")).To(Equal("login"))
65func TestAtlassianProviderEnrichSession(t *testing.T) {
66 g := NewWithT(t)
67 b := testAtlassianBackend(http.StatusOK, `{"account_id":"abc","email":"user@example.com"}`)
68 defer b.Close()
69 p := testAtlassianProvider(b)
71 session := CreateAuthorizedSession()
72 g.Expect(p.EnrichSession(context.Background(), session)).To(Succeed())
73 g.Expect(session.Email).To(Equal("user@example.com"))
76func TestAtlassianProviderEnrichSessionWithoutEmail(t *testing.T) {
77 g := NewWithT(t)
78 b := testAtlassianBackend(http.StatusOK, `{"account_id":"abc"}`)
79 defer b.Close()
80 p := testAtlassianProvider(b)
82 g.Expect(p.EnrichSession(context.Background(), CreateAuthorizedSession())).ToNot(Succeed())
85func TestAtlassianProviderValidateSession(t *testing.T) {
86 g := NewWithT(t)
87 b := testAtlassianBackend(http.StatusOK, `{"email":"user@example.com"}`)
88 defer b.Close()
89 p := testAtlassianProvider(b)
91 g.Expect(p.ValidateSession(context.Background(), CreateAuthorizedSession())).To(BeTrue())
92 g.Expect(p.ValidateSession(context.Background(), &sessions.SessionState{AccessToken: "wrong"})).To(BeFalse())