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