auth

Paddy 2014-11-20 Parent:eb3f2938a319 Child:11ad5eca2f82

80:dfb10e19de87 Browse Files

Add tests for redirecting to the login page. Make sure that we're redirecting to the configured login page (or returning an error) as expected when trying to obtain a grant code.

oauth2_test.go

     1.1 --- a/oauth2_test.go	Thu Nov 20 01:01:22 2014 -0500
     1.2 +++ b/oauth2_test.go	Thu Nov 20 01:32:15 2014 -0500
     1.3 @@ -520,6 +520,40 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 +func TestGetGrantCodeLoginRedirect(t *testing.T) {
     1.8 +	t.Parallel()
     1.9 +	req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil)
    1.10 +	if err != nil {
    1.11 +		t.Fatal("Can't build request:", err)
    1.12 +	}
    1.13 +	w := httptest.NewRecorder()
    1.14 +	GetGrantHandler(w, req, Context{template: template.Must(template.New(getGrantTemplateName).Parse("{{ .internal_error }}"))})
    1.15 +	if w.Code != http.StatusInternalServerError {
    1.16 +		t.Errorf("Expected status code to be %d, got %d", http.StatusInternalServerError, w.Code)
    1.17 +	}
    1.18 +	expectation := "Missing login URL."
    1.19 +	if w.Body.String() != expectation {
    1.20 +		t.Errorf(`Expected body to be "%s", got "%s"`, expectation, w.Body.String())
    1.21 +	}
    1.22 +	uri, err := url.Parse("https://client.secondbit.org/")
    1.23 +	if err != nil {
    1.24 +		t.Errorf("Unexpected error", err)
    1.25 +	}
    1.26 +	testContext := Context{
    1.27 +		loginURI: uri,
    1.28 +	}
    1.29 +	w = httptest.NewRecorder()
    1.30 +	GetGrantHandler(w, req, testContext)
    1.31 +	if w.Code != http.StatusFound {
    1.32 +		t.Errorf("Expected status code to be %d, got %d", http.StatusFound, w.Code)
    1.33 +	}
    1.34 +	redirectedTo := w.Header().Get("Location")
    1.35 +	expectation = "https://client.secondbit.org/?from=https%3A%2F%2Ftest.auth.secondbit.org%2Foauth2%2Fgrant"
    1.36 +	if redirectedTo != expectation {
    1.37 +		t.Errorf("Expected to be redirected to '%s', was redirected to '%s'", expectation, redirectedTo)
    1.38 +	}
    1.39 +}
    1.40 +
    1.41  func TestGetBasicAuth(t *testing.T) {
    1.42  	tests := map[string]struct {
    1.43  		un   string