From 7339a69bceae1501e1cf2ea5c361cd10b498227c Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 22:20:34 +0100 Subject: [PATCH 1/5] getWeeklyReport --- frontend/src/API/API.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index cfd5b61..95a7e02 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -20,6 +20,10 @@ interface API { registerUser(user: NewUser): Promise>; /** Remove a user */ removeUser(username: string, token: string): Promise>; + /** Login */ + login(NewUser: NewUser): Promise>; + /** Renew the token */ + renewToken(token: string): Promise>; /** Create a project */ createProject( project: NewProject, @@ -30,15 +34,16 @@ interface API { project: NewWeeklyReport, token: string, ): Promise>; - /** Renew the token */ - renewToken(token: string): Promise>; + /**Gets a weekly report*/ + getWeeklyReport( + projectName: string, + token: string, + ): Promise>; /** Gets all the projects of a user*/ getUserProjects( username: string, token: string, ): Promise>; - /** Login */ - login(NewUser: NewUser): Promise>; } // Export an instance of the API From 6823102b4421c4af3029aef161b92a5f3a89f46d Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 22:27:21 +0100 Subject: [PATCH 2/5] Deleted API test --- frontend/src/API/API.test.ts | 87 ------------------------------------ frontend/src/API/API.ts | 3 +- 2 files changed, 2 insertions(+), 88 deletions(-) delete mode 100644 frontend/src/API/API.test.ts diff --git a/frontend/src/API/API.test.ts b/frontend/src/API/API.test.ts deleted file mode 100644 index dbae706..0000000 --- a/frontend/src/API/API.test.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { describe, expect, test } from "@jest/globals"; -import { api } from "../API/API"; -import { NewUser, NewWeeklyReport } from "../Types/goTypes"; - -describe("API", () => { - test("registerUser", async () => { - const user: NewUser = { - username: "lol", // Add the username property - password: "lol", - }; - const response = await api.registerUser(user); - console.log(response.message); - expect(response.success).toBe(true); - expect(response.data).toHaveProperty("userId"); - }); - - test("createProject", async () => { - const project = { - name: "Project X", - description: "This is a test project", - }; - const token = - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6ZmFsc2UsImV4cCI6MTcxMDk0MDIwMywibmFtZSI6InJyZ3VtZHpwbWMifQ.V9NHoYMYV61t"; - - const response = await api.createProject(project, token); - console.log(response.message); - expect(response.success).toBe(true); - expect(response.data).toHaveProperty("projectId"); - }); - - test("renewToken", async () => { - const refreshToken = - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6ZmFsc2UsImV4cCI6MTcxMDk0MDIwMywibmFtZSI6InJyZ3VtZHpwbWMifQ.V9NHoYMYV61t"; - - const response = await api.renewToken(refreshToken); - console.log(response.message); - expect(response.success).toBe(true); - expect(response.data).toHaveProperty("accessToken"); - expect(response.data).toHaveProperty("refreshToken"); - }); - - test("getUserProjects", async () => { - const token = - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6ZmFsc2UsImV4cCI6MTcxMDk0MDIwMywibmFtZSI6InJyZ3VtZHpwbWMifQ.V9NHoYMYV61t"; - const username = "rrgumdzpmc"; - const response = await api.getUserProjects(username, token); - console.log(response.message); - expect(response.success).toBe(true); - expect(response.data).toHaveProperty("projects"); - }); - - test("submitWeeklyReport", async () => { - const report: NewWeeklyReport = { - projectName: "vtmosxssst", - week: 2, - developmentTime: 40, - meetingTime: 5, - adminTime: 2, - ownWorkTime: 10, - studyTime: 12, - testingTime: 41, - }; - const token = - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6ZmFsc2UsImV4cCI6MTcxMDk0MDIwMywibmFtZSI6InJyZ3VtZHpwbWMifQ.V9NHoYMYV61t"; - - const response = await api.submitWeeklyReport(report, token); - console.log(response.message); - expect(response.success).toBe(true); - expect(response.data).toHaveProperty( - "message", - "Report submitted successfully", - ); - }); - - test("login", async () => { - const user: NewUser = { - username: "rrgumdzpmc", // Add an empty string value for the username property - password: "always_same", - }; - - const response = await api.login(user); - console.log(response.message); - expect(response.success).toBe(true); - expect(response.data).toHaveProperty("accessToken"); - expect(response.data).toHaveProperty("refreshToken"); - }); -}); diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index 95a7e02..f6e5979 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -36,8 +36,9 @@ interface API { ): Promise>; /**Gets a weekly report*/ getWeeklyReport( + username: string, projectName: string, - token: string, + week: string, ): Promise>; /** Gets all the projects of a user*/ getUserProjects( From c3ce25236fa15083362b1eb5297a82516c4b9145 Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 22:35:51 +0100 Subject: [PATCH 3/5] Full implementation of getWeeklyProject --- frontend/src/API/API.ts | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index f6e5979..32c5eb2 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -33,13 +33,14 @@ interface API { submitWeeklyReport( project: NewWeeklyReport, token: string, - ): Promise>; + ): Promise>; /**Gets a weekly report*/ getWeeklyReport( username: string, projectName: string, week: string, - ): Promise>; + token: string, + ): Promise>; /** Gets all the projects of a user*/ getUserProjects( username: string, @@ -169,9 +170,9 @@ export const api: API = { }, async submitWeeklyReport( - project: NewWeeklyReport, + weeklyReport: NewWeeklyReport, token: string, - ): Promise> { + ): Promise> { try { const response = await fetch("/api/submitWeeklyReport", { method: "POST", @@ -179,7 +180,7 @@ export const api: API = { "Content-Type": "application/json", Authorization: "Bearer " + token, }, - body: JSON.stringify(project), + body: JSON.stringify(weeklyReport), }); if (!response.ok) { @@ -189,7 +190,7 @@ export const api: API = { }; } - const data = (await response.json()) as Project; + const data = (await response.json()) as NewWeeklyReport; return { success: true, data }; } catch (e) { return { @@ -199,6 +200,33 @@ export const api: API = { } }, + async getWeeklyReport( + username: string, + projectName: string, + week: string, + token: string, + ): Promise> { + try { + const response = await fetch("/api/getWeeklyReport", { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + body: JSON.stringify({ username, projectName, week }), + }); + + if (!response.ok) { + return { success: false, message: "Failed to get weekly report" }; + } else { + const data = (await response.json()) as NewWeeklyReport; + return { success: true, data }; + } + } catch (e) { + return { success: false, message: "Failed to get weekly report" }; + } + }, + async login(NewUser: NewUser): Promise> { try { const response = await fetch("/api/login", { From 9f20d46fa6f756ff9a3dc5a2000a9a1a761f6140 Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 22:57:19 +0100 Subject: [PATCH 4/5] Test for getWeeklyReport --- backend/internal/handlers/handlers_report_related.go | 6 ++++++ testing.py | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/backend/internal/handlers/handlers_report_related.go b/backend/internal/handlers/handlers_report_related.go index 8754afd..79da61a 100644 --- a/backend/internal/handlers/handlers_report_related.go +++ b/backend/internal/handlers/handlers_report_related.go @@ -37,13 +37,19 @@ func (gs *GState) SubmitWeeklyReport(c *fiber.Ctx) error { // Handler for retrieving weekly report func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error { // Extract the necessary parameters from the request + println("GetWeeklyReport") user := c.Locals("user").(*jwt.Token) + println(user) claims := user.Claims.(jwt.MapClaims) + println(claims) username := claims["name"].(string) + println(username) // Extract project name and week from query parameters projectName := c.Query("projectName") + println(projectName) week := c.Query("week") + println(week) // Convert week to integer weekInt, err := strconv.Atoi(week) diff --git a/testing.py b/testing.py index d2c64fe..d75d0f2 100644 --- a/testing.py +++ b/testing.py @@ -21,6 +21,7 @@ registerPath = base_url + "/api/register" loginPath = base_url + "/api/login" addProjectPath = base_url + "/api/project" submitReportPath = base_url + "/api/submitReport" +getWeeklyReportPath = base_url + "/api/getWeeklyReport" # Posts the username and password to the register endpoint @@ -89,9 +90,18 @@ def test_submit_report(): assert response.status_code == 200, "Submit report failed" print("Submit report successful") +def test_get_weekly_report(): + token = login(username, "always_same").json()["token"] + response = requests.get( + getWeeklyReportPath, + headers={"Authorization": "Bearer " + token}, + params={"username": username, "projectName": "report1", "week": 1} + ) + print(response.text) if __name__ == "__main__": test_create_user() test_login() test_add_project() test_submit_report() + test_get_weekly_report() \ No newline at end of file From 9070846b0bbf6bdc12a42f3ae1bb01a282ee0e91 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 17 Mar 2024 23:17:06 +0100 Subject: [PATCH 5/5] Fixing getWeeklyReport tests --- backend/internal/handlers/handlers_report_related.go | 3 --- backend/main.go | 1 + testing.py | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/backend/internal/handlers/handlers_report_related.go b/backend/internal/handlers/handlers_report_related.go index 79da61a..506225b 100644 --- a/backend/internal/handlers/handlers_report_related.go +++ b/backend/internal/handlers/handlers_report_related.go @@ -39,11 +39,8 @@ func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error { // Extract the necessary parameters from the request println("GetWeeklyReport") user := c.Locals("user").(*jwt.Token) - println(user) claims := user.Claims.(jwt.MapClaims) - println(claims) username := claims["name"].(string) - println(username) // Extract project name and week from query parameters projectName := c.Query("projectName") diff --git a/backend/main.go b/backend/main.go index 7f0f81e..bc33942 100644 --- a/backend/main.go +++ b/backend/main.go @@ -78,6 +78,7 @@ func main() { server.Post("/api/loginrenew", gs.LoginRenew) server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches server.Post("/api/project", gs.CreateProject) + server.Get("/api/getWeeklyReport", gs.GetWeeklyReport) // Announce the port we are listening on and start the server err = server.Listen(fmt.Sprintf(":%d", conf.Port)) diff --git a/testing.py b/testing.py index d75d0f2..38d09cc 100644 --- a/testing.py +++ b/testing.py @@ -75,7 +75,7 @@ def test_submit_report(): response = requests.post( submitReportPath, json={ - "projectName": "report1", + "projectName": projectName, "week": 1, "developmentTime": 10, "meetingTime": 5, @@ -95,7 +95,7 @@ def test_get_weekly_report(): response = requests.get( getWeeklyReportPath, headers={"Authorization": "Bearer " + token}, - params={"username": username, "projectName": "report1", "week": 1} + params={"username": username, "projectName": projectName , "week": 1} ) print(response.text)