|
| 1 | +const supertest = require('supertest'); |
| 2 | +const app = require('../app'); |
| 3 | +const request = supertest(app); |
| 4 | + |
| 5 | +const { setupIntegrationDB } = require('../setup-test'); |
| 6 | +setupIntegrationDB('api-users'); |
| 7 | + |
| 8 | +const backendHeaders = process.env.CUSTOM_REQUEST_HEADER; |
| 9 | + |
| 10 | +const submittedData = { |
| 11 | + name: { |
| 12 | + firstName: 'test', |
| 13 | + lastName: 'user', |
| 14 | + }, |
| 15 | + email: 'newtest@test.com', |
| 16 | +}; |
| 17 | +var createdUserId = ''; |
| 18 | + |
| 19 | +describe('CREATE', () => { |
| 20 | + test('Create a User with POST to /api/users/', async (done) => { |
| 21 | + // Submit a User |
| 22 | + const res = await request |
| 23 | + .post('/api/users/') |
| 24 | + .set('Accept', 'application/json') |
| 25 | + .set('x-customrequired-header', backendHeaders) |
| 26 | + .send(submittedData); |
| 27 | + expect(res.status).toBe(201); |
| 28 | + expect(res.body.name).toMatchObject(submittedData.name); |
| 29 | + |
| 30 | + createdUserId = res.body._id; |
| 31 | + |
| 32 | + done(); |
| 33 | + }); |
| 34 | + |
| 35 | + test('Fail when creating a User with duplicate email', async (done) => { |
| 36 | + // Submit a User |
| 37 | + const res = await request |
| 38 | + .post('/api/users/') |
| 39 | + .set('Accept', 'application/json') |
| 40 | + .set('x-customrequired-header', backendHeaders) |
| 41 | + .send(submittedData); |
| 42 | + expect(res.status).toBe(409); |
| 43 | + expect(res.body).toMatchObject({error: { code: 11000, driver: true, name: 'MongoError', index: 0 }, message: 'User already exists'}); |
| 44 | + |
| 45 | + done(); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('READ', () => { |
| 50 | + test('Get a list of Users with with GET to /api/users/', async (done) => { |
| 51 | + // Get all Users |
| 52 | + const res = await request.get('/api/users/').set('x-customrequired-header', backendHeaders); |
| 53 | + expect(res.status).toBe(200); |
| 54 | + |
| 55 | + const APIData = res.body[0]; |
| 56 | + expect(APIData.name).toMatchObject(submittedData.name); |
| 57 | + |
| 58 | + done(); |
| 59 | + }); |
| 60 | + test('Get a specific User by param with GET to /api/users?email=<query>', async (done) => { |
| 61 | + // Get User by query of email |
| 62 | + const res = await request |
| 63 | + .get('/api/users?email=newtest@test.com') |
| 64 | + .set('x-customrequired-header', backendHeaders); |
| 65 | + expect(res.status).toBe(200); |
| 66 | + |
| 67 | + const APIData = res.body[0]; |
| 68 | + expect(APIData.name).toMatchObject(submittedData.name); |
| 69 | + |
| 70 | + done(); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Get a specific User by UserId with GET to /api/users/:UserId', async (done) => { |
| 74 | + // Get User by UserId |
| 75 | + const res = await request |
| 76 | + .get(`/api/users/${createdUserId}`) |
| 77 | + .set('x-customrequired-header', backendHeaders); |
| 78 | + expect(res.status).toBe(200); |
| 79 | + |
| 80 | + const APIData = res.body; |
| 81 | + expect(APIData.email).toBe(submittedData.email); |
| 82 | + expect(APIData.name).toMatchObject(submittedData.name); |
| 83 | + |
| 84 | + done(); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('UPDATE', () => { |
| 89 | + test('Update a User with PATCH to /api/users/:UserId', async (done) => { |
| 90 | + const updatedEmail = { |
| 91 | + email: 'newtest2@test.com', |
| 92 | + }; |
| 93 | + |
| 94 | + // Update User |
| 95 | + const resUpdate = await request |
| 96 | + .patch(`/api/users/${createdUserId}`) |
| 97 | + .set('Accept', 'application/json') |
| 98 | + .set('x-customrequired-header', backendHeaders) |
| 99 | + .send(updatedEmail); |
| 100 | + expect(resUpdate.status).toBe(200); |
| 101 | + expect(resUpdate.body.name).toMatchObject(submittedData.name); |
| 102 | + |
| 103 | + const res2 = await request |
| 104 | + .get(`/api/users/${createdUserId}`) |
| 105 | + .set('x-customrequired-header', backendHeaders); |
| 106 | + expect(res2.status).toBe(200); |
| 107 | + |
| 108 | + const APIData = res2.body; |
| 109 | + expect(APIData.email).toBe(updatedEmail.email); |
| 110 | + expect(APIData.name).toMatchObject(submittedData.name); |
| 111 | + |
| 112 | + done(); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('DELETE', () => { |
| 117 | + test('Delete a specific user by Id with DELETE /api/users/:UserId', async (done) => { |
| 118 | + // Delete User |
| 119 | + const res = await request |
| 120 | + .delete(`/api/users/${createdUserId}`) |
| 121 | + .set('x-customrequired-header', backendHeaders); |
| 122 | + expect(res.status).toBe(200); |
| 123 | + |
| 124 | + const APIData = res.body; |
| 125 | + expect(APIData.name).toMatchObject(submittedData.name); |
| 126 | + |
| 127 | + // Check to see that deleted User is gone |
| 128 | + const res2 = await request |
| 129 | + .get(`/api/users/${createdUserId}`) |
| 130 | + .set('x-customrequired-header', backendHeaders); |
| 131 | + expect(res2.body).toEqual({}); |
| 132 | + |
| 133 | + |
| 134 | + done(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments