Skip to content

Commit 827898b

Browse files
authored
Merge pull request #1431 from CVEProject/emathew/test-registry-flag-user-get
Resolves issue 1417, Testing User Get Request for /api/org with the `registry=true` flag
2 parents b4943d9 + 951bc4a commit 827898b

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const chai = require('chai')
2+
chai.use(require('chai-http'))
3+
const expect = chai.expect
4+
5+
const constants = require('../constants.js')
6+
const app = require('../../../src/index.js')
7+
const BASE_URL = '/api'
8+
/**
9+
* Unit Tests for testing User Get Request for /api/org with the `registry=true` flag
10+
*/
11+
12+
describe('Testing /api/org/ user endpoints with `registry=true`', () => {
13+
// Testing USER GET Endpoints with `registry=true` flag
14+
describe('Testing USER GET endpoint with `registry=true`', () => {
15+
/* Positive Tests */
16+
it('secretariat users can request a list of all users', async () => {
17+
await chai.request(app)
18+
.get(`${BASE_URL}/users?registry=true`)
19+
.set(constants.headers)
20+
.send({
21+
})
22+
.then((res) => {
23+
expect(res).to.have.status(200)
24+
// check the fields returned
25+
})
26+
})
27+
it('page must be a positive int', async () => {
28+
await chai.request(app)
29+
.get(`${BASE_URL}/users?registry=true&page=1`)
30+
.set(constants.headers)
31+
.send()
32+
.then((res) => {
33+
expect(res).to.have.status(200)
34+
})
35+
})
36+
it('can retrieve user after an update', async () => {
37+
const user = constants.nonSecretariatUserHeaders3['CVE-API-USER']
38+
const org = constants.nonSecretariatUserHeaders3['CVE-API-ORG']
39+
const newFirstName = 'testFirstName'
40+
var oldFirstName = ''
41+
await chai.request(app)
42+
.get(`${BASE_URL}/org/${org}/user/${user}?registry=true`)
43+
.set(constants.headers)
44+
.send()
45+
.then((res) => {
46+
expect(res).to.have.status(200)
47+
oldFirstName = res.body.name.first
48+
})
49+
await chai.request(app)
50+
.put(`${BASE_URL}/org/${org}/user/${user}?registry=true&name.first=${newFirstName}`)
51+
.set(constants.headers)
52+
.send()
53+
.then((res) => {
54+
expect(res).to.have.status(200)
55+
})
56+
await chai.request(app)
57+
.get(`${BASE_URL}/org/${org}/user/${user}?registry=true`)
58+
.set(constants.headers)
59+
.send()
60+
.then((res) => {
61+
expect(res).to.have.status(200)
62+
expect(res.body.name.first).to.contain(newFirstName)
63+
})
64+
await chai.request(app)
65+
.put(`${BASE_URL}/org/${org}/user/${user}?registry=true&name.first=${oldFirstName}`)
66+
.set(constants.headers)
67+
.send()
68+
.then((res) => {
69+
expect(res).to.have.status(200)
70+
})
71+
})
72+
})
73+
/* Negative Tests */
74+
context('Negative Test', () => {
75+
it('regular users cannot request a list of all users', async () => {
76+
await chai.request(app)
77+
.get(`${BASE_URL}/users?registry=true`)
78+
.set(constants.nonSecretariatUserHeaders)
79+
.send({
80+
})
81+
.then((res) => {
82+
expect(res).to.have.status(403)
83+
expect(res.body.error).to.contain('SECRETARIAT_ONLY')
84+
})
85+
})
86+
it('org admins cannot request a list of all users', async () => {
87+
await chai.request(app)
88+
.get(`${BASE_URL}/users?registry=true`)
89+
.set(constants.nonSecretariatUserHeaders2)
90+
.send({
91+
})
92+
.then((res) => {
93+
expect(res).to.have.status(403)
94+
expect(res.body.error).to.contain('SECRETARIAT_ONLY')
95+
})
96+
})
97+
it('page must be a positive int', async () => {
98+
// test negative int
99+
await chai.request(app)
100+
.get(`${BASE_URL}/users?registry=true&page=-1`)
101+
.set(constants.headers)
102+
.send({})
103+
.then((res) => {
104+
expect(res).to.have.status(400)
105+
expect(res.body.error).to.contain('BAD_INPUT')
106+
})
107+
// test string
108+
await chai.request(app)
109+
.get(`${BASE_URL}/users?registry=true&page=abc`)
110+
.set(constants.headers)
111+
.send({})
112+
.then((res) => {
113+
expect(res).to.have.status(400)
114+
expect(res.body.error).to.contain('BAD_INPUT')
115+
})
116+
})
117+
})
118+
})

test/integration-tests/user/updateUserTest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,17 @@ describe('Testing Edit user endpoint', () => {
120120
expect(res.body.error).to.contain('BAD_INPUT')
121121
})
122122
})
123+
it('expect error when trying to add existing user to the same org', async () => {
124+
const user = constants.nonSecretariatUserHeaders3['CVE-API-USER']
125+
const org = constants.nonSecretariatUserHeaders3['CVE-API-ORG']
126+
await chai.request(app)
127+
.put(`/api/org/${org}/user/${user}?registry=true&org_short_name=${org}`)
128+
.set(constants.headers)
129+
.send()
130+
.then((res) => {
131+
expect(res).to.have.status(403)
132+
expect(res.body.error).to.contain('USER_ALREADY_IN_ORG')
133+
})
134+
})
123135
})
124136
})

0 commit comments

Comments
 (0)