Merge remote-tracking branch 'origin/dev' into BumBranch
This commit is contained in:
commit
d0cc6f2c1b
7 changed files with 76 additions and 27 deletions
|
@ -107,7 +107,10 @@ func (d *Db) GetAllProjects() ([]types.Project, error) {
|
||||||
// GetProject retrieves a specific project by its ID.
|
// GetProject retrieves a specific project by its ID.
|
||||||
func (d *Db) GetProject(projectId int) (types.Project, error) {
|
func (d *Db) GetProject(projectId int) (types.Project, error) {
|
||||||
var project types.Project
|
var project types.Project
|
||||||
err := d.Select(&project, "SELECT * FROM projects WHERE id = ?")
|
err := d.Get(&project, "SELECT * FROM projects WHERE id = ?", projectId)
|
||||||
|
if err != nil {
|
||||||
|
println("Error getting project: ", err)
|
||||||
|
}
|
||||||
return project, err
|
return project, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -537,45 +537,32 @@ func TestSignWeeklyReportByAnotherProjectManager(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsSiteAdmin(t *testing.T) {
|
func TestGetProject(t *testing.T) {
|
||||||
db, err := setupState()
|
db, err := setupState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("setupState failed:", err)
|
t.Error("setupState failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a site admin
|
// Add a user
|
||||||
err = db.AddUser("admin", "password")
|
err = db.AddUser("testuser", "password")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("AddUser failed:", err)
|
t.Error("AddUser failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Promote the user to site admin
|
// Add a project
|
||||||
err = db.PromoteToAdmin("admin")
|
err = db.AddProject("testproject", "description", "testuser")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("PromoteToAdmin failed:", err)
|
t.Error("AddProject failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the user is a site admin
|
// Retrieve the added project
|
||||||
isAdmin, err := db.IsSiteAdmin("admin")
|
project, err := db.GetProject(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("IsSiteAdmin failed:", err)
|
t.Error("GetProject failed:", err)
|
||||||
}
|
|
||||||
if !isAdmin {
|
|
||||||
t.Error("IsSiteAdmin failed: expected true, got false")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a regular user
|
// Check if the retrieved project matches the expected values
|
||||||
err = db.AddUser("regularuser", "password")
|
if project.Name != "testproject" {
|
||||||
if err != nil {
|
t.Errorf("Expected Name to be testproject, got %s", project.Name)
|
||||||
t.Error("AddUser failed:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the regular user is not a site admin
|
|
||||||
isRegularUserAdmin, err := db.IsSiteAdmin("regularuser")
|
|
||||||
if err != nil {
|
|
||||||
t.Error("IsSiteAdmin failed:", err)
|
|
||||||
}
|
|
||||||
if isRegularUserAdmin {
|
|
||||||
t.Error("IsSiteAdmin failed: expected false, got true")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ type GlobalState interface {
|
||||||
SubmitWeeklyReport(c *fiber.Ctx) error
|
SubmitWeeklyReport(c *fiber.Ctx) error
|
||||||
GetWeeklyReport(c *fiber.Ctx) error
|
GetWeeklyReport(c *fiber.Ctx) error
|
||||||
SignReport(c *fiber.Ctx) error
|
SignReport(c *fiber.Ctx) error
|
||||||
|
GetProject(c *fiber.Ctx) error
|
||||||
AddUserToProjectHandler(c *fiber.Ctx) error
|
AddUserToProjectHandler(c *fiber.Ctx) error
|
||||||
PromoteToAdmin(c *fiber.Ctx) error
|
PromoteToAdmin(c *fiber.Ctx) error
|
||||||
// GetProject(c *fiber.Ctx) error // To get a specific project
|
// GetProject(c *fiber.Ctx) error // To get a specific project
|
||||||
|
|
|
@ -66,6 +66,10 @@ func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error {
|
||||||
func (gs *GState) GetProject(c *fiber.Ctx) error {
|
func (gs *GState) GetProject(c *fiber.Ctx) error {
|
||||||
// Extract the project ID from the request parameters or body
|
// Extract the project ID from the request parameters or body
|
||||||
projectID := c.Params("projectID")
|
projectID := c.Params("projectID")
|
||||||
|
if projectID == "" {
|
||||||
|
return c.Status(400).SendString("No project ID provided")
|
||||||
|
}
|
||||||
|
println("Getting project with ID: ", projectID)
|
||||||
|
|
||||||
// Parse the project ID into an integer
|
// Parse the project ID into an integer
|
||||||
projectIDInt, err := strconv.Atoi(projectID)
|
projectIDInt, err := strconv.Atoi(projectID)
|
||||||
|
@ -80,6 +84,7 @@ func (gs *GState) GetProject(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the project as JSON
|
// Return the project as JSON
|
||||||
|
println("Returning project: ", project.Name)
|
||||||
return c.JSON(project)
|
return c.JSON(project)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,7 @@ func main() {
|
||||||
server.Post("/api/loginrenew", gs.LoginRenew)
|
server.Post("/api/loginrenew", gs.LoginRenew)
|
||||||
server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches
|
server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches
|
||||||
server.Post("/api/project", gs.CreateProject)
|
server.Post("/api/project", gs.CreateProject)
|
||||||
|
server.Get("/api/project/:projectId", gs.GetProject)
|
||||||
server.Get("/api/getWeeklyReport", gs.GetWeeklyReport)
|
server.Get("/api/getWeeklyReport", gs.GetWeeklyReport)
|
||||||
server.Post("/api/signReport", gs.SignReport)
|
server.Post("/api/signReport", gs.SignReport)
|
||||||
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
||||||
|
|
|
@ -46,6 +46,13 @@ interface API {
|
||||||
week: string,
|
week: string,
|
||||||
token: string,
|
token: string,
|
||||||
): Promise<APIResponse<NewWeeklyReport>>;
|
): Promise<APIResponse<NewWeeklyReport>>;
|
||||||
|
/** Gets all the projects of a user*/
|
||||||
|
getUserProjects(
|
||||||
|
username: string,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<Project[]>>;
|
||||||
|
/** Gets a project from id*/
|
||||||
|
getProject(id: number): Promise<APIResponse<Project>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export an instance of the API
|
// Export an instance of the API
|
||||||
|
@ -253,4 +260,30 @@ export const api: API = {
|
||||||
return Promise.resolve({ success: false, message: "Failed to login" });
|
return Promise.resolve({ success: false, message: "Failed to login" });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Gets a projet by id, currently untested since we have no javascript-based tests
|
||||||
|
async getProject(id: number): Promise<APIResponse<Project>> {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/project/${id}`, {
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Failed to get project: Response code " + response.status,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const data = (await response.json()) as Project;
|
||||||
|
return { success: true, data };
|
||||||
|
}
|
||||||
|
// The code below is garbage but satisfies the linter
|
||||||
|
// This needs fixing, do not copy this pattern
|
||||||
|
} catch (e: unknown) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Failed to get project: " + (e as Error).toString(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
19
testing.py
19
testing.py
|
@ -22,9 +22,13 @@ loginPath = base_url + "/api/login"
|
||||||
addProjectPath = base_url + "/api/project"
|
addProjectPath = base_url + "/api/project"
|
||||||
submitReportPath = base_url + "/api/submitReport"
|
submitReportPath = base_url + "/api/submitReport"
|
||||||
getWeeklyReportPath = base_url + "/api/getWeeklyReport"
|
getWeeklyReportPath = base_url + "/api/getWeeklyReport"
|
||||||
|
<<<<<<< HEAD
|
||||||
|
getProjectPath = base_url + "/api/project"
|
||||||
|
=======
|
||||||
signReportPath = base_url + "/api/signReport"
|
signReportPath = base_url + "/api/signReport"
|
||||||
addUserToProjectPath = base_url + "/api/addUserToProject"
|
addUserToProjectPath = base_url + "/api/addUserToProject"
|
||||||
promoteToAdminPath = base_url + "/api/promoteToAdmin"
|
promoteToAdminPath = base_url + "/api/promoteToAdmin"
|
||||||
|
>>>>>>> 9ad89d60636ac6091d71b0bf307982becc9b89fe
|
||||||
|
|
||||||
|
|
||||||
# Posts the username and password to the register endpoint
|
# Posts the username and password to the register endpoint
|
||||||
|
@ -102,6 +106,17 @@ def test_get_weekly_report():
|
||||||
params={"username": username, "projectName": projectName , "week": 1}
|
params={"username": username, "projectName": projectName , "week": 1}
|
||||||
)
|
)
|
||||||
print(response.text)
|
print(response.text)
|
||||||
|
assert response.status_code == 200, "Get weekly report failed"
|
||||||
|
|
||||||
|
# Tests getting a project by id
|
||||||
|
def test_get_project():
|
||||||
|
token = login(username, "always_same").json()["token"]
|
||||||
|
response = requests.get(
|
||||||
|
getProjectPath + "/1", # Assumes that the project with id 1 exists
|
||||||
|
headers={"Authorization": "Bearer " + token},
|
||||||
|
)
|
||||||
|
print(response.text)
|
||||||
|
assert response.status_code == 200, "Get project failed"
|
||||||
|
|
||||||
# Test function to add a user to a project
|
# Test function to add a user to a project
|
||||||
def test_add_user_to_project():
|
def test_add_user_to_project():
|
||||||
|
@ -217,5 +232,9 @@ if __name__ == "__main__":
|
||||||
test_add_project()
|
test_add_project()
|
||||||
test_submit_report()
|
test_submit_report()
|
||||||
test_get_weekly_report()
|
test_get_weekly_report()
|
||||||
|
<<<<<<< HEAD
|
||||||
|
test_get_project()
|
||||||
|
=======
|
||||||
test_sign_report()
|
test_sign_report()
|
||||||
test_add_user_to_project()
|
test_add_user_to_project()
|
||||||
|
>>>>>>> 9ad89d60636ac6091d71b0bf307982becc9b89fe
|
||||||
|
|
Loading…
Reference in a new issue