~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/idmclient/ussologin/visitwebpage_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package ussologin_test
 
5
 
 
6
import (
 
7
        "encoding/json"
 
8
        "errors"
 
9
        "fmt"
 
10
        "io/ioutil"
 
11
        "net/http"
 
12
        "net/http/httptest"
 
13
        "net/url"
 
14
 
 
15
        "github.com/juju/testing"
 
16
        jc "github.com/juju/testing/checkers"
 
17
        "github.com/juju/usso"
 
18
        gc "gopkg.in/check.v1"
 
19
        "gopkg.in/juju/environschema.v1/form"
 
20
        "gopkg.in/macaroon-bakery.v1/httpbakery"
 
21
 
 
22
        "github.com/juju/idmclient/params"
 
23
        "github.com/juju/idmclient/ussologin"
 
24
)
 
25
 
 
26
type visitWebPageSuite struct {
 
27
        testing.CleanupSuite
 
28
        server *httptest.Server
 
29
}
 
30
 
 
31
var _ = gc.Suite(&visitWebPageSuite{})
 
32
 
 
33
func (s *visitWebPageSuite) SetUpTest(c *gc.C) {
 
34
        s.server = httptest.NewServer(&loginMethodsHandler{"http://example.com"})
 
35
}
 
36
 
 
37
func (s *visitWebPageSuite) TearDownTest(c *gc.C) {
 
38
        s.server.Close()
 
39
}
 
40
 
 
41
func (s *visitWebPageSuite) TestCorrectUserPasswordSentToUSSOServer(c *gc.C) {
 
42
        ussoStub := &ussoServerStub{}
 
43
        s.PatchValue(ussologin.Server, ussoStub)
 
44
        filler := &testFiller{
 
45
                map[string]interface{}{
 
46
                        ussologin.UserKey: "foobar",
 
47
                        ussologin.PassKey: "pass",
 
48
                        ussologin.OTPKey:  "1234",
 
49
                }}
 
50
        store := &testTokenStore{}
 
51
        f := ussologin.VisitWebPage("testToken", &http.Client{}, filler, store)
 
52
        u, err := url.Parse(s.server.URL)
 
53
        c.Assert(err, jc.ErrorIsNil)
 
54
        err = f(u)
 
55
        c.Assert(err, jc.ErrorIsNil)
 
56
        ussoStub.CheckCall(c, 0, "GetTokenWithOTP", "foobar", "pass", "1234", "testToken")
 
57
        store.CheckCallNames(c, "Get", "Put")
 
58
}
 
59
 
 
60
func (s *visitWebPageSuite) TestLoginFailsToGetToken(c *gc.C) {
 
61
        ussoStub := &ussoServerStub{}
 
62
        ussoStub.SetErrors(errors.New("something failed"))
 
63
        s.PatchValue(ussologin.Server, ussoStub)
 
64
        filler := &testFiller{
 
65
                map[string]interface{}{
 
66
                        ussologin.UserKey: "foobar",
 
67
                        ussologin.PassKey: "pass",
 
68
                        ussologin.OTPKey:  "1234",
 
69
                }}
 
70
        f := ussologin.VisitWebPage("testToken", &http.Client{}, filler, &testTokenStore{})
 
71
        u, err := url.Parse(s.server.URL)
 
72
        c.Assert(err, jc.ErrorIsNil)
 
73
        err = f(u)
 
74
        c.Assert(err, gc.ErrorMatches, "cannot get token: something failed")
 
75
}
 
76
 
 
77
func (s *visitWebPageSuite) TestLoginWithExistingToken(c *gc.C) {
 
78
        ussoStub := &ussoServerStub{}
 
79
        s.PatchValue(ussologin.Server, ussoStub)
 
80
        f := ussologin.VisitWebPage("testToken", &http.Client{}, &testFiller{}, &testTokenStore{tok: &usso.SSOData{}})
 
81
        u, err := url.Parse(s.server.URL)
 
82
        c.Assert(err, jc.ErrorIsNil)
 
83
        err = f(u)
 
84
        c.Assert(err, jc.ErrorIsNil)
 
85
        ussoStub.CheckNoCalls(c) //If we have a token we shouldn't call the ussoServer
 
86
}
 
87
 
 
88
func (s *visitWebPageSuite) TestLoginWithExistingMalformedToken(c *gc.C) {
 
89
        ussoStub := &ussoServerStub{}
 
90
        s.PatchValue(ussologin.Server, ussoStub)
 
91
        filler := &testFiller{
 
92
                map[string]interface{}{
 
93
                        ussologin.UserKey: "foobar",
 
94
                        ussologin.PassKey: "pass",
 
95
                        ussologin.OTPKey:  "1234",
 
96
                }}
 
97
        tokenPath := fmt.Sprintf("%s/token", c.MkDir())
 
98
        err := ioutil.WriteFile(tokenPath, []byte("foobar"), 0600) // Write a malformed token
 
99
        c.Assert(err, jc.ErrorIsNil)
 
100
        f := ussologin.VisitWebPage("testToken", &http.Client{}, filler, ussologin.NewFileTokenStore(tokenPath))
 
101
        u, err := url.Parse(s.server.URL)
 
102
        c.Assert(err, jc.ErrorIsNil)
 
103
        err = f(u)
 
104
        c.Assert(err, jc.ErrorIsNil)
 
105
        ussoStub.CheckCall(c, 0, "GetTokenWithOTP", "foobar", "pass", "1234", "testToken")
 
106
}
 
107
 
 
108
func (s *visitWebPageSuite) TestVisitWebPageWorksIfNilStoreGiven(c *gc.C) {
 
109
        ussoStub := &ussoServerStub{}
 
110
        s.PatchValue(ussologin.Server, ussoStub)
 
111
        filler := &testFiller{
 
112
                map[string]interface{}{
 
113
                        ussologin.UserKey: "foobar",
 
114
                        ussologin.PassKey: "pass",
 
115
                        ussologin.OTPKey:  "1234",
 
116
                }}
 
117
        f := ussologin.VisitWebPage("testToken", &http.Client{}, filler, nil)
 
118
        u, err := url.Parse(s.server.URL)
 
119
        c.Assert(err, jc.ErrorIsNil)
 
120
        err = f(u)
 
121
        c.Assert(err, jc.ErrorIsNil)
 
122
        ussoStub.CheckCall(c, 0, "GetTokenWithOTP", "foobar", "pass", "1234", "testToken")
 
123
}
 
124
 
 
125
func (s *visitWebPageSuite) TestFailedToReadLoginParameters(c *gc.C) {
 
126
        ussoStub := &ussoServerStub{}
 
127
        s.PatchValue(ussologin.Server, ussoStub)
 
128
        filler := &errFiller{}
 
129
        f := ussologin.VisitWebPage("testToken", &http.Client{}, filler, &testTokenStore{})
 
130
        u, err := url.Parse(s.server.URL)
 
131
        c.Assert(err, jc.ErrorIsNil)
 
132
        err = f(u)
 
133
        c.Assert(err, gc.ErrorMatches, "cannot read login parameters: something failed")
 
134
        ussoStub.CheckNoCalls(c)
 
135
}
 
136
 
 
137
type testFiller struct {
 
138
        form map[string]interface{}
 
139
}
 
140
 
 
141
func (t *testFiller) Fill(f form.Form) (map[string]interface{}, error) {
 
142
        return t.form, nil
 
143
}
 
144
 
 
145
type errFiller struct{}
 
146
 
 
147
func (t *errFiller) Fill(f form.Form) (map[string]interface{}, error) {
 
148
        return nil, errors.New("something failed")
 
149
}
 
150
 
 
151
type ussoServerStub struct {
 
152
        testing.Stub
 
153
}
 
154
 
 
155
func (u *ussoServerStub) GetTokenWithOTP(email, password, otp, tokenName string) (*usso.SSOData, error) {
 
156
        u.AddCall("GetTokenWithOTP", email, password, otp, tokenName)
 
157
        return &usso.SSOData{}, u.NextErr()
 
158
}
 
159
 
 
160
type loginMethodsHandler struct {
 
161
        responseURL string
 
162
}
 
163
 
 
164
func (l *loginMethodsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
165
        w.Header().Set("Content-Type", "application/json")
 
166
        lm := params.LoginMethods{
 
167
                UbuntuSSOOAuth: l.responseURL,
 
168
        }
 
169
        writer := json.NewEncoder(w)
 
170
        err := writer.Encode(&lm)
 
171
        if err != nil {
 
172
                panic(err)
 
173
        }
 
174
}
 
175
 
 
176
var _ httpbakery.Visitor = &ussologin.Visitor{}