git.iko.soy/typeable/oauth2-proxy
on request-rules help sign in
typeable/oauth2-proxy/providers/nextcloud_test.go
139 lines 4.1 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/pkg/apis/sessions"
11 "github.com/stretchr/testify/assert"
14const formatJSON = "format=json"
15const userPath = "/ocs/v2.php/cloud/user"
17func testNextcloudProvider(hostname string) *NextcloudProvider {
18 p := NewNextcloudProvider(
19 &ProviderData{
20 ProviderName: "",
21 LoginURL: &url.URL{},
22 RedeemURL: &url.URL{},
23 ProfileURL: &url.URL{},
24 ValidateURL: &url.URL{},
25 Scope: ""})
26 if hostname != "" {
27 updateURL(p.Data().LoginURL, hostname)
28 updateURL(p.Data().RedeemURL, hostname)
29 updateURL(p.Data().ProfileURL, hostname)
30 updateURL(p.Data().ValidateURL, hostname)
31 }
32 return p
35func testNextcloudBackend(payload string) *httptest.Server {
36 path := userPath
37 query := formatJSON
39 return httptest.NewServer(http.HandlerFunc(
40 func(w http.ResponseWriter, r *http.Request) {
41 if r.URL.Path != path || r.URL.RawQuery != query {
42 w.WriteHeader(404)
43 } else if !IsAuthorizedInHeader(r.Header) {
44 w.WriteHeader(403)
45 } else {
46 w.WriteHeader(200)
47 w.Write([]byte(payload))
48 }
49 }))
52func TestNextcloudProviderDefaults(t *testing.T) {
53 p := testNextcloudProvider("")
54 assert.NotEqual(t, nil, p)
55 assert.Equal(t, "Nextcloud", p.Data().ProviderName)
56 assert.Equal(t, "",
57 p.Data().LoginURL.String())
58 assert.Equal(t, "",
59 p.Data().RedeemURL.String())
60 assert.Equal(t, "",
61 p.Data().ValidateURL.String())
64func TestNextcloudProviderOverrides(t *testing.T) {
65 p := NewNextcloudProvider(
66 &ProviderData{
67 LoginURL: &url.URL{
68 Scheme: "https",
69 Host: "example.com",
70 Path: "/index.php/apps/oauth2/authorize"},
71 RedeemURL: &url.URL{
72 Scheme: "https",
73 Host: "example.com",
74 Path: "/index.php/apps/oauth2/api/v1/token"},
75 ValidateURL: &url.URL{
76 Scheme: "https",
77 Host: "example.com",
78 Path: "/test/ocs/v2.php/cloud/user",
79 RawQuery: formatJSON},
80 Scope: "profile"})
81 assert.NotEqual(t, nil, p)
82 assert.Equal(t, "Nextcloud", p.Data().ProviderName)
83 assert.Equal(t, "https://example.com/index.php/apps/oauth2/authorize",
84 p.Data().LoginURL.String())
85 assert.Equal(t, "https://example.com/index.php/apps/oauth2/api/v1/token",
86 p.Data().RedeemURL.String())
87 assert.Equal(t, "https://example.com/test/ocs/v2.php/cloud/user?"+formatJSON,
88 p.Data().ValidateURL.String())
91func TestNextcloudProviderGetEmailAddress(t *testing.T) {
92 b := testNextcloudBackend("{\"ocs\": {\"data\": { \"email\": \"michael.bland@gsa.gov\"}}}")
93 defer b.Close()
95 bURL, _ := url.Parse(b.URL)
96 p := testNextcloudProvider(bURL.Host)
97 p.ValidateURL.Path = userPath
98 p.ValidateURL.RawQuery = formatJSON
100 session := CreateAuthorizedSession()
101 email, err := p.GetEmailAddress(context.Background(), session)
102 assert.Equal(t, nil, err)
103 assert.Equal(t, "michael.bland@gsa.gov", email)
106// Note that trying to trigger the "failed building request" case is not
107// practical, since the only way it can fail is if the URL fails to parse.
108func TestNextcloudProviderGetEmailAddressFailedRequest(t *testing.T) {
109 b := testNextcloudBackend("unused payload")
110 defer b.Close()
112 bURL, _ := url.Parse(b.URL)
113 p := testNextcloudProvider(bURL.Host)
114 p.ValidateURL.Path = userPath
115 p.ValidateURL.RawQuery = formatJSON
117 // We'll trigger a request failure by using an unexpected access
118 // token. Alternatively, we could allow the parsing of the payload as
119 // JSON to fail.
120 session := &sessions.SessionState{AccessToken: "unexpected_access_token"}
121 email, err := p.GetEmailAddress(context.Background(), session)
122 assert.NotEqual(t, nil, err)
123 assert.Equal(t, "", email)
126func TestNextcloudProviderGetEmailAddressEmailNotPresentInPayload(t *testing.T) {
127 b := testNextcloudBackend("{\"foo\": \"bar\"}")
128 defer b.Close()
130 bURL, _ := url.Parse(b.URL)
131 p := testNextcloudProvider(bURL.Host)
132 p.ValidateURL.Path = userPath
133 p.ValidateURL.RawQuery = formatJSON
135 session := CreateAuthorizedSession()
136 email, err := p.GetEmailAddress(context.Background(), session)
137 assert.NotEqual(t, nil, err)
138 assert.Equal(t, "", email)