Compare commits
No commits in common. "4392b6839742a891355709cce6e52c3206e42a80" and "805d05f8a599cf6a1bd999524a949a248162816b" have entirely different histories.
4392b68397
...
805d05f8a5
7 changed files with 32 additions and 76 deletions
|
@ -107,10 +107,7 @@ 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.Get(&project, "SELECT * FROM projects WHERE id = ?", projectId)
|
err := d.Select(&project, "SELECT * FROM projects WHERE id = ?")
|
||||||
if err != nil {
|
|
||||||
println("Error getting project: ", err)
|
|
||||||
}
|
|
||||||
return project, err
|
return project, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -537,32 +537,45 @@ func TestSignWeeklyReportByAnotherProjectManager(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetProject(t *testing.T) {
|
func TestIsSiteAdmin(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 user
|
// Add a site admin
|
||||||
err = db.AddUser("testuser", "password")
|
err = db.AddUser("admin", "password")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("AddUser failed:", err)
|
t.Error("AddUser failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a project
|
// Promote the user to site admin
|
||||||
err = db.AddProject("testproject", "description", "testuser")
|
err = db.PromoteToAdmin("admin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("AddProject failed:", err)
|
t.Error("PromoteToAdmin failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the added project
|
// Check if the user is a site admin
|
||||||
project, err := db.GetProject(1)
|
isAdmin, err := db.IsSiteAdmin("admin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("GetProject failed:", err)
|
t.Error("IsSiteAdmin failed:", err)
|
||||||
|
}
|
||||||
|
if !isAdmin {
|
||||||
|
t.Error("IsSiteAdmin failed: expected true, got false")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the retrieved project matches the expected values
|
// Add a regular user
|
||||||
if project.Name != "testproject" {
|
err = db.AddUser("regularuser", "password")
|
||||||
t.Errorf("Expected Name to be testproject, got %s", project.Name)
|
if err != nil {
|
||||||
|
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,7 +17,6 @@ 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,10 +66,6 @@ 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)
|
||||||
|
@ -84,7 +80,6 @@ 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,7 +78,6 @@ 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)
|
||||||
|
|
|
@ -29,6 +29,11 @@ interface API {
|
||||||
project: NewProject,
|
project: NewProject,
|
||||||
token: string,
|
token: string,
|
||||||
): Promise<APIResponse<Project>>;
|
): Promise<APIResponse<Project>>;
|
||||||
|
/** Gets all the projects of a user*/
|
||||||
|
getUserProjects(
|
||||||
|
username: string,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<Project[]>>;
|
||||||
/** Submit a weekly report */
|
/** Submit a weekly report */
|
||||||
submitWeeklyReport(
|
submitWeeklyReport(
|
||||||
project: NewWeeklyReport,
|
project: NewWeeklyReport,
|
||||||
|
@ -41,13 +46,6 @@ 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
|
||||||
|
@ -255,30 +253,4 @@ 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(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
21
testing.py
21
testing.py
|
@ -22,13 +22,9 @@ 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
|
||||||
|
@ -106,17 +102,6 @@ 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():
|
||||||
|
@ -232,9 +217,5 @@ 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