Skip to content

Commit 93538b9

Browse files
committed
Refactor repetitive code
1 parent efb8375 commit 93538b9

File tree

8 files changed

+128
-246
lines changed

8 files changed

+128
-246
lines changed

app/tests/permissions/add-org-team.test.js

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,32 @@
11
const test = require('ava')
22
const db = require('../../db')
33
const path = require('path')
4-
const hydra = require('../../lib/hydra')
5-
const sinon = require('sinon')
4+
const { initializeContext, createOrg, destroyOrg } = require('./initialization')
65

76
const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations')
87

9-
let agent
10-
test.before(async () => {
11-
const conn = await db()
12-
await conn.migrate.latest({ directory: migrationsDirectory })
13-
14-
// seed
15-
await conn('users').insert({ id: 100 })
16-
await conn('users').insert({ id: 101 })
17-
18-
// stub hydra introspect
19-
let introspectStub = sinon.stub(hydra, 'introspect')
20-
introspectStub.withArgs('user100').returns({
21-
active: true,
22-
sub: '100'
23-
})
24-
25-
introspectStub.withArgs('user101').returns({
26-
active: true,
27-
sub: '101'
28-
})
29-
introspectStub.withArgs('invalidToken').returns({ active: false })
30-
31-
agent = require('supertest').agent(await require('../../index')())
32-
})
8+
test.before(initializeContext)
339

3410
test.after.always(async () => {
3511
const conn = await db()
3612
await conn.migrate.rollback({ directory: migrationsDirectory })
3713
conn.destroy()
3814
})
3915

16+
// set up and tear down an org for each test
17+
test.beforeEach(createOrg)
18+
test.afterEach(destroyOrg)
19+
4020
/**
4121
* An org owner can create a team
4222
* We create an org with the user100 user
4323
* We check that user100 can create a team in the org
4424
*
4525
*/
4626
test('org owner can create a team', async t => {
47-
const orgName = 'org owner can create a team'
4827
const teamName = 'org owner can create a team - team'
4928

50-
let res = await agent.post('/api/organizations')
51-
.send({ name: orgName })
52-
.set('Authorization', 'Bearer user100')
53-
.expect(200)
54-
55-
let res2 = await agent.post(`/api/organizations/${res.body.id}/teams`)
29+
let res2 = await t.context.agent.post(`/api/organizations/${t.context.org.id}/teams`)
5630
.set('Authorization', 'Bearer user100')
5731
.send({ name: teamName })
5832

@@ -66,20 +40,14 @@ test('org owner can create a team', async t => {
6640
*
6741
*/
6842
test('org manager can create a team', async t => {
69-
const orgName = 'org manager can create a team'
7043
const teamName = 'org manager can create a team - team'
7144

72-
let res = await agent.post('/api/organizations')
73-
.send({ name: orgName })
74-
.set('Authorization', 'Bearer user100')
75-
.expect(200)
76-
7745
// We create a manager role for user 101
78-
await agent.put(`/api/organizations/${res.body.id}/addManager/101`)
46+
await t.context.agent.put(`/api/organizations/${t.context.org.id}/addManager/101`)
7947
.set('Authorization', 'Bearer user100')
8048
.expect(200)
8149

82-
let res2 = await agent.post(`/api/organizations/${res.body.id}/teams`)
50+
let res2 = await t.context.agent.post(`/api/organizations/${t.context.org.id}/teams`)
8351
.set('Authorization', 'Bearer user101')
8452
.send({ name: teamName })
8553

@@ -88,20 +56,13 @@ test('org manager can create a team', async t => {
8856

8957
/**
9058
* An non-org manager cannot create a team
91-
* We create an org with the user100 user and assign user101
92-
* as a manager. We check that user101 can create a team in the org
59+
* We create an org with the user100 user and check that a non
9360
*
9461
*/
9562
test('non-org manager cannot create a team', async t => {
96-
const orgName = 'non-org manager cannot create a team'
9763
const teamName = 'non-org manager cannot create a team - team'
9864

99-
let res = await agent.post('/api/organizations')
100-
.send({ name: orgName })
101-
.set('Authorization', 'Bearer user100')
102-
.expect(200)
103-
104-
let res2 = await agent.post(`/api/organizations/${res.body.id}/teams`)
65+
let res2 = await t.context.agent.post(`/api/organizations/${t.context.org.id}/teams`)
10566
.set('Authorization', 'Bearer user101')
10667
.send({ name: teamName })
10768

app/tests/permissions/create-team.test.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
const test = require('ava')
22
const db = require('../../db')
33
const path = require('path')
4-
const hydra = require('../../lib/hydra')
5-
const sinon = require('sinon')
4+
const { initializeContext } = require('./initialization')
65

76
const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations')
87

9-
let agent
10-
test.before(async () => {
11-
const conn = await db()
12-
await conn.migrate.latest({ directory: migrationsDirectory })
13-
14-
// seed
15-
await conn('users').insert({ id: 100 })
16-
17-
// stub hydra introspect
18-
let introspectStub = sinon.stub(hydra, 'introspect')
19-
introspectStub.withArgs('validToken').returns({
20-
active: true,
21-
sub: '100'
22-
})
23-
introspectStub.withArgs('invalidToken').returns({ active: false })
24-
25-
agent = require('supertest').agent(await require('../../index')())
26-
})
8+
test.before(initializeContext)
279

2810
test.after.always(async () => {
2911
const conn = await db()
@@ -32,16 +14,16 @@ test.after.always(async () => {
3214
})
3315

3416
test('an authenticated user can create a team', async t => {
35-
let res = await agent.post('/api/teams')
17+
let res = await t.context.agent.post('/api/teams')
3618
.send({ name: 'road team 1' })
37-
.set('Authorization', `Bearer validToken`)
19+
.set('Authorization', 'Bearer user100')
3820
.expect(200)
3921

4022
t.is(res.body.name, 'road team 1')
4123
})
4224

4325
test('an unauthenticated user cannot create a team', async t => {
44-
let res = await agent.post('/api/teams')
26+
let res = await t.context.agent.post('/api/teams')
4527
.send({ name: 'road team 2' })
4628

4729
t.is(res.status, 401)
Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
11
const test = require('ava')
22
const db = require('../../db')
33
const path = require('path')
4-
const hydra = require('../../lib/hydra')
5-
const sinon = require('sinon')
4+
const { initializeContext } = require('./initialization')
65

76
const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations')
87

9-
let agent
10-
test.before(async () => {
11-
const conn = await db()
12-
await conn.migrate.latest({ directory: migrationsDirectory })
13-
14-
// seed
15-
await conn('users').insert({ id: 100 })
16-
await conn('users').insert({ id: 101 })
17-
18-
// stub hydra introspect
19-
let introspectStub = sinon.stub(hydra, 'introspect')
20-
introspectStub.withArgs('validToken').returns({
21-
active: true,
22-
sub: '100'
23-
})
24-
introspectStub.withArgs('differentUser').returns({
25-
active: true,
26-
sub: '101'
27-
})
28-
introspectStub.withArgs('invalidToken').returns({ active: false })
29-
30-
agent = require('supertest').agent(await require('../../index')())
31-
})
8+
test.before(initializeContext)
329

3310
test.after.always(async () => {
3411
const conn = await db()
@@ -37,25 +14,25 @@ test.after.always(async () => {
3714
})
3815

3916
test('a team moderator can delete a team', async t => {
40-
let res = await agent.post('/api/teams')
17+
let res = await t.context.agent.post('/api/teams')
4118
.send({ name: 'road team 1' })
42-
.set('Authorization', `Bearer validToken`)
19+
.set('Authorization', `Bearer user100`)
4320
.expect(200)
4421

45-
let res2 = await agent.delete(`/api/teams/${res.body.id}`)
46-
.set('Authorization', `Bearer validToken`)
22+
let res2 = await t.context.agent.delete(`/api/teams/${res.body.id}`)
23+
.set('Authorization', `Bearer user100`)
4724

4825
t.is(res2.status, 200)
4926
})
5027

5128
test('a non-team moderator cannot delete a team', async t => {
52-
let res = await agent.post('/api/teams')
29+
let res = await t.context.agent.post('/api/teams')
5330
.send({ name: 'road team 2' })
54-
.set('Authorization', `Bearer validToken`)
31+
.set('Authorization', `Bearer user100`)
5532
.expect(200)
5633

57-
let res2 = await agent.delete(`/api/teams/${res.body.id}`)
58-
.set('Authorization', `Bearer differentUser`)
34+
let res2 = await t.context.agent.delete(`/api/teams/${res.body.id}`)
35+
.set('Authorization', `Bearer user101`)
5936

6037
t.is(res2.status, 401)
6138
})

app/tests/permissions/edit-organization.test.js

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,31 @@
11
const test = require('ava')
22
const db = require('../../db')
33
const path = require('path')
4-
const hydra = require('../../lib/hydra')
5-
const sinon = require('sinon')
4+
const { initializeContext, createOrg, destroyOrg } = require('./initialization')
65

76
const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations')
87

9-
let agent
10-
test.before(async () => {
11-
const conn = await db()
12-
await conn.migrate.latest({ directory: migrationsDirectory })
13-
14-
// seed
15-
await conn('users').insert({ id: 100 })
16-
await conn('users').insert({ id: 101 })
17-
await conn('users').insert({ id: 102 })
18-
19-
// stub hydra introspect
20-
let introspectStub = sinon.stub(hydra, 'introspect')
21-
introspectStub.withArgs('user100').returns({
22-
active: true,
23-
sub: '100'
24-
})
25-
26-
introspectStub.withArgs('user101').returns({
27-
active: true,
28-
sub: '101'
29-
})
30-
31-
introspectStub.withArgs('invalidToken').returns({ active: false })
32-
33-
agent = require('supertest').agent(await require('../../index')())
34-
})
8+
test.before(initializeContext)
359

3610
test.after.always(async () => {
3711
const conn = await db()
3812
await conn.migrate.rollback({ directory: migrationsDirectory })
3913
conn.destroy()
4014
})
4115

16+
// set up and tear down an org for each test
17+
test.beforeEach(createOrg)
18+
test.afterEach(destroyOrg)
19+
4220
/**
4321
* An org owner can update the org
4422
* We create an org with the user100 and then
4523
* see if they can edit the organization
4624
*
4725
*/
4826
test('org owner can update an org', async t => {
49-
const orgName = 'org owner can update an org'
5027
const orgName2 = 'org owner can update an org - org 2'
51-
const res = await agent.post('/api/organizations')
52-
.send({ name: orgName })
53-
.set('Authorization', `Bearer user100`)
54-
.expect(200)
55-
56-
const res2 = await agent.put(`/api/organizations/${res.body.id}`)
28+
const res2 = await t.context.agent.put(`/api/organizations/${t.context.org.id}`)
5729
.set('Authorization', `Bearer user100`)
5830
.send({ name: orgName2 })
5931

@@ -66,19 +38,14 @@ test('org owner can update an org', async t => {
6638
*
6739
*/
6840
test('org manager cannot update an org', async t => {
69-
const orgName = 'org manager cannot update an org'
7041
const orgName2 = 'org manager cannot update an org - org 2'
71-
const res = await agent.post('/api/organizations')
72-
.send({ name: orgName })
73-
.set('Authorization', `Bearer user100`)
74-
.expect(200)
7542

7643
// We create a manager role for user 101
77-
await agent.post(`/api/organizations/${res.body.id}/addManager/101`)
44+
await t.context.agent.post(`/api/organizations/${t.context.org.id}/addManager/101`)
7845
.set('Authorization', `Bearer user100`)
7946
.expect(200)
8047

81-
const res3 = await agent.put(`/api/organizations/${res.body.id}`)
48+
const res3 = await t.context.agent.put(`/api/organizations/${t.context.org.id}`)
8249
.set('Authorization', `Bearer user101`)
8350
.send({ name: orgName2 })
8451

@@ -91,14 +58,9 @@ test('org manager cannot update an org', async t => {
9158
*
9259
*/
9360
test('no-role user cannot update an org', async t => {
94-
const orgName = 'no-role user cannot update an org'
9561
const orgName2 = 'no-role user cannot update an org - org 2'
96-
const res = await agent.post('/api/organizations')
97-
.send({ name: orgName })
98-
.set('Authorization', `Bearer user100`)
99-
.expect(200)
10062

101-
const res2 = await agent.put(`/api/organizations/${res.body.id}`)
63+
const res2 = await t.context.agent.put(`/api/organizations/${t.context.org.id}`)
10264
.set('Authorization', `Bearer user101`)
10365
.send({ name: orgName2 })
10466

0 commit comments

Comments
 (0)