auth

Paddy 2014-10-16 Parent:aff6863e3cb3 Child:a5987795707e

53:28d48fdb0dd1 Go to Latest

auth/http_test.go

Test all possible successful requests, fix query setting in test. Test all the possible successful requests for an authorization code grant. Fix a bug wherein the query string wasn't actually set for the test.

History
1 package auth
3 import (
4 "html/template"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "testing"
9 )
11 const (
12 scopeSet = 1 << iota
13 stateSet
14 uriSet
15 )
17 func TestGetGrantCodeSuccess(t *testing.T) {
18 t.Parallel()
19 store := NewMemstore()
20 testContext := Context{
21 template: template.Must(template.New(getGrantTemplateName).Parse("Get auth grant")),
22 clients: store,
23 grants: store,
24 profiles: store,
25 tokens: store,
26 }
27 req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil)
28 if err != nil {
29 t.Fatal("Can't build request:", err)
30 }
31 for i := 0; i < 1<<3; i++ {
32 w := httptest.NewRecorder()
33 params := url.Values{}
34 // see OAuth 2.0 spec, section 4.1.1
35 params.Set("response_type", "code")
36 params.Set("client_id", "test_client_id")
37 if i&uriSet != 0 {
38 params.Set("redirect_uri", "https://test.secondbit.org/redirect")
39 }
40 if i&scopeSet != 0 {
41 params.Set("scope", "testscope")
42 }
43 if i&stateSet != 0 {
44 params.Set("state", "my super secure state string")
45 }
46 req.URL.RawQuery = params.Encode()
47 GetGrantHandler(w, req, testContext)
48 if w.Code != http.StatusOK {
49 t.Errorf("Expected status code to be %d, got %d for %s", http.StatusOK, w.Code, req.URL.String())
50 }
51 if w.Body.String() != "Get auth grant" {
52 t.Errorf("Expected body to be `%s`, got `%s` for %s", "Get auth grant", w.Body.String(), req.URL.String())
53 }
54 }
55 }