Skip to content

Commit

Permalink
Add integration tests for /theses route
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksTeresh committed May 15, 2024
1 parent d353294 commit ac3969d
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 20 deletions.
17 changes: 16 additions & 1 deletion setupIntegrationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import supertest from 'supertest'
import { jest } from '@jest/globals'
import * as db from './src/server/db/connection'
import app from './src/server/index'
import {
Attachment,
Author,
Supervision,
Thesis,
User,
} from './src/server/db/models'

// eslint-disable-next-line no-promise-executor-return
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
Expand All @@ -11,7 +18,7 @@ global.jest = jest
// eslint-disable-next-line import/no-mutable-exports
let request: any = null

global.beforeEach(async () => {
global.beforeAll(async () => {
// retry to connect to the DB in case the test runner starts before the DB is ready
await db.connectToDatabase(3)
request = supertest(app)
Expand All @@ -33,6 +40,14 @@ global.beforeEach(async () => {
}, 20000)

global.afterEach(async () => {
await Attachment.destroy({ where: {} })
await Supervision.destroy({ where: {} })
await Author.destroy({ where: {} })
await Thesis.destroy({ where: {} })
await User.destroy({ where: {} })
})

global.afterAll(async () => {
await db.resetDatabase()
})

Expand Down
2 changes: 2 additions & 0 deletions src/server/db/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
InferAttributes,
InferCreationAttributes,
DataTypes,
UUIDV4,
} from 'sequelize'

import { sequelize } from '../connection'
Expand All @@ -28,6 +29,7 @@ User.init(
id: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: UUIDV4,
primaryKey: true,
},
username: {
Expand Down
228 changes: 228 additions & 0 deletions src/server/routes/thesis.integration-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
import supertest from 'supertest'
import fs from 'fs'
import path from 'path'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

import app from '../index'
import { Attachment, Author, Supervision, Thesis, User } from '../db/models'

const request = supertest(app)

describe('thesis router', () => {
let mockUnlinkSync
beforeEach(() => {
mockUnlinkSync = jest.fn()
fs.unlinkSync = mockUnlinkSync
})

describe('when there are no theses', () => {
describe('GET /api/theses', () => {
it('should return 200 and an empty array', async () => {
const response = await request.get('/api/theses')
expect(response.status).toEqual(200)
expect(response.body).toEqual([])
})
})
})

describe('when there are theses saved in the DB', () => {
let user1
let user2
let thesis1

beforeEach(async () => {
user1 = await User.create({
username: 'test1',
firstName: 'test1',
lastName: 'test1',
email: '[email protected]',
language: 'fi',
})
user2 = await User.create({
username: 'test2',
firstName: 'test2',
lastName: 'test2',
email: '[email protected]',
language: 'fi',
})
thesis1 = await Thesis.create({
programId: 'Testing program',
topic: 'test topic',
status: 'PLANNING',
startDate: '1970-01-01',
targetDate: '2070-01-01',
})
await Supervision.create({
userId: user1.id,
thesisId: thesis1.id,
percentage: 100,
})
await Author.create({
userId: user2.id,
thesisId: thesis1.id,
})
await Attachment.create({
thesisId: thesis1.id,
label: 'researchPlan',
filename: 'testfile.pdf1',
mimetype: 'application/pdf1',
originalname: 'testfile.pdf1',
})
await Attachment.create({
thesisId: thesis1.id,
label: 'waysOfWorking',
filename: 'testfile.pdf2',
mimetype: 'application/pdf2',
originalname: 'testfile.pdf2',
})
})

describe('GET /api/theses', () => {
it('should return 200 and the theses correctly associated with users and attachments', async () => {
const response = await request.get('/api/theses')
expect(response.status).toEqual(200)
expect(response.body).toMatchObject([
{
programId: 'Testing program',
topic: 'test topic',
status: 'PLANNING',
startDate: '1970-01-01T00:00:00.000Z',
targetDate: '2070-01-01T00:00:00.000Z',
supervisions: [
{
userId: user1.id,
percentage: 100,
},
],
authors: [
{
userId: user2.id,
},
],
researchPlan: {
filename: 'testfile.pdf1',
name: 'testfile.pdf1',
mimetype: 'application/pdf1',
},
waysOfWorking: {
filename: 'testfile.pdf2',
name: 'testfile.pdf2',
mimetype: 'application/pdf2',
},
},
])
})
})

describe('DELETE /api/theses/:id', () => {
it('should return 204 and delete the thesis', async () => {
const response = await request.delete(`/api/theses/${thesis1.id}`)
expect(response.status).toEqual(204)
const thesis = await Thesis.findByPk(thesis1.id)
expect(thesis).toBeNull()

expect(fs.unlinkSync).toHaveBeenCalledTimes(2)
expect(fs.unlinkSync).toHaveBeenCalledWith(
'/opt/app-root/src/uploads/testfile.pdf1'
)
expect(fs.unlinkSync).toHaveBeenCalledWith(
'/opt/app-root/src/uploads/testfile.pdf2'
)
})
})

describe('POST /api/theses', () => {
it('should return 201 and create a new thesis', async () => {
const newThesis = {
programId: 'New program',
topic: 'New topic',
status: 'PLANNING',
startDate: '1970-01-01T00:00:00.000Z',
targetDate: '2070-01-01T00:00:00.000Z',
supervisions: [
{
userId: user1.id,
percentage: 100,
},
],
authors: [
{
userId: user2.id,
},
],
}
const response = await request
.post('/api/theses')
.attach(
'waysOfWorking',
path.resolve(dirname(fileURLToPath(import.meta.url)), './index.ts')
)
.attach(
'researchPlan',
path.resolve(dirname(fileURLToPath(import.meta.url)), './index.ts')
)
.field('json', JSON.stringify(newThesis))
expect(response.status).toEqual(201)

delete newThesis.supervisions
delete newThesis.authors
expect(response.body).toMatchObject(newThesis)

const thesis = await Thesis.findByPk(response.body.id)
expect(thesis).not.toBeNull()
})
})

describe('PUT /api/theses/:id', () => {
it('should return 200 and update the thesis', async () => {
const updatedThesis = {
programId: 'Updated program',
topic: 'Updated topic',
status: 'PLANNING',
startDate: '1970-01-01T00:00:00.000Z',
targetDate: '2070-01-01T00:00:00.000Z',
supervisions: [
{
userId: user1.id,
percentage: 100,
},
],
authors: [
{
userId: user2.id,
},
],
}
const response = await request
.put(`/api/theses/${thesis1.id}`)
.attach(
'waysOfWorking',
path.resolve(dirname(fileURLToPath(import.meta.url)), './index.ts')
)
.attach(
'researchPlan',
path.resolve(dirname(fileURLToPath(import.meta.url)), './index.ts')
)
.field('json', JSON.stringify(updatedThesis))

expect(fs.unlinkSync).toHaveBeenCalledTimes(2)
expect(fs.unlinkSync).toHaveBeenCalledWith(
'/opt/app-root/src/uploads/testfile.pdf1'
)
expect(fs.unlinkSync).toHaveBeenCalledWith(
'/opt/app-root/src/uploads/testfile.pdf2'
)

expect(response.status).toEqual(200)
delete updatedThesis.supervisions
delete updatedThesis.authors
expect(response.body).toMatchObject(updatedThesis)

const thesis = await Thesis.findByPk(thesis1.id)
expect(thesis.programId).toEqual('Updated program')
expect(thesis.topic).toEqual('Updated topic')
})
})
})
})
17 changes: 0 additions & 17 deletions src/server/routes/thesis.integration-test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/server/routes/thesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ thesisRouter.post(
return newThesis.toJSON()
})

res.send(createdThesis)
res.status(201).send(createdThesis)
}
)

Expand Down Expand Up @@ -234,7 +234,7 @@ thesisRouter.delete('/:id', async (req, res) => {
await deleteThesis(id, t)
})

res.send(`Deleted thesis with id ${id}`)
res.status(204).send(`Deleted thesis with id ${id}`)
})

export default thesisRouter

0 comments on commit ac3969d

Please sign in to comment.