|
| 1 | +const chai = require('chai') |
| 2 | +const { expect } = require('chai') |
| 3 | +const chaiHttp = require('chai-http') |
| 4 | + |
| 5 | +const app = require('../../server') |
| 6 | +const authService = require('../../services/authService') |
| 7 | +const addUser = require('../utils/addUser') |
| 8 | +const cleanDb = require('../utils/cleanDb') |
| 9 | +const featureFlagQuery = require('../../models/featureFlags') |
| 10 | + |
| 11 | +// Import fixtures |
| 12 | +const userData = require('../fixtures/user/user')() |
| 13 | +const featureFlagData = require('../fixtures/featureFlag/featureFlag')() |
| 14 | + |
| 15 | +const config = require('config') |
| 16 | +const cookieName = config.get('userToken.cookieName') |
| 17 | + |
| 18 | +const appOwner = userData[5] |
| 19 | +chai.use(chaiHttp) |
| 20 | + |
| 21 | +describe('FeatureFlag', function () { |
| 22 | + let jwt |
| 23 | + let featureFlag |
| 24 | + beforeEach(async function () { |
| 25 | + const userId = await addUser(appOwner) |
| 26 | + jwt = authService.generateAuthToken({ userId }) |
| 27 | + featureFlag = await featureFlagQuery.addFeatureFlags(featureFlagData[0], appOwner.username) |
| 28 | + }) |
| 29 | + |
| 30 | + afterEach(async function () { |
| 31 | + await cleanDb() |
| 32 | + }) |
| 33 | + |
| 34 | + describe('GET /featureFlags', function () { |
| 35 | + it('Should return all feature flags', function (done) { |
| 36 | + chai |
| 37 | + .request(app) |
| 38 | + .get('/featureFlags') |
| 39 | + .end((res, err) => { |
| 40 | + if (err) { return done() } |
| 41 | + |
| 42 | + expect(res).to.have.status(200) |
| 43 | + expect(res.body).to.be.a('object') |
| 44 | + expect(res.body.message).to.equal('FeatureFlags returned successfully!') |
| 45 | + expect(res.body.featureFlags).to.be.a('array') |
| 46 | + |
| 47 | + return done() |
| 48 | + }) |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + describe('POST /featureFlags', function () { |
| 53 | + it('Should add the feature flag in db', function (done) { |
| 54 | + chai |
| 55 | + .request(app) |
| 56 | + .post('/featureFlags') |
| 57 | + .set('cookie', `${cookieName} = ${jwt}`) |
| 58 | + .send({ |
| 59 | + name: 'test', |
| 60 | + title: 'test-feature', |
| 61 | + config: { |
| 62 | + enabled: true |
| 63 | + } |
| 64 | + }, appOwner.username) |
| 65 | + .end((err, res) => { |
| 66 | + if (err) { return done() } |
| 67 | + expect(res).to.have.status(200) |
| 68 | + expect(res.body).to.be.a('object') |
| 69 | + const { data } = res.body |
| 70 | + expect(data).to.be.a('object') |
| 71 | + expect(data.name).to.be.a('string') |
| 72 | + expect(data.title).to.be.a('string') |
| 73 | + expect(data.created_at).to.be.a('number') |
| 74 | + expect(data.updated_at).to.be.a('number') |
| 75 | + expect(data.config).to.be.a('object') |
| 76 | + expect(data.config.enabled).to.be.a('boolean') |
| 77 | + expect(data.owner).to.be.a('string') |
| 78 | + expect(res.body.message).to.equal('FeatureFlag added successfully!') |
| 79 | + |
| 80 | + return done() |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('Should return 401 if user not logged in', function (done) { |
| 85 | + chai |
| 86 | + .request(app) |
| 87 | + .post('/featureFlags') |
| 88 | + .end((res, err) => { |
| 89 | + if (err) { return done() } |
| 90 | + |
| 91 | + expect(res).to.have.status(401) |
| 92 | + expect(res.body).to.be.an('object') |
| 93 | + expect(res.body).to.eql({ |
| 94 | + statusCode: 401, |
| 95 | + error: 'Unauthorized', |
| 96 | + message: 'Unauthenticated User' |
| 97 | + }) |
| 98 | + |
| 99 | + return done() |
| 100 | + }) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + describe('PATCH /featureFlags/:id', function () { |
| 105 | + it('Should update the feature flag', function (done) { |
| 106 | + chai |
| 107 | + .request(app) |
| 108 | + .patch(`/featureFlags/${featureFlag.id}`) |
| 109 | + .set('cookie', `${cookieName}=${jwt}`) |
| 110 | + .send({ |
| 111 | + config: { |
| 112 | + enabled: false |
| 113 | + } |
| 114 | + }) |
| 115 | + .end((err, res) => { |
| 116 | + if (err) { return done() } |
| 117 | + expect(res).to.have.status(204) |
| 118 | + |
| 119 | + return done() |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + it('Should return 401 if user not logged in', function (done) { |
| 124 | + chai |
| 125 | + .request(app) |
| 126 | + .patch(`/featureFlags/${featureFlag.id}`) |
| 127 | + .end((res, err) => { |
| 128 | + if (err) { return done() } |
| 129 | + |
| 130 | + expect(res).to.have.status(401) |
| 131 | + expect(res.body).to.be.an('object') |
| 132 | + expect(res.body).to.eql({ |
| 133 | + statusCode: 401, |
| 134 | + error: 'Unauthorized', |
| 135 | + message: 'Unauthenticated User' |
| 136 | + }) |
| 137 | + |
| 138 | + return done() |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('Should return 404 if feature flag does not exist', function (done) { |
| 143 | + chai |
| 144 | + .request(app) |
| 145 | + .patch('/featureFlags/featureFlagId') |
| 146 | + .end((res, err) => { |
| 147 | + if (err) { return done() } |
| 148 | + |
| 149 | + expect(res).to.have.status(404) |
| 150 | + expect(res.body).to.be.an('object') |
| 151 | + expect(res.body).to.eql({ |
| 152 | + statusCode: 404, |
| 153 | + error: 'Not Found', |
| 154 | + message: 'No featureFlag found' |
| 155 | + }) |
| 156 | + |
| 157 | + return done() |
| 158 | + }) |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + describe('DELETE /featureFlags/:id', function () { |
| 163 | + it('Should delete the feature flag', function (done) { |
| 164 | + chai |
| 165 | + .request(app) |
| 166 | + .delete(`/featureFlags/${featureFlag.id}`) |
| 167 | + .set('cookie', `${cookieName}=${jwt}`) |
| 168 | + .end((err, res) => { |
| 169 | + if (err) { return done() } |
| 170 | + |
| 171 | + expect(res).to.have.status(200) |
| 172 | + |
| 173 | + return done() |
| 174 | + }) |
| 175 | + }) |
| 176 | + |
| 177 | + it('Should return 401 if user not logged in', function (done) { |
| 178 | + chai |
| 179 | + .request(app) |
| 180 | + .delete(`/featureFlags/${featureFlag.id}`) |
| 181 | + .end((res, err) => { |
| 182 | + if (err) { return done() } |
| 183 | + |
| 184 | + expect(res).to.have.status(401) |
| 185 | + expect(res.body).to.be.an('object') |
| 186 | + expect(res.body).to.eql({ |
| 187 | + statusCode: 401, |
| 188 | + error: 'Unauthorized', |
| 189 | + message: 'Unauthenticated User' |
| 190 | + }) |
| 191 | + |
| 192 | + return done() |
| 193 | + }) |
| 194 | + }) |
| 195 | + |
| 196 | + it('Should return 404 if feature flag does not exist', function (done) { |
| 197 | + chai |
| 198 | + .request(app) |
| 199 | + .delete('/featureFlags/featureFlagId') |
| 200 | + .end((res, err) => { |
| 201 | + if (err) { return done() } |
| 202 | + |
| 203 | + expect(res).to.have.status(404) |
| 204 | + expect(res.body).to.be.an('object') |
| 205 | + expect(res.body).to.eql({ |
| 206 | + statusCode: 404, |
| 207 | + error: 'Not Found', |
| 208 | + message: 'No feature flag found' |
| 209 | + }) |
| 210 | + |
| 211 | + return done() |
| 212 | + }) |
| 213 | + }) |
| 214 | + }) |
| 215 | +}) |
0 commit comments