@@ -0,0 +1,135 @@
1+package providers
2+
3+import (
4+ "github.com/bmizerany/assert"
5+ "net/http"
6+ "net/http/httptest"
7+ "net/url"
8+ "testing"
9+)
10+
11+func testAzureProvider(hostname string) *AzureProvider {
12+ p := NewAzureProvider(
13+ &ProviderData{
14+ ProviderName: "",
15+ LoginURL: &url.URL{},
16+ RedeemURL: &url.URL{},
17+ ProfileURL: &url.URL{},
18+ ValidateURL: &url.URL{},
19+ ProtectedResource: &url.URL{},
20+ Scope: ""})
21+ if hostname != "" {
22+ updateURL(p.Data().LoginURL, hostname)
23+ updateURL(p.Data().RedeemURL, hostname)
24+ updateURL(p.Data().ProfileURL, hostname)
25+ updateURL(p.Data().ValidateURL, hostname)
26+ updateURL(p.Data().ProtectedResource, hostname)
27+ }
28+ return p
29+}
30+
31+func TestAzureProviderDefaults(t *testing.T) {
32+ p := testAzureProvider("")
33+ assert.NotEqual(t, nil, p)
34+ p.Configure("")
35+ assert.Equal(t, "Azure", p.Data().ProviderName)
36+ assert.Equal(t, "common", p.Tenant)
37+ assert.Equal(t, "https://login.microsoftonline.com/common/oauth2/authorize",
38+ p.Data().LoginURL.String())
39+ assert.Equal(t, "https://login.microsoftonline.com/common/oauth2/token",
40+ p.Data().RedeemURL.String())
41+ assert.Equal(t, "https://graph.windows.net/me?api-version=1.6",
42+ p.Data().ProfileURL.String())
43+ assert.Equal(t, "https://graph.windows.net",
44+ p.Data().ProtectedResource.String())
45+ assert.Equal(t, "",
46+ p.Data().ValidateURL.String())
47+ assert.Equal(t, "openid", p.Data().Scope)
48+}
49+
50+func TestAzureProviderOverrides(t *testing.T) {
51+ p := NewAzureProvider(
52+ &ProviderData{
53+ LoginURL: &url.URL{
54+ Scheme: "https",
55+ Host: "example.com",
56+ Path: "/oauth/auth"},
57+ RedeemURL: &url.URL{
58+ Scheme: "https",
59+ Host: "example.com",
60+ Path: "/oauth/token"},
61+ ProfileURL: &url.URL{
62+ Scheme: "https",
63+ Host: "example.com",
64+ Path: "/oauth/profile"},
65+ ValidateURL: &url.URL{
66+ Scheme: "https",
67+ Host: "example.com",
68+ Path: "/oauth/tokeninfo"},
69+ ProtectedResource: &url.URL{
70+ Scheme: "https",
71+ Host: "example.com"},
72+ Scope: "profile"})
73+ assert.NotEqual(t, nil, p)
74+ assert.Equal(t, "Azure", p.Data().ProviderName)
75+ assert.Equal(t, "https://example.com/oauth/auth",
76+ p.Data().LoginURL.String())
77+ assert.Equal(t, "https://example.com/oauth/token",
78+ p.Data().RedeemURL.String())
79+ assert.Equal(t, "https://example.com/oauth/profile",
80+ p.Data().ProfileURL.String())
81+ assert.Equal(t, "https://example.com/oauth/tokeninfo",
82+ p.Data().ValidateURL.String())
83+ assert.Equal(t, "https://example.com",
84+ p.Data().ProtectedResource.String())
85+ assert.Equal(t, "profile", p.Data().Scope)
86+}
87+
88+func TestAzureSetTenant(t *testing.T) {
89+ p := testAzureProvider("")
90+ p.Configure("example")
91+ assert.Equal(t, "Azure", p.Data().ProviderName)
92+ assert.Equal(t, "example", p.Tenant)
93+ assert.Equal(t, "https://login.microsoftonline.com/example/oauth2/authorize",
94+ p.Data().LoginURL.String())
95+ assert.Equal(t, "https://login.microsoftonline.com/example/oauth2/token",
96+ p.Data().RedeemURL.String())
97+ assert.Equal(t, "https://graph.windows.net/me?api-version=1.6",
98+ p.Data().ProfileURL.String())
99+ assert.Equal(t, "https://graph.windows.net",
100+ p.Data().ProtectedResource.String())
101+ assert.Equal(t, "",
102+ p.Data().ValidateURL.String())
103+ assert.Equal(t, "openid", p.Data().Scope)
104+}
105+
106+func testAzureBackend(payload string) *httptest.Server {
107+ path := "/me"
108+ query := "api-version=1.6"
109+
110+ return httptest.NewServer(http.HandlerFunc(
111+ func(w http.ResponseWriter, r *http.Request) {
112+ url := r.URL
113+ if url.Path != path || url.RawQuery != query {
114+ w.WriteHeader(404)
115+ } else if r.Header.Get("Authorization") != "Bearer imaginary_access_token" {
116+ w.WriteHeader(403)
117+ } else {
118+ w.WriteHeader(200)
119+ w.Write([]byte(payload))
120+ }
121+ }))
122+}
123+
124+func TestAzureProviderGetEmailAddress(t *testing.T) {
125+ b := testAzureBackend(`{ "mail": "user@windows.net" }`)
126+ defer b.Close()
127+
128+ b_url, _ := url.Parse(b.URL)
129+ p := testAzureProvider(b_url.Host)
130+
131+ session := &SessionState{AccessToken: "imaginary_access_token"}
132+ email, err := p.GetEmailAddress(session)
133+ assert.Equal(t, nil, err)
134+ assert.Equal(t, "user@windows.net", email)
135+}