From acb41d4b6d6f17b2953b89bde4664f340f2eacb7 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Wed, 29 Jan 2020 00:50:56 -0500 Subject: [PATCH] WIP: test rework --- src/auth/loadPolicy.js | 2 +- src/auth/routes.js | 1 + src/mongoose/requestHandlers/create.js | 5 +- src/tests/integration/api.spec.js | 185 +++++++++++++------------ 4 files changed, 104 insertions(+), 89 deletions(-) diff --git a/src/auth/loadPolicy.js b/src/auth/loadPolicy.js index 44a3356c6f..718f990e1f 100644 --- a/src/auth/loadPolicy.js +++ b/src/auth/loadPolicy.js @@ -24,7 +24,7 @@ const loadPolicy = (policy) => { return next(); } - return requireAuth(req, res); + return requireAuth(req, res, next); }]; }; diff --git a/src/auth/routes.js b/src/auth/routes.js index 3df25cdc96..2e477f8834 100644 --- a/src/auth/routes.js +++ b/src/auth/routes.js @@ -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); } diff --git a/src/mongoose/requestHandlers/create.js b/src/mongoose/requestHandlers/create.js index a686b42936..383792d7e7 100644 --- a/src/mongoose/requestHandlers/create.js +++ b/src/mongoose/requestHandlers/create.js @@ -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')); }); }; diff --git a/src/tests/integration/api.spec.js b/src/tests/integration/api.spec.js index af99b5e9c7..346c91abbc 100644 --- a/src/tests/integration/api.spec.js +++ b/src/tests/integration/api.spec.js @@ -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); + // }); + // }); });