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/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/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/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') diff --git a/tests/api/routes/deliveryLog.spec.js b/tests/api/routes/deliveryLog.spec.js index 227813a4..de59c459 100644 --- a/tests/api/routes/deliveryLog.spec.js +++ b/tests/api/routes/deliveryLog.spec.js @@ -7,14 +7,13 @@ 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') - 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 = { cohort_id: cohort.id, - userId: teacher.id, lines: [{ id: 1, content: 'today in the class we covered abstraction' }] } const response = await supertest(app) @@ -30,9 +29,9 @@ 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(teacher.id, process.env.JWT_SECRET) + const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET) const request = {} const response = await supertest(app) @@ -47,15 +46,21 @@ 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(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}`) 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( 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 0caec265..fa4800f8 100644 --- a/tests/setupTests.js +++ b/tests/setupTests.js @@ -2,12 +2,13 @@ import dbClient from '../src/utils/dbClient.js' const deleteTables = () => { const deleteTables = [ + dbClient.user.deleteMany(), dbClient.deliveryLogLine.deleteMany(), dbClient.deliveryLog.deleteMany(), dbClient.cohort.deleteMany(), dbClient.post.deleteMany(), - dbClient.profile.deleteMany(), - dbClient.user.deleteMany() + dbClient.note.deleteMany(), + dbClient.profile.deleteMany() ] return dbClient.$transaction(deleteTables) } @@ -21,5 +22,5 @@ global.afterEach(() => { }) global.afterAll(() => { - return dbClient.$disconnect + return dbClient.$disconnect() })