Merge lp:~rvb/usso/usso-refactor-tests into lp:usso

Proposed by Raphaël Badin
Status: Merged
Approved by: Vincenzo Di Somma
Approved revision: 14
Merged at revision: 13
Proposed branch: lp:~rvb/usso/usso-refactor-tests
Merge into: lp:usso
Diff against target: 191 lines (+92/-86)
1 file modified
usso_test.go (+92/-86)
To merge this branch: bzr merge lp:~rvb/usso/usso-refactor-tests
Reviewer Review Type Date Requested Status
Vincenzo Di Somma (community) Approve
Review via email: mp+143907@code.launchpad.net

Commit message

Refactor the tests to use launchpad.net/gocheck.

Description of the change

This branch:
- refactors the tests to use launchpad.net/gocheck
- adds a small utility to create a mock Ubuntu SSO server
- renames the two tests to better convey what they actually do

To post a comment you must log in.
Revision history for this message
Vincenzo Di Somma (vds) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'usso_test.go'
2--- usso_test.go 2013-01-17 19:18:55 +0000
3+++ usso_test.go 2013-01-18 16:15:26 +0000
4@@ -4,95 +4,101 @@
5 package usso
6
7 import (
8- "io"
9+ "encoding/json"
10+ "fmt"
11 "io/ioutil"
12- "log"
13+ . "launchpad.net/gocheck"
14 "net/http"
15- "strings"
16+ "net/http/httptest"
17+ "net/url"
18 "testing"
19 )
20
21-func return_tokens(w http.ResponseWriter, request *http.Request) {
22- // Utility function to test GetToken
23- body, _ := ioutil.ReadAll(request.Body)
24- if string(body) != `{"email":"foo@bar.com","password":"foobarpwd",`+
25- `"token_name":"token_name"}` {
26- log.Fatalf("Wrong Credential sent to server.\n")
27- }
28- io.WriteString(
29- w,
30- `{
31- "token_name": "foo",
32- "date_updated": "2013-01-16 14:03:36",
33- "token_key": "abcs",
34- "consumer_secret": "rwDkQkkdfdfdeAslkmmxAOjOAT",
35- "href": "/api/v2/tokens/abcd",
36- "date_created": "2013-01-16 14:03:36",
37- "consumer_key": "rfyzhdQ",
38- "token_secret": "mTBgLxtTRUdfqewqgrqsvxlijbMWkPBajgKcoZCrDwv"
39- }`)
40-}
41-
42-func TestGetToken(test *testing.T) {
43- // Test that GetToken connects to the right
44- http.HandleFunc("/", return_tokens)
45- go http.ListenAndServe(":8123", nil)
46- creds := Credentials{
47- "foo@bar.com",
48- "foobarpwd",
49- "token_name",
50- "http://localhost:8123"}
51- ssodata, _ := GetToken(&creds)
52- if ssodata.ConsumerKey != "rfyzhdQ" {
53- test.Fatalf("Wrong consumer key: %s", ssodata.ConsumerKey)
54- }
55- if ssodata.ConsumerSecret != "rwDkQkkdfdfdeAslkmmxAOjOAT" {
56- test.Fatalf("Wrong consumer secret: %s", ssodata.ConsumerSecret)
57- }
58- if ssodata.TokenKey != "abcs" {
59- test.Fatalf("Wrong token key: %s", ssodata.TokenKey)
60- }
61- if ssodata.TokenName != "foo" {
62- test.Fatalf("Wrong token name: %s", ssodata.TokenName)
63- }
64- if ssodata.TokenSecret != "mTBgLxtTRUdfqewqgrqsvxlijbMWkPBajgKcoZCrDwv" {
65- test.Fatalf("Wrong Token secret: %s", ssodata.TokenSecret)
66- }
67-}
68-
69-func TestSignRequest(test *testing.T) {
70- // Test SignRequest
71-
72- test_items := [...]string{
73- `OAuth realm="API"`,
74- `oauth_consumer_key="rfyzhdQ"`,
75- `oauth_token="abcs"`,
76- `oauth_signature="rwDkQkkdfdfdeAslkmmxAOjOAT%26mTBgLxtTRUdfqewqgrqsvx` +
77- `lijbMWkPBajgKcoZCrDwv"`}
78-
79- creds := Credentials{
80- "foo@bar.com",
81- "foobarpwd",
82- "token_name",
83- "http://localhost:8123"}
84- ssodata, _ := GetToken(&creds)
85- ssodata.BaseURL = "https://login.staging.ubuntu.com/api/v2/accounts/"
86- request, _ := http.NewRequest(
87- "GET",
88- "https://login.staging.ubuntu.com/api/v2/accounts/"+ssodata.ConsumerKey,
89- nil)
90- ssodata.Sign(request)
91- if !strings.Contains(
92- request.Header["Authorization"][0],
93- `OAuth realm="API"`) {
94- test.Fail()
95- }
96-
97- for _, c := range test_items {
98- if !strings.Contains(
99- request.Header["Authorization"][0],
100- c) {
101- test.Fail()
102- }
103- }
104+func Test(test *testing.T) { TestingT(test) }
105+
106+type USSOTestSuite struct{}
107+
108+var _ = Suite(&USSOTestSuite{})
109+
110+const (
111+ tokenName = "foo"
112+ tokenKey = "abcs"
113+ tokenSecret = "mTBgLxtTRUdfqewqgrqsvxlijbMWkPBajgKcoZCrDwv"
114+ consumerKey = "rfyzhdQ"
115+ consumerSecret = "rwDkQkkdfdfdeAslkmmxAOjOAT"
116+ email = "foo@bar.com"
117+ password = "foobarpwd"
118+)
119+
120+type SingleServingServer struct {
121+ *httptest.Server
122+ requestContent *string
123+}
124+
125+// newSingleServingServer create a single-serving test http server which will
126+// return only one response as defined by the passed arguments.
127+func (suite *USSOTestSuite) newSingleServingServer(uri string, response string, code int) *SingleServingServer {
128+ var requestContent string
129+ var requested bool
130+ handler := func(w http.ResponseWriter, r *http.Request) {
131+ if requested {
132+ http.Error(w, "Already requested", http.StatusServiceUnavailable)
133+ }
134+ res, err := ioutil.ReadAll(r.Body)
135+ if err != nil {
136+ panic(err)
137+ }
138+ requestContent = string(res)
139+ if r.URL.String() != uri {
140+ http.Error(w, "404 page not found", http.StatusNotFound)
141+ } else {
142+ w.WriteHeader(code)
143+ fmt.Fprint(w, response)
144+ }
145+ requested = true
146+ }
147+ server := httptest.NewServer(http.HandlerFunc(handler))
148+ return &SingleServingServer{server, &requestContent}
149+}
150+
151+func (suite *USSOTestSuite) TestGetTokenReturnsTokens(c *C) {
152+ // Simulate a valid Ubuntu SSO Server response.
153+ serverResponseData := map[string]string{
154+ "date_updated": "2013-01-16 14:03:36",
155+ "date_created": "2013-01-16 14:03:36",
156+ "href": "/api/v2/tokens/" + tokenKey,
157+ "token_name": tokenName,
158+ "token_key": tokenKey,
159+ "token_secret": tokenSecret,
160+ "consumer_key": consumerKey,
161+ "consumer_secret": consumerSecret,
162+ }
163+ jsonServerResponseData, _ := json.Marshal(serverResponseData)
164+ server := suite.newSingleServingServer("/", string(jsonServerResponseData), 200)
165+ defer server.Close()
166+
167+ // The returned information is correct.
168+ creds := Credentials{Email: email, Password: password, TokenName: tokenName, SSOServerURL: server.URL}
169+ ssodata, err := GetToken(&creds)
170+ c.Assert(err, IsNil)
171+ expectedSSOData := &SSOData{ConsumerKey: consumerKey, ConsumerSecret: consumerSecret, TokenKey: tokenKey, TokenSecret: tokenSecret, TokenName: tokenName}
172+ c.Assert(ssodata, DeepEquals, expectedSSOData)
173+ // The request that the fake Ubuntu SSO Server got contained the credentials.
174+ expectedRequestContent, _ := json.Marshal(creds)
175+ c.Assert(*server.requestContent, Equals, string(expectedRequestContent))
176+}
177+
178+func (suite *USSOTestSuite) TestSignRequestPlainText(c *C) {
179+ baseUrl := "https://localhost"
180+ ssoData := SSOData{BaseURL: baseUrl, ConsumerKey: consumerKey, ConsumerSecret: consumerSecret, TokenKey: tokenKey, TokenName: tokenName, TokenSecret: tokenSecret}
181+ request, _ := http.NewRequest("GET", baseUrl, nil)
182+
183+ err := ssoData.Sign(request)
184+
185+ c.Assert(err, IsNil)
186+ authHeader := request.Header["Authorization"][0]
187+ c.Assert(authHeader, Matches, `.*OAuth realm="API".*`)
188+ c.Assert(authHeader, Matches, `.*oauth_consumer_key="`+url.QueryEscape(ssoData.ConsumerKey)+`".*`)
189+ c.Assert(authHeader, Matches, `.*oauth_token="`+url.QueryEscape(ssoData.TokenKey)+`".*`)
190+ c.Assert(authHeader, Matches, `.*oauth_signature="`+url.QueryEscape(ssoData.ConsumerSecret+`&`+ssoData.TokenSecret)+`.*`)
191 }

Subscribers

People subscribed via source and target branches