Implemented PromoteToAdmin and corresponding Test

This commit is contained in:
dDogge 2024-03-07 13:21:47 +01:00
parent 3adf0b7ef5
commit 18eefab292
2 changed files with 24 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import (
type Database interface { type Database interface {
AddUser(username string, password string) error AddUser(username string, password string) error
RemoveUser(username string) error RemoveUser(username string) error
PromoteToAdmin(username string) error
GetUserId(username string) (int, error) GetUserId(username string) (int, error)
AddProject(name string, description string, username string) error AddProject(name string, description string, username string) error
Migrate(dirname string) error Migrate(dirname string) error
@ -30,6 +31,7 @@ var scripts embed.FS
const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)" const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?" const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?"
const promoteToAdmin = "INSERT INTO site_admin (admin_id) SELECT id FROM users WHERE username = ?"
// DbConnect connects to the database // DbConnect connects to the database
func DbConnect(dbpath string) Database { func DbConnect(dbpath string) Database {
@ -60,6 +62,11 @@ func (d *Db) RemoveUser(username string) error {
return err return err
} }
func (d *Db) PromoteToAdmin(username string) error {
_, err := d.Exec(promoteToAdmin, username)
return err
}
func (d *Db) GetUserId(username string) (int, error) { func (d *Db) GetUserId(username string) (int, error) {
var id int var id int
err := d.Get(&id, "SELECT id FROM users WHERE username = ?", username) err := d.Get(&id, "SELECT id FROM users WHERE username = ?", username)

View file

@ -74,3 +74,20 @@ func TestDbRemoveUser(t *testing.T) {
t.Error("RemoveUser failed:", err) t.Error("RemoveUser failed:", err)
} }
} }
func TestPromoteToAdmin(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
err = db.AddUser("test", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
err = db.PromoteToAdmin("test")
if err != nil {
t.Error("PromoteToAdmin failed:", err)
}
}