Merge lp:~vds/usso/test_refactoring into lp:usso

Proposed by Vincenzo Di Somma
Status: Merged
Approved by: Vincenzo Di Somma
Approved revision: 32
Merged at revision: 32
Proposed branch: lp:~vds/usso/test_refactoring
Merge into: lp:usso
Prerequisite: lp:~vds/usso/u1_file_sync_registration
Diff against target: 222 lines (+88/-39)
3 files modified
example/usso_example.go (+5/-2)
usso.go (+6/-6)
usso_test.go (+77/-31)
To merge this branch: bzr merge lp:~vds/usso/test_refactoring
Reviewer Review Type Date Requested Status
Domas Monkus Approve
Review via email: mp+202718@code.launchpad.net

This proposal supersedes a proposal from 2014-01-22.

Description of the change

Refactoring usso tests.

To post a comment you must log in.
Revision history for this message
Domas Monkus (tasdomas) :
review: Approve
Revision history for this message
Andrew W. Deane (andrew-w-deane) : Posted in a previous version of this proposal
review: Approve

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 2014-01-22 17:36:47 +0000
3+++ example/usso_example.go 2014-01-22 17:36:47 +0000
4@@ -66,6 +66,9 @@
5 fmt.Printf("response: %+v\n", b.String())
6 token_details, _ := server.GetTokenDetails(ssodata)
7 fmt.Printf("token details: %s\n", token_details)
8- token_u1registration, _ := server.RegisterTokenToU1FileSync(ssodata)
9- fmt.Printf("token registration: %s\n", token_u1registration)
10+ err = server.RegisterTokenToU1FileSync(ssodata)
11+ if err != nil {
12+ fmt.Printf("Error: %s\n", err)
13+ }
14+ fmt.Println("Token successfully registered to U1 File Sync Service.")
15 }
16
17=== modified file 'usso.go'
18--- usso.go 2014-01-22 17:36:47 +0000
19+++ usso.go 2014-01-22 17:36:47 +0000
20@@ -12,7 +12,8 @@
21 )
22
23 type UbuntuSSOServer struct {
24- baseUrl string
25+ baseUrl string
26+ tokenRegistrationUrl string
27 }
28
29 // tokenURL returns the URL where the Ubuntu SSO tokens can be requested.
30@@ -34,16 +35,15 @@
31
32 // ProductionUbuntuSSOServer represents the production Ubuntu SSO server
33 // located at https://login.ubuntu.com.
34-var ProductionUbuntuSSOServer = UbuntuSSOServer{"https://login.ubuntu.com"}
35+var ProductionUbuntuSSOServer = UbuntuSSOServer{"https://login.ubuntu.com", "https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/"}
36
37 // StagingUbuntuSSOServer represents the staging Ubuntu SSO server located
38 // at https://login.staging.ubuntu.com. Use it for testing.
39-var StagingUbuntuSSOServer = UbuntuSSOServer{"https://login.staging.ubuntu.com"}
40+var StagingUbuntuSSOServer = UbuntuSSOServer{"https://login.staging.ubuntu.com", "https://one.staging.ubuntu.com/oauth/sso-finished-so-get-tokens/"}
41
42 // Giving user credentials and token name, retrieves oauth credentials
43 // for the users, the oauth credentials can be used later to sign requests.
44-func (server UbuntuSSOServer) GetToken(
45- email string, password string, tokenName string) (*SSOData, error) {
46+func (server UbuntuSSOServer) GetToken(email string, password string, tokenName string) (*SSOData, error) {
47 credentials := map[string]string{
48 "email": email,
49 "password": password,
50@@ -156,7 +156,7 @@
51 // Register the toke to the U1 File Sync Service.
52 func (server UbuntuSSOServer) RegisterTokenToU1FileSync(ssodata *SSOData) (err error) {
53 rp := RequestParameters{
54- BaseURL: "https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/",
55+ BaseURL: server.tokenRegistrationUrl,
56 HTTPMethod: "GET",
57 SignatureMethod: HMACSHA1{}}
58
59
60=== modified file 'usso_test.go'
61--- usso_test.go 2014-01-22 17:36:47 +0000
62+++ usso_test.go 2014-01-22 17:36:47 +0000
63@@ -28,11 +28,6 @@
64 password = "foobarpwd"
65 )
66
67-type SingleServingServer struct {
68- *httptest.Server
69- requestContent *string
70-}
71-
72 // TestProductionUbuntuSSOServerURLs tests the URLs of the production server.
73 func (suite *USSOTestSuite) TestProductionUbuntuSSOServerURLs(c *C) {
74 tokenURL := ProductionUbuntuSSOServer.tokenURL()
75@@ -45,31 +40,47 @@
76 c.Assert(tokenURL, Equals, "https://login.staging.ubuntu.com/api/v2/tokens/oauth")
77 }
78
79-// newSingleServingServer create a single-serving test http server which will
80-// return only one response as defined by the passed arguments.
81-func newSingleServingServer(
82- uri string, response string, code int) *SingleServingServer {
83+type TestServer struct {
84+ *httptest.Server
85+ requestContent *string
86+}
87+
88+// newTestServer http server to mock U1 SSO server.
89+func newTestServer(response, tokenDetails string, code int) *TestServer {
90 var requestContent string
91- var requested bool
92 handler := func(w http.ResponseWriter, r *http.Request) {
93- if requested {
94- http.Error(w, "Already requested", http.StatusServiceUnavailable)
95- }
96 res, err := ioutil.ReadAll(r.Body)
97 if err != nil {
98 panic(err)
99 }
100- requestContent = string(res)
101- if r.URL.String() != uri || strings.Contains(requestContent, "WRONG") {
102+ if strings.Contains(string(res), "WRONG") {
103 http.Error(w, "404 page not found", http.StatusNotFound)
104+ }
105+ if r.URL.String() == "/api/v2/tokens/oauth" {
106+ requestContent = string(res)
107+ fmt.Fprint(w, response)
108+ return
109+ }
110+ if r.URL.String() == "/api/v2/tokens/oauth/abcs" {
111+ fmt.Fprint(w, tokenDetails)
112+ return
113+ }
114+ if r.URL.String() == "/oauth/sso-finished-so-get-tokens/" {
115+ fmt.Fprint(w, "ok")
116+
117+ concat := ""
118+ for _, v := range r.Header["Authorization"] {
119+ concat += v
120+ }
121+ requestContent = concat
122+ return
123 } else {
124- w.WriteHeader(code)
125- fmt.Fprint(w, response)
126+ http.Error(w, "404 page not found", http.StatusNotFound)
127+ return
128 }
129- requested = true
130 }
131 server := httptest.NewServer(http.HandlerFunc(handler))
132- return &SingleServingServer{server, &requestContent}
133+ return &TestServer{server, &requestContent}
134 }
135
136 func (suite *USSOTestSuite) TestGetTokenReturnsTokens(c *C) {
137@@ -88,9 +99,8 @@
138 if err != nil {
139 panic(err)
140 }
141- server := newSingleServingServer("/api/v2/tokens/oauth",
142- string(jsonServerResponseData), 200)
143- var testSSOServer = &UbuntuSSOServer{server.URL}
144+ server := newTestServer(string(jsonServerResponseData), "{}", 200)
145+ var testSSOServer = &UbuntuSSOServer{server.URL, ""}
146 defer server.Close()
147
148 // The returned information is correct.
149@@ -115,11 +125,10 @@
150
151 // GetToken should return empty credentials and an error, if wrong account is provided.
152 func (suite *USSOTestSuite) TestGetTokenFails(c *C) {
153- server := newSingleServingServer("/api/v2/tokens/oauth", "{}", 200)
154- var testSSOServer = &UbuntuSSOServer{server.URL}
155+ server := newTestServer("{}", "{}", 200)
156+ var testSSOServer = &UbuntuSSOServer{server.URL, ""}
157 defer server.Close()
158 ssodata, err := testSSOServer.GetToken(email, "WRONG", tokenName)
159- fmt.Println(ssodata, err)
160 c.Assert(err, NotNil)
161 c.Assert(ssodata, IsNil)
162 }
163@@ -138,16 +147,53 @@
164 if err != nil {
165 panic(err)
166 }
167- server := newSingleServingServer("/api/v2/tokens/oauth",
168- string(jsonServerResponseData), 200)
169- var testSSOServer = &UbuntuSSOServer{server.URL}
170+ tokenDetails := map[string]string{
171+ "token_name": tokenName,
172+ "date_updated": "2014-01-22T13:35:49.867",
173+ "token_key": tokenKey,
174+ "href": "/api/v2/tokens/oauth/JckChNpbXxPRmPkElLglSnqnjsnGseWJmNqTJCWfUtNBSsGtoG",
175+ "date_created": "2014-01-17T20:03:24.993",
176+ "consumer_key": consumerKey,
177+ }
178+ jsonTokenDetails, err := json.Marshal(tokenDetails)
179+ if err != nil {
180+ panic(err)
181+ }
182+ server := newTestServer(string(jsonServerResponseData), string(jsonTokenDetails), 200)
183+ var testSSOServer = &UbuntuSSOServer{server.URL, ""}
184 defer server.Close()
185 ssodata, err := testSSOServer.GetToken(email, password, tokenName)
186-
187 // The returned information is correct.
188 token_details, err := testSSOServer.GetTokenDetails(ssodata)
189 c.Assert(err, IsNil)
190-
191 //The request that the fake Ubuntu SSO Server has the token details.
192- fmt.Printf("%s\n", token_details)
193+ c.Assert(token_details, Equals, string(jsonTokenDetails))
194+}
195+
196+func (suite *USSOTestSuite) TestRegisterToken(c *C) {
197+ // Simulate a valid Ubuntu SSO Server response.
198+ serverResponseData := map[string]string{
199+ "date_updated": "2013-01-16 14:03:36",
200+ "date_created": "2013-01-16 14:03:36",
201+ "href": "/api/v2/tokens/" + tokenKey,
202+ "token_name": tokenName,
203+ "token_key": tokenKey,
204+ "consumer_key": consumerKey,
205+ }
206+ jsonServerResponseData, err := json.Marshal(serverResponseData)
207+ if err != nil {
208+ panic(err)
209+ }
210+ server := newTestServer(string(jsonServerResponseData), "{}", 200)
211+ var testSSOServer = &UbuntuSSOServer{server.URL, server.URL + "/oauth/sso-finished-so-get-tokens/"}
212+ defer server.Close()
213+ ssodata, err := testSSOServer.GetToken(email, password, tokenName)
214+ err = testSSOServer.RegisterTokenToU1FileSync(ssodata)
215+ c.Assert(err, IsNil)
216+ c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_consumer_key=\"rfyzhdQ\""))
217+ c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_token=\"abcs\""))
218+ c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_signature_method=\"HMAC-SHA1\""))
219+ c.Assert(true, Equals, strings.Contains(*server.requestContent, "oauth_version=\"1.0\""))
220+ c.Assert(true, Equals, strings.Contains(*server.requestContent, ""))
221+
222 }

Subscribers

People subscribed via source and target branches

to all changes: