auth
auth/http_test.go
Update Context with new CheckEndpoint and CountEndpoints. Update the Context.CheckEndpoint method to pass through the new "strict" parameter that controls how URLs are checked for validation. Add a Context.CountEndpoints method to safely access the number of endpoints in stored for the client in the associent ClientStore. Finally, remove the error return from Context.Render, as we'd only ever want to log that, anyways. So we may as well just log it from the central function.
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 }