auth
auth/http_test.go
Parse the redirect URI early, add new failure modes to tests. When obtaining a grant code, parse the redirect_uri early as a URL, so we can fail without even hitting the database, if possible. Add a test to cover the scenario where the client doesn't specify an endpoint, and the client has no endpoints registered. Add a test to cover the scenario where the client has two endpoints registered, but doesn't specify an endpoint. Fix the test that tested for invalid URLs by actually using an invalid URL. Apparently, "not a URL" is a valid URL. Go figure.
1.1 --- a/http_test.go Sun Nov 02 21:15:46 2014 -0500 1.2 +++ b/http_test.go Sun Nov 02 22:28:49 2014 -0500 1.3 @@ -164,20 +164,10 @@ 1.4 if err != nil { 1.5 t.Fatal("Can't parse URL:", err) 1.6 } 1.7 - endpoint := Endpoint{ 1.8 - ID: uuid.NewID(), 1.9 - ClientID: client.ID, 1.10 - URI: *uri, 1.11 - Added: time.Now(), 1.12 - } 1.13 err = testContext.SaveClient(client) 1.14 if err != nil { 1.15 t.Fatal("Can't store client:", err) 1.16 } 1.17 - err = testContext.AddEndpoint(client.ID, endpoint) 1.18 - if err != nil { 1.19 - t.Fatal("Can't store endpoint:", err) 1.20 - } 1.21 req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil) 1.22 if err != nil { 1.23 t.Fatal("Can't build request:", err) 1.24 @@ -186,6 +176,25 @@ 1.25 params := url.Values{} 1.26 params.Set("response_type", "code") 1.27 params.Set("client_id", client.ID.String()) 1.28 + req.URL.RawQuery = params.Encode() 1.29 + GetGrantHandler(w, req, testContext) 1.30 + if w.Code != http.StatusBadRequest { 1.31 + t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.32 + } 1.33 + if w.Body.String() != "The redirect_uri specified is not valid." { 1.34 + t.Errorf(`Expected output to be "%s", got "%s" instead.`, "The redirect_uri specified is not valid.", w.Body.String()) 1.35 + } 1.36 + endpoint := Endpoint{ 1.37 + ID: uuid.NewID(), 1.38 + ClientID: client.ID, 1.39 + URI: *uri, 1.40 + Added: time.Now(), 1.41 + } 1.42 + err = testContext.AddEndpoint(client.ID, endpoint) 1.43 + if err != nil { 1.44 + t.Fatal("Can't store endpoint:", err) 1.45 + } 1.46 + w = httptest.NewRecorder() 1.47 params.Set("redirect_uri", "https://test.secondbit.org/wrong") 1.48 req.URL.RawQuery = params.Encode() 1.49 GetGrantHandler(w, req, testContext) 1.50 @@ -195,12 +204,28 @@ 1.51 if w.Body.String() != "The redirect_uri specified is not valid." { 1.52 t.Errorf(`Expected output to be "%s", got "%s" instead.`, "The redirect_uri specified is not valid.", w.Body.String()) 1.53 } 1.54 - req, err = http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil) 1.55 + endpoint2 := Endpoint{ 1.56 + ID: uuid.NewID(), 1.57 + ClientID: client.ID, 1.58 + URI: *uri, 1.59 + Added: time.Now(), 1.60 + } 1.61 + err = testContext.AddEndpoint(client.ID, endpoint2) 1.62 if err != nil { 1.63 - t.Fatal("Can't build request:", err) 1.64 + t.Fatal("Can't store endpoint:", err) 1.65 } 1.66 w = httptest.NewRecorder() 1.67 - params.Set("redirect_uri", "not a URL") 1.68 + params.Set("redirect_uri", "") 1.69 + req.URL.RawQuery = params.Encode() 1.70 + GetGrantHandler(w, req, testContext) 1.71 + if w.Code != http.StatusBadRequest { 1.72 + t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.73 + } 1.74 + if w.Body.String() != "The redirect_uri specified is not valid." { 1.75 + t.Errorf(`Expected output to be "%s", got "%s" instead.`, "The redirect_uri specified is not valid.", w.Body.String()) 1.76 + } 1.77 + w = httptest.NewRecorder() 1.78 + params.Set("redirect_uri", "://not a URL") 1.79 req.URL.RawQuery = params.Encode() 1.80 GetGrantHandler(w, req, testContext) 1.81 if w.Code != http.StatusBadRequest {