|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | import test from 'ava'; |
4 | | -import app from '../../../config/lib/app'; |
5 | | -import supertest from 'supertest'; |
| 4 | +import request from 'request'; |
6 | 5 |
|
7 | 6 | /** |
8 | 7 | * Pre-condition for this test is to run on a clean database setup with no records existing |
9 | 8 | */ |
10 | 9 |
|
11 | | -// Bootstrap the application components (db, orm and express app) |
12 | | -// Make the components available to tests through the shared object `t.context` |
13 | | -var appComponents; |
14 | | -function bootstrapTest() { |
15 | | - return app.bootstrap(); |
16 | | -} |
17 | 10 |
|
18 | | -// Before all tests would run we will bootstrap the ExpressJS app server |
19 | | -// and it's related components (Mongoose DB and Sequelize ORM) |
20 | | -test.before('Bootstrapping App for Tasks Routes test', async t => { |
| 11 | +// Before each test we setup a request object with defaults |
| 12 | +// Making the request object available to tests through the shared object `t.context` |
| 13 | +test.beforeEach('Setting up test defaults', t => { |
| 14 | + const requestObject = request.defaults({ |
21 | 15 |
|
22 | | - appComponents = await bootstrapTest(); |
| 16 | + // Set the Base URL for all API requests |
| 17 | + baseUrl: 'http://localhost:3001', |
| 18 | + // Set data send/received to be JSON compatible |
| 19 | + json: true, |
| 20 | + // Set the cookie jar option to true which persists cookies |
| 21 | + // between API requests made, which enables us to perform |
| 22 | + // login and further API calls as a logged-in user. |
| 23 | + jar: true |
| 24 | + }); |
23 | 25 |
|
24 | | - t.truthy(appComponents.db, 'bootstrapped with db property'); |
25 | | - t.truthy(appComponents.orm, 'bootstrapped with orm property'); |
26 | | - t.truthy(appComponents.app, 'bootstrapped with app property'); |
| 26 | + t.context.request = requestObject; |
27 | 27 | }); |
28 | 28 |
|
29 | | -test('API: Get All Tasks for anonymous user', async t => { |
30 | | - let response = await supertest(appComponents.app) |
31 | | - .get('/api/tasks') |
32 | | - .expect(200); |
33 | | - |
34 | | - t.is(200, response.statusCode, 'successful'); |
35 | | - t.true(response.body.length === 0, 'returns empty results'); |
36 | | - |
| 29 | +test('API: Get All Tasks as anonymous user', async t => { |
| 30 | + let response = await new Promise(function (resolve, reject) { |
| 31 | + t.context.request.get({ |
| 32 | + uri: '/api/tasks' |
| 33 | + }, function (error, response, body) { |
| 34 | + if (error) { |
| 35 | + reject(error); |
| 36 | + } |
| 37 | + resolve(response); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + t.is(response.statusCode, 200, response.body.toString()); |
| 42 | + t.true(response.body.length === 0, response.body.toString()); |
37 | 43 | }); |
38 | 44 |
|
39 | | -test('API: Get My Tasks for anonymous user', async t => { |
40 | | - let response = await supertest(appComponents.app) |
41 | | - .get('/api/tasks/me') |
42 | | - .expect(401); |
43 | | - |
44 | | - t.is(401, response.statusCode, 'successful'); |
45 | | - t.true(response.body.message == 'No session user', 'No session user'); |
46 | | - |
| 45 | +test('API: Get My Tasks as anonymous user', async t => { |
| 46 | + let response = await new Promise(function (resolve, reject) { |
| 47 | + t.context.request.get({ |
| 48 | + uri: '/api/tasks/me' |
| 49 | + }, function (error, response, body) { |
| 50 | + if (error) { |
| 51 | + reject(error); |
| 52 | + } |
| 53 | + resolve(response); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + t.is(response.statusCode, 401, response.body.toString()); |
| 58 | + t.true(response.body.message === 'No session user', response.body.toString()); |
47 | 59 | }); |
48 | 60 |
|
49 | | -test('API: Create Task with anonymous user', async t => { |
50 | | - let response = await supertest(appComponents.app) |
51 | | - .post('/api/tasks') |
52 | | - .expect(401); |
53 | | - |
54 | | - t.is(401, response.statusCode, 'successful'); |
55 | | - t.true(response.body.message == 'No session user', 'No session user'); |
56 | | - |
| 61 | +test('API: Create Task as anonymous user', async t => { |
| 62 | + let response = await new Promise(function (resolve, reject) { |
| 63 | + t.context.request.post({ |
| 64 | + uri: '/api/tasks', |
| 65 | + body: { |
| 66 | + title: 'my test task' |
| 67 | + } |
| 68 | + }, function (error, response, body) { |
| 69 | + if (error) { |
| 70 | + reject(error); |
| 71 | + } |
| 72 | + resolve(response); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + t.is(response.statusCode, 401, response.body.toString()); |
| 77 | + t.true(response.body.message === 'No session user', response.body.toString()); |
57 | 78 | }); |
58 | 79 |
|
59 | | -test('API: Update Task with anonymous user', async t => { |
60 | | - let response = await supertest(appComponents.app) |
61 | | - .put('/api/tasks') |
62 | | - .expect(401); |
63 | | - |
64 | | - t.is(401, response.statusCode, 'successful'); |
65 | | - t.true(response.body.message == 'No session user', 'No session user'); |
66 | | - |
| 80 | +test('API: Update Task as anonymous user', async t => { |
| 81 | + let response = await new Promise(function (resolve, reject) { |
| 82 | + t.context.request.put({ |
| 83 | + uri: '/api/tasks', |
| 84 | + body: { |
| 85 | + title: 'my test task' |
| 86 | + } |
| 87 | + }, function (error, response, body) { |
| 88 | + if (error) { |
| 89 | + reject(error); |
| 90 | + } |
| 91 | + resolve(response); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + t.is(response.statusCode, 401, response.body.toString()); |
| 96 | + t.true(response.body.message === 'No session user', response.body.toString()); |
67 | 97 | }); |
68 | 98 |
|
69 | | -test('API: Delete Task with anonymous user', async t => { |
70 | | - let response = await supertest(appComponents.app) |
71 | | - .delete('/api/tasks') |
72 | | - .expect(401); |
73 | | - |
74 | | - t.is(401, response.statusCode, 'successful'); |
75 | | - t.true(response.body.message == 'No session user', 'No session user'); |
76 | | - |
| 99 | +test('API: Delete Task as anonymous user', async t => { |
| 100 | + let response = await new Promise(function (resolve, reject) { |
| 101 | + t.context.request.delete({ |
| 102 | + uri: '/api/tasks' |
| 103 | + }, function (error, response, body) { |
| 104 | + if (error) { |
| 105 | + reject(error); |
| 106 | + } |
| 107 | + resolve(response); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + t.is(response.statusCode, 401, response.body.toString()); |
| 112 | + t.true(response.body.message === 'No session user', response.body.toString()); |
77 | 113 | }); |
0 commit comments