Handler for ChangeUserName changed and corresponding test added

This commit is contained in:
dDogge 2024-03-21 00:16:51 +01:00
parent acdee28eb0
commit 44f6b31056
2 changed files with 56 additions and 16 deletions

View file

@ -234,33 +234,33 @@ func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
// Changes a users name in the database
// ChangeUserName changes a user's username in the database
func (gs *GState) ChangeUserName(c *fiber.Ctx) error {
//check token and get username of current user
// Check token and get username of current user
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
projectManagerUsername := claims["name"].(string)
log.Info(projectManagerUsername)
adminUsername := claims["name"].(string)
log.Info(adminUsername)
// Extract the necessary parameters from the request
data := new(types.NameChange)
data := new(types.StrNameChange)
if err := c.BodyParser(data); err != nil {
log.Info("error parsing username, project or role")
log.Info("Error parsing username")
return c.Status(400).SendString(err.Error())
}
// dubble diping and checcking if current user is
if ismanager, err := gs.Db.IsProjectManager(projectManagerUsername, c.Params(data.Name)); err != nil {
log.Warn("Error checking if projectmanager:", err)
// Check if the current user is an admin
isAdmin, err := gs.Db.IsSiteAdmin(adminUsername)
if err != nil {
log.Warn("Error checking if admin:", err)
return c.Status(500).SendString(err.Error())
} else if !ismanager {
log.Warn("tried changing name when not projectmanager:", err)
return c.Status(401).SendString("you can not change name when not projectmanager")
} else if !isAdmin {
log.Warn("Tried changing name when not admin")
return c.Status(401).SendString("You cannot change name unless you are an admin")
}
// Change the user's name within the project in the database
if err := gs.Db.ChangeUserName(projectManagerUsername, data.Name); err != nil {
// Change the user's name in the database
if err := gs.Db.ChangeUserName(data.PrevName, data.NewName); err != nil {
return c.Status(500).SendString(err.Error())
}

View file

@ -41,6 +41,7 @@ getWeeklyReportsUserPath = base_url + "/api/getWeeklyReportsUser"
checkIfProjectManagerPath = base_url + "/api/checkIfProjectManager"
ProjectRoleChangePath = base_url + "/api/ProjectRoleChange"
getUsersProjectPath = base_url + "/api/getUsersProject"
getChangeUserNamePath = base_url + "/api/changeUserName"
#ta bort auth i handlern för att få testet att gå igenom
def test_ProjectRoleChange():
@ -367,6 +368,44 @@ def test_ensure_manager_of_created_project():
assert response.json()["isProjectManager"] == True, "User is not project manager"
gprint("test_ensure_admin_of_created_project successful")
def test_change_user_name():
# Register a new user
new_user = randomString()
register(new_user, "password")
# Log in as the new user
token = login(new_user, "password").json()["token"]
# Register a new admin
admin_username = randomString()
admin_password = "admin_password"
dprint(
"Registering with username: ", admin_username, " and password: ", admin_password
)
response = requests.post(
registerPath, json={"username": admin_username, "password": admin_password}
)
admin_token = login(admin_username, admin_password).json()["token"]
# Promote to admin
response = requests.post(
promoteToAdminPath,
json={"username": admin_username},
headers={"Authorization": "Bearer " + admin_token},
)
# Login as admin
# Change the user's name
response = requests.put(
getChangeUserNamePath,
json={"prevName": new_user, "newName": "new_username"},
headers={"Authorization": "Bearer " + admin_token},
)
# Check if the change was successful
assert response.status_code == 200, "Change user name failed"
gprint("test_change_user_name successful")
if __name__ == "__main__":
test_get_user_projects()
@ -383,3 +422,4 @@ if __name__ == "__main__":
test_ProjectRoleChange()
#test_list_all_users_project()
test_ensure_manager_of_created_project()
test_change_user_name()