Skip to content

Commit

Permalink
Refactored lesson, message, notification and quiz tests (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tolik170 authored Dec 20, 2023
1 parent cfeaa9d commit 4d9cfa3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docs/quiz/quiz-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ definitions:
type: boolean
pointValues:
type: boolean
scoredResponces:
scoredResponses:
type: boolean
correctAnswers:
type: boolean
Expand Down Expand Up @@ -66,7 +66,7 @@ definitions:
type: boolean
pointValues:
type: boolean
scoredResponces:
scoredResponses:
type: boolean
correctAnswers:
type: boolean
Expand Down Expand Up @@ -100,7 +100,7 @@ definitions:
type: boolean
pointValues:
type: boolean
scoredResponces:
scoredResponses:
type: boolean
correctAnswers:
type: boolean
Expand Down
8 changes: 4 additions & 4 deletions docs/quiz/quiz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ paths:
view: Scroll,
shuffle: true,
pointValues: false,
scoredResponces: false,
scoredResponses: false,
correctAnswers: true
}
- _id: 64ca5932b57f2442403394a9
Expand All @@ -61,7 +61,7 @@ paths:
view: Scroll,
shuffle: true,
pointValues: false,
scoredResponces: false,
scoredResponses: false,
correctAnswers: true
}
count: 2
Expand Down Expand Up @@ -122,7 +122,7 @@ paths:
view: Scroll,
shuffle: false,
pointValues: false,
scoredResponces: false,
scoredResponses: false,
correctAnswers: false
}
createdAt: 2023-02-14T23:44:21.334Z
Expand Down Expand Up @@ -182,7 +182,7 @@ paths:
view: Scroll,
shuffle: true,
pointValues: false,
scoredResponces: false,
scoredResponses: false,
correctAnswers: true
}
401:
Expand Down
2 changes: 1 addition & 1 deletion models/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const quizSchema = new Schema(
type: Boolean,
default: false
},
scoredResponces: {
scoredResponses: {
type: Boolean,
default: false
},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/controllers/lesson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const testLesson = {
title: 'title',
description: 'description',
category: '6502ec2060ec37be943353e2',
content: '<h1>Title</h1>',
content: '<h1>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</h1>',
attachments: ['65bed8ef260f18d04ab22da3', '65bed9ef260f19d05ab25bc6']
}

Expand Down
13 changes: 8 additions & 5 deletions test/integration/controllers/message.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,19 @@ describe('Message controller', () => {

describe(`PATCH ${endpointUrl}`, () => {
it('should clear chat history for current user', async () => {
const response = await app.patch(endpointUrl(messageBody.chat)).set('Authorization', `Bearer ${accessToken}`)
const response = await app.patch(endpointUrl(messageBody.chat)).set('Cookie', [`accessToken=${accessToken}`])

expect(response.statusCode).toBe(200)
expect(response.body).toEqual(clearedStatusBody)
})

it('should throw NOT MODIFIED if there is nothing to clear', async () => {
chatResponse = await app.post(chatEndpointUrl).set('Authorization', `Bearer ${accessToken}`).send(chatBody)
chatResponse = await app
.post(chatEndpointUrl)
.set('Cookie', [`accessToken=${accessToken}`])
.send(chatBody)
messageBody.chat = chatResponse.body._id
const response = await app.patch(endpointUrl(messageBody.chat)).set('Authorization', `Bearer ${accessToken}`)
const response = await app.patch(endpointUrl(messageBody.chat)).set('Cookie', [`accessToken=${accessToken}`])

expect(response.statusCode).toBe(304)
})
Expand All @@ -194,13 +197,13 @@ describe('Message controller', () => {

const response = await app
.patch(endpointUrl(messageBody.chat))
.set('Authorization', `Bearer ${accessTokenForbidden}`)
.set('Cookie', [`accessToken=${accessTokenForbidden}`])

expectError(403, FORBIDDEN, response)
})

it('should throw DOCUMENT_NOT_FOUND for chat', async () => {
const response = await app.patch(endpointUrl(nonExistingChatId)).set('Authorization', `Bearer ${accessToken}`)
const response = await app.patch(endpointUrl(nonExistingChatId)).set('Cookie', [`accessToken=${accessToken}`])

expectError(404, DOCUMENT_NOT_FOUND([Chat.modelName]), response)
})
Expand Down
9 changes: 6 additions & 3 deletions test/integration/controllers/notification.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { serverInit, serverCleanup, stopServer } = require('~/test/setup')
const { expectError } = require('~/test/helpers')
const { UNAUTHORIZED, DOCUMENT_NOT_FOUND } = require('~/consts/errors')
const {
enums: { NOTIFICATION_TYPE_ENUM }
} = require('~/consts/validation')
const testUserAuthentication = require('~/utils/testUserAuth')
const Notification = require('~/models/notification')
const TokenService = require('~/services/token')
Expand Down Expand Up @@ -29,7 +32,7 @@ describe('Notification controller', () => {
testNotification = await Notification.create({
user: currentUser.id,
userRole: currentUser.role,
type: 'review',
type: NOTIFICATION_TYPE_ENUM[0],
...testNotificationData
})
})
Expand All @@ -43,7 +46,7 @@ describe('Notification controller', () => {
})

describe(`GET ${endpointUrl}`, () => {
it("should get user's notifications and count them", async () => {
it('should get user`s notifications and count them', async () => {
const response = await app.get(endpointUrl).set('Cookie', [`accessToken=${accessToken}`])

expect(response.statusCode).toBe(200)
Expand All @@ -64,7 +67,7 @@ describe('Notification controller', () => {
})

describe(`DELETE ${endpointUrl}`, () => {
it("should clear all user's notifications", async () => {
it('should clear all user`s notifications', async () => {
const response = await app.delete(endpointUrl).set('Cookie', [`accessToken=${accessToken}`])

const notifications = await app.get(endpointUrl).set('Cookie', [`accessToken=${accessToken}`])
Expand Down
46 changes: 41 additions & 5 deletions test/integration/controllers/quiz.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,40 @@ const TokenService = require('~/services/token')
const {
roles: { TUTOR }
} = require('~/consts/auth')
const {
enums: { QUIZ_VIEW_ENUM }
} = require('~/consts/validation')

const endpointUrl = '/quizzes/'
const questionEndpointUrl = '/questions/'

const testQuizData = {
title: 'Assembly',
description: 'Description',
items: ['6527ed6c14c6b72f36962364']
settings: {
correctAnswers: false,
pointValues: false,
scoredResponses: false,
shuffle: false,
view: QUIZ_VIEW_ENUM[1]
}
}

const updateData = {
title: 'WebAssembly'
}

const testQuestionData = {
title: 'Assembly',
text: 'What is Assembly',
answers: [
{ text: 'Yes', isCorrect: true },
{ text: 'Yes, of course', isCorrect: false }
],
category: null,
type: 'multipleChoice'
}

const studentUserData = {
role: 'student',
firstName: 'Yamada',
Expand All @@ -32,7 +53,7 @@ const studentUserData = {
}

describe('Quiz controller', () => {
let app, server, accessToken, currentUser, studentAccessToken, testQuiz, testQuizId
let app, server, accessToken, currentUser, studentAccessToken, testQuiz, testQuizId, testQuestion, testQuestionId

beforeAll(async () => {
;({ app, server } = await serverInit())
Expand All @@ -44,10 +65,16 @@ describe('Quiz controller', () => {

currentUser = TokenService.validateAccessToken(accessToken)

testQuestion = await app
.post(questionEndpointUrl)
.send(testQuestionData)
.set('Cookie', [`accessToken=${accessToken}`])
testQuestionId = testQuestion._body._id

testQuiz = await app
.post(endpointUrl)
.set('Cookie', [`accessToken=${accessToken}`])
.send(testQuizData)
.send({ ...testQuizData, items: [testQuestionId] })
testQuizId = testQuiz.body._id
})

Expand Down Expand Up @@ -101,7 +128,8 @@ describe('Quiz controller', () => {
createdAt: expect.any(String),
updatedAt: expect.any(String),
...testQuizData,
category: null
category: null,
items: [testQuestionId]
}
],
count: 1
Expand Down Expand Up @@ -131,7 +159,15 @@ describe('Quiz controller', () => {
author: currentUser.id,
createdAt: expect.any(String),
updatedAt: expect.any(String),
...testQuizData
...testQuizData,
items: [
{
_id: testQuestionId,
title: testQuestionData.title,
text: testQuestionData.text,
answers: testQuestionData.answers
}
]
})
})

Expand Down

0 comments on commit 4d9cfa3

Please sign in to comment.