@@ -0,0 +1,134 @@
4+ "github.com/bitly/go-simplejson"
5+ "github.com/bmizerany/assert"
12+func updateUrl(url *url.URL, hostname string) {
17+func testMyUsaProvider(hostname string) *MyUsaProvider {
18+ p := NewMyUsaProvider(
21+ LoginUrl: &url.URL{},
22+ RedeemUrl: &url.URL{},
23+ ProfileUrl: &url.URL{},
26+ updateUrl(p.Data().LoginUrl, hostname)
27+ updateUrl(p.Data().RedeemUrl, hostname)
28+ updateUrl(p.Data().ProfileUrl, hostname)
33+func testMyUsaBackend(payload string) *httptest.Server {
34+ path := "/api/v1/profile"
35+ query := "access_token=imaginary_access_token"
37+ return httptest.NewServer(http.HandlerFunc(
38+ func(w http.ResponseWriter, r *http.Request) {
40+ if url.Path != path || url.RawQuery != query {
44+ w.Write([]byte(payload))
49+func TestMyUsaProviderDefaults(t *testing.T) {
50+ p := testMyUsaProvider("")
51+ assert.NotEqual(t, nil, p)
52+ assert.Equal(t, "MyUSA", p.Data().ProviderName)
53+ assert.Equal(t, "https://alpha.my.usa.gov/oauth/authorize",
54+ p.Data().LoginUrl.String())
55+ assert.Equal(t, "https://alpha.my.usa.gov/oauth/token",
56+ p.Data().RedeemUrl.String())
57+ assert.Equal(t, "https://alpha.my.usa.gov/api/v1/profile",
58+ p.Data().ProfileUrl.String())
59+ assert.Equal(t, "profile.email", p.Data().Scope)
62+func TestMyUsaProviderOverrides(t *testing.T) {
63+ p := NewMyUsaProvider(
68+ Path: "/oauth/auth"},
72+ Path: "/oauth/token"},
73+ ProfileUrl: &url.URL{
76+ Path: "/oauth/profile"},
78+ assert.NotEqual(t, nil, p)
79+ assert.Equal(t, "MyUSA", p.Data().ProviderName)
80+ assert.Equal(t, "https://example.com/oauth/auth",
81+ p.Data().LoginUrl.String())
82+ assert.Equal(t, "https://example.com/oauth/token",
83+ p.Data().RedeemUrl.String())
84+ assert.Equal(t, "https://example.com/oauth/profile",
85+ p.Data().ProfileUrl.String())
86+ assert.Equal(t, "profile", p.Data().Scope)
89+func TestMyUsaProviderGetEmailAddress(t *testing.T) {
90+ b := testMyUsaBackend("{\"email\": \"michael.bland@gsa.gov\"}")
93+ b_url, _ := url.Parse(b.URL)
94+ p := testMyUsaProvider(b_url.Host)
95+ unused_auth_response := simplejson.New()
97+ email, err := p.GetEmailAddress(unused_auth_response,
98+ "imaginary_access_token")
99+ assert.Equal(t, nil, err)
100+ assert.Equal(t, "michael.bland@gsa.gov", email)
103+// Note that trying to trigger the "failed building request" case is not
104+// practical, since the only way it can fail is if the URL fails to parse.
105+func TestMyUsaProviderGetEmailAddressFailedRequest(t *testing.T) {
106+ b := testMyUsaBackend("unused payload")
109+ b_url, _ := url.Parse(b.URL)
110+ p := testMyUsaProvider(b_url.Host)
111+ unused_auth_response := simplejson.New()
113+ // We'll trigger a request failure by using an unexpected access
114+ // token. Alternatively, we could allow the parsing of the payload as
116+ email, err := p.GetEmailAddress(unused_auth_response,
117+ "unexpected_access_token")
118+ assert.NotEqual(t, nil, err)
119+ assert.Equal(t, "", email)
122+func TestMyUsaProviderGetEmailAddressEmailNotPresentInPayload(t *testing.T) {
123+ b := testMyUsaBackend("{\"foo\": \"bar\"}")
126+ b_url, _ := url.Parse(b.URL)
127+ p := testMyUsaProvider(b_url.Host)
128+ unused_auth_response := simplejson.New()
130+ email, err := p.GetEmailAddress(unused_auth_response,
131+ "imaginary_access_token")
132+ assert.NotEqual(t, nil, err)
133+ assert.Equal(t, "", email)