|
1 | | -const express = require('express') |
2 | | -const app = express() |
| 1 | +const sinon = require('sinon') |
3 | 2 | const chai = require('chai') |
4 | 3 | const expect = chai.expect |
5 | | -chai.use(require('chai-http')) |
6 | | - |
7 | | -// Body Parser Middleware |
8 | | -app.use(express.json()) // Allows us to handle raw JSON data |
9 | | -app.use(express.urlencoded({ extended: false })) // Allows us to handle url encoded data |
10 | | -const middleware = require('../../../src/middleware/middleware') |
11 | | -app.use(middleware.createCtxAndReqUUID) |
12 | | - |
13 | | -const errors = require('../../../src/controller/org.controller/error') |
14 | | -const error = new errors.OrgControllerError() |
15 | | - |
16 | | -const orgFixtures = require('./mockObjects.org') |
17 | | -const orgController = require('../../../src/controller/org.controller/org.controller') |
18 | | -const orgParams = require('../../../src/controller/org.controller/org.middleware') |
19 | | - |
20 | | -class OrgGet { |
21 | | - async aggregate (aggregation) { |
22 | | - if (aggregation[0].$match.short_name === orgFixtures.existentOrg.short_name) { |
23 | | - return [orgFixtures.existentOrg] |
24 | | - } else if (aggregation[0].$match.short_name === orgFixtures.owningOrg.short_name) { |
25 | | - return [orgFixtures.owningOrg] |
26 | | - } else if (aggregation[0].$match.UUID === orgFixtures.owningOrg.UUID) { |
27 | | - return [orgFixtures.owningOrg] |
28 | | - } |
29 | | - |
30 | | - return [] |
| 4 | +const { faker } = require('@faker-js/faker') |
| 5 | +const mongoose = require('mongoose') |
| 6 | + |
| 7 | +const { ORG_SINGLE } = require('../../../src/controller/org.controller/org.controller') |
| 8 | +const BaseOrgRepository = require('../../../src/repositories/baseOrgRepository.js') |
| 9 | +const BaseOrg = require('../../../src/model/baseorg.js') |
| 10 | + |
| 11 | +const { OrgControllerError } = require('../../../src/controller/org.controller/error.js') |
| 12 | +const error = new OrgControllerError() |
| 13 | + |
| 14 | +// Test Fixtures |
| 15 | +const orgFixtures = { |
| 16 | + secretariatOrg: { |
| 17 | + UUID: '1633f81e-9202-4688-929a-6a549554a8e2', |
| 18 | + short_name: 'mitre', |
| 19 | + name: 'The MITRE Corporation', |
| 20 | + authority: { active_roles: ['CNA', 'SECRETARIAT'] }, |
| 21 | + policies: { id_quota: 1000 } |
| 22 | + }, |
| 23 | + regularOrg: { |
| 24 | + UUID: '2744f81e-9202-4688-929a-6a549554a8e3', |
| 25 | + short_name: 'cisco', |
| 26 | + name: 'Cisco Systems', |
| 27 | + authority: { active_roles: ['CNA'] }, |
| 28 | + policies: { id_quota: 500 } |
| 29 | + }, |
| 30 | + targetOrg: { |
| 31 | + UUID: '3855f81e-9202-4688-929a-6a549554a8e4', |
| 32 | + short_name: 'targetorg', |
| 33 | + name: 'Target Organization', |
| 34 | + authority: { active_roles: ['CNA'] }, |
| 35 | + policies: { id_quota: 300 } |
31 | 36 | } |
| 37 | +} |
32 | 38 |
|
33 | | - async isSecretariat (shortname) { |
34 | | - if (shortname === orgFixtures.secretariatHeader['CVE-API-ORG']) { |
35 | | - return true |
36 | | - } else { |
37 | | - return false |
| 39 | +const fakeSecretariatOrgDocument = new BaseOrg(orgFixtures.secretariatOrg) |
| 40 | +const fakeRegularOrgDocument = new BaseOrg(orgFixtures.regularOrg) |
| 41 | +const fakeTargetOrgDocument = new BaseOrg(orgFixtures.targetOrg) |
| 42 | + |
| 43 | +describe('Testing the GET /org/:identifier endpoint in Org Controller', () => { |
| 44 | + let status, json, res, next, mockSession, baseOrgRepo, getBaseOrgRepository, req |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + status = sinon.stub() |
| 48 | + json = sinon.spy() |
| 49 | + res = { json, status } |
| 50 | + next = sinon.spy() |
| 51 | + status.returns(res) |
| 52 | + |
| 53 | + // Stub Mongoose session methods |
| 54 | + mockSession = { |
| 55 | + startTransaction: sinon.stub(), |
| 56 | + commitTransaction: sinon.stub().resolves(), |
| 57 | + abortTransaction: sinon.stub().resolves(), |
| 58 | + endSession: sinon.stub().resolves() |
38 | 59 | } |
39 | | - } |
| 60 | + sinon.stub(mongoose, 'startSession').resolves(mockSession) |
| 61 | + |
| 62 | + baseOrgRepo = new BaseOrgRepository() |
| 63 | + getBaseOrgRepository = sinon.stub().returns(baseOrgRepo) |
| 64 | + |
| 65 | + req = { |
| 66 | + ctx: { |
| 67 | + org: orgFixtures.secretariatOrg.short_name, |
| 68 | + uuid: faker.datatype.uuid(), |
| 69 | + params: { |
| 70 | + identifier: orgFixtures.targetOrg.short_name |
| 71 | + }, |
| 72 | + repositories: { |
| 73 | + getBaseOrgRepository |
| 74 | + } |
| 75 | + }, |
| 76 | + useRegistry: false |
| 77 | + } |
| 78 | + }) |
40 | 79 |
|
41 | | - async findOneByShortName (shortname) { |
42 | | - return orgFixtures.owningOrg |
43 | | - } |
44 | | -} |
| 80 | + afterEach(() => { |
| 81 | + sinon.restore() |
| 82 | + }) |
45 | 83 |
|
46 | | -describe.skip('Testing the GET /org/:identifier endpoint in Org Controller', () => { |
47 | 84 | context('Negative Tests', () => { |
48 | | - it('Org does not exists', async () => { |
49 | | - app.route('/org-cant-get-doesnt-exist/:identifier') |
50 | | - .get((req, res, next) => { |
51 | | - const factory = { |
52 | | - getOrgRepository: () => { return new OrgGet() } |
53 | | - } |
54 | | - req.ctx.repositories = factory |
55 | | - next() |
56 | | - }, orgParams.parseGetParams, orgController.ORG_SINGLE) |
57 | | - |
58 | | - const res = await chai.request(app) |
59 | | - .get(`/org-cant-get-doesnt-exist/${orgFixtures.nonExistentOrg.short_name}`) |
60 | | - .set(orgFixtures.secretariatHeader) |
61 | | - |
62 | | - expect(res).to.have.status(404) |
63 | | - expect(res).to.have.property('body').and.to.be.a('object') |
64 | | - const errObj = error.orgDne(orgFixtures.nonExistentOrg.short_name, 'identifier', 'path') |
65 | | - expect(res.body.error).to.equal(errObj.error) |
66 | | - expect(res.body.message).to.equal(errObj.message) |
| 85 | + it('Org does not exist', async () => { |
| 86 | + req.ctx.params.identifier = 'nonexistent-org' |
| 87 | + sinon.stub(baseOrgRepo, 'findOneByShortName').resolves(fakeSecretariatOrgDocument) |
| 88 | + sinon.stub(baseOrgRepo, 'isSecretariat').resolves(true) |
| 89 | + sinon.stub(baseOrgRepo, 'getOrg').resolves(null) |
| 90 | + |
| 91 | + await ORG_SINGLE(req, res, next) |
| 92 | + |
| 93 | + const errObj = error.orgDne('nonexistent-org', 'identifier', 'path') |
| 94 | + expect(status.args[0][0]).to.equal(404) |
| 95 | + expect(res.json.args[0][0].error).to.equal(errObj.error) |
| 96 | + expect(res.json.args[0][0].message).to.equal(errObj.message) |
67 | 97 | }) |
68 | 98 |
|
69 | | - it('Org exists and requester is not a user of the same org or is secretariat', (done) => { |
70 | | - app.route('/org-cant-get-user-not-secretariat-or-same-org/:identifier') |
71 | | - .get((req, res, next) => { |
72 | | - const factory = { |
73 | | - getOrgRepository: () => { return new OrgGet() } |
74 | | - } |
75 | | - req.ctx.repositories = factory |
76 | | - next() |
77 | | - }, orgParams.parseGetParams, orgController.ORG_SINGLE) |
78 | | - |
79 | | - chai.request(app) |
80 | | - .get(`/org-cant-get-user-not-secretariat-or-same-org/${orgFixtures.owningOrg.short_name}`) |
81 | | - .set(orgFixtures.orgHeader) |
82 | | - .end((err, res) => { |
83 | | - if (err) { |
84 | | - done(err) |
85 | | - } |
86 | | - |
87 | | - expect(res).to.have.status(403) |
88 | | - expect(res).to.have.property('body').and.to.be.a('object') |
89 | | - const errObj = error.notSameOrgOrSecretariat() |
90 | | - expect(res.body.error).to.equal(errObj.error) |
91 | | - expect(res.body.message).to.equal(errObj.message) |
92 | | - done() |
93 | | - }) |
| 99 | + it('Org exists but requester is not same org or secretariat', async () => { |
| 100 | + req.ctx.org = orgFixtures.regularOrg.short_name // Regular org |
| 101 | + req.ctx.params.identifier = orgFixtures.targetOrg.short_name |
| 102 | + sinon.stub(baseOrgRepo, 'findOneByShortName').resolves(fakeRegularOrgDocument) |
| 103 | + sinon.stub(baseOrgRepo, 'isSecretariat').resolves(false) |
| 104 | + |
| 105 | + await ORG_SINGLE(req, res, next) |
| 106 | + |
| 107 | + const errObj = error.notSameOrgOrSecretariat() |
| 108 | + expect(status.args[0][0]).to.equal(403) |
| 109 | + expect(res.json.args[0][0].error).to.equal(errObj.error) |
| 110 | + expect(res.json.args[0][0].message).to.equal(errObj.message) |
94 | 111 | }) |
95 | 112 |
|
96 | | - it('Invalid UUID requesting an org', (done) => { |
97 | | - app.route('/org-get-org-by-invalid-uuid/:identifier') |
98 | | - .get((req, res, next) => { |
99 | | - const factory = { |
100 | | - getOrgRepository: () => { return new OrgGet() } |
101 | | - } |
102 | | - req.ctx.repositories = factory |
103 | | - next() |
104 | | - }, orgParams.parseGetParams, orgController.ORG_SINGLE) |
105 | | - |
106 | | - chai.request(app) |
107 | | - .get(`/org-get-org-by-uuid/${'nonexistent123'}`) |
108 | | - .set(orgFixtures.owningOrgHeader) |
109 | | - .end((err, res) => { |
110 | | - if (err) { |
111 | | - done(err) |
112 | | - } |
113 | | - expect(res).to.have.status(404) |
114 | | - expect(res).to.have.property('body').and.to.be.a('object') |
115 | | - done() |
116 | | - }) |
| 113 | + it('Invalid UUID requesting an org', async () => { |
| 114 | + // UUID format is invalid and will cause the controller to search by short name |
| 115 | + req.ctx.params.identifier = 'invalid-uuid-123' |
| 116 | + sinon.stub(baseOrgRepo, 'findOneByShortName').resolves(fakeSecretariatOrgDocument) |
| 117 | + sinon.stub(baseOrgRepo, 'isSecretariat').resolves(true) |
| 118 | + sinon.stub(baseOrgRepo, 'getOrg').resolves(null) |
| 119 | + |
| 120 | + await ORG_SINGLE(req, res, next) |
| 121 | + |
| 122 | + expect(status.args[0][0]).to.equal(404) |
| 123 | + expect(res.json.args[0][0]).to.have.property('error') |
117 | 124 | }) |
118 | 125 | }) |
119 | 126 |
|
120 | 127 | context('Positive Tests', () => { |
121 | | - it('Org exists and requester is secretariat', (done) => { |
122 | | - app.route('/org-get-does-exist/:identifier') |
123 | | - .get((req, res, next) => { |
124 | | - const factory = { |
125 | | - getOrgRepository: () => { return new OrgGet() } |
126 | | - } |
127 | | - req.ctx.repositories = factory |
128 | | - next() |
129 | | - }, orgParams.parseGetParams, orgController.ORG_SINGLE) |
130 | | - |
131 | | - chai.request(app) |
132 | | - .get(`/org-get-does-exist/${orgFixtures.existentOrg.short_name}`) |
133 | | - .set(orgFixtures.secretariatHeader) |
134 | | - .end((err, res) => { |
135 | | - if (err) { |
136 | | - done(err) |
137 | | - } |
138 | | - |
139 | | - expect(res).to.have.status(200) |
140 | | - expect(res).to.have.property('body').and.to.be.a('object') |
141 | | - expect(res.body).to.have.property('short_name').and.to.equal(orgFixtures.existentOrg.short_name) |
142 | | - done() |
143 | | - }) |
| 128 | + it('Secretariat can access any org by shortname', async () => { |
| 129 | + // Org exists and requester is secretariat |
| 130 | + sinon.stub(baseOrgRepo, 'findOneByShortName').resolves(fakeSecretariatOrgDocument) |
| 131 | + sinon.stub(baseOrgRepo, 'isSecretariat').resolves(true) |
| 132 | + sinon.stub(baseOrgRepo, 'getOrg').resolves(orgFixtures.targetOrg) |
| 133 | + |
| 134 | + await ORG_SINGLE(req, res, next) |
| 135 | + |
| 136 | + expect(status.args[0][0]).to.equal(200) |
| 137 | + expect(res.json.args[0][0]).to.deep.equal(orgFixtures.targetOrg) |
144 | 138 | }) |
145 | 139 |
|
146 | | - it('Org exists and requester is a user of the same org', (done) => { |
147 | | - app.route('/org-get-does-exist-user-same-org/:identifier') |
148 | | - .get((req, res, next) => { |
149 | | - const factory = { |
150 | | - getOrgRepository: () => { return new OrgGet() } |
151 | | - } |
152 | | - req.ctx.repositories = factory |
153 | | - next() |
154 | | - }, orgParams.parseGetParams, orgController.ORG_SINGLE) |
155 | | - |
156 | | - chai.request(app) |
157 | | - .get(`/org-get-does-exist-user-same-org/${orgFixtures.owningOrg.short_name}`) |
158 | | - .set(orgFixtures.owningOrgHeader) |
159 | | - .end((err, res) => { |
160 | | - if (err) { |
161 | | - done(err) |
162 | | - } |
163 | | - |
164 | | - expect(res).to.have.status(200) |
165 | | - expect(res).to.have.property('body').and.to.be.a('object') |
166 | | - expect(res.body).to.have.property('short_name').and.to.equal(orgFixtures.owningOrg.short_name) |
167 | | - done() |
168 | | - }) |
| 140 | + it('Non-secretariat can access same org by shortname', async () => { |
| 141 | + // Org exists and requester is a user of the same org |
| 142 | + req.ctx.org = orgFixtures.targetOrg.short_name |
| 143 | + req.ctx.params.identifier = orgFixtures.targetOrg.short_name |
| 144 | + sinon.stub(baseOrgRepo, 'findOneByShortName').resolves(fakeTargetOrgDocument) |
| 145 | + sinon.stub(baseOrgRepo, 'isSecretariat').resolves(false) |
| 146 | + sinon.stub(baseOrgRepo, 'getOrg').resolves(orgFixtures.targetOrg) |
| 147 | + |
| 148 | + await ORG_SINGLE(req, res, next) |
| 149 | + |
| 150 | + expect(status.args[0][0]).to.equal(200) |
| 151 | + expect(res.json.args[0][0]).to.deep.equal(orgFixtures.targetOrg) |
169 | 152 | }) |
170 | 153 |
|
171 | | - it('Valid UUID requesting an org', (done) => { |
172 | | - app.route('/org-get-org-by-uuid/:identifier') |
173 | | - .get((req, res, next) => { |
174 | | - const factory = { |
175 | | - getOrgRepository: () => { return new OrgGet() } |
176 | | - } |
177 | | - req.ctx.repositories = factory |
178 | | - next() |
179 | | - }, orgParams.parseGetParams, orgController.ORG_SINGLE) |
180 | | - |
181 | | - chai.request(app) |
182 | | - .get(`/org-get-org-by-uuid/${orgFixtures.owningOrg.UUID}`) |
183 | | - .set(orgFixtures.owningOrgHeader) |
184 | | - .end((err, res) => { |
185 | | - if (err) { |
186 | | - done(err) |
187 | | - } |
188 | | - |
189 | | - expect(res).to.have.status(200) |
190 | | - expect(res).to.have.property('body').and.to.be.a('object') |
191 | | - expect(res.body).to.have.property('UUID').and.to.equal(orgFixtures.owningOrg.UUID) |
192 | | - done() |
193 | | - }) |
| 154 | + it('Non-secretariat can access same org by UUID', async () => { |
| 155 | + req.ctx.org = orgFixtures.targetOrg.short_name |
| 156 | + req.ctx.params.identifier = orgFixtures.targetOrg.UUID |
| 157 | + sinon.stub(baseOrgRepo, 'findOneByShortName').resolves(fakeTargetOrgDocument) |
| 158 | + sinon.stub(baseOrgRepo, 'isSecretariat').resolves(false) |
| 159 | + sinon.stub(baseOrgRepo, 'getOrg').resolves(orgFixtures.targetOrg) |
| 160 | + |
| 161 | + await ORG_SINGLE(req, res, next) |
| 162 | + |
| 163 | + expect(status.args[0][0]).to.equal(200) |
| 164 | + expect(res.json.args[0][0]).to.deep.equal(orgFixtures.targetOrg) |
| 165 | + }) |
| 166 | + |
| 167 | + it('Secretariat can access an org by UUID', async () => { |
| 168 | + req.ctx.params.identifier = orgFixtures.targetOrg.UUID |
| 169 | + sinon.stub(baseOrgRepo, 'findOneByShortName').resolves(fakeSecretariatOrgDocument) |
| 170 | + sinon.stub(baseOrgRepo, 'isSecretariat').resolves(true) |
| 171 | + sinon.stub(baseOrgRepo, 'getOrg').resolves(orgFixtures.targetOrg) |
| 172 | + |
| 173 | + await ORG_SINGLE(req, res, next) |
| 174 | + |
| 175 | + expect(status.args[0][0]).to.equal(200) |
| 176 | + expect(res.json.args[0][0]).to.deep.equal(orgFixtures.targetOrg) |
194 | 177 | }) |
195 | 178 | }) |
196 | 179 | }) |
0 commit comments