From 13aced6fa64844512f920863e9ae25ad7bf3b9ed Mon Sep 17 00:00:00 2001 From: Will Baxter Date: Wed, 24 Jul 2024 11:13:00 +0100 Subject: [PATCH 1/6] Added scripts to JSON, fixed cohort test --- package.json | 3 ++- tests/api/routes/cohort.spec.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1f5ade47..bab79117 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "test-login": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/login.spec.js", "test-cohorts": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/cohort.spec.js", "test-logs": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/deliveryLog.spec.js", - "test-users": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/user.spec.js" + "test-users": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/user.spec.js", + "test-auth": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/auth.spec.js" }, "jest": { "transform": { diff --git a/tests/api/routes/cohort.spec.js b/tests/api/routes/cohort.spec.js index 801b9f79..addb9207 100644 --- a/tests/api/routes/cohort.spec.js +++ b/tests/api/routes/cohort.spec.js @@ -8,7 +8,7 @@ describe('Cohort Endpoint', () => { it('will allow a user with the role of teacher to create a new cohort', async () => { const uniqueEmail = `testuser${Date.now()}@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') - const token = jwt.sign(teacher.id, process.env.JWT_SECRET) + const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const request = {} const response = await supertest(app) .post('/cohorts') From 8d9a3d4233f44f39ca66c9e761f2183250d24054 Mon Sep 17 00:00:00 2001 From: Will Baxter Date: Wed, 24 Jul 2024 11:25:02 +0100 Subject: [PATCH 2/6] Fixed deliveryLog tests --- package.json | 3 ++- tests/api/routes/deliveryLog.spec.js | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index bab79117..3767b826 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "test-cohorts": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/cohort.spec.js", "test-logs": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/deliveryLog.spec.js", "test-users": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/user.spec.js", - "test-auth": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/auth.spec.js" + "test-auth": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/auth.spec.js", + "test-delivery-log": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/deliveryLog.spec.js" }, "jest": { "transform": { diff --git a/tests/api/routes/deliveryLog.spec.js b/tests/api/routes/deliveryLog.spec.js index 227813a4..59339205 100644 --- a/tests/api/routes/deliveryLog.spec.js +++ b/tests/api/routes/deliveryLog.spec.js @@ -9,7 +9,7 @@ describe('DeliveryLog Endpoint', () => { it('will allow teachers to create delivery logs for each cohort', async () => { const uniqueEmail = `testuser${Date.now()}@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') - const token = jwt.sign(teacher.id, process.env.JWT_SECRET) + const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const cohort = await createCohort() const request = { @@ -32,7 +32,7 @@ describe('DeliveryLog Endpoint', () => { it('will send a status code 400 if the cohortId or lines is missing from the request body', async () => { const uniqueEmail = `testuser${Date.now()}@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') - const token = jwt.sign(teacher.id, process.env.JWT_SECRET) + const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const request = {} const response = await supertest(app) @@ -49,13 +49,19 @@ describe('DeliveryLog Endpoint', () => { it('will send a status code 404 if the the cohort does not exist', async () => { const uniqueEmail = `testuser${Date.now()}@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') - const token = jwt.sign(teacher.id, process.env.JWT_SECRET) + const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const cohort = await createCohort() + let cohortId = 1 + while (cohort.id === cohortId) { + cohortId++ + } + const request = { - cohort_id: 100, + cohort_id: cohortId, lines: 'today in the class we covered abstraction' } + const response = await supertest(app) .post('/logs') .set('Authorization', `Bearer ${token}`) From e3feea1b15c8ba6ecd34b993f43af58138030255 Mon Sep 17 00:00:00 2001 From: Will Baxter Date: Wed, 24 Jul 2024 11:28:30 +0100 Subject: [PATCH 3/6] Fixed user tests --- tests/api/routes/user.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/api/routes/user.spec.js b/tests/api/routes/user.spec.js index 0baff5fa..f6aad5f5 100644 --- a/tests/api/routes/user.spec.js +++ b/tests/api/routes/user.spec.js @@ -83,8 +83,8 @@ describe('Users Endpoint', () => { .get(`/users/${user.id}`) .set('Authorization', `Bearer ${token}`) - expect(response.status).toEqual(200) - expect(response.body.data.user.email).toEqual(user.email) + expect(response.status).toEqual(201) + expect(response.body.data.user.user.email).toEqual(user.email) }) it('should throw an error if no user exists with that ID', async () => { const user1 = await createUser( From 0cfcfcb5b929a877226ca7a46a9f7f30f0ab73b5 Mon Sep 17 00:00:00 2001 From: Will Baxter Date: Wed, 24 Jul 2024 14:24:01 +0100 Subject: [PATCH 4/6] Updated tests --- package.json | 3 +-- src/controllers/deliveryLog.js | 1 + tests/api/routes/deliveryLog.spec.js | 2 +- tests/setupTests.js | 8 +++++++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 3767b826..bab79117 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,7 @@ "test-cohorts": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/cohort.spec.js", "test-logs": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/deliveryLog.spec.js", "test-users": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/user.spec.js", - "test-auth": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/auth.spec.js", - "test-delivery-log": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/deliveryLog.spec.js" + "test-auth": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/auth.spec.js" }, "jest": { "transform": { diff --git a/src/controllers/deliveryLog.js b/src/controllers/deliveryLog.js index 11459a8a..3f7af785 100644 --- a/src/controllers/deliveryLog.js +++ b/src/controllers/deliveryLog.js @@ -18,6 +18,7 @@ export const createDeliveryLog = async (req, res) => { if (!cohortExists) { return sendDataResponse(res, 404, { error: ERR.COHORT_NOT_FOUND }) } + const log = await createDeliveryLogDb(cohortId, req.user.id, lines) const logLines = log.lines.map((line) => ({ diff --git a/tests/api/routes/deliveryLog.spec.js b/tests/api/routes/deliveryLog.spec.js index 59339205..065af8b5 100644 --- a/tests/api/routes/deliveryLog.spec.js +++ b/tests/api/routes/deliveryLog.spec.js @@ -9,12 +9,12 @@ describe('DeliveryLog Endpoint', () => { it('will allow teachers to create delivery logs for each cohort', async () => { const uniqueEmail = `testuser${Date.now()}@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') + console.log(teacher) const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const cohort = await createCohort() const request = { cohort_id: cohort.id, - userId: teacher.id, lines: [{ id: 1, content: 'today in the class we covered abstraction' }] } const response = await supertest(app) diff --git a/tests/setupTests.js b/tests/setupTests.js index 0caec265..b258c7bf 100644 --- a/tests/setupTests.js +++ b/tests/setupTests.js @@ -1,11 +1,13 @@ import dbClient from '../src/utils/dbClient.js' const deleteTables = () => { + console.log('fired') const deleteTables = [ dbClient.deliveryLogLine.deleteMany(), dbClient.deliveryLog.deleteMany(), dbClient.cohort.deleteMany(), dbClient.post.deleteMany(), + dbClient.note.deleteMany(), dbClient.profile.deleteMany(), dbClient.user.deleteMany() ] @@ -16,10 +18,14 @@ global.beforeAll(() => { return deleteTables() }) +global.beforeEach(() => { + return deleteTables() +}) + global.afterEach(() => { return deleteTables() }) global.afterAll(() => { - return dbClient.$disconnect + return dbClient.$disconnect() }) From b8f2cf226591e699c7f0981b2be709b180368b31 Mon Sep 17 00:00:00 2001 From: Will Baxter Date: Thu, 25 Jul 2024 09:41:36 +0100 Subject: [PATCH 5/6] Refactored posts tests --- .../migration.sql | 47 +++++++++++++++++++ prisma/schema.prisma | 16 +++---- tests/api/routes/deliveryLog.spec.js | 7 ++- tests/helpers/createCohort.js | 6 +-- tests/setupTests.js | 9 +--- 5 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql diff --git a/prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql b/prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql new file mode 100644 index 00000000..45f92d3b --- /dev/null +++ b/prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql @@ -0,0 +1,47 @@ +-- DropForeignKey +ALTER TABLE "DeliveryLog" DROP CONSTRAINT "DeliveryLog_cohortId_fkey"; + +-- DropForeignKey +ALTER TABLE "DeliveryLog" DROP CONSTRAINT "DeliveryLog_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "DeliveryLogLine" DROP CONSTRAINT "DeliveryLogLine_logId_fkey"; + +-- DropForeignKey +ALTER TABLE "Note" DROP CONSTRAINT "Note_studentId_fkey"; + +-- DropForeignKey +ALTER TABLE "Note" DROP CONSTRAINT "Note_teacherId_fkey"; + +-- DropForeignKey +ALTER TABLE "Post" DROP CONSTRAINT "Post_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "User" DROP CONSTRAINT "User_cohortId_fkey"; + +-- AddForeignKey +ALTER TABLE "User" ADD CONSTRAINT "User_cohortId_fkey" FOREIGN KEY ("cohortId") REFERENCES "Cohort"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Post" ADD CONSTRAINT "Post_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "DeliveryLog" ADD CONSTRAINT "DeliveryLog_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "DeliveryLog" ADD CONSTRAINT "DeliveryLog_cohortId_fkey" FOREIGN KEY ("cohortId") REFERENCES "Cohort"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "DeliveryLogLine" ADD CONSTRAINT "DeliveryLogLine_logId_fkey" FOREIGN KEY ("logId") REFERENCES "DeliveryLog"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Note" ADD CONSTRAINT "Note_teacherId_fkey" FOREIGN KEY ("teacherId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Note" ADD CONSTRAINT "Note_studentId_fkey" FOREIGN KEY ("studentId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 087470bd..1fd1eab9 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -22,7 +22,7 @@ model User { role UserRole @default(STUDENT) profile Profile? cohortId Int? - cohort Cohort? @relation(fields: [cohortId], references: [id]) + cohort Cohort? @relation(fields: [cohortId], references: [id], onDelete: Cascade) posts Post[] deliveryLogs DeliveryLog[] notesCreated Note[] @relation("TeacherNotes") @@ -32,7 +32,7 @@ model User { model Profile { id Int @id @default(autoincrement()) userId Int @unique - user User @relation(fields: [userId], references: [id]) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) firstName String? lastName String? username String? @@ -51,7 +51,7 @@ model Post { id Int @id @default(autoincrement()) content String userId Int - user User @relation(fields: [userId], references: [id]) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) updatedAt DateTime? @updatedAt } @@ -60,9 +60,9 @@ model DeliveryLog { id Int @id @default(autoincrement()) date DateTime @default(now()) userId Int - user User @relation(fields: [userId], references: [id]) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) cohortId Int - cohort Cohort @relation(fields: [cohortId], references: [id]) + cohort Cohort @relation(fields: [cohortId], references: [id], onDelete: Cascade) lines DeliveryLogLine[] } @@ -70,16 +70,16 @@ model DeliveryLogLine { id Int @id @default(autoincrement()) content String logId Int - log DeliveryLog @relation(fields: [logId], references: [id]) + log DeliveryLog @relation(fields: [logId], references: [id], onDelete: Cascade) } model Note { id Int @id @default(autoincrement()) content String teacherId Int - teacher User @relation("TeacherNotes", fields: [teacherId], references: [id]) + teacher User @relation("TeacherNotes", fields: [teacherId], references: [id], onDelete: Cascade) studentId Int - student User @relation("StudentNotes", fields: [studentId], references: [id]) + student User @relation("StudentNotes", fields: [studentId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) updatedAt DateTime? @updatedAt } diff --git a/tests/api/routes/deliveryLog.spec.js b/tests/api/routes/deliveryLog.spec.js index 065af8b5..de59c459 100644 --- a/tests/api/routes/deliveryLog.spec.js +++ b/tests/api/routes/deliveryLog.spec.js @@ -7,9 +7,8 @@ import { createCohort } from '../../helpers/createCohort.js' describe('DeliveryLog Endpoint', () => { describe('POST/logs', () => { it('will allow teachers to create delivery logs for each cohort', async () => { - const uniqueEmail = `testuser${Date.now()}@gmail.com` + const uniqueEmail = `testuser@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') - console.log(teacher) const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const cohort = await createCohort() @@ -30,7 +29,7 @@ describe('DeliveryLog Endpoint', () => { ) }) it('will send a status code 400 if the cohortId or lines is missing from the request body', async () => { - const uniqueEmail = `testuser${Date.now()}@gmail.com` + const uniqueEmail = `testuser@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) @@ -47,7 +46,7 @@ describe('DeliveryLog Endpoint', () => { ) }) it('will send a status code 404 if the the cohort does not exist', async () => { - const uniqueEmail = `testuser${Date.now()}@gmail.com` + const uniqueEmail = `testuser@gmail.com` const teacher = await createUser(uniqueEmail, 'password', 'TEACHER') const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const cohort = await createCohort() diff --git a/tests/helpers/createCohort.js b/tests/helpers/createCohort.js index 5a6846b6..ba624e56 100644 --- a/tests/helpers/createCohort.js +++ b/tests/helpers/createCohort.js @@ -1,9 +1,5 @@ import dbClient from '../../src/utils/dbClient' export const createCohort = async () => { - return await dbClient.cohort.create({ - data: { - id: 1 - } - }) + return await dbClient.cohort.create() } diff --git a/tests/setupTests.js b/tests/setupTests.js index b258c7bf..fa4800f8 100644 --- a/tests/setupTests.js +++ b/tests/setupTests.js @@ -1,15 +1,14 @@ import dbClient from '../src/utils/dbClient.js' const deleteTables = () => { - console.log('fired') const deleteTables = [ + dbClient.user.deleteMany(), dbClient.deliveryLogLine.deleteMany(), dbClient.deliveryLog.deleteMany(), dbClient.cohort.deleteMany(), dbClient.post.deleteMany(), dbClient.note.deleteMany(), - dbClient.profile.deleteMany(), - dbClient.user.deleteMany() + dbClient.profile.deleteMany() ] return dbClient.$transaction(deleteTables) } @@ -18,10 +17,6 @@ global.beforeAll(() => { return deleteTables() }) -global.beforeEach(() => { - return deleteTables() -}) - global.afterEach(() => { return deleteTables() }) From 14e32da2ae13c7060d732937279e4c3985deb830 Mon Sep 17 00:00:00 2001 From: Will Baxter Date: Thu, 25 Jul 2024 09:44:21 +0100 Subject: [PATCH 6/6] Removed unnecessary migration --- .../migration.sql | 47 ------------------- 1 file changed, 47 deletions(-) delete mode 100644 prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql diff --git a/prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql b/prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql deleted file mode 100644 index 45f92d3b..00000000 --- a/prisma/migrations/20240724144338_explicit_cascade_delete/migration.sql +++ /dev/null @@ -1,47 +0,0 @@ --- DropForeignKey -ALTER TABLE "DeliveryLog" DROP CONSTRAINT "DeliveryLog_cohortId_fkey"; - --- DropForeignKey -ALTER TABLE "DeliveryLog" DROP CONSTRAINT "DeliveryLog_userId_fkey"; - --- DropForeignKey -ALTER TABLE "DeliveryLogLine" DROP CONSTRAINT "DeliveryLogLine_logId_fkey"; - --- DropForeignKey -ALTER TABLE "Note" DROP CONSTRAINT "Note_studentId_fkey"; - --- DropForeignKey -ALTER TABLE "Note" DROP CONSTRAINT "Note_teacherId_fkey"; - --- DropForeignKey -ALTER TABLE "Post" DROP CONSTRAINT "Post_userId_fkey"; - --- DropForeignKey -ALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey"; - --- DropForeignKey -ALTER TABLE "User" DROP CONSTRAINT "User_cohortId_fkey"; - --- AddForeignKey -ALTER TABLE "User" ADD CONSTRAINT "User_cohortId_fkey" FOREIGN KEY ("cohortId") REFERENCES "Cohort"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Post" ADD CONSTRAINT "Post_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "DeliveryLog" ADD CONSTRAINT "DeliveryLog_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "DeliveryLog" ADD CONSTRAINT "DeliveryLog_cohortId_fkey" FOREIGN KEY ("cohortId") REFERENCES "Cohort"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "DeliveryLogLine" ADD CONSTRAINT "DeliveryLogLine_logId_fkey" FOREIGN KEY ("logId") REFERENCES "DeliveryLog"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Note" ADD CONSTRAINT "Note_teacherId_fkey" FOREIGN KEY ("teacherId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "Note" ADD CONSTRAINT "Note_studentId_fkey" FOREIGN KEY ("studentId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;