Merge branch 'dev' into BumBranch
This commit is contained in:
commit
382ea38558
6 changed files with 115 additions and 2 deletions
|
@ -35,6 +35,7 @@ type Database interface {
|
|||
GetWeeklyReportsUser(username string, projectname string) ([]types.WeeklyReportList, error)
|
||||
SignWeeklyReport(reportId int, projectManagerId int) error
|
||||
IsSiteAdmin(username string) (bool, error)
|
||||
IsProjectManager(username string, projectname string) (bool, error)
|
||||
}
|
||||
|
||||
// This struct is a wrapper type that holds the database connection
|
||||
|
@ -433,6 +434,27 @@ func (d *Db) GetWeeklyReportsUser(username string, projectName string) ([]types.
|
|||
return reports, nil
|
||||
}
|
||||
|
||||
// IsProjectManager checks if a given username is a project manager for the specified project
|
||||
func (d *Db) IsProjectManager(username string, projectname string) (bool, error) {
|
||||
// Define the SQL query to check if the user is a project manager for the project
|
||||
query := `
|
||||
SELECT COUNT(*) FROM user_roles
|
||||
JOIN users ON user_roles.user_id = users.id
|
||||
JOIN projects ON user_roles.project_id = projects.id
|
||||
WHERE users.username = ? AND projects.name = ? AND user_roles.p_role = 'project_manager'
|
||||
`
|
||||
|
||||
// Execute the query
|
||||
var count int
|
||||
err := d.Get(&count, query, username, projectname)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// If count is greater than 0, the user is a project manager for the project
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// MigrateSampleData applies sample data to the database.
|
||||
func (d *Db) MigrateSampleData() error {
|
||||
// Insert sample data
|
||||
|
|
|
@ -602,6 +602,58 @@ func TestGetWeeklyReportsUser(t *testing.T) {
|
|||
if len(reports) != 2 {
|
||||
t.Errorf("Expected 1 report, got %d", len(reports))
|
||||
}
|
||||
|
||||
// You can add further checks here if needed
|
||||
}
|
||||
|
||||
func TestIsProjectManager(t *testing.T) {
|
||||
db, err := setupState()
|
||||
if err != nil {
|
||||
t.Error("setupState failed:", err)
|
||||
}
|
||||
|
||||
// Add a project manager
|
||||
err = db.AddUser("projectManager", "password")
|
||||
if err != nil {
|
||||
t.Error("AddUser failed:", err)
|
||||
}
|
||||
|
||||
// Add a regular user
|
||||
err = db.AddUser("testuser", "password")
|
||||
if err != nil {
|
||||
t.Error("AddUser failed:", err)
|
||||
}
|
||||
|
||||
// Add project
|
||||
err = db.AddProject("testproject", "description", "projectManager")
|
||||
if err != nil {
|
||||
t.Error("AddProject failed:", err)
|
||||
}
|
||||
|
||||
// Add both regular users as members to the project
|
||||
err = db.AddUserToProject("testuser", "testproject", "member")
|
||||
if err != nil {
|
||||
t.Error("AddUserToProject failed:", err)
|
||||
}
|
||||
|
||||
err = db.AddUserToProject("projectManager", "testproject", "project_manager")
|
||||
if err != nil {
|
||||
t.Error("AddUserToProject failed:", err)
|
||||
}
|
||||
|
||||
// Check if the regular user is not a project manager
|
||||
isManager, err := db.IsProjectManager("testuser", "testproject")
|
||||
if err != nil {
|
||||
t.Error("IsProjectManager failed:", err)
|
||||
}
|
||||
if isManager {
|
||||
t.Error("Expected testuser not to be a project manager, but it is.")
|
||||
}
|
||||
|
||||
// Check if the project manager is indeed a project manager
|
||||
isManager, err = db.IsProjectManager("projectManager", "testproject")
|
||||
if err != nil {
|
||||
t.Error("IsProjectManager failed:", err)
|
||||
}
|
||||
if !isManager {
|
||||
t.Error("Expected projectManager to be a project manager, but it's not.")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ type GlobalState interface {
|
|||
AddUserToProjectHandler(c *fiber.Ctx) error
|
||||
PromoteToAdmin(c *fiber.Ctx) error
|
||||
GetWeeklyReportsUserHandler(c *fiber.Ctx) error
|
||||
IsProjectManagerHandler(c *fiber.Ctx) error
|
||||
// GetProject(c *fiber.Ctx) error // To get a specific project
|
||||
// UpdateProject(c *fiber.Ctx) error // To update a project
|
||||
// DeleteProject(c *fiber.Ctx) error // To delete a project
|
||||
|
|
|
@ -154,3 +154,20 @@ func (gs *GState) AddUserToProjectHandler(c *fiber.Ctx) error {
|
|||
log.Info("User added to project successfully:", requestData.Username)
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
// IsProjectManagerHandler is a handler that checks if a user is a project manager for a given project
|
||||
func (gs *GState) IsProjectManagerHandler(c *fiber.Ctx) error {
|
||||
// Extract necessary parameters from the request query string
|
||||
username := c.Query("username")
|
||||
projectName := c.Query("projectName")
|
||||
|
||||
// Check if the user is a project manager for the specified project
|
||||
isManager, err := gs.Db.IsProjectManager(username, projectName)
|
||||
if err != nil {
|
||||
log.Info("Error checking project manager status:", err)
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
|
||||
// Return the result as JSON
|
||||
return c.JSON(map[string]bool{"isProjectManager": isManager})
|
||||
}
|
||||
|
|
|
@ -95,6 +95,7 @@ func main() {
|
|||
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
||||
server.Get("/api/users/all", gs.ListAllUsers)
|
||||
server.Get("/api/getWeeklyReportsUser", gs.GetWeeklyReportsUserHandler)
|
||||
server.Get("api/checkIfProjectManager", gs.IsProjectManagerHandler)
|
||||
|
||||
// Announce the port we are listening on and start the server
|
||||
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
||||
|
|
20
testing.py
20
testing.py
|
@ -38,6 +38,7 @@ addUserToProjectPath = base_url + "/api/addUserToProject"
|
|||
promoteToAdminPath = base_url + "/api/promoteToAdmin"
|
||||
getUserProjectsPath = base_url + "/api/getUserProjects"
|
||||
getWeeklyReportsUserPath = base_url + "/api/getWeeklyReportsUser"
|
||||
checkIfProjectManagerPath = base_url + "/api/checkIfProjectManager"
|
||||
|
||||
|
||||
def test_get_user_projects():
|
||||
|
@ -292,6 +293,24 @@ def test_get_weekly_reports_user():
|
|||
assert response.status_code == 200, "Get weekly reports for user failed"
|
||||
gprint("test_get_weekly_reports_user successful")
|
||||
|
||||
# Test function to check if a user is a project manager
|
||||
def test_check_if_project_manager():
|
||||
# Log in as the user
|
||||
token = login(username, "always_same").json()["token"]
|
||||
|
||||
# Check if the user is a project manager for the project
|
||||
response = requests.get(
|
||||
checkIfProjectManagerPath,
|
||||
headers={"Authorization": "Bearer " + token},
|
||||
params={"username": username, "projectName": projectName},
|
||||
)
|
||||
|
||||
dprint(response.text)
|
||||
assert response.status_code == 200, "Check if project manager failed"
|
||||
gprint("test_check_if_project_manager successful")
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_get_user_projects()
|
||||
|
@ -304,3 +323,4 @@ if __name__ == "__main__":
|
|||
test_sign_report()
|
||||
test_add_user_to_project()
|
||||
test_get_weekly_reports_user()
|
||||
test_check_if_project_manager()
|
||||
|
|
Loading…
Reference in a new issue