10
10
type AuthS struct {
15
18
var _ = Suite(&AuthS{})
21
loginURL, prevUser string
24
func (ui *dummyUI) Credentials(loginURL, prevUser string) (user, passwd string, err error) {
25
ui.loginURL = loginURL
26
ui.prevUser = prevUser
27
return "myuser", "mypasswd", nil
17
30
func (s *AuthS) SetUpTest(c *C) {
18
s.auth = &rietveld.StandardAuth{
21
LoginURL: testServer.URL + "/lurl",
23
err := s.auth.Init(testServer.URL + "/rurl")
25
31
rietveld.SetDebug(true)
26
32
rietveld.SetLogger(c)
35
s.loginURL = testServer.URL + "/lurl"
36
s.rietveldURL = testServer.URL + "/rurl"
37
s.auth = rietveld.NewAuth(s.ui, false, s.loginURL)
29
40
func (s *AuthS) TestLoginSignLogout(c *C) {
36
47
testServer.PrepareResponse(302, headers, "")
49
err := s.auth.Login(s.rietveldURL)
39
50
c.Assert(err, IsNil)
51
c.Assert(s.ui.loginURL, Equals, s.loginURL)
52
c.Assert(s.ui.prevUser, Equals, "")
41
54
req := testServer.WaitRequest()
42
55
c.Assert(req.Method, Equals, "POST")
43
56
c.Assert(req.URL.RawPath, Equals, "/lurl")
44
c.Assert(req.Form["Email"], Equals, []string{"myemail"})
57
c.Assert(req.Form["Email"], Equals, []string{"myuser"})
45
58
c.Assert(req.Form["Passwd"], Equals, []string{"mypasswd"})
46
59
c.Assert(req.Form["source"], Equals, []string{"goetveld"})
47
60
c.Assert(req.Form["service"], Equals, []string{"ah"})
55
68
req, err = http.NewRequest("POST", "http://example.com", nil)
56
69
c.Assert(err, IsNil)
58
err = s.auth.Sign(req)
71
err = s.auth.Sign(s.rietveldURL, req)
59
72
c.Assert(err, IsNil)
60
73
c.Assert(req.Header["Cookie"], Equals, []string{"some=cookie"})
75
err = s.auth.Logout(s.rietveldURL)
63
76
c.Assert(err, IsNil)
65
78
req, err = http.NewRequest("POST", "http://example.com", nil)
66
79
c.Assert(err, IsNil)
68
err = s.auth.Sign(req)
81
err = s.auth.Sign(s.rietveldURL, req)
69
82
c.Assert(err, IsNil)
70
83
c.Assert(req.Header["Cookie"], Equals, []string{})
74
87
testServer.PrepareResponse(200, nil, "Auth=the-auth")
75
88
testServer.PrepareResponse(404, nil, "")
90
err := s.auth.Login(s.rietveldURL)
78
91
c.Assert(err, ErrorMatches, "error authorizing on rietveld: 404 Not Found")
80
93
testServer.PrepareResponse(200, nil, "Auth=the-auth")
81
94
headers := map[string]string{"location": "http://example.com"}
82
95
testServer.PrepareResponse(302, headers, "")
97
err = s.auth.Login(s.rietveldURL)
85
98
c.Assert(err, ErrorMatches, "error authorizing on rietveld: .* redirect blocked")
88
101
var errorTests = []struct{ response, msg string }{
89
102
{"", "error reading response: EOF"},
90
103
{"Error=Whatever", "unknown error during ClientLogin: Whatever"},
91
{"Error=BadAuthentication", "invalid email or password"},
104
{"Error=BadAuthentication", "invalid user or password"},
92
105
{"Error=BadAuthentication\nInfo=InvalidSecondFactor", "application-specific password required"},
93
106
{"Error=CaptchaRequired", "captcha required; visit http://j.mp/unlock-captcha and retry"},
94
107
{"Error=NotVerified", "account not verified"},
102
115
func (s *AuthS) TestLoginError(c *C) {
103
116
for _, t := range errorTests {
104
117
testServer.PrepareResponse(200, nil, t.response)
105
c.Assert(s.auth.Login(), ErrorMatches, t.msg)
118
c.Assert(s.auth.Login(s.rietveldURL), ErrorMatches, t.msg)
109
func (s *AuthS) TestStoredLogin(c *C) {
122
func (s *AuthS) TestCachedLogin(c *C) {
110
123
defer func(old string) {
111
124
os.Setenv("HOME", old)
112
125
}(os.Getenv("HOME"))
113
126
os.Setenv("HOME", c.MkDir())
115
auth := (*rietveld.StoredAuth)(s.auth.(*rietveld.StandardAuth))
128
auth := rietveld.NewAuth(s.ui, true, testServer.URL+"/lurl")
117
130
testServer.PrepareResponse(200, nil, "Auth=the-auth")
123
136
testServer.PrepareResponse(302, headers, "")
138
err := auth.Login(s.rietveldURL)
126
139
c.Assert(err, IsNil)
140
c.Assert(s.ui.loginURL, Equals, testServer.URL+"/lurl")
141
c.Assert(s.ui.prevUser, Equals, "")
128
newauth := &rietveld.StoredAuth{}
129
newauth.Init(testServer.URL + "/rurl")
143
// Build a new Auth to make use of cached credentials.
144
newauth := rietveld.NewAuth(s.ui, true, "")
131
146
req, err := http.NewRequest("POST", "http://example.com", nil)
132
147
c.Assert(err, IsNil)
134
err = newauth.Sign(req)
149
err = newauth.Sign(s.rietveldURL, req)
135
150
c.Assert(err, IsNil)
136
151
c.Assert(req.Header["Cookie"], Equals, []string{"some=cookie"})
139
c.Assert(newauth, Equals, auth)
153
// Login again to check usage of previousUser on credentials.
154
testServer.PrepareResponse(200, nil, "Auth=the-auth")
155
testServer.PrepareResponse(302, headers, "")
156
err = newauth.Login(s.rietveldURL)
158
c.Assert(s.ui.loginURL, Equals, s.loginURL)
159
c.Assert(s.ui.prevUser, Equals, "myuser")
161
// Check that logging out removes cached credentials as well.
141
162
path := os.ShellExpand("$HOME/.goetveld_localhost:4444")
142
163
stat, err := os.Stat(path)
143
164
c.Assert(stat, NotNil)
145
err = newauth.Logout()
166
err = newauth.Logout(s.rietveldURL)
146
167
c.Assert(err, IsNil)
148
169
stat, err = os.Stat(path)
151
172
req, err = http.NewRequest("POST", "http://example.com", nil)
152
173
c.Assert(err, IsNil)
154
err = newauth.Sign(req)
175
err = newauth.Sign(s.rietveldURL, req)
155
176
c.Assert(err, IsNil)
156
177
c.Assert(req.Header["Cookie"], Equals, []string{})