auth
78:a9936cf794ba Browse Files
More tests, login redirect bugfix. Add tests for our cookie checking helper and our helper for generating login redirection URIs. Fix a bug where the URL to redirect to was being URL-encoded twice when included in the login redirect URI.
1.1 --- a/oauth2.go Tue Nov 18 03:28:14 2014 -0500 1.2 +++ b/oauth2.go Wed Nov 19 00:17:34 2014 -0500 1.3 @@ -89,7 +89,7 @@ 1.4 } 1.5 uri := *context.loginURI 1.6 q := uri.Query() 1.7 - q.Set("from", url.QueryEscape(r.URL.String())) 1.8 + q.Set("from", r.URL.String()) 1.9 uri.RawQuery = q.Encode() 1.10 return uri.String() 1.11 }
2.1 --- a/oauth2_test.go Tue Nov 18 03:28:14 2014 -0500 2.2 +++ b/oauth2_test.go Wed Nov 19 00:17:34 2014 -0500 2.3 @@ -552,3 +552,121 @@ 2.4 } 2.5 } 2.6 } 2.7 + 2.8 +func TestCheckCookie(t *testing.T) { 2.9 + t.Parallel() 2.10 + req, err := http.NewRequest("GET", "https://auth.secondbit.org", nil) 2.11 + if err != nil { 2.12 + t.Error("Unexpected error creating base request:", err) 2.13 + } 2.14 + store := NewMemstore() 2.15 + testContext := Context{ 2.16 + sessions: store, 2.17 + } 2.18 + session, err := checkCookie(req, testContext) 2.19 + if err != ErrNoSession { 2.20 + t.Errorf("Expected ErrNoSession, got %s", err) 2.21 + } 2.22 + session = Session{ 2.23 + ID: "testsession", 2.24 + Active: true, 2.25 + } 2.26 + err = testContext.CreateSession(session) 2.27 + if err != nil { 2.28 + t.Error("Unexpected error persisting session:", err) 2.29 + } 2.30 + invalidSession := Session{ 2.31 + ID: "testsession2", 2.32 + Active: false, 2.33 + } 2.34 + err = testContext.CreateSession(invalidSession) 2.35 + if err != nil { 2.36 + t.Error("Unexpected error persisting session:", err) 2.37 + } 2.38 + result, err := checkCookie(req, testContext) 2.39 + if err != ErrNoSession { 2.40 + t.Errorf("Expected ErrNoSession, got %s", err) 2.41 + } 2.42 + req.AddCookie(&http.Cookie{ 2.43 + Name: "wrongcookie", 2.44 + Value: "wrong value", 2.45 + }) 2.46 + result, err = checkCookie(req, testContext) 2.47 + if err != ErrNoSession { 2.48 + t.Error("Expected ErrNoSession, got", err) 2.49 + } 2.50 + req, err = http.NewRequest("GET", "https://auth.secondbit.org", nil) 2.51 + if err != nil { 2.52 + t.Error("Unexpected error creating base request:", err) 2.53 + } 2.54 + req.AddCookie(&http.Cookie{ 2.55 + Name: "Stillwrongcookie", 2.56 + Value: session.ID, 2.57 + }) 2.58 + result, err = checkCookie(req, testContext) 2.59 + if err != ErrNoSession { 2.60 + t.Error("Expected ErrNoSession, got", err) 2.61 + } 2.62 + req, err = http.NewRequest("GET", "https://auth.secondbit.org", nil) 2.63 + if err != nil { 2.64 + t.Error("Unexpected error creating base request:", err) 2.65 + } 2.66 + req.AddCookie(&http.Cookie{ 2.67 + Name: authCookieName, 2.68 + Value: "wrong value", 2.69 + }) 2.70 + result, err = checkCookie(req, testContext) 2.71 + if err != ErrInvalidSession { 2.72 + t.Error("Expected ErrInvalidSession, got", err) 2.73 + } 2.74 + req, err = http.NewRequest("GET", "https://auth.secondbit.org", nil) 2.75 + if err != nil { 2.76 + t.Error("Unexpected error creating base request:", err) 2.77 + } 2.78 + req.AddCookie(&http.Cookie{ 2.79 + Name: authCookieName, 2.80 + Value: invalidSession.ID, 2.81 + }) 2.82 + result, err = checkCookie(req, testContext) 2.83 + if err != ErrInvalidSession { 2.84 + t.Error("Expected ErrInvalidSession, got", err) 2.85 + } 2.86 + req, err = http.NewRequest("GET", "https://auth.secondbit.org", nil) 2.87 + if err != nil { 2.88 + t.Error("Unexpected error creating base request:", err) 2.89 + } 2.90 + req.AddCookie(&http.Cookie{ 2.91 + Name: authCookieName, 2.92 + Value: session.ID, 2.93 + }) 2.94 + result, err = checkCookie(req, testContext) 2.95 + if err != nil { 2.96 + t.Error("Unexpected error:", err) 2.97 + } 2.98 + success, field, expectation, outcome := compareSessions(session, result) 2.99 + if !success { 2.100 + t.Errorf(`Expected field %s to be %v, but got %v`, field, expectation, outcome) 2.101 + } 2.102 +} 2.103 + 2.104 +func TestBuildLoginRedirect(t *testing.T) { 2.105 + t.Parallel() 2.106 + req, err := http.NewRequest("GET", "https://client.secondbit.org/my/awesome/path?has=query¶ms=to&screw=this&all=up", nil) 2.107 + if err != nil { 2.108 + t.Error("Unexpected error creating base request:", err) 2.109 + } 2.110 + result := buildLoginRedirect(req, Context{}) 2.111 + if result != "" { 2.112 + t.Error("Expected empty string as the result, got", result) 2.113 + } 2.114 + uri, err := url.Parse("https://auth.secondbit.org/login?query=string&other=param") 2.115 + if err != nil { 2.116 + t.Error("Unexpected error parsing URL:", err) 2.117 + } 2.118 + c := Context{loginURI: uri} 2.119 + result = buildLoginRedirect(req, c) 2.120 + expectation := "https://auth.secondbit.org/login?from=https%3A%2F%2Fclient.secondbit.org%2Fmy%2Fawesome%2Fpath%3Fhas%3Dquery%26params%3Dto%26screw%3Dthis%26all%3Dup&other=param&query=string" 2.121 + if result != expectation { 2.122 + t.Errorf(`Expected result string to be "%s", was "%s"`, expectation, result) 2.123 + } 2.124 +}