|
1 | 1 | const chai = require('chai') |
2 | 2 | const expect = chai.expect |
3 | 3 | chai.use(require('chai-http')) |
| 4 | +const sinon = require('sinon') |
| 5 | +const { v4: uuidv4 } = require('uuid') |
4 | 6 |
|
5 | 7 | const constants = require('../constants.js') |
6 | 8 | const app = require('../../../src/index.js') |
| 9 | +const logger = require('../../../src/middleware/logger') |
7 | 10 |
|
8 | 11 | const secretariatHeaders = { ...constants.headers, 'content-type': 'application/json' } |
9 | 12 |
|
| 13 | +const getLoggedPayload = (loggerInfoStub, action) => { |
| 14 | + return loggerInfoStub.getCalls() |
| 15 | + .map(call => call.args[0]) |
| 16 | + .filter(arg => typeof arg === 'string') |
| 17 | + .map((arg) => { |
| 18 | + try { |
| 19 | + return JSON.parse(arg) |
| 20 | + } catch { |
| 21 | + return null |
| 22 | + } |
| 23 | + }) |
| 24 | + .find(payload => payload?.action === action) |
| 25 | +} |
| 26 | + |
| 27 | +const postNewOrg = async (shortName) => { |
| 28 | + return chai.request(app) |
| 29 | + .post('/api/registry/org') |
| 30 | + .set(secretariatHeaders) |
| 31 | + .send({ |
| 32 | + short_name: shortName, |
| 33 | + long_name: shortName, |
| 34 | + authority: ['CNA'], |
| 35 | + id_quota: 1000 |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +const postNewUser = async (orgShortName, username) => { |
| 40 | + return chai.request(app) |
| 41 | + .post(`/api/registryUser/${orgShortName}`) |
| 42 | + .set(secretariatHeaders) |
| 43 | + .send({ |
| 44 | + username, |
| 45 | + name: { |
| 46 | + first: 'Registry', |
| 47 | + last: 'User' |
| 48 | + }, |
| 49 | + status: 'active' |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +const createRegistryUser = async () => { |
| 54 | + const orgShortName = `reguser${uuidv4().replace(/-/g, '').slice(0, 12)}` |
| 55 | + const username = `user_${uuidv4().replace(/-/g, '').slice(0, 12)}@example.com` |
| 56 | + |
| 57 | + await postNewOrg(orgShortName) |
| 58 | + .then((res) => { |
| 59 | + expect(res).to.have.status(200) |
| 60 | + }) |
| 61 | + |
| 62 | + let createdUser |
| 63 | + await postNewUser(orgShortName, username) |
| 64 | + .then((res) => { |
| 65 | + expect(res).to.have.status(200) |
| 66 | + createdUser = res.body.created |
| 67 | + }) |
| 68 | + |
| 69 | + return { orgShortName, createdUser } |
| 70 | +} |
| 71 | + |
10 | 72 | describe('Testing /registryUser endpoints', () => { |
11 | 73 | context('Positive Tests', () => { |
12 | | - // TODO |
| 74 | + it('Logs the updated user UUID when updating a registry user by identifier', async () => { |
| 75 | + const { createdUser } = await createRegistryUser() |
| 76 | + const loggerInfoStub = sinon.stub(logger, 'info') |
| 77 | + |
| 78 | + try { |
| 79 | + await chai.request(app) |
| 80 | + .put(`/api/registryUser/${createdUser.UUID}`) |
| 81 | + .set(secretariatHeaders) |
| 82 | + .send({ |
| 83 | + UUID: createdUser.UUID, |
| 84 | + username: createdUser.username, |
| 85 | + name: { |
| 86 | + first: 'Updated', |
| 87 | + last: 'User' |
| 88 | + }, |
| 89 | + status: 'active' |
| 90 | + }) |
| 91 | + .then((res) => { |
| 92 | + expect(res).to.have.status(200) |
| 93 | + expect(res.body.updated.UUID).to.equal(createdUser.UUID) |
| 94 | + }) |
| 95 | + |
| 96 | + const payload = getLoggedPayload(loggerInfoStub, 'update_registry_user') |
| 97 | + expect(payload).to.not.equal(undefined) |
| 98 | + expect(payload.user_UUID).to.equal(createdUser.UUID) |
| 99 | + } finally { |
| 100 | + loggerInfoStub.restore() |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + it('Logs the deleted user UUID when deleting a registry user by identifier', async () => { |
| 105 | + const { createdUser } = await createRegistryUser() |
| 106 | + const loggerInfoStub = sinon.stub(logger, 'info') |
| 107 | + |
| 108 | + try { |
| 109 | + await chai.request(app) |
| 110 | + .delete(`/api/registryUser/${createdUser.UUID}`) |
| 111 | + .set(secretariatHeaders) |
| 112 | + .then((res) => { |
| 113 | + expect(res).to.have.status(200) |
| 114 | + }) |
| 115 | + |
| 116 | + const payload = getLoggedPayload(loggerInfoStub, 'delete_registry_user') |
| 117 | + expect(payload).to.not.equal(undefined) |
| 118 | + expect(payload.user_UUID).to.equal(createdUser.UUID) |
| 119 | + } finally { |
| 120 | + loggerInfoStub.restore() |
| 121 | + } |
| 122 | + }) |
13 | 123 | }) |
14 | 124 | context('Negative Tests', () => { |
15 | 125 | it('Fails when page query parameter is not an integer', async () => { |
|
0 commit comments