Skip to content

Instantly share code, notes, and snippets.

@rsheppard-dev
Last active May 28, 2021 20:06
Show Gist options
  • Save rsheppard-dev/fe03d596206ed841448bf42cd9f0818c to your computer and use it in GitHub Desktop.
Save rsheppard-dev/fe03d596206ed841448bf42cd9f0818c to your computer and use it in GitHub Desktop.
Extra Test's Solutions
const request = require('supertest')
const app = require('../src/app')
const Task = require('../src/models/task')
const {
userOneID,
userOne,
userTwoID,
userTwo,
taskOne,
taskTwo,
taskThree,
setupDatabase
} = require('./fixtures/db')
beforeEach(setupDatabase)
test('Should create task for user', async () => {
const response = await request(app)
.post('/tasks')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
description: 'Run node scripts'
})
.expect(201)
const task = await Task.findById(response.body._id)
expect(task).not.toBeNull()
expect(task.completed).toBe(false)
})
test('Should get all tasks for a user', async () => {
const response = await request(app)
.get('/tasks')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(response.body.length).toBe(2)
})
test('Check user cannot delete someone elses tasks', async () => {
await request(app)
.delete('/tasks/' + taskOne._id)
.set('Authorization', `Bearer ${userTwo.tokens[0].token}`)
.send()
.expect(404)
const task = await Task.findById(taskOne._id)
expect(task).not.toBeNull()
})
test('Should not create task with invalid description/completed', async () => {
await request(app)
.post('/tasks')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
description: ''
})
.expect(400)
await request(app)
.post('/tasks')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
description: 'Create test suite',
completed: 'Yes'
})
.expect(400)
})
test('Should not update task with invalid description/completed', async () => {
await request(app)
.patch('/tasks/' + taskOne._id)
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
description: ''
})
.expect(500)
await request(app)
.patch('/tasks/' + taskOne._id)
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
description: 'Create test suite',
completed: 'Yes'
})
.expect(500)
})
test('Should delete a task', async () => {
await request(app)
.delete('/tasks/' + taskOne._id)
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
const task = await Task.findById(taskOne._id)
expect(task).toBeNull()
})
test('Should not delete task if unauthenticated', async () => {
await request(app)
.delete('/tasks/' + taskOne._id)
.send()
.expect(401)
const task = await Task.findById(taskOne._id)
expect(task).not.toBeNull()
})
test('Should not update other users task', async () => {
await request(app)
.patch('/tasks/' + taskOne._id)
.set('Authorization', `Bearer ${userTwo.tokens[0].token}`)
.send({
completed: true
})
.expect(404)
})
test('Should fetch user task by id', async () => {
await request(app)
.get('/tasks/' + taskOne._id)
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
})
test('Should not fetch user task if unauthenticated', async () => {
await request(app)
.get('/tasks/' + taskOne._id)
.send()
.expect(401)
})
test('Should not fetch other users task by id', async () => {
await request(app)
.get('/tasks/' + taskOne._id)
.set('Authorization', `Bearer ${userTwo.tokens[0].token}`)
.send()
.expect(404)
})
test('Should fetch only completed tasks', async () => {
const response = await request(app)
.get('/tasks?completed=true')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(response.body.length).toBe(1)
})
test('Should fetch only incomplete tasks', async () => {
const { body: tasks } = await request(app)
.get('/tasks?completed=false')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(tasks.length).toBe(1)
})
test('Should sort tasks by description/completed/createdAt/updatedAt', async () => {
const { body: tasksByDescription } = await request(app)
.get('/tasks?sortBy=description_desc')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(tasksByDescription[0]._id.toString()).toBe(taskTwo._id.toString())
const { body: tasksByCompleted } = await request(app)
.get('/tasks?sortBy=completed_desc')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(tasksByCompleted[0]._id.toString()).toBe(taskTwo._id.toString())
const { body: tasksByCreatedAt } = await request(app)
.get('/tasks?sortBy=createdAt_desc')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(tasksByCreatedAt[0]._id.toString()).toBe(taskTwo._id.toString())
await Task.findByIdAndUpdate(taskOne._id, { completed: true })
const { body: tasksByUpdatedAt } = await request(app)
.get('/tasks?sortBy=updatedAt_desc')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(tasksByUpdatedAt[0]._id.toString()).toBe(taskOne._id.toString())
})
test('Should fetch page of tasks', async () => {
const { body: tasks } = await request(app)
.get('/tasks?skip=0&limit=1')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
expect(tasks.length).toBe(1)
})
const request = require('supertest')
const app = require('../src/app')
const User = require('../src/models/user')
const { userOneID, userOne, setupDatabase } = require('./fixtures/db')
beforeEach(setupDatabase)
test('Should sign up a new user', async () => {
const response = await request(app)
.post('/users')
.send({
name: 'Roy',
email: 'rsheppard83@gmail.com',
password: 'MyPass777!'
}).expect(201)
// assert that the database was changed correctly
const user = await User.findById(response.body.user._id)
expect(user).not.toBeNull()
// assertions about body response
expect(response.body).toMatchObject({
user: {
name: 'Roy',
email: 'rsheppard83@gmail.com'
},
token: user.tokens[0].token
})
// ensure password is hashed before storing in database
expect(user.password).not.toBe('myPass777!')
})
test('Should login existing user', async () => {
const response = await request(app).post('/users/login').send({
email: userOne.email,
password: userOne.password
}).expect(200)
// validate new json web token is saved
const user = await User.findById(userOneID)
expect(response.body.token).toBe(user.tokens[1].token)
})
test('Should not login non-existent user', async () => {
await request(app).post('/users/login').send({
email: 'jacob@fakeemail.com',
password: userOne.password
}).expect(400)
})
test('Should get profile for user', async () => {
await request(app)
.get('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
})
test('Should not login unauthenticated user', async () => {
await request(app)
.get('/users/me')
.send()
.expect(401)
})
test('Should delete account for user', async () => {
await request(app)
.delete('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send()
.expect(200)
// validate user is removed
const user = await User.findById(userOneID)
expect(user).toBeNull()
})
test('Should not delete account for an unauthenticated user', async () => {
await request(app)
.delete('/users/me')
.send()
.expect(401)
})
test('Should upload an avatar image', async () => {
await request(app)
.post('/users/me/avatar')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.attach('avatar', 'tests/fixtures/profile-pic.jpg')
.expect(200)
// check buffer stored in database
const user = await User.findById(userOneID)
expect(user.avatar).toEqual(expect.any(Buffer))
})
test('Should update valid user fields', async () => {
await request(app)
.patch('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({ name: 'Dexter' })
.expect(200)
// check change has taken effect in db
const user = await User.findById(userOneID)
expect(user.name).toBe('Dexter')
})
test('Should not update invalid user fields', async () => {
await request(app)
.patch('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({ location: 'Watford' })
.expect(400)
})
test('Should not signup user with invalid name/email/password', async () => {
await request(app)
.post('/users')
.send({
name: '',
email: 'rsheppard83@gmail.com',
password: 'myPass777!'
})
.expect(400)
await request(app)
.post('/users')
.send({
name: 'Roy',
email: 'rsheppard83atgmail.com',
password: 'myPass777!'
})
.expect(400)
await request(app)
.post('/users')
.send({
name: 'Roy',
email: 'rsheppard83@gmail.com',
password: 'myPassword'
})
.expect(400)
})
test('Should not update user if unauthenticated', async () => {
await request(app)
.patch('/users/me')
.send({ name: 'Dexter' })
.expect(401)
})
test('Should not update user with invalid name/email/password', async () => {
await request(app)
.patch('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
name: '',
email: 'rsheppard83@gmail.com',
password: 'myPass777!'
})
.expect(400)
await request(app)
.patch('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
name: 'Roy',
email: 'rsheppard83atgmail.com',
password: 'myPass777!'
})
.expect(400)
await request(app)
.patch('/users/me')
.set('Authorization', `Bearer ${userOne.tokens[0].token}`)
.send({
name: 'Roy',
email: 'rsheppard83@gmail.com',
password: 'myPassword'
})
.expect(400)
})
test('Should not delete user if unauthenticated', async () => {
await request(app)
.delete('/users/me')
.send()
.expect(401)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment