ChangeUserName function in db.go fixed, corresponding test added

This commit is contained in:
dDogge 2024-03-20 16:46:17 +01:00
parent ef6c3951fd
commit 88f232e21b
3 changed files with 47 additions and 15 deletions

View file

@ -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.

View file

@ -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")
}
}

View file

@ -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"`
}