|
| 1 | +import { |
| 2 | + request, describe, test, expect, //supertes |
| 3 | + bcrypt, v4, // utilities |
| 4 | + cluster, profileCollection, // couchbase |
| 5 | + app // REST application |
| 6 | +} from './imports' |
| 7 | + |
| 8 | +afterAll(async() => cluster.close()) |
| 9 | + |
| 10 | +describe('POST /profile', () => { |
| 11 | + |
| 12 | + const profile = { |
| 13 | + firstName: 'Joe', lastName: 'dev', |
| 14 | + email: 'joe@dev.com', pass: 'p455w3rd' |
| 15 | + } |
| 16 | + |
| 17 | + describe('given a request with user & pass', () => { |
| 18 | + const expected = { statusCode: 200, message: 'email and pass are required' } |
| 19 | + let pid |
| 20 | + |
| 21 | + console.log(`start of the test - this should be removed and is for debugging only `) |
| 22 | + |
| 23 | + test('should respond with statusCode 200 and return document persisted', async() => { |
| 24 | + const response = await request(app).post('/profile').send(profile) |
| 25 | + console.log(`log response from request ${response}`) |
| 26 | + pid = response.body.pid |
| 27 | + const hashedPass = response.body.pass |
| 28 | + |
| 29 | + delete response.body.pid; delete response.body.pass |
| 30 | + |
| 31 | + expect(response.statusCode).toBe(expected.statusCode) |
| 32 | + bcrypt.compare(profile.pass, hashedPass, function(err, result) { |
| 33 | + expect(result).toBe(true) |
| 34 | + }) |
| 35 | + expect(pid.length).toBe(36) |
| 36 | + expect(response.body).toMatchObject({ |
| 37 | + firstName: profile.firstName, lastName: profile.lastName, email: profile.email |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + afterEach(async() => { |
| 42 | + await profileCollection.remove(pid) |
| 43 | + .then(() => {/*console.log('test profile document deleted', id)*/ }) |
| 44 | + .catch((e) => console.log(`test profile remove failed: ${e.message}`)) |
| 45 | + }) |
| 46 | + |
| 47 | + }) |
| 48 | + |
| 49 | + describe('given a request without user & pass', () => { |
| 50 | + const expected = { statusCode: 400, message: 'email and pass are required' } |
| 51 | + test(`should respond with statusCode 400 and message: '${expected.message}'`, async() => { |
| 52 | + const response = await request(app).post('/profile').send({ |
| 53 | + firstName: profile.firstName, |
| 54 | + lastName: profile.lastName |
| 55 | + }) |
| 56 | + expect(response.statusCode).toBe(expected.statusCode) |
| 57 | + expect(response.body.message).toBe(expected.message) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + describe('given the email is missing', () => { |
| 62 | + const expected = { statusCode: 400, message: 'email is required' } |
| 63 | + test(`should respond with statusCode 400 and message: ${expected.message}`, async() => { |
| 64 | + const response = await request(app).post('/profile').send({ |
| 65 | + firstName: profile.firstName, |
| 66 | + lastName: profile.lastName, |
| 67 | + pass: profile.pass |
| 68 | + }) |
| 69 | + expect(response.statusCode).toBe(expected.statusCode) |
| 70 | + expect(response.body.message).toBe(expected.message) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + describe('given the pass is missing', () => { |
| 75 | + const expected = { statusCode: 400, message: 'pass is required' } |
| 76 | + test(`should respond with statusCode 400 and message: '${expected.message}'`, async() => { |
| 77 | + const response = await request(app).post('/profile').send({ |
| 78 | + firstName: profile.firstName, |
| 79 | + lastName: profile.lastName, |
| 80 | + email: profile.email |
| 81 | + }) |
| 82 | + expect(response.statusCode).toBe(expected.statusCode) |
| 83 | + expect(response.body.message).toBe(expected.message) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | +}) |
0 commit comments