|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | +const chai = require('chai') |
| 3 | +chai.use(require('chai-http')) |
| 4 | +const expect = chai.expect |
| 5 | +const _ = require('lodash') |
| 6 | + |
| 7 | +const constants = require('../constants.js') |
| 8 | +const app = require('../../../src/index.js') |
| 9 | + |
| 10 | +describe('Testing user can be created by org with /registryOrg endpoint', () => { |
| 11 | + context('Positive Tests', () => { |
| 12 | + it('Allows creation of user by org', async () => { |
| 13 | + // Create a new org |
| 14 | + await chai.request(app) |
| 15 | + .post('/api/registryOrg') |
| 16 | + .set(constants.headers) |
| 17 | + .send({ long_name: 'Test Registry Create Org', short_name: 'testOrg' }) |
| 18 | + |
| 19 | + const payload = { |
| 20 | + user_id: 'fakeregistryuser999', |
| 21 | + name: { |
| 22 | + first: 'TestFirstName', |
| 23 | + last: 'FakeLastName', |
| 24 | + middle: 'Cool', |
| 25 | + suffix: 'Mr.' |
| 26 | + }, |
| 27 | + authority: { |
| 28 | + active_roles: ['CNA'] |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Create a new user by org |
| 33 | + const res = await chai |
| 34 | + .request(app) |
| 35 | + .post('/api/registryOrg/testOrg/user') |
| 36 | + .set(constants.headers) |
| 37 | + .send(payload) |
| 38 | + |
| 39 | + expect(res).to.have.status(200) |
| 40 | + expect(res.body).to.have.property('message', `${payload.user_id} was successfully created.`) |
| 41 | + expect(res.body).to.have.property('created').that.is.an('object') |
| 42 | + |
| 43 | + const created = res.body.created |
| 44 | + expect(created).to.include({ |
| 45 | + user_id: payload.user_id |
| 46 | + }) |
| 47 | + expect(created.name).to.deep.include({ |
| 48 | + first: payload.name.first, |
| 49 | + last: payload.name.last, |
| 50 | + middle: payload.name.middle, |
| 51 | + suffix: payload.name.suffix |
| 52 | + }) |
| 53 | + |
| 54 | + expect(created).to.have.property('UUID').that.is.a('string') |
| 55 | + expect(created).to.have.property('last_active', null) |
| 56 | + expect(created).to.have.property('deactivation_date', null) |
| 57 | + expect(created).to.have.property('org_affiliations') |
| 58 | + expect(created.org_affiliations[0]).to.have.nested.property('org_id') |
| 59 | + }) |
| 60 | + }) |
| 61 | + context('Negative Tests', () => { |
| 62 | + it('Fails creation of user for nonSecretariat role', async () => { |
| 63 | + const payload = { |
| 64 | + user_id: 'fakeregistryuser999', |
| 65 | + name: { |
| 66 | + first: 'TestFirstName', |
| 67 | + last: 'FakeLastName', |
| 68 | + middle: 'Cool', |
| 69 | + suffix: 'Mr.' |
| 70 | + }, |
| 71 | + authority: { |
| 72 | + active_roles: ['CNA'] |
| 73 | + } |
| 74 | + } |
| 75 | + await chai |
| 76 | + .request(app) |
| 77 | + .post('/api/registryOrg/testOrg/user') |
| 78 | + .set(constants.nonSecretariatUserHeaders) |
| 79 | + .send(payload) |
| 80 | + .then((res, err) => { |
| 81 | + expect(res).to.have.status(403) |
| 82 | + expect(res.body.error).to.equal('SECRETARIAT_ONLY') |
| 83 | + // expect(res.body.error).to.equal('NOT_ORG_ADMIN_OR_SECRETARIAT') // Will be this error when validation is fixed |
| 84 | + }) |
| 85 | + }) |
| 86 | + it('Fails creation of user if they exist', async () => { |
| 87 | + const payload = { |
| 88 | + user_id: 'fakeregistryuser999', |
| 89 | + name: { |
| 90 | + first: 'TestFirstName', |
| 91 | + last: 'FakeLastName', |
| 92 | + middle: 'Cool', |
| 93 | + suffix: 'Mr.' |
| 94 | + }, |
| 95 | + authority: { |
| 96 | + active_roles: ['CNA'] |
| 97 | + } |
| 98 | + } |
| 99 | + await chai |
| 100 | + .request(app) |
| 101 | + .post('/api/registryOrg/testOrg/user') |
| 102 | + .set(constants.headers) |
| 103 | + .send(payload) |
| 104 | + .then((res, err) => { |
| 105 | + expect(res).to.have.status(400) |
| 106 | + expect(res.body.error).to.equal('USER_EXISTS') |
| 107 | + }) |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments