~andrewsmedina/goetveld/rietveld

« back to all changes in this revision

Viewing changes to auth_test.go

  • Committer: Gustavo Niemeyer
  • Date: 2011-11-14 10:43:42 UTC
  • Revision ID: gustavo@niemeyer.net-20111114104342-2do1ob8l2t2q75wd
Refactored Auth so a single instance is used with any number
of rietveld servers. That enabled introducing a global
DefaultAuth, and turning CodeReview into a *Rietveld.

Also introduced the concept of an AuthUI, and copied password
reading from exp/terminal to introduce a console AuthUI.

Other minor fixes, refactorings, and cleanups.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
type AuthS struct {
11
11
        HTTPSuite
12
 
        auth rietveld.Auth
 
12
        auth        rietveld.Auth
 
13
        ui          *dummyUI
 
14
        loginURL    string
 
15
        rietveldURL string
13
16
}
14
17
 
15
18
var _ = Suite(&AuthS{})
16
19
 
 
20
type dummyUI struct {
 
21
        loginURL, prevUser string
 
22
}
 
23
 
 
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
 
28
}
 
29
 
17
30
func (s *AuthS) SetUpTest(c *C) {
18
 
        s.auth = &rietveld.StandardAuth{
19
 
                Email:    "myemail",
20
 
                Passwd:   "mypasswd",
21
 
                LoginURL: testServer.URL + "/lurl",
22
 
        }
23
 
        err := s.auth.Init(testServer.URL + "/rurl")
24
 
        c.Assert(err, IsNil)
25
31
        rietveld.SetDebug(true)
26
32
        rietveld.SetLogger(c)
 
33
 
 
34
        s.ui = &dummyUI{}
 
35
        s.loginURL = testServer.URL + "/lurl"
 
36
        s.rietveldURL = testServer.URL + "/rurl"
 
37
        s.auth = rietveld.NewAuth(s.ui, false, s.loginURL)
27
38
}
28
39
 
29
40
func (s *AuthS) TestLoginSignLogout(c *C) {
35
46
        }
36
47
        testServer.PrepareResponse(302, headers, "")
37
48
 
38
 
        err := s.auth.Login()
 
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, "")
40
53
 
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)
57
70
 
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"})
61
74
 
62
 
        err = s.auth.Logout()
 
75
        err = s.auth.Logout(s.rietveldURL)
63
76
        c.Assert(err, IsNil)
64
77
 
65
78
        req, err = http.NewRequest("POST", "http://example.com", nil)
66
79
        c.Assert(err, IsNil)
67
80
 
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{})
71
84
}
74
87
        testServer.PrepareResponse(200, nil, "Auth=the-auth")
75
88
        testServer.PrepareResponse(404, nil, "")
76
89
 
77
 
        err := s.auth.Login()
 
90
        err := s.auth.Login(s.rietveldURL)
78
91
        c.Assert(err, ErrorMatches, "error authorizing on rietveld: 404 Not Found")
79
92
 
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, "")
83
96
 
84
 
        err = s.auth.Login()
 
97
        err = s.auth.Login(s.rietveldURL)
85
98
        c.Assert(err, ErrorMatches, "error authorizing on rietveld: .* redirect blocked")
86
99
}
87
100
 
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)
106
119
        }
107
120
}
108
121
 
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())
114
127
 
115
 
        auth := (*rietveld.StoredAuth)(s.auth.(*rietveld.StandardAuth))
 
128
        auth := rietveld.NewAuth(s.ui, true, testServer.URL+"/lurl")
116
129
 
117
130
        testServer.PrepareResponse(200, nil, "Auth=the-auth")
118
131
 
122
135
        }
123
136
        testServer.PrepareResponse(302, headers, "")
124
137
 
125
 
        err := auth.Login()
 
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, "")
127
142
 
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, "")
130
145
 
131
146
        req, err := http.NewRequest("POST", "http://example.com", nil)
132
147
        c.Assert(err, IsNil)
133
148
 
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"})
137
152
 
138
 
        auth.Passwd = ""
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)
 
157
        c.Assert(err, IsNil)
 
158
        c.Assert(s.ui.loginURL, Equals, s.loginURL)
 
159
        c.Assert(s.ui.prevUser, Equals, "myuser")
140
160
 
 
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)
144
165
 
145
 
        err = newauth.Logout()
 
166
        err = newauth.Logout(s.rietveldURL)
146
167
        c.Assert(err, IsNil)
147
168
 
148
169
        stat, err = os.Stat(path)
151
172
        req, err = http.NewRequest("POST", "http://example.com", nil)
152
173
        c.Assert(err, IsNil)
153
174
 
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{})
157
178
}