| 
 | 1 | +const should = require('should');  | 
 | 2 | +const testLib = require('./lib');  | 
 | 3 | +const request = testLib.request;  | 
 | 4 | +const {merge} = require('lodash');  | 
 | 5 | + | 
 | 6 | +const USER_1 = {  | 
 | 7 | + name: 'Dadu',  | 
 | 8 | + email: 'dadu@test.com',  | 
 | 9 | +};  | 
 | 10 | + | 
 | 11 | +const USER_2 = {  | 
 | 12 | + name: 'Marcos',  | 
 | 13 | + email: 'marcos@test.com',  | 
 | 14 | +};  | 
 | 15 | + | 
 | 16 | +describe('e2e User Auth Tests', function () {  | 
 | 17 | + let clout;  | 
 | 18 | + | 
 | 19 | + before(() => {  | 
 | 20 | + process.env.PORT = 8420;  | 
 | 21 | + process.env.NODE_ENV = 'test';  | 
 | 22 | + clout = testLib.createInstance();  | 
 | 23 | + });  | 
 | 24 | + | 
 | 25 | + it('start server', (done) => {  | 
 | 26 | + clout.start();  | 
 | 27 | + clout.on('started', () => {  | 
 | 28 | + let server = clout.server['http'];  | 
 | 29 | + if (server) {  | 
 | 30 | + let port = server.address().port;  | 
 | 31 | + serverAddress = `http://localhost:${port}`;  | 
 | 32 | + }  | 
 | 33 | + done();  | 
 | 34 | + });  | 
 | 35 | + });  | 
 | 36 | + | 
 | 37 | + describe('/user - non authenticated', () => {  | 
 | 38 | + it('should give 403 /user', async () => {  | 
 | 39 | + const response = await request({ uri: `/api/user`, json: true });  | 
 | 40 | + should(response.statusCode).be.equal(401);  | 
 | 41 | + });  | 
 | 42 | + | 
 | 43 | + it('should add 1 user to /user', async () => {  | 
 | 44 | + const response = await request({  | 
 | 45 | + method: 'put',  | 
 | 46 | + uri: `/api/user`,  | 
 | 47 | + body: USER_1,  | 
 | 48 | + json: true  | 
 | 49 | + });  | 
 | 50 | + | 
 | 51 | + should(response.body).be.deepEqual(merge({ id: 1 }, USER_1));  | 
 | 52 | + });  | 
 | 53 | + });  | 
 | 54 | + | 
 | 55 | + | 
 | 56 | + | 
 | 57 | + describe('/user - example api', () => {  | 
 | 58 | + const headers = {  | 
 | 59 | + 'X-Auth-Token': 'test-auth-token',  | 
 | 60 | + }  | 
 | 61 | + | 
 | 62 | + it('should return 1 item from /user', async () => {  | 
 | 63 | + const response = await request({ uri: `/api/user`, json: true , headers});  | 
 | 64 | +   | 
 | 65 | + should(response.statusCode).be.equal(200);  | 
 | 66 | + should(response.body.length).be.equal(1);  | 
 | 67 | + });  | 
 | 68 | + | 
 | 69 | + it('should add 1 more item to /user', async () => {  | 
 | 70 | + const response = await request({  | 
 | 71 | + method: 'put',  | 
 | 72 | + uri: `/api/user`,  | 
 | 73 | + body: USER_2,  | 
 | 74 | + json: true,  | 
 | 75 | + headers,  | 
 | 76 | + });  | 
 | 77 | + | 
 | 78 | + should(response.body).be.deepEqual(merge({ id: 2 }, USER_2));  | 
 | 79 | + });  | 
 | 80 | + | 
 | 81 | + it('should return 2 items from /user', async () => {  | 
 | 82 | + const response = await request({ uri: `/api/user`, json: true, headers });  | 
 | 83 | +   | 
 | 84 | + should(response.statusCode).be.equal(200);  | 
 | 85 | + should(response.body.length).be.equal(2);  | 
 | 86 | + });  | 
 | 87 | + | 
 | 88 | + it('should delete an item from /user/2 & /user/3', async () => {  | 
 | 89 | + await request({ uri: `/api/user/1`, method: 'delete', json: true, headers });  | 
 | 90 | + await request({ uri: `/api/user/2`, method: 'delete', json: true, headers });  | 
 | 91 | +   | 
 | 92 | + const response = await request({ uri: `/api/user`, json: true, headers });  | 
 | 93 | + | 
 | 94 | + should(response.statusCode).be.equal(200);  | 
 | 95 | + should(response.body.length).be.equal(0);  | 
 | 96 | + });  | 
 | 97 | + });  | 
 | 98 | + | 
 | 99 | + after('stop server', (done) => {  | 
 | 100 | + clout.on('stopped', () => done());  | 
 | 101 | + clout.stop();  | 
 | 102 | + });  | 
 | 103 | +});  | 
0 commit comments