From 95072ba1a989d33d7d18c9e8fa1cd4a2cfede7f5 Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Fri, 21 May 2021 13:57:29 -0400 Subject: [PATCH 1/8] First test. --- .../tests/{doucmentation.test.js => documentation.test.js} | 0 frontend/src/tests/instructor-permission-test.js | 7 ++++++- package.json | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) rename frontend/src/tests/{doucmentation.test.js => documentation.test.js} (100%) diff --git a/frontend/src/tests/doucmentation.test.js b/frontend/src/tests/documentation.test.js similarity index 100% rename from frontend/src/tests/doucmentation.test.js rename to frontend/src/tests/documentation.test.js diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index a0210d992..3b3977e5e 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -475,7 +475,12 @@ export function instructorsPermissionTests(api) { await restoreDefaultUser(); }); - it.todo("fetch Ddahs"); + it("fetch Ddahs", async () => { + await switchToInstructorOnlyUser(); + resp = await apiGET(`/instructor/sessions/${session.id}/ddahs`); + expect(resp).toHaveStatus("success"); + }); + it.todo("fetch Ddahs a position associated with self"); it.todo("fetch Ddahs an assignment associated with self"); it.todo("cannot fetch Ddahs for assignment not associated with self"); diff --git a/package.json b/package.json index 69a3266f6..4a880994b 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,8 @@ "react-app", "react-app/jest" ] + }, + "scripts": { + "test": "./tests/run-unit-tests.sh" } } From 926badff41f1f706db935029fab83837f9b3454b Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Sun, 23 May 2021 13:36:05 -0400 Subject: [PATCH 2/8] Add instructor before querying DDAHs. --- .../src/tests/instructor-permission-test.js | 38 ++++++++++++++++++- package.json | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index 3b3977e5e..42a5fa7f7 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -33,6 +33,33 @@ export function instructorsPermissionTests(api) { expect(respSwitchToInstOnlyUser).toHaveStatus("success"); } + async function createDDAH() { + const assignments = databaseSeeder.seededData.assignments; + const newDdah = { + assignment_id: assignments[0].id, + duties: [ + { + order: 2, + hours: 25, + description: "marking:Marking the midterm", + }, + { + order: 1, + hours: 4, + description: "training:Initial training", + }, + { + order: 3, + hours: 40, + description: "contact:Running tutorials", + }, + ], + }; + + const respCreateDDAH = await apiPOST(`/admin/ddahs`, newDdah); + expect(respCreateDDAH).toHaveStatus("success"); + } + /** * Restores the active user to the default user (the user logged during test setup in beforeAll). * @@ -476,9 +503,18 @@ export function instructorsPermissionTests(api) { }); it("fetch Ddahs", async () => { + const instructorObject = { + first_name: 'Jane', + last_name: 'Smith', + email: 'jane.smith@gmail.com', + utorid: instructorUser.utorid + }; + await apiPOST(`/admin/instructors`, instructorObject); + await switchToInstructorOnlyUser(); - resp = await apiGET(`/instructor/sessions/${session.id}/ddahs`); + let resp = await apiGET(`/instructor/sessions/${session.id}/ddahs`); expect(resp).toHaveStatus("success"); + expect(resp.payload).toHaveLength(0) }); it.todo("fetch Ddahs a position associated with self"); diff --git a/package.json b/package.json index 4a880994b..19314a18c 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,6 @@ ] }, "scripts": { - "test": "./tests/run-unit-tests.sh" + "test": "sudo ./tests/run-unit-tests.sh" } } From 92aa8d6c78109f482690dfd788fce7793dcc34f5 Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Fri, 28 May 2021 13:22:26 -0400 Subject: [PATCH 3/8] Also add position related to DDAH assignment. --- .../src/tests/instructor-permission-test.js | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index 42a5fa7f7..3730dfbec 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -33,10 +33,42 @@ export function instructorsPermissionTests(api) { expect(respSwitchToInstOnlyUser).toHaveStatus("success"); } - async function createDDAH() { - const assignments = databaseSeeder.seededData.assignments; + /** + * Restores the active user to the default user (the user logged during test setup in beforeAll). + * + * @returns {Promise} + */ + async function restoreDefaultUser() { + let respSwitchBackUser = await apiPOST( + `/debug/active_user`, + defaultUser + ); + expect(respSwitchBackUser).toHaveStatus("success"); + } + + /** + * TODO: write comment. + * + * + * @returns {Promise} + */ + async function createDDAH(intsructorId) { + // We first need to update position to include our instructor + const existingPosition = databaseSeeder.seededData.positions[0]; + const updatedPosition = { + ...existingPosition, + instructor_ids: [...existingPosition.instructor_ids, intsructorId], + } + const respUpdatePosition = await apiPOST(`/admin/positions`, updatedPosition); + expect(respUpdatePosition).toHaveStatus("success"); + + // We then proceed to create a DDAh for that position + switchToInstructorOnlyUser() + const assignments = await apiGET(`/admin/sessions/${session.id}/assignments`); + expect(assignments).toHaveStatus("success"); + expect(assignments.payload.length).toBeGreaterThan(0); const newDdah = { - assignment_id: assignments[0].id, + assignment_id: assignments.payload[0].id, duties: [ { order: 2, @@ -56,23 +88,11 @@ export function instructorsPermissionTests(api) { ], }; + restoreDefaultUser() const respCreateDDAH = await apiPOST(`/admin/ddahs`, newDdah); expect(respCreateDDAH).toHaveStatus("success"); } - /** - * Restores the active user to the default user (the user logged during test setup in beforeAll). - * - * @returns {Promise} - */ - async function restoreDefaultUser() { - let respSwitchBackUser = await apiPOST( - `/debug/active_user`, - defaultUser - ); - expect(respSwitchBackUser).toHaveStatus("success"); - } - beforeAll(async () => { await databaseSeeder.seed(api); await databaseSeeder.seedForInstructors(api); @@ -510,11 +530,12 @@ export function instructorsPermissionTests(api) { utorid: instructorUser.utorid }; await apiPOST(`/admin/instructors`, instructorObject); + await createDDAH(); await switchToInstructorOnlyUser(); let resp = await apiGET(`/instructor/sessions/${session.id}/ddahs`); expect(resp).toHaveStatus("success"); - expect(resp.payload).toHaveLength(0) + expect(resp.payload).toHaveLength(1) }); it.todo("fetch Ddahs a position associated with self"); From 0c50744503606c32f6d9c8e61ccad67c5dc67aa8 Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Fri, 28 May 2021 18:00:58 -0400 Subject: [PATCH 4/8] Clean up. --- .../src/tests/instructor-permission-test.js | 41 ++++++++++++------- package.json | 3 -- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index 3730dfbec..50e6996d5 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -52,19 +52,20 @@ export function instructorsPermissionTests(api) { * * @returns {Promise} */ - async function createDDAH(intsructorId) { + async function createDDAH(instructorId) { // We first need to update position to include our instructor const existingPosition = databaseSeeder.seededData.positions[0]; const updatedPosition = { ...existingPosition, - instructor_ids: [...existingPosition.instructor_ids, intsructorId], + instructor_ids: [...existingPosition.instructor_ids, instructorId], } - const respUpdatePosition = await apiPOST(`/admin/positions`, updatedPosition); - expect(respUpdatePosition).toHaveStatus("success"); + const positionResponse = await apiPOST(`/admin/positions`, updatedPosition); + expect(positionResponse).toHaveStatus("success"); // We then proceed to create a DDAh for that position - switchToInstructorOnlyUser() - const assignments = await apiGET(`/admin/sessions/${session.id}/assignments`); + // Switch to instructor user so we only have assignments for that instructor + await switchToInstructorOnlyUser(); + const assignments = await apiGET(`/instructor/sessions/${session.id}/assignments`); expect(assignments).toHaveStatus("success"); expect(assignments.payload.length).toBeGreaterThan(0); const newDdah = { @@ -88,9 +89,10 @@ export function instructorsPermissionTests(api) { ], }; - restoreDefaultUser() - const respCreateDDAH = await apiPOST(`/admin/ddahs`, newDdah); - expect(respCreateDDAH).toHaveStatus("success"); + await restoreDefaultUser(); + const ddahResponse = await apiPOST(`/admin/ddahs`, newDdah); + expect(ddahResponse).toHaveStatus("success"); + return ddahResponse.payload; } beforeAll(async () => { @@ -523,19 +525,30 @@ export function instructorsPermissionTests(api) { }); it("fetch Ddahs", async () => { + // If a user is not in Instructors table, it is not considered an instructor + // for the purpose of fetching DDAHs - so we create one const instructorObject = { first_name: 'Jane', last_name: 'Smith', email: 'jane.smith@gmail.com', utorid: instructorUser.utorid }; - await apiPOST(`/admin/instructors`, instructorObject); - await createDDAH(); + const instructorResponse = await apiPOST(`/admin/instructors`, instructorObject); + expect(instructorResponse).toHaveStatus("success"); + // Get newly created instructor and create a DDAH for them + const instructorsResponse = await apiGET(`/admin/instructors`); + expect(instructorsResponse).toHaveStatus("success"); + const instructorId = instructorsResponse.payload.find(instructor => instructor.utorid === instructorUser.utorid)?.id; + expect(instructorId).toBeDefined(); + const newDDAH = await createDDAH(instructorId); + + // Test the DDAH is fetched properly await switchToInstructorOnlyUser(); - let resp = await apiGET(`/instructor/sessions/${session.id}/ddahs`); - expect(resp).toHaveStatus("success"); - expect(resp.payload).toHaveLength(1) + const ddahsResponse = await apiGET(`/instructor/sessions/${session.id}/ddahs`); + expect(ddahsResponse).toHaveStatus("success"); + expect(ddahsResponse.payload).toHaveLength(1); + expect(ddahsResponse.payload[0]).toStrictEqual(newDDAH); }); it.todo("fetch Ddahs a position associated with self"); diff --git a/package.json b/package.json index 19314a18c..69a3266f6 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,5 @@ "react-app", "react-app/jest" ] - }, - "scripts": { - "test": "sudo ./tests/run-unit-tests.sh" } } From f8ff7d23b5600b39d3e20a7e00b1e7e655dff0ee Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Fri, 28 May 2021 18:09:48 -0400 Subject: [PATCH 5/8] Lint. --- .../src/tests/instructor-permission-test.js | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index 50e6996d5..331c4f450 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -46,9 +46,9 @@ export function instructorsPermissionTests(api) { expect(respSwitchBackUser).toHaveStatus("success"); } - /** + /** * TODO: write comment. - * + * * * @returns {Promise} */ @@ -58,14 +58,19 @@ export function instructorsPermissionTests(api) { const updatedPosition = { ...existingPosition, instructor_ids: [...existingPosition.instructor_ids, instructorId], - } - const positionResponse = await apiPOST(`/admin/positions`, updatedPosition); + }; + const positionResponse = await apiPOST( + `/admin/positions`, + updatedPosition + ); expect(positionResponse).toHaveStatus("success"); - + // We then proceed to create a DDAh for that position // Switch to instructor user so we only have assignments for that instructor await switchToInstructorOnlyUser(); - const assignments = await apiGET(`/instructor/sessions/${session.id}/assignments`); + const assignments = await apiGET( + `/instructor/sessions/${session.id}/assignments` + ); expect(assignments).toHaveStatus("success"); expect(assignments.payload.length).toBeGreaterThan(0); const newDdah = { @@ -528,24 +533,31 @@ export function instructorsPermissionTests(api) { // If a user is not in Instructors table, it is not considered an instructor // for the purpose of fetching DDAHs - so we create one const instructorObject = { - first_name: 'Jane', - last_name: 'Smith', - email: 'jane.smith@gmail.com', - utorid: instructorUser.utorid + first_name: "Jane", + last_name: "Smith", + email: "jane.smith@gmail.com", + utorid: instructorUser.utorid, }; - const instructorResponse = await apiPOST(`/admin/instructors`, instructorObject); + const instructorResponse = await apiPOST( + `/admin/instructors`, + instructorObject + ); expect(instructorResponse).toHaveStatus("success"); // Get newly created instructor and create a DDAH for them const instructorsResponse = await apiGET(`/admin/instructors`); expect(instructorsResponse).toHaveStatus("success"); - const instructorId = instructorsResponse.payload.find(instructor => instructor.utorid === instructorUser.utorid)?.id; + const instructorId = instructorsResponse.payload.find( + (instructor) => instructor.utorid === instructorUser.utorid + )?.id; expect(instructorId).toBeDefined(); const newDDAH = await createDDAH(instructorId); // Test the DDAH is fetched properly await switchToInstructorOnlyUser(); - const ddahsResponse = await apiGET(`/instructor/sessions/${session.id}/ddahs`); + const ddahsResponse = await apiGET( + `/instructor/sessions/${session.id}/ddahs` + ); expect(ddahsResponse).toHaveStatus("success"); expect(ddahsResponse.payload).toHaveLength(1); expect(ddahsResponse.payload[0]).toStrictEqual(newDDAH); From 10bfb838e1cff2bccf4e07b6ee422b22bf734d3c Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Fri, 28 May 2021 18:15:30 -0400 Subject: [PATCH 6/8] Add a comment for the helper function. --- frontend/src/tests/instructor-permission-test.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index 331c4f450..da97af272 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -47,10 +47,13 @@ export function instructorsPermissionTests(api) { } /** - * TODO: write comment. + * Updates a position to include instructor with and + * creates a DDAH for one of the assignments realted to that + * instructor. * + * @param instructorId: int - the unique Id of an instructor * - * @returns {Promise} + * @returns {Promise} */ async function createDDAH(instructorId) { // We first need to update position to include our instructor @@ -65,7 +68,7 @@ export function instructorsPermissionTests(api) { ); expect(positionResponse).toHaveStatus("success"); - // We then proceed to create a DDAh for that position + // We then proceed to create a DDAH for that position // Switch to instructor user so we only have assignments for that instructor await switchToInstructorOnlyUser(); const assignments = await apiGET( From 4572a3b18741a6384a4f90fa3e0a47f95333496b Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Sun, 30 May 2021 19:03:12 -0400 Subject: [PATCH 7/8] Address feedback. --- .../src/tests/instructor-permission-test.js | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index da97af272..447cee45b 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -47,7 +47,7 @@ export function instructorsPermissionTests(api) { } /** - * Updates a position to include instructor with and + * Updates a position to include instructor with `instructorId` and * creates a DDAH for one of the assignments realted to that * instructor. * @@ -55,22 +55,20 @@ export function instructorsPermissionTests(api) { * * @returns {Promise} */ - async function createDDAH(instructorId) { + async function createDdahWithFixedDuties(instructorId) { // We first need to update position to include our instructor const existingPosition = databaseSeeder.seededData.positions[0]; - const updatedPosition = { + const positionWithInstructor = { ...existingPosition, instructor_ids: [...existingPosition.instructor_ids, instructorId], }; const positionResponse = await apiPOST( `/admin/positions`, - updatedPosition + positionWithInstructor ); expect(positionResponse).toHaveStatus("success"); // We then proceed to create a DDAH for that position - // Switch to instructor user so we only have assignments for that instructor - await switchToInstructorOnlyUser(); const assignments = await apiGET( `/instructor/sessions/${session.id}/assignments` ); @@ -97,7 +95,6 @@ export function instructorsPermissionTests(api) { ], }; - await restoreDefaultUser(); const ddahResponse = await apiPOST(`/admin/ddahs`, newDdah); expect(ddahResponse).toHaveStatus("success"); return ddahResponse.payload; @@ -535,35 +532,30 @@ export function instructorsPermissionTests(api) { it("fetch Ddahs", async () => { // If a user is not in Instructors table, it is not considered an instructor // for the purpose of fetching DDAHs - so we create one - const instructorObject = { + const newInstructor = { first_name: "Jane", last_name: "Smith", email: "jane.smith@gmail.com", utorid: instructorUser.utorid, }; - const instructorResponse = await apiPOST( - `/admin/instructors`, - instructorObject - ); - expect(instructorResponse).toHaveStatus("success"); + let response = await apiPOST(`/admin/instructors`, newInstructor); + expect(response).toHaveStatus("success"); // Get newly created instructor and create a DDAH for them - const instructorsResponse = await apiGET(`/admin/instructors`); - expect(instructorsResponse).toHaveStatus("success"); - const instructorId = instructorsResponse.payload.find( + response = await apiGET(`/admin/instructors`); + expect(response).toHaveStatus("success"); + const instructorId = response.payload.find( (instructor) => instructor.utorid === instructorUser.utorid )?.id; expect(instructorId).toBeDefined(); - const newDDAH = await createDDAH(instructorId); + const newDDAH = await createDdahWithFixedDuties(instructorId); // Test the DDAH is fetched properly await switchToInstructorOnlyUser(); - const ddahsResponse = await apiGET( - `/instructor/sessions/${session.id}/ddahs` - ); - expect(ddahsResponse).toHaveStatus("success"); - expect(ddahsResponse.payload).toHaveLength(1); - expect(ddahsResponse.payload[0]).toStrictEqual(newDDAH); + response = await apiGET(`/instructor/sessions/${session.id}/ddahs`); + expect(response).toHaveStatus("success"); + expect(response.payload).toHaveLength(1); + expect(response.payload[0]).toStrictEqual(newDDAH); }); it.todo("fetch Ddahs a position associated with self"); From eafdd2364ee20bb67896b244d26c16535d498d1e Mon Sep 17 00:00:00 2001 From: Alex Kozin Date: Sun, 30 May 2021 19:06:03 -0400 Subject: [PATCH 8/8] Some more. --- .../src/tests/instructor-permission-test.js | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/frontend/src/tests/instructor-permission-test.js b/frontend/src/tests/instructor-permission-test.js index 447cee45b..0cbb54e64 100644 --- a/frontend/src/tests/instructor-permission-test.js +++ b/frontend/src/tests/instructor-permission-test.js @@ -62,20 +62,15 @@ export function instructorsPermissionTests(api) { ...existingPosition, instructor_ids: [...existingPosition.instructor_ids, instructorId], }; - const positionResponse = await apiPOST( - `/admin/positions`, - positionWithInstructor - ); - expect(positionResponse).toHaveStatus("success"); + let resp = await apiPOST(`/admin/positions`, positionWithInstructor); + expect(resp).toHaveStatus("success"); // We then proceed to create a DDAH for that position - const assignments = await apiGET( - `/instructor/sessions/${session.id}/assignments` - ); - expect(assignments).toHaveStatus("success"); - expect(assignments.payload.length).toBeGreaterThan(0); + resp = await apiGET(`/instructor/sessions/${session.id}/assignments`); + expect(resp).toHaveStatus("success"); + expect(resp.payload.length).toBeGreaterThan(0); const newDdah = { - assignment_id: assignments.payload[0].id, + assignment_id: resp.payload[0].id, duties: [ { order: 2, @@ -95,9 +90,9 @@ export function instructorsPermissionTests(api) { ], }; - const ddahResponse = await apiPOST(`/admin/ddahs`, newDdah); - expect(ddahResponse).toHaveStatus("success"); - return ddahResponse.payload; + resp = await apiPOST(`/admin/ddahs`, newDdah); + expect(resp).toHaveStatus("success"); + return resp.payload; } beforeAll(async () => { @@ -538,13 +533,13 @@ export function instructorsPermissionTests(api) { email: "jane.smith@gmail.com", utorid: instructorUser.utorid, }; - let response = await apiPOST(`/admin/instructors`, newInstructor); - expect(response).toHaveStatus("success"); + let resp = await apiPOST(`/admin/instructors`, newInstructor); + expect(resp).toHaveStatus("success"); // Get newly created instructor and create a DDAH for them - response = await apiGET(`/admin/instructors`); - expect(response).toHaveStatus("success"); - const instructorId = response.payload.find( + resp = await apiGET(`/admin/instructors`); + expect(resp).toHaveStatus("success"); + const instructorId = resp.payload.find( (instructor) => instructor.utorid === instructorUser.utorid )?.id; expect(instructorId).toBeDefined(); @@ -552,10 +547,10 @@ export function instructorsPermissionTests(api) { // Test the DDAH is fetched properly await switchToInstructorOnlyUser(); - response = await apiGET(`/instructor/sessions/${session.id}/ddahs`); - expect(response).toHaveStatus("success"); - expect(response.payload).toHaveLength(1); - expect(response.payload[0]).toStrictEqual(newDDAH); + resp = await apiGET(`/instructor/sessions/${session.id}/ddahs`); + expect(resp).toHaveStatus("success"); + expect(resp.payload).toHaveLength(1); + expect(resp.payload[0]).toStrictEqual(newDDAH); }); it.todo("fetch Ddahs a position associated with self");