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) t.Fatalf("failed to get migration version: %v", err)
} }
if version != 2 { // Current number of migrations in production code if version != 1 { // Current number of migrations in production code
t.Errorf("expected migration version 2, got %d", version) t.Errorf("expected migration version 1, got %d", version)
} }
// Verify number of migration entries matches versions applied // 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) t.Fatalf("failed to count migrations: %v", err)
} }
if count != 2 { if count != 1 {
t.Errorf("expected 2 migration entries, got %d", count) 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) t.Fatalf("failed to count migrations: %v", err)
} }
if count != 2 { if count != 1 {
t.Errorf("expected 2 migration entries, got %d", count) 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) t.Fatalf("failed to get migration version: %v", err)
} }
if version != 2 { if version != 1 {
t.Errorf("expected migration version to remain at 2, got %d", version) 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, user_id, name,
theme, auto_save, show_hidden_files, theme, auto_save, show_hidden_files,
git_enabled, git_url, git_user, git_token, git_enabled, git_url, git_user, git_token,
git_auto_commit, git_commit_msg_template git_auto_commit, git_commit_msg_template,
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, git_commit_name, git_commit_email
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
workspace.UserID, workspace.Name, workspace.UserID, workspace.Name,
workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles, workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles,
workspace.GitEnabled, workspace.GitURL, workspace.GitUser, workspace.GitToken, workspace.GitEnabled, workspace.GitURL, workspace.GitUser, workspace.GitToken,
workspace.GitAutoCommit, workspace.GitCommitMsgTemplate, workspace.GitAutoCommit, workspace.GitCommitMsgTemplate,
workspace.GitCommitName, workspace.GitCommitEmail,
) )
if err != nil { if err != nil {
return err return err

View File

@@ -23,7 +23,8 @@ func (db *database) CreateWorkspace(workspace *models.Workspace) error {
INSERT INTO workspaces ( INSERT INTO workspaces (
user_id, name, theme, auto_save, show_hidden_files, user_id, name, theme, auto_save, show_hidden_files,
git_enabled, git_url, git_user, git_token, 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
workspace.UserID, workspace.Name, workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles, workspace.UserID, workspace.Name, workspace.Theme, workspace.AutoSave, workspace.ShowHiddenFiles,
workspace.GitEnabled, workspace.GitURL, workspace.GitUser, encryptedToken, 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, id, user_id, name, created_at,
theme, auto_save, show_hidden_files, theme, auto_save, show_hidden_files,
git_enabled, git_url, git_user, git_token, 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 FROM workspaces
WHERE id = ?`, WHERE id = ?`,
id, id,
@@ -93,7 +95,8 @@ func (db *database) GetWorkspaceByName(userID int, workspaceName string) (*model
&workspace.ID, &workspace.UserID, &workspace.Name, &workspace.CreatedAt, &workspace.ID, &workspace.UserID, &workspace.Name, &workspace.CreatedAt,
&workspace.Theme, &workspace.AutoSave, &workspace.ShowHiddenFiles, &workspace.Theme, &workspace.AutoSave, &workspace.ShowHiddenFiles,
&workspace.GitEnabled, &workspace.GitURL, &workspace.GitUser, &encryptedToken, &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 { if err != nil {
return nil, err return nil, err

View File

@@ -70,7 +70,7 @@ func setupTestHarness(t *testing.T) *testHarness {
// Create storage with mock git client // Create storage with mock git client
storageOpts := storage.Options{ 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 return mockGit
}, },
} }

View File

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

View File

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

View File

@@ -23,7 +23,7 @@ type Workspace struct {
GitAutoCommit bool `json:"gitAutoCommit"` GitAutoCommit bool `json:"gitAutoCommit"`
GitCommitMsgTemplate string `json:"gitCommitMsgTemplate"` GitCommitMsgTemplate string `json:"gitCommitMsgTemplate"`
GitCommitName string `json:"gitCommitName"` 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 // Validate validates the workspace struct