|
| 1 | +/* eslint no-unused-expressions: off */ |
| 2 | +/* eslint func-names: off */ |
| 3 | +/* eslint no-underscore-dangle: off */ |
| 4 | +const { Given, When, Then } = require('cucumber'); |
| 5 | +const fetch = require('node-fetch'); |
| 6 | +const { expect } = require('chai'); |
| 7 | +const waitOn = require('wait-on'); |
| 8 | + |
| 9 | +const hcBackendUrl = 'http://localhost:3030'; |
| 10 | + |
| 11 | +let currentUser; |
| 12 | +let currentUserPassword; |
| 13 | +let httpResponse; |
| 14 | +let currentUserAccessToken; |
| 15 | + |
| 16 | +function authenticate(email, plainTextPassword) { |
| 17 | + const formData = { |
| 18 | + email, |
| 19 | + password: plainTextPassword, |
| 20 | + strategy: 'local', |
| 21 | + }; |
| 22 | + return fetch(`${hcBackendUrl}/authentication`, { |
| 23 | + method: 'post', |
| 24 | + body: JSON.stringify(formData), |
| 25 | + headers: { 'Content-Type': 'application/json' }, |
| 26 | + }).then(response => response.json()) |
| 27 | + .catch((err) => { |
| 28 | + throw (err); |
| 29 | + }) |
| 30 | + .then(json => json.accessToken); |
| 31 | +} |
| 32 | + |
| 33 | +function postRequest(route, body, callback) { |
| 34 | + const params = { |
| 35 | + method: 'post', |
| 36 | + body, |
| 37 | + headers: { 'Content-Type': 'application/json' }, |
| 38 | + }; |
| 39 | + if (currentUserAccessToken) { |
| 40 | + params.headers.Authorization = `Bearer ${currentUserAccessToken}`; |
| 41 | + } |
| 42 | + fetch(`${hcBackendUrl}${route}`, params) |
| 43 | + .then(response => response.json()) |
| 44 | + .catch((err) => { |
| 45 | + throw (err); |
| 46 | + }) |
| 47 | + .then((json) => { |
| 48 | + httpResponse = json; |
| 49 | + callback(); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +Given(/^the Human Connection API is up and running(?: on "http:\/\/localhost:3030")?/, (callback) => { |
| 54 | + waitOn({ resources: ['tcp:3030'], timeout: 30000 }, (err) => { |
| 55 | + if (err) throw (err); |
| 56 | + return callback(); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +Given('there is a 3rd party application running, e.g. \'Democracy\'', () => { |
| 61 | + // Just documentation |
| 62 | +}); |
| 63 | + |
| 64 | +Given('there is a user in Human Connection with these credentials:', function (dataTable) { |
| 65 | + const params = dataTable.hashes()[0]; |
| 66 | + currentUserPassword = params.password; |
| 67 | + return this.app.service('users').create(params).then((user) => { |
| 68 | + currentUser = user; |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +Given('you are authenticated', () => authenticate(currentUser.email, currentUserPassword).then((accessToken) => { |
| 73 | + currentUserAccessToken = accessToken; |
| 74 | +})); |
| 75 | + |
| 76 | +When('you send a POST request to {string} with:', (route, body, callback) => postRequest(route, body, callback)); |
| 77 | + |
| 78 | +Then('there is an access token in the response:', (jsonResponse) => { |
| 79 | + expect(httpResponse.accessToken).to.be.a('string'); |
| 80 | + expect(httpResponse.accessToken.length).to.eq(342); |
| 81 | + const expectedAccessToken = JSON.parse(jsonResponse).accessToken; |
| 82 | + const expectedFirstPartOfJwt = expectedAccessToken.split('.')[0]; |
| 83 | + expect(httpResponse.accessToken.split('.')[0]).to.eq(expectedFirstPartOfJwt); |
| 84 | +}); |
| 85 | + |
| 86 | +Then('a new post is created', function () { |
| 87 | + return this.app.service('contributions').find({}).then((contributions) => { |
| 88 | + expect(contributions.total).to.eq(1); |
| 89 | + expect(contributions.data[0].type).to.eq('post'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | + |
| 94 | +Then('your language {string} is stored in your user settings', function (lang) { |
| 95 | + return this.app.service('usersettings').find({userId: currentUser._id.toString()}).then((settings) => { |
| 96 | + expect(settings.total).to.eq(1); |
| 97 | + expect(settings.data[0].uiLanguage).to.eq(lang); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +Then('debug', function() { |
| 102 | + // eslint-disable-next-line no-debugger |
| 103 | + debugger; |
| 104 | +}); |
| 105 | + |
| 106 | +When('you create your user settings via POST request to {string} with:', function (route, body, callback) { |
| 107 | + let jsonBody = JSON.parse(body); |
| 108 | + jsonBody.userId = currentUser._id.toString(); |
| 109 | + postRequest(route, JSON.stringify(jsonBody), callback); |
| 110 | +}); |
| 111 | + |
0 commit comments