Skip to content

Instantly share code, notes, and snippets.

@danielebarbaro
Last active July 4, 2022 10:50
Show Gist options
  • Save danielebarbaro/edb5c5f5d035a310b61bc74bbc06cd91 to your computer and use it in GitHub Desktop.
Save danielebarbaro/edb5c5f5d035a310b61bc74bbc06cd91 to your computer and use it in GitHub Desktop.
import app from './app.js';
import supertest from 'supertest';
import { faker } from '@faker-js/faker';
import mongoose from "mongoose";
it('Testing to see if Jest works', () => {
expect(true).toBe(true);
});
// it('[GET] - Call /init with success', async () => {
// await supertest(app).get('/init').expect(204);
// });
let token = '';
let firstUser = '';
const dummyUser = {
username: faker.internet.userName(),
password: faker.internet.password(10),
email: faker.internet.email(),
name: faker.name.firstName(),
surname: faker.name.lastName(),
};
beforeAll(async () => {
const response = await supertest(app)
.post('/api/login')
.send({
email: '[email protected]',
password: 'qwerty123!',
})
.expect(200);
token = response.body.token;
expect(response.body.success).toBe(true);
expect(response.body.success).not.toBeNull();
const users = await supertest(app).get('/api/users').set('Authorization', `Bearer ${token}`).expect(200);
firstUser = users.body.data.shift();
});
it('[GET] - Check the list of users /api/users with success', async () => {
await supertest(app)
.get('/api/users')
.set('Authorization', `Bearer ${token}`)
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const user = result.data.shift();
expect(user).toHaveProperty('_id');
expect(user).toHaveProperty('username');
expect(user).toHaveProperty('email');
expect(user).toHaveProperty('name');
expect(user).toHaveProperty('surname');
expect(user).toHaveProperty('author');
expect(user).toHaveProperty('active');
expect(user).toHaveProperty('createdAt');
expect(user).toHaveProperty('lastLogin');
expect(user).toHaveProperty('_v1');
expect(typeof user.active === 'boolean').toBeTruthy();
expect(typeof user.author === 'boolean').toBeTruthy();
expect(user.lastLogin).toBeNull();
expect(user).not.toHaveProperty('isAdmin');
expect(user).not.toHaveProperty('password');
});
});
it('[GET] - Fail to Call /api/users without Bearer', async () => {
await supertest(app)
.get('/api/users')
.expect(401)
.then((response) => {
const result = response.body;
expect(result.success).toBeFalsy();
expect(result.code).toBe(2001);
expect(result.error).toBe('Unauthorized');
});
});
it('[GET] - Call /api/users/:id with success', async () => {
await supertest(app)
.get(`/api/users/${firstUser._id}`)
.set('Authorization', `Bearer ${token}`)
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const user = result.data.shift();
expect(user).toHaveProperty('_id');
expect(user).toHaveProperty('username');
expect(user).toHaveProperty('email');
expect(user).toHaveProperty('name');
expect(user).toHaveProperty('surname');
expect(user).toHaveProperty('author');
expect(user).toHaveProperty('active');
expect(user).toHaveProperty('createdAt');
expect(user).toHaveProperty('lastLogin');
expect(user).toHaveProperty('_v1');
expect(typeof user.active === 'boolean').toBeTruthy();
expect(typeof user.author === 'boolean').toBeTruthy();
expect(user).not.toHaveProperty('isAdmin');
expect(user).not.toHaveProperty('password');
});
});
it('[GET] - Fail to Call /api/users/:id without Bearer', async () => {
await supertest(app)
.get('/api/users/it-does-not-matter ')
.expect(401)
.then((response) => {
const result = response.body;
expect(result.success).toBeFalsy();
expect(result.code).toBe(2001);
expect(result.error).toBe('Unauthorized');
});
});
it('[GET] - Fail to Call /api/users/:id', async () => {
await supertest(app)
.get('/api/users/it-does-not-matter-the-id')
.set('Authorization', `Bearer ${token}`)
.expect(404)
.then((response) => {
const result = response.body;
expect(result.success).toBeFalsy();
expect(result.code).toBe(4001);
expect(result.error).toBe('Resource not found');
});
});
it('[POST] - Create a new user /api/users/register with success', async () => {
await supertest(app)
.post('/api/users/register')
.set('Authorization', `Bearer ${token}`)
.send(dummyUser)
.expect(201)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data).toHaveLength(1);
expect(Array.isArray(result.data)).toBeTruthy();
const user = result.data.shift();
expect(user).toHaveProperty('_id');
expect(user).toHaveProperty('username');
expect(user).toHaveProperty('email');
expect(user).toHaveProperty('name');
expect(user).toHaveProperty('surname');
expect(user).toHaveProperty('author');
expect(user).toHaveProperty('active');
expect(user).toHaveProperty('createdAt');
expect(user).toHaveProperty('lastLogin');
expect(user).toHaveProperty('_v1');
expect(user.password).not.toBe(dummyUser.password);
expect(typeof user.active === 'boolean').toBeTruthy();
expect(user.active).toBeTruthy();
expect(typeof user.author === 'boolean').toBeTruthy();
expect(user.author).not.toBeTruthy();
expect(user.lastLogin).toBeNull();
expect(user.createdAt).not.toBeNull();
expect(user).not.toHaveProperty('isAdmin');
expect(user).not.toHaveProperty('password');
});
});
it('[GET] - Call /api/posts with success', async () => {
await supertest(app)
.get('/api/posts')
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const post = result.data.shift();
expect(post).toHaveProperty('_id');
expect(post).toHaveProperty('author');
expect(post).toHaveProperty('title');
expect(post).toHaveProperty('content');
expect(post).toHaveProperty('public');
expect(post).toHaveProperty('date');
expect(post).toHaveProperty('updated');
expect(post).toHaveProperty('_v1');
expect(post.public).toBeTruthy();
const author = post.author;
expect(author).toHaveProperty('_id');
expect(author).toHaveProperty('username');
expect(author).toHaveProperty('email');
expect(Object.keys(author)).toHaveLength(3);
});
});
it('[GET] - Call /api/posts/outdated', async () => {
await supertest(app)
.get('/api/posts/outdated')
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const post = result.data.shift();
expect(post).toHaveProperty('_id');
expect(post).toHaveProperty('author');
expect(post).toHaveProperty('title');
expect(post).toHaveProperty('content');
expect(post).toHaveProperty('public');
expect(post).toHaveProperty('date');
expect(post).toHaveProperty('updated');
expect(post).toHaveProperty('_v1');
expect(post.public).not.toBeTruthy();
const author = post.author;
expect(author).toHaveProperty('_id');
expect(author).toHaveProperty('username');
expect(author).toHaveProperty('email');
expect(Object.keys(author)).toHaveLength(3);
});
});
it('[GET] - Call /api/posts/:id', async () => {
const posts = await supertest(app).get('/api/posts').expect(200);
const firstPost = posts.body.data.shift();
await supertest(app)
.get(`/api/posts/${firstPost._id}`)
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const post = result.data.shift();
expect(post).toHaveProperty('_id');
expect(post).toHaveProperty('author');
expect(post).toHaveProperty('title');
expect(post).toHaveProperty('content');
expect(post).toHaveProperty('public');
expect(post).toHaveProperty('date');
expect(post).toHaveProperty('updated');
expect(post).toHaveProperty('_v1');
expect(post._id).toBe(firstPost._id);
const author = post.author;
expect(author).toHaveProperty('_id');
expect(author).toHaveProperty('username');
expect(author).toHaveProperty('email');
expect(Object.keys(author)).toHaveLength(3);
});
});
it('[GET] - get all post by date /api/posts?date=2021-10-01', async () => {
await supertest(app)
.get('/api/posts?date=2021-10-01')
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const post = result.data.shift();
expect(post).toHaveProperty('_id');
expect(post).toHaveProperty('author');
expect(post).toHaveProperty('title');
expect(post).toHaveProperty('content');
expect(post).toHaveProperty('public');
expect(post).toHaveProperty('date');
expect(post).toHaveProperty('updated');
expect(post).toHaveProperty('_v1');
expect(post.date).toBeGreaterThan(new Date('2021-10-01'));
const author = post.author;
expect(author).toHaveProperty('_id');
expect(author).toHaveProperty('username');
expect(author).toHaveProperty('email');
expect(Object.keys(author)).toHaveLength(3);
});
});
it('[GET] - Fail to get all post by date /api/posts?date=2023-10-01', async () => {
await supertest(app)
.get('/api/posts?date=2023-10-01')
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data).toHaveLength(0);
expect(Array.isArray(result.data)).toBeTruthy();
});
});
it('[POST] - create a new post /api/posts', async () => {
await supertest(app)
.post('/api/posts/')
.set('Authorization', `Bearer ${token}`)
.send({
user: `${firstUser.username}`,
date: '2022-05-20',
title: 'Corso Node',
content: 'Lorem ipsum',
})
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const post = result.data.shift();
expect(post).toHaveProperty('_id');
expect(post).toHaveProperty('author');
expect(post).toHaveProperty('title');
expect(post).toHaveProperty('content');
expect(post).toHaveProperty('public');
expect(post).toHaveProperty('date');
expect(post).toHaveProperty('updated');
expect(post).toHaveProperty('_v1');
const author = post.author;
expect(author).toHaveProperty('_id');
expect(author).toHaveProperty('username');
expect(author).toHaveProperty('email');
expect(author.username).toBe(firstUser.username);
expect(Object.keys(author)).toHaveLength(3);
});
});
it('[POST] - fail to create a new post /api/posts without auth', async () => {
await supertest(app)
.post('/api/posts/')
.expect(401)
.then((response) => {
const result = response.body;
expect(result.success).toBeFalsy();
expect(result.code).toBe(2001);
expect(result.error).toBe('Unauthorized');
});
});
it('[POST] - fail to create a new post with a wrong user /api/posts', async () => {
await supertest(app)
.post('/api/posts/')
.set('Authorization', `Bearer ${token}`)
.send({
user: `dummy-username-fake`,
date: '2022-05-20',
title: 'Corso Node',
content: 'Lorem ipsum',
})
.expect(404)
.then((response) => {
const result = response.body;
expect(result.data.message).toBe('User does not exist');
});
});
it('[POST] - create a new post with current user /api/posts', async () => {
const response = await supertest(app)
.post('/api/login')
.send({
email: dummyUser.email,
password: dummyUser.password,
})
.expect(200);
expect(response.success).toBeTruthy();
token = response.body.token;
await supertest(app)
.post('/api/posts/')
.set('Authorization', `Bearer ${token}`)
.send({
date: '2022-05-20',
title: 'Corso Node',
content: 'Lorem ipsum',
})
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data.length).toBeGreaterThan(1);
expect(Array.isArray(result.data)).toBeTruthy();
const post = result.data.shift();
expect(post).toHaveProperty('_id');
expect(post).toHaveProperty('author');
expect(post).toHaveProperty('title');
expect(post).toHaveProperty('content');
expect(post).toHaveProperty('public');
expect(post).toHaveProperty('date');
expect(post).toHaveProperty('updated');
expect(post).toHaveProperty('_v1');
const author = post.author;
expect(author).toHaveProperty('_id');
expect(author).toHaveProperty('username');
expect(author).toHaveProperty('email');
expect(author.username).toBe(dummyUser.username);
expect(Object.keys(author)).toHaveLength(3);
});
});
it('[PUT] - update a post /api/posts', async () => {
await supertest(app)
.put('/api/posts/:id')
.set('Authorization', `Bearer ${token}`)
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
});
});
it('[PUT] - fail to update a post /api/posts', async () => {
await supertest(app)
.put('/api/posts/what-an-id')
.expect(401)
.then((response) => {
const result = response.body;
expect(result.success).toBeFalsy();
expect(result.code).toBe(2001);
expect(result.error).toBe('Unauthorized');
});
});
it('[GET] - Call api/users/:id/password-update', async () => {
await supertest(app)
.put(`api/users/${firstUser._id}/password-update`)
.set('Authorization', `Bearer ${token}`)
.send({
current: 'password',
password: 'new-password',
passwordCheck: 'new-password',
})
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data).toBe('Password updated.');
});
});
it('[GET] - fail id api/users/:id/password-update', async () => {
await supertest(app)
.put(`api/users/${firstUser._id}/password-update`)
.set('Authorization', `Bearer ${token}`)
.send({
current: 'password',
password: 'new-password',
passwordCheck: 'new-password1233',
})
.expect(200)
.then((response) => {
const result = response.body;
expect(result.success).toBeTruthy();
expect(result.data).toBe('Password updated.');
});
});
it('[GET] - fail auth api/users/:id/password-update', async () => {
await supertest(app)
.put(`api/users/${firstUser._id}/password-update`)
.send({
current: 'password',
password: 'new-password',
passwordCheck: 'new-password',
})
.expect(401)
.then((response) => {
const result = response.body;
expect(result.success).toBeFalsy();
expect(result.code).toBe(2001);
expect(result.error).toBe('Unauthorized');
});
});
afterAll(() => mongoose.disconnect())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment