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

Subscribers

People subscribed via source and target branches

to all changes: