Role change database interface with corresponding tests

This commit is contained in:
Imbus 2024-03-14 13:47:04 +01:00
parent 6c8abf1f53
commit d12e3a26ef
2 changed files with 27 additions and 8 deletions

View file

@ -14,7 +14,6 @@ import (
type Database interface { type Database interface {
// Insert a new user into the database, password should be hashed before calling // Insert a new user into the database, password should be hashed before calling
AddUser(username string, password string) error AddUser(username string, password string) error
RemoveUser(username string) error RemoveUser(username string) error
PromoteToAdmin(username string) error PromoteToAdmin(username string) error
GetUserId(username string) (int, error) GetUserId(username string) (int, error)
@ -22,10 +21,7 @@ type Database interface {
Migrate(dirname string) error Migrate(dirname string) error
AddTimeReport(projectName string, userName string, start time.Time, end time.Time) error AddTimeReport(projectName string, userName string, start time.Time, end time.Time) error
AddUserToProject(username string, projectname string, role string) error AddUserToProject(username string, projectname string, role string) error
// ChangeUserRole(username string, projectname string, role string) error ChangeUserRole(username string, projectname string, role string) error
// AddTimeReport(projectname string, start time.Time, end time.Time) error
// AddUserToProject(username string, projectname string) error
// ChangeUserRole(username string, projectname string, role string) error
} }
// This struct is a wrapper type that holds the database connection // This struct is a wrapper type that holds the database connection
@ -46,7 +42,7 @@ const addTimeReport = `WITH UserLookup AS (SELECT id FROM users WHERE username =
INSERT INTO time_reports (project_id, user_id, start, end) INSERT INTO time_reports (project_id, user_id, start, end)
VALUES ((SELECT id FROM ProjectLookup), (SELECT id FROM UserLookup), ?, ?);` 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 (?, ?, ?)" // WIP
const changeUserRole = "UPDATE project_member SET role = ? WHERE user_id = ? AND project_id = ?" const changeUserRole = "UPDATE user_roles SET p_role = ? WHERE user_id = ? AND project_id = ?"
// DbConnect connects to the database // DbConnect connects to the database
func DbConnect(dbpath string) Database { func DbConnect(dbpath string) Database {

View file

@ -152,6 +152,29 @@ func TestAddUserToProject(t *testing.T) {
// } // }
// func TestChangeUserRole(t *testing.T) { func TestChangeUserRole(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
// } err = db.AddUser("testuser", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
err = db.AddProject("testproject", "description", "testuser")
if err != nil {
t.Error("AddProject failed:", err)
}
err = db.AddUserToProject("testuser", "testproject", "user")
if err != nil {
t.Error("AddUserToProject failed:", err)
}
err = db.ChangeUserRole("testuser", "testproject", "admin")
if err != nil {
t.Error("ChangeUserRole failed:", err)
}
}