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
=== modified file 'usso_test.go'
--- usso_test.go 2013-01-17 19:18:55 +0000
+++ usso_test.go 2013-01-18 16:15:26 +0000
@@ -4,95 +4,101 @@
4package usso4package usso
55
6import (6import (
7 "io"7 "encoding/json"
8 "fmt"
8 "io/ioutil"9 "io/ioutil"
9 "log"10 . "launchpad.net/gocheck"
10 "net/http"11 "net/http"
11 "strings"12 "net/http/httptest"
13 "net/url"
12 "testing"14 "testing"
13)15)
1416
15func return_tokens(w http.ResponseWriter, request *http.Request) {17func Test(test *testing.T) { TestingT(test) }
16 // Utility function to test GetToken18
17 body, _ := ioutil.ReadAll(request.Body)19type USSOTestSuite struct{}
18 if string(body) != `{"email":"foo@bar.com","password":"foobarpwd",`+20
19 `"token_name":"token_name"}` {21var _ = Suite(&USSOTestSuite{})
20 log.Fatalf("Wrong Credential sent to server.\n")22
21 }23const (
22 io.WriteString(24 tokenName = "foo"
23 w,25 tokenKey = "abcs"
24 `{26 tokenSecret = "mTBgLxtTRUdfqewqgrqsvxlijbMWkPBajgKcoZCrDwv"
25 "token_name": "foo", 27 consumerKey = "rfyzhdQ"
26 "date_updated": "2013-01-16 14:03:36", 28 consumerSecret = "rwDkQkkdfdfdeAslkmmxAOjOAT"
27 "token_key": "abcs", 29 email = "foo@bar.com"
28 "consumer_secret": "rwDkQkkdfdfdeAslkmmxAOjOAT", 30 password = "foobarpwd"
29 "href": "/api/v2/tokens/abcd", 31)
30 "date_created": "2013-01-16 14:03:36", 32
31 "consumer_key": "rfyzhdQ", 33type SingleServingServer struct {
32 "token_secret": "mTBgLxtTRUdfqewqgrqsvxlijbMWkPBajgKcoZCrDwv"34 *httptest.Server
33 }`)35 requestContent *string
34}36}
3537
36func TestGetToken(test *testing.T) {38// newSingleServingServer create a single-serving test http server which will
37 // Test that GetToken connects to the right39// return only one response as defined by the passed arguments.
38 http.HandleFunc("/", return_tokens)40func (suite *USSOTestSuite) newSingleServingServer(uri string, response string, code int) *SingleServingServer {
39 go http.ListenAndServe(":8123", nil)41 var requestContent string
40 creds := Credentials{42 var requested bool
41 "foo@bar.com",43 handler := func(w http.ResponseWriter, r *http.Request) {
42 "foobarpwd",44 if requested {
43 "token_name",45 http.Error(w, "Already requested", http.StatusServiceUnavailable)
44 "http://localhost:8123"}46 }
45 ssodata, _ := GetToken(&creds)47 res, err := ioutil.ReadAll(r.Body)
46 if ssodata.ConsumerKey != "rfyzhdQ" {48 if err != nil {
47 test.Fatalf("Wrong consumer key: %s", ssodata.ConsumerKey)49 panic(err)
48 }50 }
49 if ssodata.ConsumerSecret != "rwDkQkkdfdfdeAslkmmxAOjOAT" {51 requestContent = string(res)
50 test.Fatalf("Wrong consumer secret: %s", ssodata.ConsumerSecret)52 if r.URL.String() != uri {
51 }53 http.Error(w, "404 page not found", http.StatusNotFound)
52 if ssodata.TokenKey != "abcs" {54 } else {
53 test.Fatalf("Wrong token key: %s", ssodata.TokenKey)55 w.WriteHeader(code)
54 }56 fmt.Fprint(w, response)
55 if ssodata.TokenName != "foo" {57 }
56 test.Fatalf("Wrong token name: %s", ssodata.TokenName)58 requested = true
57 }59 }
58 if ssodata.TokenSecret != "mTBgLxtTRUdfqewqgrqsvxlijbMWkPBajgKcoZCrDwv" {60 server := httptest.NewServer(http.HandlerFunc(handler))
59 test.Fatalf("Wrong Token secret: %s", ssodata.TokenSecret)61 return &SingleServingServer{server, &requestContent}
60 }62}
61}63
6264func (suite *USSOTestSuite) TestGetTokenReturnsTokens(c *C) {
63func TestSignRequest(test *testing.T) {65 // Simulate a valid Ubuntu SSO Server response.
64 // Test SignRequest66 serverResponseData := map[string]string{
6567 "date_updated": "2013-01-16 14:03:36",
66 test_items := [...]string{68 "date_created": "2013-01-16 14:03:36",
67 `OAuth realm="API"`,69 "href": "/api/v2/tokens/" + tokenKey,
68 `oauth_consumer_key="rfyzhdQ"`,70 "token_name": tokenName,
69 `oauth_token="abcs"`,71 "token_key": tokenKey,
70 `oauth_signature="rwDkQkkdfdfdeAslkmmxAOjOAT%26mTBgLxtTRUdfqewqgrqsvx` +72 "token_secret": tokenSecret,
71 `lijbMWkPBajgKcoZCrDwv"`}73 "consumer_key": consumerKey,
7274 "consumer_secret": consumerSecret,
73 creds := Credentials{75 }
74 "foo@bar.com",76 jsonServerResponseData, _ := json.Marshal(serverResponseData)
75 "foobarpwd",77 server := suite.newSingleServingServer("/", string(jsonServerResponseData), 200)
76 "token_name",78 defer server.Close()
77 "http://localhost:8123"}79
78 ssodata, _ := GetToken(&creds)80 // The returned information is correct.
79 ssodata.BaseURL = "https://login.staging.ubuntu.com/api/v2/accounts/"81 creds := Credentials{Email: email, Password: password, TokenName: tokenName, SSOServerURL: server.URL}
80 request, _ := http.NewRequest(82 ssodata, err := GetToken(&creds)
81 "GET",83 c.Assert(err, IsNil)
82 "https://login.staging.ubuntu.com/api/v2/accounts/"+ssodata.ConsumerKey,84 expectedSSOData := &SSOData{ConsumerKey: consumerKey, ConsumerSecret: consumerSecret, TokenKey: tokenKey, TokenSecret: tokenSecret, TokenName: tokenName}
83 nil)85 c.Assert(ssodata, DeepEquals, expectedSSOData)
84 ssodata.Sign(request)86 // The request that the fake Ubuntu SSO Server got contained the credentials.
85 if !strings.Contains(87 expectedRequestContent, _ := json.Marshal(creds)
86 request.Header["Authorization"][0],88 c.Assert(*server.requestContent, Equals, string(expectedRequestContent))
87 `OAuth realm="API"`) {89}
88 test.Fail()90
89 }91func (suite *USSOTestSuite) TestSignRequestPlainText(c *C) {
9092 baseUrl := "https://localhost"
91 for _, c := range test_items {93 ssoData := SSOData{BaseURL: baseUrl, ConsumerKey: consumerKey, ConsumerSecret: consumerSecret, TokenKey: tokenKey, TokenName: tokenName, TokenSecret: tokenSecret}
92 if !strings.Contains(94 request, _ := http.NewRequest("GET", baseUrl, nil)
93 request.Header["Authorization"][0],95
94 c) {96 err := ssoData.Sign(request)
95 test.Fail()97
96 }98 c.Assert(err, IsNil)
97 }99 authHeader := request.Header["Authorization"][0]
100 c.Assert(authHeader, Matches, `.*OAuth realm="API".*`)
101 c.Assert(authHeader, Matches, `.*oauth_consumer_key="`+url.QueryEscape(ssoData.ConsumerKey)+`".*`)
102 c.Assert(authHeader, Matches, `.*oauth_token="`+url.QueryEscape(ssoData.TokenKey)+`".*`)
103 c.Assert(authHeader, Matches, `.*oauth_signature="`+url.QueryEscape(ssoData.ConsumerSecret+`&`+ssoData.TokenSecret)+`.*`)
98}104}

Subscribers

People subscribed via source and target branches