WIP: test rework

This commit is contained in:
Elliot DeNolf
2020-01-29 00:50:56 -05:00
parent c85cc40d54
commit acb41d4b6d
4 changed files with 104 additions and 89 deletions

View File

@@ -24,7 +24,7 @@ const loadPolicy = (policy) => {
return next();
}
return requireAuth(req, res);
return requireAuth(req, res, next);
}];
};

View File

@@ -38,6 +38,7 @@ const authRoutes = (userConfig, User) => {
}, (req, res, next) => {
User.register(new User(req.body), req.body.password, (err) => {
if (err) {
console.log('err', err);
const error = new APIError('Authentication error', httpStatus.UNAUTHORIZED);
return res.status(httpStatus.UNAUTHORIZED).json(error);
}

View File

@@ -5,13 +5,14 @@ const formatSuccessResponse = require('../../responses/formatSuccess');
const create = (req, res) => {
req.model.create(req.body, (err) => {
if (err) {
console.log('create error', err);
res.status(httpStatus.INTERNAL_SERVER_ERROR)
.json(formatErrorResponse(err, 'mongoose'));
return;
}
res.status(httpStatus.CREATED)
.json(formatSuccessResponse(`${req.collection.labels.singular} successfully created.`, 'message'));
res.status(httpStatus.CREATED);
// .send(formatSuccessResponse(`${req.collection.labels.singular} successfully created.`, 'message'));
});
};

View File

@@ -7,106 +7,119 @@ import * as faker from 'faker';
describe('API', () => {
const token = null;
let email = '';
const email = 'test@test.com';
const password = 'test123';
describe('register', () => {
it('should allow create user', async () => {
email = `${faker.name.firstName()}@test.com`;
const createResponse = await axios.post('http://localhost:3000/users', {
email: `${email}`,
describe('first register', () => {
it('should allow first user to register', async () => {
const createResponse = await axios.post('http://localhost:3000/first-register', {
email,
password,
}, { headers: { 'Content-Type': 'application/json' } });
expect(createResponse.status).toBe(201);
expect(createResponse.data.result).toHaveProperty('email');
expect(createResponse.data.result).toHaveProperty('role');
expect(createResponse.data.result).toHaveProperty('createdAt');
expect(createResponse.status).toBe(200);
console.log('data', createResponse.data);
expect(createResponse.data).toHaveProperty('message');
expect(createResponse.data).toHaveProperty('token');
});
});
// TODO: No idea why this is failing
// describe('login', () => {
// it('should allow login', async () => {
// const loginResponse = await axios.post('http://localhost:3000/login', {
// email: email,
// password: password,
// describe('register', () => {
// it('should allow create user', async () => {
// email = `${faker.name.firstName()}@test.com`;
// const createResponse = await axios.post('http://localhost:3000/users', {
// email: `${email}`,
//
// password,
// }, { headers: { 'Content-Type': 'application/json' } });
// token = loginResponse.data.token;
// expect(loginResponse.status).toBe(200);
// })
// })
// expect(createResponse.status).toBe(201);
//
// expect(createResponse.data.result).toHaveProperty('email');
// expect(createResponse.data.result).toHaveProperty('role');
// expect(createResponse.data.result).toHaveProperty('createdAt');
// });
// });
describe('login', () => {
it('should allow login', async () => {
const loginResponse = await axios.post('http://localhost:3000/login', {
email,
password,
}, { headers: { 'Content-Type': 'application/json' } });
expect(loginResponse.status).toBe(200);
expect(loginResponse.data.token).not.toBeNull();
});
});
// TODO: Currently crashes from dup key error
describe('2', () => {
it('should allow create page', async () => {
const createResponse = await axios.post('http://localhost:3000/pages', {
it('should allow create post', async () => {
const createResponse = await axios.post('http://localhost:3000/posts', {
title: faker.name.firstName(),
content: faker.lorem.lines(20),
description: faker.lorem.lines(20),
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(createResponse.status).toBe(201);
expect(createResponse.data.result.title).not.toBeNull();
});
});
describe('3', () => {
it('should allow create page - locale', async () => {
const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
title: `English-${faker.name.firstName()}`,
content: faker.lorem.lines(20),
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(englishCreateResponse.status).toBe(201);
const { id } = englishCreateResponse.data.result;
const spanishCreateResponse = await axios.put(`http://localhost:3000/pages/${id}?locale=es`, {
title: `Spanish-${faker.name.firstName()}`,
content: faker.lorem.lines(20),
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(spanishCreateResponse.status).toBe(200);
});
});
describe('4', () => {
it('should allow querying of page', async () => {
const title = `English-${faker.name.firstName()}`;
const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
title,
content: faker.lorem.lines(20),
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(englishCreateResponse.status).toBe(201);
const getResponse = await axios.get(`http://localhost:3000/pages?title=${title}`,
{ headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(getResponse.data.totalDocs).toEqual(1);
expect(getResponse.data.docs[0].title).toEqual(title);
});
});
describe('5', () => {
it('should allow querying of page - locale', async () => {
const englishTitle = `English-${faker.name.firstName()}`;
const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
title: englishTitle,
content: faker.lorem.lines(20),
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(englishCreateResponse.status).toBe(201);
const { id } = englishCreateResponse.data.result;
const spanishTitle = `Spanish-${faker.name.firstName()}`;
const spanishCreateResponse = await axios.put(`http://localhost:3000/pages/${id}?locale=es`, {
title: spanishTitle,
content: faker.lorem.lines(20),
}, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(spanishCreateResponse.status).toBe(200);
const getResponse1 = await axios.get(`http://localhost:3000/pages?title=${spanishTitle}`,
{ headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(getResponse1.data.totalDocs).toEqual(0);
const getResponse2 = await axios.get(`http://localhost:3000/pages?title=${spanishTitle}&locale=es`,
{ headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
expect(getResponse2.data.totalDocs).toEqual(1);
expect(getResponse2.data.docs[0].title).toEqual(spanishTitle);
});
});
//
// describe('3', () => {
// it('should allow create page - locale', async () => {
// const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
// title: `English-${faker.name.firstName()}`,
// content: faker.lorem.lines(20),
// }, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(englishCreateResponse.status).toBe(201);
// const { id } = englishCreateResponse.data.result;
//
// const spanishCreateResponse = await axios.put(`http://localhost:3000/pages/${id}?locale=es`, {
// title: `Spanish-${faker.name.firstName()}`,
// content: faker.lorem.lines(20),
// }, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(spanishCreateResponse.status).toBe(200);
// });
// });
//
// describe('4', () => {
// it('should allow querying of page', async () => {
// const title = `English-${faker.name.firstName()}`;
// const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
// title,
// content: faker.lorem.lines(20),
// }, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(englishCreateResponse.status).toBe(201);
//
// const getResponse = await axios.get(`http://localhost:3000/pages?title=${title}`,
// { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(getResponse.data.totalDocs).toEqual(1);
// expect(getResponse.data.docs[0].title).toEqual(title);
// });
// });
//
// describe('5', () => {
// it('should allow querying of page - locale', async () => {
// const englishTitle = `English-${faker.name.firstName()}`;
// const englishCreateResponse = await axios.post('http://localhost:3000/pages', {
// title: englishTitle,
// content: faker.lorem.lines(20),
// }, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(englishCreateResponse.status).toBe(201);
// const { id } = englishCreateResponse.data.result;
//
// const spanishTitle = `Spanish-${faker.name.firstName()}`;
// const spanishCreateResponse = await axios.put(`http://localhost:3000/pages/${id}?locale=es`, {
// title: spanishTitle,
// content: faker.lorem.lines(20),
// }, { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(spanishCreateResponse.status).toBe(200);
//
// const getResponse1 = await axios.get(`http://localhost:3000/pages?title=${spanishTitle}`,
// { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(getResponse1.data.totalDocs).toEqual(0);
//
// const getResponse2 = await axios.get(`http://localhost:3000/pages?title=${spanishTitle}&locale=es`,
// { headers: { Authorization: `JWT ${token}`, 'Content-Type': 'application/json' } });
// expect(getResponse2.data.totalDocs).toEqual(1);
// expect(getResponse2.data.docs[0].title).toEqual(spanishTitle);
// });
// });
});