Update handlers and db

This commit is contained in:
2024-11-30 16:12:51 +01:00
parent 4359267d55
commit 31d00681a1
7 changed files with 48 additions and 29 deletions

View File

@@ -27,8 +27,8 @@ func TestMigrate(t *testing.T) {
t.Fatalf("failed to get migration version: %v", err)
}
if version != 2 { // Current number of migrations in production code
t.Errorf("expected migration version 2, got %d", version)
if version != 1 { // Current number of migrations in production code
t.Errorf("expected migration version 1, got %d", version)
}
// Verify number of migration entries matches versions applied
@@ -38,8 +38,8 @@ func TestMigrate(t *testing.T) {
t.Fatalf("failed to count migrations: %v", err)
}
if count != 2 {
t.Errorf("expected 2 migration entries, got %d", count)
if count != 1 {
t.Errorf("expected 1 migration entries, got %d", count)
}
})
@@ -82,8 +82,8 @@ func TestMigrate(t *testing.T) {
t.Fatalf("failed to count migrations: %v", err)
}
if count != 2 {
t.Errorf("expected 2 migration entries, got %d", count)
if count != 1 {
t.Errorf("expected 1 migration entries, got %d", count)
}
})
@@ -118,8 +118,8 @@ func TestMigrate(t *testing.T) {
t.Fatalf("failed to get migration version: %v", err)
}
if version != 2 {
t.Errorf("expected migration version to remain at 2, got %d", version)
if version != 1 {
t.Errorf("expected migration version to remain at 1, got %d", version)
}
})
}

View File

@@ -68,12 +68,14 @@ func (db *database) createWorkspaceTx(tx *sql.Tx, workspace *models.Workspace) e
user_id, name,
theme, auto_save, show_hidden_files,
git_enabled, git_url, git_user, git_token,
git_auto_commit, git_commit_msg_template
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
git_auto_commit, git_commit_msg_template,
git_commit_name, git_commit_email
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
workspace.UserID, workspace.Name,
workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles,
workspace.GitEnabled, workspace.GitURL, workspace.GitUser, workspace.GitToken,
workspace.GitAutoCommit, workspace.GitCommitMsgTemplate,
workspace.GitCommitName, workspace.GitCommitEmail,
)
if err != nil {
return err

View File

@@ -23,7 +23,8 @@ func (db *database) CreateWorkspace(workspace *models.Workspace) error {
INSERT INTO workspaces (
user_id, name, theme, auto_save, show_hidden_files,
git_enabled, git_url, git_user, git_token,
git_auto_commit, git_commit_msg_template, git_commit_name, git_commit_email
git_auto_commit, git_commit_msg_template,
git_commit_name, git_commit_email
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
workspace.UserID, workspace.Name, workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles,
workspace.GitEnabled, workspace.GitURL, workspace.GitUser, encryptedToken,
@@ -51,7 +52,8 @@ func (db *database) GetWorkspaceByID(id int) (*models.Workspace, error) {
id, user_id, name, created_at,
theme, auto_save, show_hidden_files,
git_enabled, git_url, git_user, git_token,
git_auto_commit, git_commit_msg_template, git_commit_name, git_commit_email
git_auto_commit, git_commit_msg_template,
git_commit_name, git_commit_email
FROM workspaces
WHERE id = ?`,
id,
@@ -93,7 +95,8 @@ func (db *database) GetWorkspaceByName(userID int, workspaceName string) (*model
&workspace.ID, &workspace.UserID, &workspace.Name, &workspace.CreatedAt,
&workspace.Theme, &workspace.AutoSave, &workspace.ShowHiddenFiles,
&workspace.GitEnabled, &workspace.GitURL, &workspace.GitUser, &encryptedToken,
&workspace.GitAutoCommit, &workspace.GitCommitMsgTemplate, &workspace.GitCommitName, &workspace.GitCommitEmail,
&workspace.GitAutoCommit, &workspace.GitCommitMsgTemplate,
&workspace.GitCommitName, &workspace.GitCommitEmail,
)
if err != nil {
return nil, err

View File

@@ -70,7 +70,7 @@ func setupTestHarness(t *testing.T) *testHarness {
// Create storage with mock git client
storageOpts := storage.Options{
NewGitClient: func(url, user, token, path string) git.Client {
NewGitClient: func(url, user, token, path, commitName, commitEmail string) git.Client {
return mockGit
},
}

View File

@@ -64,6 +64,8 @@ func (h *Handler) CreateWorkspace() http.HandlerFunc {
workspace.GitURL,
workspace.GitUser,
workspace.GitToken,
workspace.GitCommitName,
workspace.GitCommitEmail,
); err != nil {
http.Error(w, "Failed to setup git repo: "+err.Error(), http.StatusInternalServerError)
return
@@ -96,7 +98,9 @@ func gitSettingsChanged(new, old *models.Workspace) bool {
if new.GitEnabled {
return new.GitURL != old.GitURL ||
new.GitUser != old.GitUser ||
new.GitToken != old.GitToken
new.GitToken != old.GitToken ||
new.GitCommitName != old.GitCommitName ||
new.GitCommitEmail != old.GitCommitEmail
}
return false
@@ -135,6 +139,8 @@ func (h *Handler) UpdateWorkspace() http.HandlerFunc {
workspace.GitURL,
workspace.GitUser,
workspace.GitToken,
workspace.GitCommitName,
workspace.GitCommitEmail,
); err != nil {
http.Error(w, "Failed to setup git repo: "+err.Error(), http.StatusInternalServerError)
return

View File

@@ -60,6 +60,8 @@ func TestWorkspaceHandlers_Integration(t *testing.T) {
GitUser: "testuser",
GitToken: "testtoken",
GitAutoCommit: true,
GitCommitName: "Test User",
GitCommitEmail: "test@example.com",
}
rr := h.makeRequest(t, http.MethodPost, "/api/v1/workspaces", workspace, h.RegularToken, nil)
@@ -73,6 +75,8 @@ func TestWorkspaceHandlers_Integration(t *testing.T) {
assert.Equal(t, workspace.GitUser, created.GitUser)
assert.Equal(t, workspace.GitToken, created.GitToken)
assert.Equal(t, workspace.GitAutoCommit, created.GitAutoCommit)
assert.Equal(t, workspace.GitCommitName, created.GitCommitName)
assert.Equal(t, workspace.GitCommitEmail, created.GitCommitEmail)
})
t.Run("invalid workspace", func(t *testing.T) {
@@ -168,6 +172,8 @@ func TestWorkspaceHandlers_Integration(t *testing.T) {
GitUser: "testuser",
GitToken: "testtoken",
GitAutoCommit: true,
GitCommitName: "Test User",
GitCommitEmail: "test@example.com",
}
rr := h.makeRequest(t, http.MethodPut, baseURL, update, h.RegularToken, nil)
@@ -180,6 +186,8 @@ func TestWorkspaceHandlers_Integration(t *testing.T) {
assert.Equal(t, update.GitURL, updated.GitURL)
assert.Equal(t, update.GitUser, updated.GitUser)
assert.Equal(t, update.GitToken, updated.GitToken)
assert.Equal(t, update.GitAutoCommit, updated.GitAutoCommit)
assert.Equal(t, update.GitCommitName, updated.GitCommitName)
// Mock should have been called to setup git
assert.True(t, h.MockGit.IsInitialized())

View File

@@ -23,7 +23,7 @@ type Workspace struct {
GitAutoCommit bool `json:"gitAutoCommit"`
GitCommitMsgTemplate string `json:"gitCommitMsgTemplate"`
GitCommitName string `json:"gitCommitName"`
GitCommitEmail string `json:"gitCommitEmail" validate:"required_if=GitEnabled true,email"`
GitCommitEmail string `json:"gitCommitEmail" validate:"omitempty,required_if=GitEnabled true,email"`
}
// Validate validates the workspace struct