auth
2014-07-18
auth/authorize_test.go
Start rewriting the repo. This code originally was a carbon copy of https://github.com/RangelReale/osin, but I am methodically stripping out the extensible nature of it for a simpler interface, while simultaneously bringing the style into line with the Ducky style.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/authorize_test.go Fri Jul 18 07:13:22 2014 -0400 1.3 @@ -0,0 +1,88 @@ 1.4 +package oauth2 1.5 + 1.6 +import ( 1.7 + "net/http" 1.8 + "net/url" 1.9 + "testing" 1.10 +) 1.11 + 1.12 +func TestAuthorizeCode(t *testing.T) { 1.13 + sconfig := NewServerConfig() 1.14 + sconfig.AllowedAuthorizeTypes = AllowedAuthorizeType{CODE} 1.15 + server := NewServer(sconfig, NewTestingStorage()) 1.16 + server.AuthorizeTokenGen = &TestingAuthorizeTokenGen{} 1.17 + resp := server.NewResponse() 1.18 + 1.19 + req, err := http.NewRequest("GET", "http://localhost:14000/appauth", nil) 1.20 + if err != nil { 1.21 + t.Fatal(err) 1.22 + } 1.23 + req.Form = make(url.Values) 1.24 + req.Form.Set("response_type", string(CODE)) 1.25 + req.Form.Set("client_id", "1234") 1.26 + req.Form.Set("state", "a") 1.27 + 1.28 + if ar := server.HandleAuthorizeRequest(resp, req); ar != nil { 1.29 + ar.Authorized = true 1.30 + server.FinishAuthorizeRequest(resp, req, ar) 1.31 + } 1.32 + 1.33 + //fmt.Printf("%+v", resp) 1.34 + 1.35 + if resp.IsError && resp.InternalError != nil { 1.36 + t.Fatalf("Error in response: %s", resp.InternalError) 1.37 + } 1.38 + 1.39 + if resp.IsError { 1.40 + t.Fatalf("Should not be an error") 1.41 + } 1.42 + 1.43 + if resp.Type != REDIRECT { 1.44 + t.Fatalf("Response should be a redirect") 1.45 + } 1.46 + 1.47 + if d := resp.Output["code"]; d != "1" { 1.48 + t.Fatalf("Unexpected authorization code: %s", d) 1.49 + } 1.50 +} 1.51 + 1.52 +func TestAuthorizeToken(t *testing.T) { 1.53 + sconfig := NewServerConfig() 1.54 + sconfig.AllowedAuthorizeTypes = AllowedAuthorizeType{TOKEN} 1.55 + server := NewServer(sconfig, NewTestingStorage()) 1.56 + server.AuthorizeTokenGen = &TestingAuthorizeTokenGen{} 1.57 + server.AccessTokenGen = &TestingAccessTokenGen{} 1.58 + resp := server.NewResponse() 1.59 + 1.60 + req, err := http.NewRequest("GET", "http://localhost:14000/appauth", nil) 1.61 + if err != nil { 1.62 + t.Fatal(err) 1.63 + } 1.64 + req.Form = make(url.Values) 1.65 + req.Form.Set("response_type", string(TOKEN)) 1.66 + req.Form.Set("client_id", "1234") 1.67 + req.Form.Set("state", "a") 1.68 + 1.69 + if ar := server.HandleAuthorizeRequest(resp, req); ar != nil { 1.70 + ar.Authorized = true 1.71 + server.FinishAuthorizeRequest(resp, req, ar) 1.72 + } 1.73 + 1.74 + //fmt.Printf("%+v", resp) 1.75 + 1.76 + if resp.IsError && resp.InternalError != nil { 1.77 + t.Fatalf("Error in response: %s", resp.InternalError) 1.78 + } 1.79 + 1.80 + if resp.IsError { 1.81 + t.Fatalf("Should not be an error") 1.82 + } 1.83 + 1.84 + if resp.Type != REDIRECT || !resp.RedirectInFragment { 1.85 + t.Fatalf("Response should be a redirect with fragment") 1.86 + } 1.87 + 1.88 + if d := resp.Output["access_token"]; d != "1" { 1.89 + t.Fatalf("Unexpected access token: %s", d) 1.90 + } 1.91 +}