Merge lp:~rvb/usso/prod-staging into lp:usso

Proposed by Raphaël Badin
Status: Merged
Approved by: Raphaël Badin
Approved revision: 22
Merge reported by: Vincenzo Di Somma
Merged at revision: not available
Proposed branch: lp:~rvb/usso/prod-staging
Merge into: lp:usso
Diff against target: 167 lines (+93/-17)
3 files modified
example/usso_example.go (+41/-0)
usso.go (+23/-11)
usso_test.go (+29/-6)
To merge this branch: bzr merge lp:~rvb/usso/prod-staging
Reviewer Review Type Date Requested Status
Vincenzo Di Somma (community) Approve
Jeroen T. Vermeulen (community) Approve
Review via email: mp+144140@code.launchpad.net

Commit message

Remove Credentials structure and reformat GetToken accordingly. Create entities for the production and the staging servers.

This was pre-imp'ed with Vincenzo.

- Remove the Credentials struct because it makes more sense to have an abstraction for the server user and not bundle the server info with the credential info (password, email, token name). This forces the user of the library to explicitly specify which server he is using. This also gives us more room to support future versions of the Ubuntu SSO Server API.
- Add usso_example.go to show how the library is used.
- Drive-by fix: newSingleServingServer does not need to a method on USSOTestSuite, it can be a simple helper function.

To post a comment you must log in.
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

That's a lot of panic() calls, but I guess in examples and in test setup that's fine. A call to GetToken becomes a lot easier the way you did it.

review: Approve
Revision history for this message
Raphaël Badin (rvb) wrote :

> That's a lot of panic() calls, but I guess in examples and in test setup
> that's fine. A call to GetToken becomes a lot easier the way you did it.

Right, I think that in the tests and in the example, the main goal is for the errors not to be silently ignored, hence the usage of "panic()".

Thanks for the review!

Revision history for this message
Vincenzo Di Somma (vds) wrote :

I'll be much happier if you could add a sort of "doc string" to funcs and tests.

lp:~rvb/usso/prod-staging updated
20. By Raphaël Badin

Make GetToken a method on UbuntuSSOServer.

21. By Raphaël Badin

Get rid of SSOServer interface.

22. By Raphaël Badin

Add comments.

Revision history for this message
Raphaël Badin (rvb) wrote :

> I'll be much happier if you could add a sort of "doc string" to funcs and
> tests.

Good point, I've added a few comments.

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=== added directory 'example'
2=== added file 'example/usso_example.go'
3--- example/usso_example.go 1970-01-01 00:00:00 +0000
4+++ example/usso_example.go 2013-01-22 10:41:20 +0000
5@@ -0,0 +1,41 @@
6+package main
7+
8+import (
9+ "encoding/json"
10+ "fmt"
11+ "launchpad.net/usso"
12+)
13+
14+var email string
15+var password string
16+var tokenName string
17+
18+func inputParams() {
19+ fmt.Println("This application will query the staging Ubuntu SSO Server to fetch authorisation tokens.")
20+ fmt.Print("Enter email: ")
21+ fmt.Scanf("%s", &email)
22+ fmt.Print("Enter password: ")
23+ fmt.Scanf("%s", &password)
24+ fmt.Print("Enter token name: ")
25+ fmt.Scanf("%s", &tokenName)
26+}
27+
28+func main() {
29+ inputParams()
30+
31+ // Fetch the tokens using usso.GetToken.
32+ fmt.Println("Fetching tokens from staging server...")
33+ server := usso.StagingUbuntuSSOServer
34+ // One would use server := usso.ProductionUbuntuSSOServer to use the production Ubuntu SSO Server.
35+ token, err := server.GetToken(email, password, tokenName)
36+ if err != nil {
37+ panic(err)
38+ }
39+ // Format the result as json for displaying it:
40+ json_token, err := json.Marshal(token)
41+ if err != nil {
42+ panic(err)
43+ }
44+ fmt.Printf("Got tokens: %s\n", json_token)
45+
46+}
47
48=== modified file 'usso.go'
49--- usso.go 2013-01-17 14:08:33 +0000
50+++ usso.go 2013-01-22 10:41:20 +0000
51@@ -20,16 +20,24 @@
52 rand.Seed(time.Now().UTC().UnixNano())
53 }
54
55-type Credentials struct {
56- // Contains the user credentials used to get access token.
57- Email string `json:"email"`
58- Password string `json:"password"`
59- TokenName string `json:"token_name"`
60- SSOServerURL string `json:"-"`
61-}
62+type UbuntuSSOServer struct {
63+ baseUrl string
64+}
65+
66+// tokenURL returns the URL where the Ubuntu SSO tokens can be requested.
67+func (server UbuntuSSOServer) tokenURL() string {
68+ return server.baseUrl + "/api/v2/tokens"
69+}
70+
71+// ProductionUbuntuSSOServer represents the production Ubuntu SSO server
72+// located at https://login.ubuntu.com.
73+var ProductionUbuntuSSOServer = UbuntuSSOServer{"https://login.ubuntu.com"}
74+
75+// StagingUbuntuSSOServer represents the staging Ubuntu SSO server located
76+// at https://login.staging.ubuntu.com. Use it for testing.
77+var StagingUbuntuSSOServer = UbuntuSSOServer{"https://login.staging.ubuntu.com"}
78
79 type SSOData struct {
80- // Contains the
81 BaseURL string
82 ConsumerKey string `json:"consumer_key"`
83 ConsumerSecret string `json:"consumer_secret"`
84@@ -38,15 +46,19 @@
85 TokenSecret string `json:"token_secret"`
86 }
87
88-func GetToken(credentials *Credentials) (*SSOData, error) {
89- // Get a valid access token from credentials.
90+func (server UbuntuSSOServer) GetToken(email string, password string, tokenName string) (*SSOData, error) {
91+ credentials := map[string]string{
92+ "email": email,
93+ "password": password,
94+ "token_name": tokenName,
95+ }
96 json_credentials, err := json.Marshal(credentials)
97 if err != nil {
98 log.Printf("Error: %s\n", err)
99 return nil, err
100 }
101 response, err := http.Post(
102- credentials.SSOServerURL,
103+ server.tokenURL(),
104 "application/json",
105 strings.NewReader(string(json_credentials)))
106 if err != nil {
107
108=== modified file 'usso_test.go'
109--- usso_test.go 2013-01-18 16:12:41 +0000
110+++ usso_test.go 2013-01-22 10:41:20 +0000
111@@ -35,9 +35,21 @@
112 requestContent *string
113 }
114
115+// TestProductionUbuntuSSOServerURLs tests the URLs of the production server.
116+func (suite *USSOTestSuite) TestProductionUbuntuSSOServerURLs(c *C) {
117+ tokenURL := ProductionUbuntuSSOServer.tokenURL()
118+ c.Assert(tokenURL, Equals, "https://login.ubuntu.com/api/v2/tokens")
119+}
120+
121+// TestStagingUbuntuSSOServerURLs tests the URLs of the staging server.
122+func (suite *USSOTestSuite) TestStagingUbuntuSSOServerURLs(c *C) {
123+ tokenURL := StagingUbuntuSSOServer.tokenURL()
124+ c.Assert(tokenURL, Equals, "https://login.staging.ubuntu.com/api/v2/tokens")
125+}
126+
127 // newSingleServingServer create a single-serving test http server which will
128 // return only one response as defined by the passed arguments.
129-func (suite *USSOTestSuite) newSingleServingServer(uri string, response string, code int) *SingleServingServer {
130+func newSingleServingServer(uri string, response string, code int) *SingleServingServer {
131 var requestContent string
132 var requested bool
133 handler := func(w http.ResponseWriter, r *http.Request) {
134@@ -73,18 +85,29 @@
135 "consumer_key": consumerKey,
136 "consumer_secret": consumerSecret,
137 }
138- jsonServerResponseData, _ := json.Marshal(serverResponseData)
139- server := suite.newSingleServingServer("/", string(jsonServerResponseData), 200)
140+ jsonServerResponseData, err := json.Marshal(serverResponseData)
141+ if err != nil {
142+ panic(err)
143+ }
144+ server := newSingleServingServer("/api/v2/tokens", string(jsonServerResponseData), 200)
145+ var testSSOServer = &UbuntuSSOServer{server.URL}
146 defer server.Close()
147
148 // The returned information is correct.
149- creds := Credentials{Email: email, Password: password, TokenName: tokenName, SSOServerURL: server.URL}
150- ssodata, err := GetToken(&creds)
151+ ssodata, err := testSSOServer.GetToken(email, password, tokenName)
152 c.Assert(err, IsNil)
153 expectedSSOData := &SSOData{ConsumerKey: consumerKey, ConsumerSecret: consumerSecret, TokenKey: tokenKey, TokenSecret: tokenSecret, TokenName: tokenName}
154 c.Assert(ssodata, DeepEquals, expectedSSOData)
155 // The request that the fake Ubuntu SSO Server got contained the credentials.
156- expectedRequestContent, _ := json.Marshal(creds)
157+ credentials := map[string]string{
158+ "email": email,
159+ "password": password,
160+ "token_name": tokenName,
161+ }
162+ expectedRequestContent, err := json.Marshal(credentials)
163+ if err != nil {
164+ panic(err)
165+ }
166 c.Assert(*server.requestContent, Equals, string(expectedRequestContent))
167 }
168

Subscribers

People subscribed via source and target branches