From 2e1ccb45d6f34a79efaf0ff761fefb772fe04e68 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 7 Dec 2024 23:16:12 +0100 Subject: [PATCH] Remove extra argument --- .../admin_handlers_integration_test.go | 46 +++++++++--------- .../auth_handlers_integration_test.go | 8 ++-- .../file_handlers_integration_test.go | 42 ++++++++-------- .../handlers/git_handlers_integration_test.go | 22 ++++----- server/internal/handlers/integration_test.go | 14 +----- .../user_handlers_integration_test.go | 36 +++++++------- .../workspace_handlers_integration_test.go | 48 +++++++++---------- 7 files changed, 103 insertions(+), 113 deletions(-) diff --git a/server/internal/handlers/admin_handlers_integration_test.go b/server/internal/handlers/admin_handlers_integration_test.go index 3a4d618..d633f92 100644 --- a/server/internal/handlers/admin_handlers_integration_test.go +++ b/server/internal/handlers/admin_handlers_integration_test.go @@ -35,7 +35,7 @@ func TestAdminHandlers_Integration(t *testing.T) { t.Run("user management", func(t *testing.T) { t.Run("list users", func(t *testing.T) { // Test with admin session - rr := h.makeRequest(t, http.MethodGet, "/api/v1/admin/users", nil, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/admin/users", nil, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var users []*models.User @@ -48,11 +48,11 @@ func TestAdminHandlers_Integration(t *testing.T) { assert.True(t, containsUser(users, h.RegularUser), "Regular user not found in users list") // Test with non-admin session - rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/users", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/users", nil, h.RegularSession) assert.Equal(t, http.StatusForbidden, rr.Code) // Test without session - rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/users", nil, nil, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/users", nil, nil) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) @@ -65,7 +65,7 @@ func TestAdminHandlers_Integration(t *testing.T) { } // Test with admin session - rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var createdUser models.User @@ -77,7 +77,7 @@ func TestAdminHandlers_Integration(t *testing.T) { assert.NotZero(t, createdUser.LastWorkspaceID) // Test duplicate email - rr = h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession) assert.Equal(t, http.StatusConflict, rr.Code) // Test invalid request (missing required fields) @@ -85,11 +85,11 @@ func TestAdminHandlers_Integration(t *testing.T) { Email: "invalid@test.com", // Missing password and role } - rr = h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", invalidReq, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", invalidReq, h.AdminSession) assert.Equal(t, http.StatusBadRequest, rr.Code) // Test with non-admin session - rr = h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.RegularSession) assert.Equal(t, http.StatusForbidden, rr.Code) }) @@ -97,7 +97,7 @@ func TestAdminHandlers_Integration(t *testing.T) { path := fmt.Sprintf("/api/v1/admin/users/%d", h.RegularUser.ID) // Test with admin session - rr := h.makeRequest(t, http.MethodGet, path, nil, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodGet, path, nil, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var user models.User @@ -106,11 +106,11 @@ func TestAdminHandlers_Integration(t *testing.T) { assert.Equal(t, h.RegularUser.ID, user.ID) // Test non-existent user - rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/users/999999", nil, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/users/999999", nil, h.AdminSession) assert.Equal(t, http.StatusNotFound, rr.Code) // Test with non-admin session - rr = h.makeRequest(t, http.MethodGet, path, nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, path, nil, h.RegularSession) assert.Equal(t, http.StatusForbidden, rr.Code) }) @@ -122,7 +122,7 @@ func TestAdminHandlers_Integration(t *testing.T) { } // Test with admin session - rr := h.makeRequest(t, http.MethodPut, path, updateReq, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodPut, path, updateReq, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var updatedUser models.User @@ -132,7 +132,7 @@ func TestAdminHandlers_Integration(t *testing.T) { assert.Equal(t, updateReq.Role, updatedUser.Role) // Test with non-admin session - rr = h.makeRequest(t, http.MethodPut, path, updateReq, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPut, path, updateReq, h.RegularSession) assert.Equal(t, http.StatusForbidden, rr.Code) }) @@ -145,7 +145,7 @@ func TestAdminHandlers_Integration(t *testing.T) { Role: models.RoleEditor, } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var createdUser models.User @@ -156,19 +156,19 @@ func TestAdminHandlers_Integration(t *testing.T) { // Test deleting own account (should fail) adminPath := fmt.Sprintf("/api/v1/admin/users/%d", h.AdminUser.ID) - rr = h.makeRequest(t, http.MethodDelete, adminPath, nil, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodDelete, adminPath, nil, h.AdminSession) assert.Equal(t, http.StatusBadRequest, rr.Code) // Test with admin session - rr = h.makeRequest(t, http.MethodDelete, path, nil, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodDelete, path, nil, h.AdminSession) assert.Equal(t, http.StatusNoContent, rr.Code) // Verify user is deleted - rr = h.makeRequest(t, http.MethodGet, path, nil, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodGet, path, nil, h.AdminSession) assert.Equal(t, http.StatusNotFound, rr.Code) // Test with non-admin session - rr = h.makeRequest(t, http.MethodDelete, path, nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodDelete, path, nil, h.RegularSession) assert.Equal(t, http.StatusForbidden, rr.Code) }) }) @@ -181,11 +181,11 @@ func TestAdminHandlers_Integration(t *testing.T) { Name: "Test Workspace", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) // Test with admin session - rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/workspaces", nil, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/workspaces", nil, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var workspaces []*handlers.WorkspaceStats @@ -207,7 +207,7 @@ func TestAdminHandlers_Integration(t *testing.T) { } // Test with non-admin session - rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/workspaces", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/workspaces", nil, h.RegularSession) assert.Equal(t, http.StatusForbidden, rr.Code) }) }) @@ -218,11 +218,11 @@ func TestAdminHandlers_Integration(t *testing.T) { UserID: h.RegularUser.ID, Name: "Stats Test Workspace", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) // Test with admin session - rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/stats", nil, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/stats", nil, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var stats handlers.SystemStats @@ -237,7 +237,7 @@ func TestAdminHandlers_Integration(t *testing.T) { assert.GreaterOrEqual(t, stats.TotalSize, int64(0)) // Test with non-admin session - rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/stats", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/admin/stats", nil, h.RegularSession) assert.Equal(t, http.StatusForbidden, rr.Code) }) } diff --git a/server/internal/handlers/auth_handlers_integration_test.go b/server/internal/handlers/auth_handlers_integration_test.go index fa979a6..4214aef 100644 --- a/server/internal/handlers/auth_handlers_integration_test.go +++ b/server/internal/handlers/auth_handlers_integration_test.go @@ -29,7 +29,7 @@ func TestAuthHandlers_Integration(t *testing.T) { Password: "admin123", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil) require.Equal(t, http.StatusOK, rr.Code) // Verify all required cookies are present with correct attributes @@ -135,7 +135,7 @@ func TestAuthHandlers_Integration(t *testing.T) { req.Body = io.NopCloser(strings.NewReader("{bad json")) rr = h.executeRequest(req) } else { - rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", tt.request, nil, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", tt.request, nil) } assert.Equal(t, tt.wantCode, rr.Code) assert.Empty(t, rr.Result().Cookies(), "failed login should not set cookies") @@ -238,7 +238,7 @@ func TestAuthHandlers_Integration(t *testing.T) { } // Verify session is actually invalidated - rr = h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularSession) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) @@ -286,7 +286,7 @@ func TestAuthHandlers_Integration(t *testing.T) { t.Run("get current user", func(t *testing.T) { t.Run("successful get current user", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var user models.User diff --git a/server/internal/handlers/file_handlers_integration_test.go b/server/internal/handlers/file_handlers_integration_test.go index e317575..0a72fab 100644 --- a/server/internal/handlers/file_handlers_integration_test.go +++ b/server/internal/handlers/file_handlers_integration_test.go @@ -27,7 +27,7 @@ func TestFileHandlers_Integration(t *testing.T) { UserID: h.RegularUser.ID, Name: "File Test Workspace", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) err := json.NewDecoder(rr.Body).Decode(workspace) @@ -37,7 +37,7 @@ func TestFileHandlers_Integration(t *testing.T) { baseURL := fmt.Sprintf("/api/v1/workspaces/%s/files", url.PathEscape(workspace.Name)) t.Run("list empty directory", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var files []storage.FileNode @@ -51,16 +51,16 @@ func TestFileHandlers_Integration(t *testing.T) { filePath := "test.md" // Save file - rr := h.makeRequestRaw(t, http.MethodPost, baseURL+"/"+filePath, strings.NewReader(content), h.RegularSession, nil) + rr := h.makeRequestRaw(t, http.MethodPost, baseURL+"/"+filePath, strings.NewReader(content), h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) // Get file content - rr = h.makeRequest(t, http.MethodGet, baseURL+"/"+filePath, nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, baseURL+"/"+filePath, nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) assert.Equal(t, content, rr.Body.String()) // List directory should now show the file - rr = h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var files []storage.FileNode @@ -80,12 +80,12 @@ func TestFileHandlers_Integration(t *testing.T) { // Create all files for path, content := range files { - rr := h.makeRequest(t, http.MethodPost, baseURL+"/"+path, content, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/"+path, content, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) } // List all files - rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var fileNodes []storage.FileNode @@ -116,11 +116,11 @@ func TestFileHandlers_Integration(t *testing.T) { // Look up a file that exists in multiple locations filename := "readme.md" dupContent := "Another readme" - rr := h.makeRequest(t, http.MethodPost, baseURL+"/projects/"+filename, dupContent, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/projects/"+filename, dupContent, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) // Search for the file - rr = h.makeRequest(t, http.MethodGet, baseURL+"/lookup?filename="+filename, nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, baseURL+"/lookup?filename="+filename, nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var response struct { @@ -131,7 +131,7 @@ func TestFileHandlers_Integration(t *testing.T) { assert.Len(t, response.Paths, 2) // Search for non-existent file - rr = h.makeRequest(t, http.MethodGet, baseURL+"/lookup?filename=nonexistent.md", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, baseURL+"/lookup?filename=nonexistent.md", nil, h.RegularSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) @@ -140,21 +140,21 @@ func TestFileHandlers_Integration(t *testing.T) { content := "This file will be deleted" // Create file - rr := h.makeRequest(t, http.MethodPost, baseURL+"/"+filePath, content, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/"+filePath, content, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) // Delete file - rr = h.makeRequest(t, http.MethodDelete, baseURL+"/"+filePath, nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodDelete, baseURL+"/"+filePath, nil, h.RegularSession) require.Equal(t, http.StatusNoContent, rr.Code) // Verify file is gone - rr = h.makeRequest(t, http.MethodGet, baseURL+"/"+filePath, nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, baseURL+"/"+filePath, nil, h.RegularSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) t.Run("last opened file", func(t *testing.T) { // Initially should be empty - rr := h.makeRequest(t, http.MethodGet, baseURL+"/last", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, baseURL+"/last", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var response struct { @@ -170,11 +170,11 @@ func TestFileHandlers_Integration(t *testing.T) { }{ FilePath: "docs/readme.md", } - rr = h.makeRequest(t, http.MethodPut, baseURL+"/last", updateReq, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPut, baseURL+"/last", updateReq, h.RegularSession) require.Equal(t, http.StatusNoContent, rr.Code) // Verify update - rr = h.makeRequest(t, http.MethodGet, baseURL+"/last", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, baseURL+"/last", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) err = json.NewDecoder(rr.Body).Decode(&response) @@ -183,7 +183,7 @@ func TestFileHandlers_Integration(t *testing.T) { // Test invalid file path updateReq.FilePath = "nonexistent.md" - rr = h.makeRequest(t, http.MethodPut, baseURL+"/last", updateReq, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPut, baseURL+"/last", updateReq, h.RegularSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) @@ -205,11 +205,11 @@ func TestFileHandlers_Integration(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // Test without session - rr := h.makeRequest(t, tc.method, tc.path, tc.body, nil, nil) + rr := h.makeRequest(t, tc.method, tc.path, tc.body, nil) assert.Equal(t, http.StatusUnauthorized, rr.Code) // Test with wrong user's session - rr = h.makeRequest(t, tc.method, tc.path, tc.body, h.AdminSession, nil) + rr = h.makeRequest(t, tc.method, tc.path, tc.body, h.AdminSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) } @@ -226,11 +226,11 @@ func TestFileHandlers_Integration(t *testing.T) { for _, path := range maliciousPaths { t.Run(path, func(t *testing.T) { // Try to read - rr := h.makeRequest(t, http.MethodGet, baseURL+"/"+path, nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, baseURL+"/"+path, nil, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) // Try to write - rr = h.makeRequest(t, http.MethodPost, baseURL+"/"+path, "malicious content", h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPost, baseURL+"/"+path, "malicious content", h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) }) } diff --git a/server/internal/handlers/git_handlers_integration_test.go b/server/internal/handlers/git_handlers_integration_test.go index f754974..c9ddba2 100644 --- a/server/internal/handlers/git_handlers_integration_test.go +++ b/server/internal/handlers/git_handlers_integration_test.go @@ -32,7 +32,7 @@ func TestGitHandlers_Integration(t *testing.T) { GitCommitMsgTemplate: "Update: {{message}}", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) err := json.NewDecoder(rr.Body).Decode(workspace) @@ -50,7 +50,7 @@ func TestGitHandlers_Integration(t *testing.T) { "message": commitMsg, } - rr := h.makeRequest(t, http.MethodPost, baseURL+"/commit", requestBody, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/commit", requestBody, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var response map[string]string @@ -70,7 +70,7 @@ func TestGitHandlers_Integration(t *testing.T) { "message": "", } - rr := h.makeRequest(t, http.MethodPost, baseURL+"/commit", requestBody, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/commit", requestBody, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) assert.Equal(t, 0, h.MockGit.GetCommitCount(), "Commit should not be called") }) @@ -83,7 +83,7 @@ func TestGitHandlers_Integration(t *testing.T) { "message": "Test message", } - rr := h.makeRequest(t, http.MethodPost, baseURL+"/commit", requestBody, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/commit", requestBody, h.RegularSession) assert.Equal(t, http.StatusInternalServerError, rr.Code) h.MockGit.SetError(nil) // Reset error state @@ -94,7 +94,7 @@ func TestGitHandlers_Integration(t *testing.T) { h.MockGit.Reset() t.Run("successful pull", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodPost, baseURL+"/pull", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/pull", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var response map[string]string @@ -109,7 +109,7 @@ func TestGitHandlers_Integration(t *testing.T) { h.MockGit.Reset() h.MockGit.SetError(fmt.Errorf("mock git error")) - rr := h.makeRequest(t, http.MethodPost, baseURL+"/pull", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, baseURL+"/pull", nil, h.RegularSession) assert.Equal(t, http.StatusInternalServerError, rr.Code) h.MockGit.SetError(nil) // Reset error state @@ -141,11 +141,11 @@ func TestGitHandlers_Integration(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // Test without session - rr := h.makeRequest(t, tc.method, tc.path, tc.body, nil, nil) + rr := h.makeRequest(t, tc.method, tc.path, tc.body, nil) assert.Equal(t, http.StatusUnauthorized, rr.Code) // Test with wrong user's session - rr = h.makeRequest(t, tc.method, tc.path, tc.body, h.AdminSession, nil) + rr = h.makeRequest(t, tc.method, tc.path, tc.body, h.AdminSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) } @@ -160,7 +160,7 @@ func TestGitHandlers_Integration(t *testing.T) { Name: "Non-Git Workspace", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", nonGitWorkspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", nonGitWorkspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) err := json.NewDecoder(rr.Body).Decode(nonGitWorkspace) @@ -170,11 +170,11 @@ func TestGitHandlers_Integration(t *testing.T) { // Try to commit commitMsg := map[string]string{"message": "test"} - rr = h.makeRequest(t, http.MethodPost, nonGitBaseURL+"/commit", commitMsg, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPost, nonGitBaseURL+"/commit", commitMsg, h.RegularSession) assert.Equal(t, http.StatusInternalServerError, rr.Code) // Try to pull - rr = h.makeRequest(t, http.MethodPost, nonGitBaseURL+"/pull", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPost, nonGitBaseURL+"/pull", nil, h.RegularSession) assert.Equal(t, http.StatusInternalServerError, rr.Code) }) }) diff --git a/server/internal/handlers/integration_test.go b/server/internal/handlers/integration_test.go index 5377a8f..bdb53fd 100644 --- a/server/internal/handlers/integration_test.go +++ b/server/internal/handlers/integration_test.go @@ -241,16 +241,11 @@ func (h *testHarness) addAuthCookies(t *testing.T, req *http.Request, session *m } // makeRequest is the main helper for making JSON requests -func (h *testHarness) makeRequest(t *testing.T, method, path string, body interface{}, session *models.Session, headers map[string]string) *httptest.ResponseRecorder { +func (h *testHarness) makeRequest(t *testing.T, method, path string, body interface{}, session *models.Session) *httptest.ResponseRecorder { t.Helper() req := h.newRequest(t, method, path, body) - // Add custom headers - for k, v := range headers { - req.Header.Set(k, v) - } - if session != nil { needsCSRF := method != http.MethodGet && method != http.MethodHead && method != http.MethodOptions csrfToken := h.addAuthCookies(t, req, session, needsCSRF) @@ -263,16 +258,11 @@ func (h *testHarness) makeRequest(t *testing.T, method, path string, body interf } // makeRequestRawWithHeaders adds support for custom headers with raw body -func (h *testHarness) makeRequestRaw(t *testing.T, method, path string, body io.Reader, session *models.Session, headers map[string]string) *httptest.ResponseRecorder { +func (h *testHarness) makeRequestRaw(t *testing.T, method, path string, body io.Reader, session *models.Session) *httptest.ResponseRecorder { t.Helper() req := h.newRequestRaw(t, method, path, body) - // Add custom headers - for k, v := range headers { - req.Header.Set(k, v) - } - if session != nil { needsCSRF := method != http.MethodGet && method != http.MethodHead && method != http.MethodOptions csrfToken := h.addAuthCookies(t, req, session, needsCSRF) diff --git a/server/internal/handlers/user_handlers_integration_test.go b/server/internal/handlers/user_handlers_integration_test.go index 378703e..480d64a 100644 --- a/server/internal/handlers/user_handlers_integration_test.go +++ b/server/internal/handlers/user_handlers_integration_test.go @@ -23,7 +23,7 @@ func TestUserHandlers_Integration(t *testing.T) { t.Run("get current user", func(t *testing.T) { t.Run("successful get", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var user models.User @@ -38,7 +38,7 @@ func TestUserHandlers_Integration(t *testing.T) { }) t.Run("unauthorized", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, nil, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/auth/me", nil, nil) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) }) @@ -49,7 +49,7 @@ func TestUserHandlers_Integration(t *testing.T) { DisplayName: "Updated Name", } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var user models.User @@ -64,7 +64,7 @@ func TestUserHandlers_Integration(t *testing.T) { CurrentPassword: currentPassword, } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var user models.User @@ -80,7 +80,7 @@ func TestUserHandlers_Integration(t *testing.T) { Email: "anotheremail@test.com", } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) }) @@ -90,7 +90,7 @@ func TestUserHandlers_Integration(t *testing.T) { CurrentPassword: "wrongpassword", } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) @@ -100,7 +100,7 @@ func TestUserHandlers_Integration(t *testing.T) { NewPassword: "newpassword123", } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) // Verify can login with new password @@ -109,7 +109,7 @@ func TestUserHandlers_Integration(t *testing.T) { Password: "newpassword123", } - rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil) assert.Equal(t, http.StatusOK, rr.Code) currentPassword = updateReq.NewPassword @@ -120,7 +120,7 @@ func TestUserHandlers_Integration(t *testing.T) { NewPassword: "newpass123", } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) }) @@ -130,7 +130,7 @@ func TestUserHandlers_Integration(t *testing.T) { NewPassword: "newpass123", } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) @@ -140,7 +140,7 @@ func TestUserHandlers_Integration(t *testing.T) { NewPassword: "short", } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) }) @@ -150,7 +150,7 @@ func TestUserHandlers_Integration(t *testing.T) { CurrentPassword: currentPassword, } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/profile", updateReq, h.RegularSession) assert.Equal(t, http.StatusConflict, rr.Code) }) }) @@ -164,7 +164,7 @@ func TestUserHandlers_Integration(t *testing.T) { Role: models.RoleEditor, } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/admin/users", createReq, h.AdminSession) require.Equal(t, http.StatusOK, rr.Code) var newUser models.User @@ -177,7 +177,7 @@ func TestUserHandlers_Integration(t *testing.T) { Password: createReq.Password, } - rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil) require.Equal(t, http.StatusOK, rr.Code) var loginResp handlers.LoginResponse @@ -197,11 +197,11 @@ func TestUserHandlers_Integration(t *testing.T) { Password: createReq.Password, } - rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, userSession, nil) + rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, userSession) require.Equal(t, http.StatusNoContent, rr.Code) // Verify user is deleted - rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/auth/login", loginReq, nil) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) @@ -210,7 +210,7 @@ func TestUserHandlers_Integration(t *testing.T) { Password: "wrongpassword", } - rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.RegularSession) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) @@ -219,7 +219,7 @@ func TestUserHandlers_Integration(t *testing.T) { Password: "admin123", // Admin password from test harness } - rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodDelete, "/api/v1/profile", deleteReq, h.AdminSession) assert.Equal(t, http.StatusForbidden, rr.Code) }) }) diff --git a/server/internal/handlers/workspace_handlers_integration_test.go b/server/internal/handlers/workspace_handlers_integration_test.go index e973b83..dbb92d7 100644 --- a/server/internal/handlers/workspace_handlers_integration_test.go +++ b/server/internal/handlers/workspace_handlers_integration_test.go @@ -20,7 +20,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { t.Run("list workspaces", func(t *testing.T) { t.Run("successful list", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var workspaces []*models.Workspace @@ -30,7 +30,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { }) t.Run("unauthorized", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces", nil, nil, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces", nil, nil) assert.Equal(t, http.StatusUnauthorized, rr.Code) }) }) @@ -41,7 +41,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { Name: "Test Workspace", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var created models.Workspace @@ -64,7 +64,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { GitCommitEmail: "test@example.com", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var created models.Workspace @@ -86,7 +86,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { // Missing required Git settings } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) }) }) @@ -95,7 +95,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { workspace := &models.Workspace{ Name: "Test Workspace Operations", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) err := json.NewDecoder(rr.Body).Decode(workspace) require.NoError(t, err) @@ -105,7 +105,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { t.Run("get workspace", func(t *testing.T) { t.Run("successful get", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var got models.Workspace @@ -116,13 +116,13 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { }) t.Run("nonexistent workspace", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/nonexistent", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/nonexistent", nil, h.RegularSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) t.Run("unauthorized access", func(t *testing.T) { // Try accessing with another user's token - rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.AdminSession, nil) + rr := h.makeRequest(t, http.MethodGet, baseURL, nil, h.AdminSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) }) @@ -131,7 +131,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { t.Run("update name", func(t *testing.T) { workspace.Name = "Updated Workspace" - rr := h.makeRequest(t, http.MethodPut, baseURL, workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, baseURL, workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var updated models.Workspace @@ -152,7 +152,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { ShowHiddenFiles: true, } - rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var updated models.Workspace @@ -176,7 +176,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { GitCommitEmail: "test@example.com", } - rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var updated models.Workspace @@ -200,14 +200,14 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { // Missing required Git settings } - rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) }) }) t.Run("last workspace", func(t *testing.T) { t.Run("get last workspace", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/last", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/last", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var response struct { @@ -225,11 +225,11 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { WorkspaceName: workspace.Name, } - rr := h.makeRequest(t, http.MethodPut, "/api/v1/workspaces/last", req, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPut, "/api/v1/workspaces/last", req, h.RegularSession) require.Equal(t, http.StatusNoContent, rr.Code) // Verify the update - rr = h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/last", nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/last", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var response struct { @@ -243,7 +243,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { t.Run("delete workspace", func(t *testing.T) { // Get current workspaces to know how many we have - rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces", nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodGet, "/api/v1/workspaces", nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var existingWorkspaces []*models.Workspace @@ -254,13 +254,13 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { newWorkspace := &models.Workspace{ Name: "Workspace To Delete", } - rr = h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", newWorkspace, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", newWorkspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) err = json.NewDecoder(rr.Body).Decode(newWorkspace) require.NoError(t, err) t.Run("successful delete", func(t *testing.T) { - rr := h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(newWorkspace.Name), nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(newWorkspace.Name), nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) var response struct { @@ -271,7 +271,7 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { assert.NotEmpty(t, response.NextWorkspaceName) // Verify workspace is deleted - rr = h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/"+url.PathEscape(newWorkspace.Name), nil, h.RegularSession, nil) + rr = h.makeRequest(t, http.MethodGet, "/api/v1/workspaces/"+url.PathEscape(newWorkspace.Name), nil, h.RegularSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) @@ -279,13 +279,13 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { // Delete all but one workspace for i := 0; i < len(existingWorkspaces)-1; i++ { ws := existingWorkspaces[i] - rr := h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(ws.Name), nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(ws.Name), nil, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) } // Try to delete the last remaining workspace lastWs := existingWorkspaces[len(existingWorkspaces)-1] - rr := h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(lastWs.Name), nil, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(lastWs.Name), nil, h.RegularSession) assert.Equal(t, http.StatusBadRequest, rr.Code) }) @@ -294,11 +294,11 @@ func TestWorkspaceHandlers_Integration(t *testing.T) { workspace := &models.Workspace{ Name: "Unauthorized Delete Test", } - rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession, nil) + rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularSession) require.Equal(t, http.StatusOK, rr.Code) // Try to delete with wrong user's token - rr = h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(workspace.Name), nil, h.AdminSession, nil) + rr = h.makeRequest(t, http.MethodDelete, "/api/v1/workspaces/"+url.PathEscape(workspace.Name), nil, h.AdminSession) assert.Equal(t, http.StatusNotFound, rr.Code) }) })