auth
auth/oauth2_test.go
Rename Grant to AuthorizationCode. God bless gofmt. Rename all our instances of Grant to AuthorizationCode (including related variables and types, like grantStore and ErrGrantNotFound, plus all our comments and error strings. Whew.) to better reflect that it is only a single type of grant that could be accepted by the server.
1.1 --- a/oauth2_test.go Sun Dec 07 02:54:42 2014 -0500 1.2 +++ b/oauth2_test.go Sun Dec 07 03:40:25 2014 -0500 1.3 @@ -26,16 +26,16 @@ 1.4 u.RawQuery = q.Encode() 1.5 } 1.6 1.7 -func TestGetGrantCodeSuccess(t *testing.T) { 1.8 +func TestGetAuthorizationCodeCodeSuccess(t *testing.T) { 1.9 t.Parallel() 1.10 store := NewMemstore() 1.11 testContext := Context{ 1.12 - template: template.Must(template.New(getGrantTemplateName).Parse("Get auth grant")), 1.13 - clients: store, 1.14 - grants: store, 1.15 - profiles: store, 1.16 - tokens: store, 1.17 - sessions: store, 1.18 + template: template.Must(template.New(getAuthorizationCodeTemplateName).Parse("Get auth grant")), 1.19 + clients: store, 1.20 + authCodes: store, 1.21 + profiles: store, 1.22 + tokens: store, 1.23 + sessions: store, 1.24 } 1.25 client := Client{ 1.26 ID: uuid.NewID(), 1.27 @@ -108,7 +108,7 @@ 1.28 req.Method = "GET" 1.29 req.Body = nil 1.30 req.Header.Del("Content-Type") 1.31 - GetGrantHandler(w, req, testContext) 1.32 + GetAuthorizationCodeHandler(w, req, testContext) 1.33 if w.Code != http.StatusOK { 1.34 t.Errorf("Expected status code to be %d, got %d for %s", http.StatusOK, w.Code, req.URL.String()) 1.35 } 1.36 @@ -122,7 +122,7 @@ 1.37 data.Set("grant", "approved") 1.38 body := bytes.NewBufferString(data.Encode()) 1.39 req.Body = ioutil.NopCloser(body) 1.40 - GetGrantHandler(w, req, testContext) 1.41 + GetAuthorizationCodeHandler(w, req, testContext) 1.42 if w.Code != http.StatusFound { 1.43 t.Errorf("Expected status code to be %d, got %d for %s", http.StatusFound, w.Code, req.URL.String()) 1.44 } 1.45 @@ -135,10 +135,10 @@ 1.46 if red.Query().Get("code") == "" { 1.47 t.Fatalf(`Expected code param in redirect URL to be set, but it wasn't for %s`, req.URL.String()) 1.48 } 1.49 - if _, err := testContext.GetGrant(red.Query().Get("code")); err != nil { 1.50 + if _, err := testContext.GetAuthorizationCode(red.Query().Get("code")); err != nil { 1.51 t.Fatalf(`Unexpected error "%s: retrieving the grant "%s" supplied in the redirect URL for %s`, err, red.Query().Get("code"), req.URL.String()) 1.52 } 1.53 - err = testContext.DeleteGrant(red.Query().Get("code")) 1.54 + err = testContext.DeleteAuthorizationCode(red.Query().Get("code")) 1.55 if err != nil { 1.56 t.Logf(`Unexpected error "%s" deleting grant "%s" for %s`, err, red.Query().Get("code"), req.URL.String()) 1.57 } 1.58 @@ -153,16 +153,16 @@ 1.59 } 1.60 } 1.61 1.62 -func TestGetGrantCodeInvalidClient(t *testing.T) { 1.63 +func TestGetAuthorizationCodeCodeInvalidClient(t *testing.T) { 1.64 t.Parallel() 1.65 store := NewMemstore() 1.66 testContext := Context{ 1.67 - template: template.Must(template.New(getGrantTemplateName).Parse("{{ .error }}")), 1.68 - clients: store, 1.69 - grants: store, 1.70 - profiles: store, 1.71 - tokens: store, 1.72 - sessions: store, 1.73 + template: template.Must(template.New(getAuthorizationCodeTemplateName).Parse("{{ .error }}")), 1.74 + clients: store, 1.75 + authCodes: store, 1.76 + profiles: store, 1.77 + tokens: store, 1.78 + sessions: store, 1.79 } 1.80 client := Client{ 1.81 ID: uuid.NewID(), 1.82 @@ -197,7 +197,7 @@ 1.83 Value: session.ID, 1.84 } 1.85 req.AddCookie(cookie) 1.86 - GetGrantHandler(w, req, testContext) 1.87 + GetAuthorizationCodeHandler(w, req, testContext) 1.88 if w.Code != http.StatusBadRequest { 1.89 t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.90 } 1.91 @@ -207,7 +207,7 @@ 1.92 w = httptest.NewRecorder() 1.93 params.Set("client_id", "Not an ID") 1.94 req.URL.RawQuery = params.Encode() 1.95 - GetGrantHandler(w, req, testContext) 1.96 + GetAuthorizationCodeHandler(w, req, testContext) 1.97 if w.Code != http.StatusBadRequest { 1.98 t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.99 } 1.100 @@ -217,7 +217,7 @@ 1.101 w = httptest.NewRecorder() 1.102 params.Set("client_id", uuid.NewID().String()) 1.103 req.URL.RawQuery = params.Encode() 1.104 - GetGrantHandler(w, req, testContext) 1.105 + GetAuthorizationCodeHandler(w, req, testContext) 1.106 if w.Code != http.StatusBadRequest { 1.107 t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.108 } 1.109 @@ -226,16 +226,16 @@ 1.110 } 1.111 } 1.112 1.113 -func TestGetGrantCodeInvalidURI(t *testing.T) { 1.114 +func TestGetAuthorizationCodeCodeInvalidURI(t *testing.T) { 1.115 t.Parallel() 1.116 store := NewMemstore() 1.117 testContext := Context{ 1.118 - template: template.Must(template.New(getGrantTemplateName).Parse("{{ .error }}")), 1.119 - clients: store, 1.120 - grants: store, 1.121 - profiles: store, 1.122 - tokens: store, 1.123 - sessions: store, 1.124 + template: template.Must(template.New(getAuthorizationCodeTemplateName).Parse("{{ .error }}")), 1.125 + clients: store, 1.126 + authCodes: store, 1.127 + profiles: store, 1.128 + tokens: store, 1.129 + sessions: store, 1.130 } 1.131 client := Client{ 1.132 ID: uuid.NewID(), 1.133 @@ -274,7 +274,7 @@ 1.134 params.Set("response_type", "code") 1.135 params.Set("client_id", client.ID.String()) 1.136 req.URL.RawQuery = params.Encode() 1.137 - GetGrantHandler(w, req, testContext) 1.138 + GetAuthorizationCodeHandler(w, req, testContext) 1.139 if w.Code != http.StatusBadRequest { 1.140 t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.141 } 1.142 @@ -294,7 +294,7 @@ 1.143 w = httptest.NewRecorder() 1.144 params.Set("redirect_uri", "https://test.secondbit.org/wrong") 1.145 req.URL.RawQuery = params.Encode() 1.146 - GetGrantHandler(w, req, testContext) 1.147 + GetAuthorizationCodeHandler(w, req, testContext) 1.148 if w.Code != http.StatusBadRequest { 1.149 t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.150 } 1.151 @@ -314,7 +314,7 @@ 1.152 w = httptest.NewRecorder() 1.153 params.Set("redirect_uri", "") 1.154 req.URL.RawQuery = params.Encode() 1.155 - GetGrantHandler(w, req, testContext) 1.156 + GetAuthorizationCodeHandler(w, req, testContext) 1.157 if w.Code != http.StatusBadRequest { 1.158 t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.159 } 1.160 @@ -324,7 +324,7 @@ 1.161 w = httptest.NewRecorder() 1.162 params.Set("redirect_uri", "://not a URL") 1.163 req.URL.RawQuery = params.Encode() 1.164 - GetGrantHandler(w, req, testContext) 1.165 + GetAuthorizationCodeHandler(w, req, testContext) 1.166 if w.Code != http.StatusBadRequest { 1.167 t.Errorf("Expected status code to be %d, got %d", http.StatusBadRequest, w.Code) 1.168 } 1.169 @@ -333,16 +333,16 @@ 1.170 } 1.171 } 1.172 1.173 -func TestGetGrantCodeInvalidResponseType(t *testing.T) { 1.174 +func TestGetAuthorizationCodeCodeInvalidResponseType(t *testing.T) { 1.175 t.Parallel() 1.176 store := NewMemstore() 1.177 testContext := Context{ 1.178 - template: template.Must(template.New(getGrantTemplateName).Parse("{{ .error }}")), 1.179 - clients: store, 1.180 - grants: store, 1.181 - profiles: store, 1.182 - tokens: store, 1.183 - sessions: store, 1.184 + template: template.Must(template.New(getAuthorizationCodeTemplateName).Parse("{{ .error }}")), 1.185 + clients: store, 1.186 + authCodes: store, 1.187 + profiles: store, 1.188 + tokens: store, 1.189 + sessions: store, 1.190 } 1.191 client := Client{ 1.192 ID: uuid.NewID(), 1.193 @@ -396,7 +396,7 @@ 1.194 params.Set("state", "my super secure state string") 1.195 req.URL.RawQuery = params.Encode() 1.196 w := httptest.NewRecorder() 1.197 - GetGrantHandler(w, req, testContext) 1.198 + GetAuthorizationCodeHandler(w, req, testContext) 1.199 if w.Code != http.StatusFound { 1.200 t.Errorf("Expected status code to be %d, got %d", http.StatusFound, w.Code) 1.201 } 1.202 @@ -418,7 +418,7 @@ 1.203 } 1.204 stripParam("response_type", req.URL) 1.205 w = httptest.NewRecorder() 1.206 - GetGrantHandler(w, req, testContext) 1.207 + GetAuthorizationCodeHandler(w, req, testContext) 1.208 if w.Code != http.StatusFound { 1.209 t.Errorf("Expected status code to be %d, got %d", http.StatusFound, w.Code) 1.210 } 1.211 @@ -440,16 +440,16 @@ 1.212 } 1.213 } 1.214 1.215 -func TestGetGrantCodeDenied(t *testing.T) { 1.216 +func TestGetAuthorizationCodeCodeDenied(t *testing.T) { 1.217 t.Parallel() 1.218 store := NewMemstore() 1.219 testContext := Context{ 1.220 - template: template.Must(template.New(getGrantTemplateName).Parse("{{ .error }}")), 1.221 - clients: store, 1.222 - grants: store, 1.223 - profiles: store, 1.224 - tokens: store, 1.225 - sessions: store, 1.226 + template: template.Must(template.New(getAuthorizationCodeTemplateName).Parse("{{ .error }}")), 1.227 + clients: store, 1.228 + authCodes: store, 1.229 + profiles: store, 1.230 + tokens: store, 1.231 + sessions: store, 1.232 } 1.233 client := Client{ 1.234 ID: uuid.NewID(), 1.235 @@ -507,7 +507,7 @@ 1.236 req.Body = ioutil.NopCloser(bytes.NewBufferString(data.Encode())) 1.237 req.Method = "POST" 1.238 w := httptest.NewRecorder() 1.239 - GetGrantHandler(w, req, testContext) 1.240 + GetAuthorizationCodeHandler(w, req, testContext) 1.241 if w.Code != http.StatusFound { 1.242 t.Errorf("Expected status code to be %d, got %d", http.StatusFound, w.Code) 1.243 } 1.244 @@ -529,14 +529,14 @@ 1.245 } 1.246 } 1.247 1.248 -func TestGetGrantCodeLoginRedirect(t *testing.T) { 1.249 +func TestGetAuthorizationCodeCodeLoginRedirect(t *testing.T) { 1.250 t.Parallel() 1.251 req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil) 1.252 if err != nil { 1.253 t.Fatal("Can't build request:", err) 1.254 } 1.255 w := httptest.NewRecorder() 1.256 - GetGrantHandler(w, req, Context{template: template.Must(template.New(getGrantTemplateName).Parse("{{ .internal_error }}"))}) 1.257 + GetAuthorizationCodeHandler(w, req, Context{template: template.Must(template.New(getAuthorizationCodeTemplateName).Parse("{{ .internal_error }}"))}) 1.258 if w.Code != http.StatusInternalServerError { 1.259 t.Errorf("Expected status code to be %d, got %d", http.StatusInternalServerError, w.Code) 1.260 } 1.261 @@ -552,7 +552,7 @@ 1.262 loginURI: uri, 1.263 } 1.264 w = httptest.NewRecorder() 1.265 - GetGrantHandler(w, req, testContext) 1.266 + GetAuthorizationCodeHandler(w, req, testContext) 1.267 if w.Code != http.StatusFound { 1.268 t.Errorf("Expected status code to be %d, got %d", http.StatusFound, w.Code) 1.269 } 1.270 @@ -752,9 +752,9 @@ 1.271 t.Parallel() 1.272 store := NewMemstore() 1.273 context := Context{ 1.274 - clients: store, 1.275 - grants: store, 1.276 - tokens: store, 1.277 + clients: store, 1.278 + authCodes: store, 1.279 + tokens: store, 1.280 } 1.281 client := Client{ 1.282 ID: uuid.NewID(), 1.283 @@ -765,7 +765,7 @@ 1.284 Website: "https://client.secondbit.org/", 1.285 Type: "confidential", 1.286 } 1.287 - grant := Grant{ 1.288 + authCode := AuthorizationCode{ 1.289 Code: "testcode", 1.290 Created: time.Now(), 1.291 ExpiresIn: 600, 1.292 @@ -775,9 +775,9 @@ 1.293 State: "teststate", 1.294 ProfileID: uuid.NewID(), 1.295 } 1.296 - err := context.SaveGrant(grant) 1.297 + err := context.SaveAuthorizationCode(authCode) 1.298 if err != nil { 1.299 - t.Error("Error saving grant:", err) 1.300 + t.Error("Error saving auth code:", err) 1.301 } 1.302 err = context.SaveClient(client) 1.303 if err != nil { 1.304 @@ -785,8 +785,8 @@ 1.305 } 1.306 data := url.Values{} 1.307 data.Set("grant_type", "authorization_code") 1.308 - data.Set("code", grant.Code) 1.309 - data.Set("redirect_uri", grant.RedirectURI) 1.310 + data.Set("code", authCode.Code) 1.311 + data.Set("redirect_uri", authCode.RedirectURI) 1.312 body := bytes.NewBufferString(data.Encode()) 1.313 req, err := http.NewRequest("POST", "https://auth.secondbit.org/", ioutil.NopCloser(body)) 1.314 if err != nil { 1.315 @@ -814,7 +814,7 @@ 1.316 if resp.ExpiresIn == 0 { 1.317 t.Error("Got blank expires in back") 1.318 } 1.319 - tokens, err := context.GetTokensByProfileID(grant.ProfileID, 1, 0) 1.320 + tokens, err := context.GetTokensByProfileID(authCode.ProfileID, 1, 0) 1.321 if err != nil { 1.322 t.Error("Error retrieving token:", err) 1.323 }