Skip to content

Commit

Permalink
Improve the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anjula-sack committed Sep 10, 2023
1 parent de258d4 commit e9d673a
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 96 deletions.
17 changes: 17 additions & 0 deletions mocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
const randomString = Math.random().toString(36)

export const mockMentor = {
email: `mentor${randomString}@gmail.com`,
password: '123'
}

export const mockAdmin = {
email: `admin${randomString}@gmail.com`,
password: 'admin123'
}

export const mockUser = {
email: `user${randomString}@gmail.com`,
password: '123'
}

export const mentorApplicationInfo = {
application: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/admin/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const addCategory = async (
categoryName
)

res.status(200).json({ category, statusCode, message })
res.status(statusCode).json({ category, message })
}
} catch (err) {
console.error('Error executing query', err)
Expand Down
26 changes: 7 additions & 19 deletions src/routes/admin/category/category.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import Profile from '../../../entities/profile.entity'
import { ProfileTypes } from '../../../enums'
import { dataSource } from '../../../configs/dbConfig'
import bcrypt from 'bcrypt'
import { mockUser, mockAdmin } from '../../../../mocks'

const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

const randomString = Math.random().toString(36)
const randomStringAdmin = Math.random().toString(36)
let server: Express
let agent: supertest.SuperAgentTest
let adminAgent: supertest.SuperAgentTest
Expand All @@ -20,28 +19,17 @@ describe('Admin category routes', () => {
agent = supertest.agent(server)
adminAgent = supertest.agent(server)

const defaultUser = {
email: `test${randomString}@gmail.com`,
password: '123'
}

await supertest(server)
.post('/api/auth/register')
.send(defaultUser)
.send(mockUser)
.expect(201)

await agent.post('/api/auth/login').send(defaultUser).expect(200)

const adminUser = {
email: `test${randomStringAdmin}@gmail.com`,
password: 'admin123'
}
await agent.post('/api/auth/login').send(mockUser).expect(200)

const profileRepository = dataSource.getRepository(Profile)

const hashedPassword = await bcrypt.hash(adminUser.password, 10)
const hashedPassword = await bcrypt.hash(mockAdmin.password, 10)
const newProfile = profileRepository.create({
primary_email: adminUser.email,
primary_email: mockAdmin.email,
password: hashedPassword,
contact_email: '',
first_name: '',
Expand All @@ -53,14 +41,14 @@ describe('Admin category routes', () => {

await profileRepository.save(newProfile)

await adminAgent.post('/api/auth/login').send(adminUser).expect(200)
await adminAgent.post('/api/auth/login').send(mockAdmin).expect(200)
}, 5000)

it('should add a category', async () => {
await adminAgent
.post('/api/admin/categories')
.send({ categoryName: 'Computer Science' })
.expect(200)
.expect(201)
})

it('should only allow admins to add a category', async () => {
Expand Down
50 changes: 20 additions & 30 deletions src/routes/admin/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import Profile from '../../../entities/profile.entity'
import { ProfileTypes } from '../../../enums'
import { dataSource } from '../../../configs/dbConfig'
import bcrypt from 'bcrypt'
import Category from '../../../entities/category.entity'
import { mentorApplicationInfo } from '../../../../mocks'
import { mentorApplicationInfo, mockAdmin, mockMentor } from '../../../../mocks'

const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

const randomString = Math.random().toString(36)
const randomStringAdmin = Math.random().toString(36)
let server: Express
let mentorAgent: supertest.SuperAgentTest
let adminAgent: supertest.SuperAgentTest
Expand All @@ -23,34 +20,13 @@ describe('Admin mentor routes', () => {
mentorAgent = supertest.agent(server)
adminAgent = supertest.agent(server)

const mentor = {
email: `mentor${randomString}@gmail.com`,
password: '123'
}

await mentorAgent.post('/api/auth/register').send(mentor).expect(201)
await mentorAgent.post('/api/auth/login').send(mentor).expect(200)

const categoryRepository = dataSource.getRepository(Category)
const newCategory = new Category('Computer Science', [])
const category = await categoryRepository.save(newCategory)

const response = await mentorAgent
.post('/api/mentors')
.send({ ...mentorApplicationInfo, categoryId: category.uuid })
.expect(201)

mentorId = response.body.mentor.uuid

const adminUser = {
email: `test${randomStringAdmin}@gmail.com`,
password: 'admin123'
}
await mentorAgent.post('/api/auth/register').send(mockMentor).expect(201)
await mentorAgent.post('/api/auth/login').send(mockMentor).expect(200)

const profileRepository = dataSource.getRepository(Profile)
const hashedPassword = await bcrypt.hash(adminUser.password, 10)
const hashedPassword = await bcrypt.hash(mockAdmin.password, 10)
const newProfile = profileRepository.create({
primary_email: adminUser.email,
primary_email: mockAdmin.email,
password: hashedPassword,
contact_email: '',
first_name: '',
Expand All @@ -62,7 +38,21 @@ describe('Admin mentor routes', () => {

await profileRepository.save(newProfile)

await adminAgent.post('/api/auth/login').send(adminUser).expect(200)
await adminAgent.post('/api/auth/login').send(mockAdmin).expect(200)
const categoryResponse = await adminAgent
.post('/api/admin/categories')
.send({ categoryName: 'Computer Science' })
.expect(201)

const response = await mentorAgent
.post('/api/mentors')
.send({
...mentorApplicationInfo,
categoryId: categoryResponse.body.category.uuid
})
.expect(201)

mentorId = response.body.mentor.uuid
}, 5000)

it('should update the mentor application state', async () => {
Expand Down
24 changes: 6 additions & 18 deletions src/routes/admin/user/user.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import Profile from '../../../entities/profile.entity'
import { ProfileTypes } from '../../../enums'
import { dataSource } from '../../../configs/dbConfig'
import bcrypt from 'bcrypt'
import { mockAdmin, mockUser } from '../../../../mocks'

const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

const randomString = Math.random().toString(36)
const randomStringAdmin = Math.random().toString(36)
let server: Express
let agent: supertest.SuperAgentTest
let adminAgent: supertest.SuperAgentTest
Expand All @@ -20,28 +19,17 @@ describe('Admin user routes', () => {
agent = supertest.agent(server)
adminAgent = supertest.agent(server)

const defaultUser = {
email: `test${randomString}@gmail.com`,
password: '123'
}

await supertest(server)
.post('/api/auth/register')
.send(defaultUser)
.send(mockUser)
.expect(201)

await agent.post('/api/auth/login').send(defaultUser).expect(200)

const adminUser = {
email: `test${randomStringAdmin}@gmail.com`,
password: 'admin123'
}
await agent.post('/api/auth/login').send(mockUser).expect(200)

const profileRepository = dataSource.getRepository(Profile)

const hashedPassword = await bcrypt.hash(adminUser.password, 10)
const hashedPassword = await bcrypt.hash(mockAdmin.password, 10)
const newProfile = profileRepository.create({
primary_email: adminUser.email,
primary_email: mockAdmin.email,
password: hashedPassword,
contact_email: '',
first_name: '',
Expand All @@ -53,7 +41,7 @@ describe('Admin user routes', () => {

await profileRepository.save(newProfile)

await adminAgent.post('/api/auth/login').send(adminUser).expect(200)
await adminAgent.post('/api/auth/login').send(mockAdmin).expect(200)
}, 5000)

it('should return a 401 when a valid access token is not provided', async () => {
Expand Down
15 changes: 5 additions & 10 deletions src/routes/auth/auth.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ import { startServer } from '../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import { dataSource } from '../../configs/dbConfig'
import { mockUser } from '../../../mocks'

const randomString = Math.random().toString(36)
const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

let server: Express
let agent: supertest.SuperAgentTest

const testUser = {
email: `test${randomString}@gmail.com`,
password: '123'
}

beforeAll(async () => {
server = await startServer(port)
agent = supertest.agent(server)
Expand All @@ -28,7 +23,7 @@ describe('auth controllers', () => {
it('should return a 201 with a user profile after successful registration', async () => {
const response = await supertest(server)
.post('/api/auth/register')
.send(testUser)
.send(mockUser)
.expect(201)

expect(response.body).toHaveProperty('message')
Expand All @@ -48,7 +43,7 @@ describe('auth controllers', () => {
it('should return a 400 when registering with a duplicate email', async () => {
await supertest(server)
.post('/api/auth/register')
.send(testUser)
.send(mockUser)
.expect(409)
})
})
Expand All @@ -61,15 +56,15 @@ describe('auth controllers', () => {
it('should return a 200 after successful login', async () => {
const response = await supertest(server)
.post('/api/auth/login')
.send(testUser)
.send(mockUser)
.expect(200)

expect(response.body).toHaveProperty('message')
})

it('should return a 401 when logging in with incorrect credentials', async () => {
const incorrectUser = {
email: `test${randomString}@gmail.com`,
email: mockUser.email,
password: 'incorrect_password'
}

Expand Down
12 changes: 3 additions & 9 deletions src/routes/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { startServer } from '../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import { dataSource } from '../../configs/dbConfig'
import { mentorApplicationInfo } from '../../../mocks'
import { mentorApplicationInfo, mockUser } from '../../../mocks'
import { v4 as uuidv4 } from 'uuid'
import Category from '../../entities/category.entity'

const randomString = Math.random().toString(36)
const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

let server: Express
Expand All @@ -18,17 +17,12 @@ describe('Mentor application', () => {
server = await startServer(port)
agent = supertest.agent(server)

const testUser = {
email: `test${randomString}@gmail.com`,
password: '123'
}

await supertest(server)
.post('/api/auth/register')
.send(testUser)
.send(mockUser)
.expect(201)

await agent.post('/api/auth/login').send(testUser).expect(200)
await agent.post('/api/auth/login').send(mockUser).expect(200)

const categoryRepository = dataSource.getRepository(Category)
const newCategory = new Category('Random Category', [])
Expand Down
11 changes: 3 additions & 8 deletions src/routes/profile/profile.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { startServer } from '../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import { dataSource } from '../../configs/dbConfig'
import { mockUser } from '../../../mocks'

const randomString = Math.random().toString(36)
const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

let server: Express
Expand All @@ -14,17 +14,12 @@ describe('profile', () => {
server = await startServer(port)
agent = supertest.agent(server)

const testUser = {
email: `test${randomString}@gmail.com`,
password: '123'
}

await supertest(server)
.post('/api/auth/register')
.send(testUser)
.send(mockUser)
.expect(201)

await agent.post('/api/auth/login').send(testUser).expect(200)
await agent.post('/api/auth/login').send(mockUser).expect(200)
}, 5000)

describe('Get profile route', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/admin/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const createCategory = async (
const saveCategory = await categoryRepository.save(newCategory)

return {
statusCode: 200,
statusCode: 201,
category: saveCategory,
message: 'Category created successfully'
}
Expand Down

0 comments on commit e9d673a

Please sign in to comment.