auth

Paddy 2014-12-06 Parent:11ad5eca2f82 Child:1dc4e152e3b0

82:0a6e3f14b054 Go to Latest

auth/oauth2_test.go

Fix go vet, fix imports, render JSON errors, deprecate getBasicAuth. Fix logging functions in our test file that were causing go vet to report errors (and which would have obscured the test output). Move some imports around to make the imports more consistent and our pre-commit hook happy. Create a helper to render JSON errors, and actually render those errors when obtaining a token using a grant. Deprecate our custom getBasicAuth in favour of the new BasicAuth() method on net/http.*Request objects, which was introduced in Go 1.4 (meaning Go 1.4 is now a requirement for compiling this.)

History
     1.1 --- a/oauth2_test.go	Sat Dec 06 00:35:03 2014 -0500
     1.2 +++ b/oauth2_test.go	Sat Dec 06 01:47:34 2014 -0500
     1.3 @@ -132,7 +132,7 @@
     1.4  		}
     1.5  		err = testContext.DeleteGrant(red.Query().Get("code"))
     1.6  		if err != nil {
     1.7 -			t.Log(`Unexpected error "%s" deleting grant "%s" for %s`, err, red.Query().Get("code"), req.URL.String())
     1.8 +			t.Logf(`Unexpected error "%s" deleting grant "%s" for %s`, err, red.Query().Get("code"), req.URL.String())
     1.9  		}
    1.10  		stripParam("code", red)
    1.11  		if red.Query().Get("state") != params.Get("state") {
    1.12 @@ -538,7 +538,7 @@
    1.13  	}
    1.14  	uri, err := url.Parse("https://client.secondbit.org/")
    1.15  	if err != nil {
    1.16 -		t.Errorf("Unexpected error", err)
    1.17 +		t.Error("Unexpected error", err)
    1.18  	}
    1.19  	testContext := Context{
    1.20  		loginURI: uri,
    1.21 @@ -555,39 +555,6 @@
    1.22  	}
    1.23  }
    1.24  
    1.25 -func TestGetBasicAuth(t *testing.T) {
    1.26 -	tests := map[string]struct {
    1.27 -		un   string
    1.28 -		pass string
    1.29 -		err  error
    1.30 -	}{
    1.31 -		"Basic dGVzdHVzZXI6cGFzc3dvcmQx": {"testuser", "password1", nil},
    1.32 -		"": {"", "", ErrNoAuth},
    1.33 -		"dGVzdHVzZXI6cGFzc3dvcmQx": {"", "", ErrInvalidAuthFormat},
    1.34 -		"Basic _*&^##$@#$@&!!@":    {"", "", ErrInvalidAuthFormat},
    1.35 -		"Basic abcdefgh":           {"", "", ErrInvalidAuthFormat},
    1.36 -		"Basic dXNlcjo=":           {"user", "", nil},
    1.37 -	}
    1.38 -
    1.39 -	for header, test := range tests {
    1.40 -		req, err := http.NewRequest("GET", "https://auth.secondbit.org", nil)
    1.41 -		if err != nil {
    1.42 -			t.Error("Unexpected error creating base request:", err)
    1.43 -		}
    1.44 -		req.Header.Set("Authorization", header)
    1.45 -		un, pass, err := getBasicAuth(req)
    1.46 -		if un != test.un {
    1.47 -			t.Errorf(`Expected header "%s" to return username "%s", got "%s"`, header, test.un, un)
    1.48 -		}
    1.49 -		if pass != test.pass {
    1.50 -			t.Errorf(`Expected header "%s" to return password "%s", got "%s"`, header, test.pass, pass)
    1.51 -		}
    1.52 -		if err != test.err {
    1.53 -			t.Errorf(`Expected header "%s" to return error "%s", got "%s"`, header, test.err, err)
    1.54 -		}
    1.55 -	}
    1.56 -}
    1.57 -
    1.58  func TestCheckCookie(t *testing.T) {
    1.59  	t.Parallel()
    1.60  	req, err := http.NewRequest("GET", "https://auth.secondbit.org", nil)