From 88f232e21ba00bdd5434dba3c72228f32a53f3cc Mon Sep 17 00:00:00 2001 From: dDogge Date: Wed, 20 Mar 2024 16:46:17 +0100 Subject: [PATCH] ChangeUserName function in db.go fixed, corresponding test added --- backend/internal/database/db.go | 21 +++++----------- backend/internal/database/db_test.go | 36 ++++++++++++++++++++++++++++ backend/internal/types/users.go | 5 ++++ 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 8b0c8c4..59d3277 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -66,10 +66,8 @@ const addWeeklyReport = `WITH UserLookup AS (SELECT id FROM users WHERE username ProjectLookup AS (SELECT id FROM projects WHERE name = ?) INSERT INTO weekly_reports (project_id, user_id, week, development_time, meeting_time, admin_time, own_work_time, study_time, testing_time) VALUES ((SELECT id FROM ProjectLookup), (SELECT id FROM UserLookup),?, ?, ?, ?, ?, ?, ?);` -const addUserToProject = "INSERT INTO user_roles (user_id, project_id, p_role) VALUES (?, ?, ?)" // WIP +const addUserToProject = "INSERT INTO user_roles (user_id, project_id, p_role) VALUES (?, ?, ?)" const changeUserRole = "UPDATE user_roles SET p_role = ? WHERE user_id = ? AND project_id = ?" -const changeUserName = "UPDATE user SET username = ? WHERE user_id = ?" // WIP - const getProjectsForUser = `SELECT p.id, p.name, p.description FROM projects p JOIN user_roles ur ON p.id = ur.project_id JOIN users u ON ur.user_id = u.id @@ -133,7 +131,7 @@ func (d *Db) AddWeeklyReport(projectName string, userName string, week int, deve } // AddUserToProject adds a user to a project with a specified role. -func (d *Db) AddUserToProject(username string, projectname string, role string) error { // WIP +func (d *Db) AddUserToProject(username string, projectname string, role string) error { var userid int userid, err := d.GetUserId(username) if err != nil { @@ -171,18 +169,11 @@ func (d *Db) ChangeUserRole(username string, projectname string, role string) er return err3 } -// ChangeUserRole changes the role of a user within a project. +// ChangeUserName changes the username of a user. func (d *Db) ChangeUserName(username string, newname string) error { - // Get the user ID - var userid int - userid, err := d.GetUserId(username) - if err != nil { - panic(err) - } - - // Execute the SQL query to change the user's role - _, err2 := d.Exec(changeUserName, username, userid) - return err2 + // Execute the SQL query to update the username + _, err := d.Exec("UPDATE users SET username = ? WHERE username = ?", newname, username) + return err } // GetUserRole retrieves the role of a user within a project. diff --git a/backend/internal/database/db_test.go b/backend/internal/database/db_test.go index 3b18abb..6757522 100644 --- a/backend/internal/database/db_test.go +++ b/backend/internal/database/db_test.go @@ -675,3 +675,39 @@ func TestIsProjectManager(t *testing.T) { t.Error("Expected projectManager to be a project manager, but it's not.") } } + +func TestChangeUserName(t *testing.T) { + db, err := setupState() + if err != nil { + t.Error("setupState failed:", err) + } + + // Add a user + err = db.AddUser("testuser", "password") + if err != nil { + t.Error("AddUser failed:", err) + } + + // Change the user's name + err = db.ChangeUserName("testuser", "newname") + if err != nil { + t.Error("ChangeUserName failed:", err) + } + + // Retrieve the user's ID + userID, err := db.GetUserId("newname") + if err != nil { + t.Error("GetUserId failed:", err) + } + + // Ensure the user's ID matches the expected value + if userID != 1 { + t.Errorf("Expected user ID to be 1, got %d", userID) + } + + // Attempt to retrieve the user by the old name + _, err = db.GetUserId("testuser") + if err == nil { + t.Error("Expected GetUserId to fail for the old name, but it didn't") + } +} diff --git a/backend/internal/types/users.go b/backend/internal/types/users.go index d3f2170..88b4f06 100644 --- a/backend/internal/types/users.go +++ b/backend/internal/types/users.go @@ -32,3 +32,8 @@ type PublicUser struct { type Token struct { Token string `json:"token"` } + +type StrNameChange struct { + PrevName string `json:"prevName" db:"prevName"` + NewName string `json:"newName" db:"newName"` +}