Merge lp:~vds/usso/test_refactoring into lp:usso
- test_refactoring
- Merge into trunk
Proposed by
Vincenzo Di Somma
Status: | Superseded |
---|---|
Proposed branch: | lp:~vds/usso/test_refactoring |
Merge into: | lp:usso |
Diff against target: |
342 lines (+131/-43) 5 files modified
example/usso_example.go (+7/-2) oauth.go (+5/-3) oauth_test.go (+1/-1) usso.go (+39/-5) usso_test.go (+79/-32) |
To merge this branch: | bzr merge lp:~vds/usso/test_refactoring |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Andrew W. Deane (community) | Approve | ||
Review via email: mp+202711@code.launchpad.net |
This proposal has been superseded by a proposal from 2014-01-22.
Commit message
Refactoring usso tests.
Description of the change
Refactoring usso tests.
To post a comment you must log in.
Revision history for this message
Andrew W. Deane (andrew-w-deane) : | # |
review:
Approve
Unmerged revisions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'example/usso_example.go' |
2 | --- example/usso_example.go 2013-10-31 17:33:39 +0000 |
3 | +++ example/usso_example.go 2014-01-22 16:59:02 +0000 |
4 | @@ -25,8 +25,8 @@ |
5 | inputParams() |
6 | // Fetch the tokens using usso.GetToken. |
7 | fmt.Println("Fetching tokens from staging server...") |
8 | - server := usso.StagingUbuntuSSOServer |
9 | - ssodata, err := server.GetToken(email, password, "usso") |
10 | + server := usso.ProductionUbuntuSSOServer |
11 | + ssodata, err := server.GetToken(email, password, "Ubuntu One @ test-duplicity") |
12 | if err != nil { |
13 | panic(err) |
14 | } |
15 | @@ -66,4 +66,9 @@ |
16 | fmt.Printf("response: %+v\n", b.String()) |
17 | token_details, _ := server.GetTokenDetails(ssodata) |
18 | fmt.Printf("token details: %s\n", token_details) |
19 | + err = server.RegisterTokenToU1FileSync(ssodata) |
20 | + if err != nil { |
21 | + fmt.Printf("Error: %s\n", err) |
22 | + } |
23 | + fmt.Println("Token successfully registered to U1 File Sync Service.") |
24 | } |
25 | |
26 | === modified file 'oauth.go' |
27 | --- oauth.go 2013-02-14 22:12:30 +0000 |
28 | +++ oauth.go 2014-01-22 16:59:02 +0000 |
29 | @@ -31,6 +31,7 @@ |
30 | type SSOData struct { |
31 | ConsumerKey string `json:"consumer_key"` |
32 | ConsumerSecret string `json:"consumer_secret"` |
33 | + Realm string `json:"realm"` |
34 | TokenKey string `json:"token_key"` |
35 | TokenName string `json:"token_name"` |
36 | TokenSecret string `json:"token_secret"` |
37 | @@ -53,7 +54,7 @@ |
38 | |
39 | type PLAINTEXT struct{} |
40 | |
41 | -// Return the name of the signature method, used to compose the |
42 | +// Return the name of the signature method, used to compose the |
43 | // Authentication Header. |
44 | func (PLAINTEXT) Name() string { return "PLAINTEXT" } |
45 | |
46 | @@ -68,7 +69,7 @@ |
47 | |
48 | type HMACSHA1 struct{} |
49 | |
50 | -// Return the name of the signature method, used to compose the |
51 | +// Return the name of the signature method, used to compose the |
52 | // Authentication Header. |
53 | func (HMACSHA1) Name() string { return "HMAC-SHA1" } |
54 | |
55 | @@ -118,7 +119,7 @@ |
56 | return "", err |
57 | } |
58 | auth := fmt.Sprintf( |
59 | - `OAuth realm="API", `+ |
60 | + `OAuth realm="%s", `+ |
61 | `oauth_consumer_key="%s", `+ |
62 | `oauth_token="%s", `+ |
63 | `oauth_signature_method="%s", `+ |
64 | @@ -126,6 +127,7 @@ |
65 | `oauth_timestamp="%s", `+ |
66 | `oauth_nonce="%s", `+ |
67 | `oauth_version="1.0"`, |
68 | + url.QueryEscape(ssodata.Realm), |
69 | url.QueryEscape(ssodata.ConsumerKey), |
70 | url.QueryEscape(ssodata.TokenKey), |
71 | rp.SignatureMethod.Name(), |
72 | |
73 | === modified file 'oauth_test.go' |
74 | --- oauth_test.go 2013-02-05 12:37:03 +0000 |
75 | +++ oauth_test.go 2014-01-22 16:59:02 +0000 |
76 | @@ -17,7 +17,7 @@ |
77 | func (suite *OAuthTestSuite) SetUpTest(c *C) { |
78 | baseUrl := "https://localhost" |
79 | suite.ssodata = SSOData{ConsumerKey: consumerKey, |
80 | - ConsumerSecret: consumerSecret, TokenKey: tokenKey, |
81 | + ConsumerSecret: consumerSecret, Realm: realm, TokenKey: tokenKey, |
82 | TokenName: tokenName, TokenSecret: tokenSecret} |
83 | suite.rp = RequestParameters{BaseURL: baseUrl, HTTPMethod: "GET", |
84 | Nonce: "10888885", Timestamp: "1358853126"} |
85 | |
86 | === modified file 'usso.go' |
87 | --- usso.go 2013-10-31 17:33:39 +0000 |
88 | +++ usso.go 2014-01-22 16:59:02 +0000 |
89 | @@ -12,7 +12,8 @@ |
90 | ) |
91 | |
92 | type UbuntuSSOServer struct { |
93 | - baseUrl string |
94 | + baseUrl string |
95 | + tokenRegistrationUrl string |
96 | } |
97 | |
98 | // tokenURL returns the URL where the Ubuntu SSO tokens can be requested. |
99 | @@ -34,16 +35,15 @@ |
100 | |
101 | // ProductionUbuntuSSOServer represents the production Ubuntu SSO server |
102 | // located at https://login.ubuntu.com. |
103 | -var ProductionUbuntuSSOServer = UbuntuSSOServer{"https://login.ubuntu.com"} |
104 | +var ProductionUbuntuSSOServer = UbuntuSSOServer{"https://login.ubuntu.com", "https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/"} |
105 | |
106 | // StagingUbuntuSSOServer represents the staging Ubuntu SSO server located |
107 | // at https://login.staging.ubuntu.com. Use it for testing. |
108 | -var StagingUbuntuSSOServer = UbuntuSSOServer{"https://login.staging.ubuntu.com"} |
109 | +var StagingUbuntuSSOServer = UbuntuSSOServer{"https://login.staging.ubuntu.com", "https://one.staging.ubuntu.com/oauth/sso-finished-so-get-tokens/"} |
110 | |
111 | // Giving user credentials and token name, retrieves oauth credentials |
112 | // for the users, the oauth credentials can be used later to sign requests. |
113 | -func (server UbuntuSSOServer) GetToken( |
114 | - email string, password string, tokenName string) (*SSOData, error) { |
115 | +func (server UbuntuSSOServer) GetToken(email string, password string, tokenName string) (*SSOData, error) { |
116 | credentials := map[string]string{ |
117 | "email": email, |
118 | "password": password, |
119 | @@ -78,6 +78,7 @@ |
120 | log.Println(err) |
121 | return nil, err |
122 | } |
123 | + ssodata.Realm = "API" |
124 | return &ssodata, nil |
125 | } |
126 | |
127 | @@ -151,3 +152,36 @@ |
128 | b.Write(body) |
129 | return fmt.Sprint(b.String()), nil |
130 | } |
131 | + |
132 | +// Register the toke to the U1 File Sync Service. |
133 | +func (server UbuntuSSOServer) RegisterTokenToU1FileSync(ssodata *SSOData) (err error) { |
134 | + rp := RequestParameters{ |
135 | + BaseURL: server.tokenRegistrationUrl, |
136 | + HTTPMethod: "GET", |
137 | + SignatureMethod: HMACSHA1{}} |
138 | + |
139 | + request, err := http.NewRequest(rp.HTTPMethod, rp.BaseURL, nil) |
140 | + if err != nil { |
141 | + return err |
142 | + } |
143 | + ssodata.Realm = "" |
144 | + err = SignRequest(ssodata, &rp, request) |
145 | + if err != nil { |
146 | + return err |
147 | + } |
148 | + client := &http.Client{} |
149 | + response, err := client.Do(request) |
150 | + if err != nil { |
151 | + fmt.Printf("Error: %s\n", err) |
152 | + } |
153 | + if response.StatusCode != 200 { |
154 | + body, err := ioutil.ReadAll(response.Body) |
155 | + if err != nil { |
156 | + fmt.Println(err) |
157 | + } |
158 | + var b bytes.Buffer |
159 | + b.Write(body) |
160 | + errors.New(fmt.Sprint(b.String())) |
161 | + } |
162 | + return nil |
163 | +} |
164 | |
165 | === modified file 'usso_test.go' |
166 | --- usso_test.go 2013-10-31 17:33:39 +0000 |
167 | +++ usso_test.go 2014-01-22 16:59:02 +0000 |
168 | @@ -21,17 +21,13 @@ |
169 | tokenName = "foo" |
170 | tokenKey = "abcs" |
171 | tokenSecret = "mTBgLxtTRUdfqewqgrqsvxlijbMWkPBajgKcoZCrDwv" |
172 | + realm = "API" |
173 | consumerKey = "rfyzhdQ" |
174 | consumerSecret = "rwDkQkkdfdfdeAslkmmxAOjOAT" |
175 | email = "foo@bar.com" |
176 | password = "foobarpwd" |
177 | ) |
178 | |
179 | -type SingleServingServer struct { |
180 | - *httptest.Server |
181 | - requestContent *string |
182 | -} |
183 | - |
184 | // TestProductionUbuntuSSOServerURLs tests the URLs of the production server. |
185 | func (suite *USSOTestSuite) TestProductionUbuntuSSOServerURLs(c *C) { |
186 | tokenURL := ProductionUbuntuSSOServer.tokenURL() |
187 | @@ -44,31 +40,47 @@ |
188 | c.Assert(tokenURL, Equals, "https://login.staging.ubuntu.com/api/v2/tokens/oauth") |
189 | } |
190 | |
191 | -// newSingleServingServer create a single-serving test http server which will |
192 | -// return only one response as defined by the passed arguments. |
193 | -func newSingleServingServer( |
194 | - uri string, response string, code int) *SingleServingServer { |
195 | +type TestServer struct { |
196 | + *httptest.Server |
197 | + requestContent *string |
198 | +} |
199 | + |
200 | +// newTestServer http server to mock U1 SSO server. |
201 | +func newTestServer(response, tokenDetails string, code int) *TestServer { |
202 | var requestContent string |
203 | - var requested bool |
204 | handler := func(w http.ResponseWriter, r *http.Request) { |
205 | - if requested { |
206 | - http.Error(w, "Already requested", http.StatusServiceUnavailable) |
207 | - } |
208 | res, err := ioutil.ReadAll(r.Body) |
209 | if err != nil { |
210 | panic(err) |
211 | } |
212 | - requestContent = string(res) |
213 | - if r.URL.String() != uri || strings.Contains(requestContent, "WRONG") { |
214 | + if strings.Contains(string(res), "WRONG") { |
215 | http.Error(w, "404 page not found", http.StatusNotFound) |
216 | + } |
217 | + if r.URL.String() == "/api/v2/tokens/oauth" { |
218 | + requestContent = string(res) |
219 | + fmt.Fprint(w, response) |
220 | + return |
221 | + } |
222 | + if r.URL.String() == "/api/v2/tokens/oauth/abcs" { |
223 | + fmt.Fprint(w, tokenDetails) |
224 | + return |
225 | + } |
226 | + if r.URL.String() == "/oauth/sso-finished-so-get-tokens/" { |
227 | + fmt.Fprint(w, "ok") |
228 | + |
229 | + concat := "" |
230 | + for _, v := range r.Header["Authorization"] { |
231 | + concat += v |
232 | + } |
233 | + requestContent = concat |
234 | + return |
235 | } else { |
236 | - w.WriteHeader(code) |
237 | - fmt.Fprint(w, response) |
238 | + http.Error(w, "404 page not found", http.StatusNotFound) |
239 | + return |
240 | } |
241 | - requested = true |
242 | } |
243 | server := httptest.NewServer(http.HandlerFunc(handler)) |
244 | - return &SingleServingServer{server, &requestContent} |
245 | + return &TestServer{server, &requestContent} |
246 | } |
247 | |
248 | func (suite *USSOTestSuite) TestGetTokenReturnsTokens(c *C) { |
249 | @@ -87,16 +99,15 @@ |
250 | if err != nil { |
251 | panic(err) |
252 | } |
253 | - server := newSingleServingServer("/api/v2/tokens/oauth", |
254 | - string(jsonServerResponseData), 200) |
255 | - var testSSOServer = &UbuntuSSOServer{server.URL} |
256 | + server := newTestServer(string(jsonServerResponseData), "{}", 200) |
257 | + var testSSOServer = &UbuntuSSOServer{server.URL, ""} |
258 | defer server.Close() |
259 | |
260 | // The returned information is correct. |
261 | ssodata, err := testSSOServer.GetToken(email, password, tokenName) |
262 | c.Assert(err, IsNil) |
263 | expectedSSOData := &SSOData{ConsumerKey: consumerKey, |
264 | - ConsumerSecret: consumerSecret, TokenKey: tokenKey, |
265 | + ConsumerSecret: consumerSecret, Realm: realm, TokenKey: tokenKey, |
266 | TokenSecret: tokenSecret, TokenName: tokenName} |
267 | c.Assert(ssodata, DeepEquals, expectedSSOData) |
268 | // The request that the fake Ubuntu SSO Server has the credentials. |
269 | @@ -114,11 +125,10 @@ |
270 | |
271 | // GetToken should return empty credentials and an error, if wrong account is provided. |
272 | func (suite *USSOTestSuite) TestGetTokenFails(c *C) { |
273 | - server := newSingleServingServer("/api/v2/tokens/oauth", "{}", 200) |
274 | - var testSSOServer = &UbuntuSSOServer{server.URL} |
275 | + server := newTestServer("{}", "{}", 200) |
276 | + var testSSOServer = &UbuntuSSOServer{server.URL, ""} |
277 | defer server.Close() |
278 | ssodata, err := testSSOServer.GetToken(email, "WRONG", tokenName) |
279 | - fmt.Println(ssodata, err) |
280 | c.Assert(err, NotNil) |
281 | c.Assert(ssodata, IsNil) |
282 | } |
283 | @@ -137,16 +147,53 @@ |
284 | if err != nil { |
285 | panic(err) |
286 | } |
287 | - server := newSingleServingServer("/api/v2/tokens/oauth", |
288 | - string(jsonServerResponseData), 200) |
289 | - var testSSOServer = &UbuntuSSOServer{server.URL} |
290 | + tokenDetails := map[string]string{ |
291 | + "token_name": tokenName, |
292 | + "date_updated": "2014-01-22T13:35:49.867", |
293 | + "token_key": tokenKey, |
294 | + "href": "/api/v2/tokens/oauth/JckChNpbXxPRmPkElLglSnqnjsnGseWJmNqTJCWfUtNBSsGtoG", |
295 | + "date_created": "2014-01-17T20:03:24.993", |
296 | + "consumer_key": consumerKey, |
297 | + } |
298 | + jsonTokenDetails, err := json.Marshal(tokenDetails) |
299 | + if err != nil { |
300 | + panic(err) |
301 | + } |
302 | + server := newTestServer(string(jsonServerResponseData), string(jsonTokenDetails), 200) |
303 | + var testSSOServer = &UbuntuSSOServer{server.URL, ""} |
304 | defer server.Close() |
305 | ssodata, err := testSSOServer.GetToken(email, password, tokenName) |
306 | - |
307 | // The returned information is correct. |
308 | token_details, err := testSSOServer.GetTokenDetails(ssodata) |
309 | c.Assert(err, IsNil) |
310 | - |
311 | //The request that the fake Ubuntu SSO Server has the token details. |
312 | - fmt.Printf("%s\n", token_details) |
313 | + c.Assert(token_details, Equals, string(jsonTokenDetails)) |
314 | +} |
315 | + |
316 | +func (suite *USSOTestSuite) TestRegisterToken(c *C) { |
317 | + // Simulate a valid Ubuntu SSO Server response. |
318 | + serverResponseData := map[string]string{ |
319 | + "date_updated": "2013-01-16 14:03:36", |
320 | + "date_created": "2013-01-16 14:03:36", |
321 | + "href": "/api/v2/tokens/" + tokenKey, |
322 | + "token_name": tokenName, |
323 | + "token_key": tokenKey, |
324 | + "consumer_key": consumerKey, |
325 | + } |
326 | + jsonServerResponseData, err := json.Marshal(serverResponseData) |
327 | + if err != nil { |
328 | + panic(err) |
329 | + } |
330 | + server := newTestServer(string(jsonServerResponseData), "{}", 200) |
331 | + var testSSOServer = &UbuntuSSOServer{server.URL, server.URL + "/oauth/sso-finished-so-get-tokens/"} |
332 | + defer server.Close() |
333 | + ssodata, err := testSSOServer.GetToken(email, password, tokenName) |
334 | + err = testSSOServer.RegisterTokenToU1FileSync(ssodata) |
335 | + c.Assert(err, IsNil) |
336 | + c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_consumer_key=\"rfyzhdQ\"")) |
337 | + c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_token=\"abcs\"")) |
338 | + c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_signature_method=\"HMAC-SHA1\"")) |
339 | + c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_version=\"1.0\"")) |
340 | + c.Assert(true, Equals, strings.Contains(*server.requestContent, "")) |
341 | + |
342 | } |