auth
2014-10-15
Child:28d48fdb0dd1
auth/http_test.go
Move HTTP tests to http_test.go, rename the GetGrant test. Rename the GetGrant test to something that makes it more obvious, and indicate that we're only testing for successful responses in this function (e.g., responses that should be successfully handled). Another function will deal with failure modes. Move the function to a new http_test.go file. The model files are shouldn't have information about how the models are being represented.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/http_test.go Wed Oct 15 23:52:49 2014 -0400 1.3 @@ -0,0 +1,38 @@ 1.4 +package auth 1.5 + 1.6 +import ( 1.7 + "html/template" 1.8 + "net/http" 1.9 + "net/http/httptest" 1.10 + "testing" 1.11 +) 1.12 + 1.13 +func TestGetGrantCodeSuccess(t *testing.T) { 1.14 + t.Parallel() 1.15 + store := NewMemstore() 1.16 + testContext := Context{ 1.17 + template: template.Must(template.New(getGrantTemplateName).Parse("Get auth grant")), 1.18 + clients: store, 1.19 + grants: store, 1.20 + profiles: store, 1.21 + tokens: store, 1.22 + } 1.23 + w := httptest.NewRecorder() 1.24 + req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil) 1.25 + if err != nil { 1.26 + t.Fatal("Can't build request:", err) 1.27 + } 1.28 + // see OAuth 2.0 spec, section 4.1.1 1.29 + req.URL.Query().Set("response_type", "code") 1.30 + req.URL.Query().Set("client_id", "test_client_id") 1.31 + req.URL.Query().Set("redirect_uri", "https://test.secondbit.org/redirect") 1.32 + req.URL.Query().Set("scope", "testscope") 1.33 + req.URL.Query().Set("state", "my super secure state string") 1.34 + GetGrantHandler(w, req, testContext) 1.35 + if w.Code != http.StatusOK { 1.36 + t.Errorf("Expected status code to be %d, got %d", http.StatusOK, w.Code) 1.37 + } 1.38 + if w.Body.String() != "Get auth grant" { 1.39 + t.Errorf("Expected body to be `%s`, got `%s`", "Get auth grant", w.Body.String()) 1.40 + } 1.41 +}