feat(admin): authorize batch purchase starts
This commit is contained in:
@@ -37,6 +37,51 @@ func TestManagerRejectsTamperedAndExpiredCookies(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAuthenticatedDoesNotCreateOrDependOnCSRFValidation(t *testing.T) {
|
||||
manager := NewManager([]byte(strings.Repeat("s", 32)), false)
|
||||
missingSession := httptest.NewRequest(http.MethodPost, "/tasks/start-purchases", nil)
|
||||
if manager.IsAuthenticated(missingSession) {
|
||||
t.Fatal("missing session was treated as authenticated")
|
||||
}
|
||||
if len(manager.sessions) != 0 {
|
||||
t.Fatalf("read-only authentication check created %d sessions", len(manager.sessions))
|
||||
}
|
||||
anonymousRequest := httptest.NewRequest(http.MethodGet, "/login", nil)
|
||||
anonymousResponse := httptest.NewRecorder()
|
||||
manager.Ensure(anonymousResponse, anonymousRequest)
|
||||
anonymousCookie := anonymousResponse.Result().Cookies()[0]
|
||||
anonymousCheck := httptest.NewRequest(http.MethodPost, "/tasks/start-purchases", nil)
|
||||
anonymousCheck.AddCookie(anonymousCookie)
|
||||
if manager.IsAuthenticated(anonymousCheck) {
|
||||
t.Fatal("anonymous CSRF session was treated as authenticated")
|
||||
}
|
||||
|
||||
loginRequest := httptest.NewRequest(http.MethodPost, "/login", nil)
|
||||
loginRequest.AddCookie(anonymousCookie)
|
||||
authenticatedResponse := httptest.NewRecorder()
|
||||
csrf := manager.RotateAuthenticated(authenticatedResponse, loginRequest)
|
||||
authenticatedCookie := authenticatedResponse.Result().Cookies()[0]
|
||||
|
||||
authenticatedCheck := httptest.NewRequest(http.MethodPost, "/tasks/start-purchases", nil)
|
||||
authenticatedCheck.AddCookie(authenticatedCookie)
|
||||
if !manager.IsAuthenticated(authenticatedCheck) {
|
||||
t.Fatal("valid authenticated session was not recognized")
|
||||
}
|
||||
if authenticated, csrfOK := manager.VerifyCSRF(authenticatedCheck, "wrong-token"); authenticated || csrfOK {
|
||||
t.Fatalf("wrong token result = (%t, %t), want (false, false)", authenticated, csrfOK)
|
||||
}
|
||||
|
||||
validRequest := httptest.NewRequest(http.MethodPost, "/tasks/start-purchases", nil)
|
||||
validRequest.AddCookie(authenticatedCookie)
|
||||
if authenticated, csrfOK := manager.VerifyCSRF(validRequest, csrf); !authenticated || !csrfOK {
|
||||
t.Fatalf("valid token result = (%t, %t), want (true, true)", authenticated, csrfOK)
|
||||
}
|
||||
|
||||
if authenticated, csrfOK := manager.VerifyCSRF(httptest.NewRequest(http.MethodPost, "/tasks/start-purchases", nil), csrf); authenticated || csrfOK {
|
||||
t.Fatalf("missing session result = (%t, %t), want (false, false)", authenticated, csrfOK)
|
||||
}
|
||||
}
|
||||
|
||||
func flipCookieValue(t *testing.T, value string) string {
|
||||
t.Helper()
|
||||
if value == "" {
|
||||
|
||||
Reference in New Issue
Block a user