auth

Paddy 2014-10-15 Parent:116342ffc65f Child:28d48fdb0dd1

52:aff6863e3cb3 Browse Files

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.

grant_test.go http_test.go

     1.1 --- a/grant_test.go	Wed Oct 15 23:27:17 2014 -0400
     1.2 +++ b/grant_test.go	Wed Oct 15 23:52:49 2014 -0400
     1.3 @@ -1,9 +1,6 @@
     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  	"time"
    1.12  
    1.13 @@ -79,33 +76,3 @@
    1.14  		}
    1.15  	}
    1.16  }
    1.17 -
    1.18 -func TestGrantCodeRedirect(t *testing.T) {
    1.19 -	t.Parallel()
    1.20 -	store := NewMemstore()
    1.21 -	testContext := Context{
    1.22 -		template: template.Must(template.New(getGrantTemplateName).Parse("Get auth grant")),
    1.23 -		clients:  store,
    1.24 -		grants:   store,
    1.25 -		profiles: store,
    1.26 -		tokens:   store,
    1.27 -	}
    1.28 -	w := httptest.NewRecorder()
    1.29 -	req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil)
    1.30 -	if err != nil {
    1.31 -		t.Fatal("Can't build request:", err)
    1.32 -	}
    1.33 -	// see OAuth 2.0 spec, section 4.1.1
    1.34 -	req.URL.Query().Set("response_type", "code")
    1.35 -	req.URL.Query().Set("client_id", "test_client_id")
    1.36 -	req.URL.Query().Set("redirect_uri", "https://test.secondbit.org/redirect")
    1.37 -	req.URL.Query().Set("scope", "testscope")
    1.38 -	req.URL.Query().Set("state", "my super secure state string")
    1.39 -	GetGrantHandler(w, req, testContext)
    1.40 -	if w.Code != http.StatusOK {
    1.41 -		t.Errorf("Expected status code to be %d, got %d", http.StatusOK, w.Code)
    1.42 -	}
    1.43 -	if w.Body.String() != "Get auth grant" {
    1.44 -		t.Errorf("Expected body to be `%s`, got `%s`", "Get auth grant", w.Body.String())
    1.45 -	}
    1.46 -}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/http_test.go	Wed Oct 15 23:52:49 2014 -0400
     2.3 @@ -0,0 +1,38 @@
     2.4 +package auth
     2.5 +
     2.6 +import (
     2.7 +	"html/template"
     2.8 +	"net/http"
     2.9 +	"net/http/httptest"
    2.10 +	"testing"
    2.11 +)
    2.12 +
    2.13 +func TestGetGrantCodeSuccess(t *testing.T) {
    2.14 +	t.Parallel()
    2.15 +	store := NewMemstore()
    2.16 +	testContext := Context{
    2.17 +		template: template.Must(template.New(getGrantTemplateName).Parse("Get auth grant")),
    2.18 +		clients:  store,
    2.19 +		grants:   store,
    2.20 +		profiles: store,
    2.21 +		tokens:   store,
    2.22 +	}
    2.23 +	w := httptest.NewRecorder()
    2.24 +	req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil)
    2.25 +	if err != nil {
    2.26 +		t.Fatal("Can't build request:", err)
    2.27 +	}
    2.28 +	// see OAuth 2.0 spec, section 4.1.1
    2.29 +	req.URL.Query().Set("response_type", "code")
    2.30 +	req.URL.Query().Set("client_id", "test_client_id")
    2.31 +	req.URL.Query().Set("redirect_uri", "https://test.secondbit.org/redirect")
    2.32 +	req.URL.Query().Set("scope", "testscope")
    2.33 +	req.URL.Query().Set("state", "my super secure state string")
    2.34 +	GetGrantHandler(w, req, testContext)
    2.35 +	if w.Code != http.StatusOK {
    2.36 +		t.Errorf("Expected status code to be %d, got %d", http.StatusOK, w.Code)
    2.37 +	}
    2.38 +	if w.Body.String() != "Get auth grant" {
    2.39 +		t.Errorf("Expected body to be `%s`, got `%s`", "Get auth grant", w.Body.String())
    2.40 +	}
    2.41 +}