Skip to content

Commit 03869ce

Browse files
authored
Merge pull request #212 from CodeForBaltimore/revjtanton/issue-96
Increasing test coverage
2 parents c2d82a6 + b97a40b commit 03869ce

File tree

3 files changed

+164
-14
lines changed

3 files changed

+164
-14
lines changed

src/models/entity.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ const entity = (sequelize, DataTypes) => {
6565
return entity
6666
}
6767

68-
Entity.findContacts = async (id) => {
69-
const entity = await Entity.findOne({
70-
where: { id }
71-
})
72-
73-
const contacts = await entity.getContacts()
74-
75-
return contacts
76-
}
77-
7868
Entity.findEntityWithAssociatedContacts = async (entityId) => {
7969
const entityContacts = await Entity.findOne({
8070
where: { id: entityId },

src/tests/entity.routes.spec.js

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,49 @@ import app from '..'
88
const { expect } = chai
99
const entity = {
1010
name: randomWords(),
11-
type: 'Test'
11+
type: 'Test',
12+
contacts: []
13+
}
14+
const contact = {
15+
name: randomWords(),
16+
phone: [
17+
{
18+
number: (Math.floor(Math.random() * Math.floor(100000000000))).toString()
19+
}
20+
],
21+
email: [
22+
{
23+
address: `${randomWords()}@test.test`
24+
}
25+
]
1226
}
1327

14-
describe('Entity positive tests', () => {
28+
describe('Entity tests', () => {
1529
const authed = new Login()
1630
let token
1731

1832
before(async () => {
1933
await authed.setToken()
2034
token = await authed.getToken()
35+
36+
const contactResponse = await request(app)
37+
.post('/contact')
38+
.set('Accept', 'application/json')
39+
.set('token', token)
40+
.send(contact)
41+
.expect('Content-Type', 'text/html; charset=utf-8')
42+
.expect(201)
43+
44+
contact.id = contactResponse.text.replace(' created', '')
45+
entity.contacts.push({ id: contact.id, title: 'test' })
2146
})
2247
after(async () => {
48+
await request(app)
49+
.delete(`/contact/${contact.id}`)
50+
.set('Accept', 'application/json')
51+
.set('token', token)
52+
.expect('Content-Type', 'text/html; charset=utf-8')
53+
.expect(200)
2354
await authed.destroyToken()
2455
})
2556

@@ -88,6 +119,7 @@ describe('Entity positive tests', () => {
88119
})
89120
it('should update an entity', done => {
90121
entity.name = randomWords()
122+
entity.checkIn = { test: 'test' }
91123
request(app)
92124
.put(`/entity`)
93125
.set('Accept', 'application/json')
@@ -101,6 +133,69 @@ describe('Entity positive tests', () => {
101133
done()
102134
})
103135
})
136+
it('should add an contact to an entity', done => {
137+
const contactIds = { contacts: [{ id: contact.id, title: 'test' }] }
138+
request(app)
139+
.post(`/entity/link/${entity.id}`)
140+
.set('Accept', 'application/json')
141+
.set('token', token)
142+
.send(contactIds)
143+
.expect('Content-Type', 'text/html; charset=utf-8')
144+
.expect(200)
145+
.end((err, res) => {
146+
if (err) return done(err)
147+
expect(res.text).to.equal(`Linking successful/already exists for entity with ID ${entity.id}`)
148+
done()
149+
})
150+
})
151+
it('should not add an contact to an entity', done => {
152+
const contactIds = { contacts: [{ id: uuid() }] }
153+
request(app)
154+
.post(`/entity/link/abc123`)
155+
.set('Accept', 'application/json')
156+
.set('token', token)
157+
.send(contactIds)
158+
.expect('Content-Type', 'text/html; charset=utf-8')
159+
.expect(400)
160+
.end((err, res) => {
161+
if (err) return done(err)
162+
expect(res.text).to.equal(`Bad Request`)
163+
done()
164+
})
165+
})
166+
167+
168+
169+
it('should remove a contact from an entity', done => {
170+
const contactIds = { contacts: [{ id: contact.id, title: 'test' }] }
171+
request(app)
172+
.post(`/entity/unlink/${entity.id}`)
173+
.set('Accept', 'application/json')
174+
.set('token', token)
175+
.send(contactIds)
176+
.expect('Content-Type', 'text/html; charset=utf-8')
177+
.expect(200)
178+
.end((err, res) => {
179+
if (err) return done(err)
180+
expect(res.text).to.equal(`Unlinking successful for entity with ID ${entity.id}`)
181+
done()
182+
})
183+
})
184+
it('should not remove a contact from an entity', done => {
185+
const contactIds = { contacts: [{ id: uuid() }] }
186+
request(app)
187+
.post(`/entity/unlink/abc123`)
188+
.set('Accept', 'application/json')
189+
.set('token', token)
190+
.send(contactIds)
191+
.expect('Content-Type', 'text/html; charset=utf-8')
192+
.expect(400)
193+
.end((err, res) => {
194+
if (err) return done(err)
195+
expect(res.text).to.equal(`Bad Request`)
196+
done()
197+
})
198+
})
104199
it('Positive Test for CSV Dump on Entity', (done) => {
105200
request(app)
106201
.get('/csv/Entity')

src/tests/user.routes.spec.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import request from 'supertest'
33
import randomWords from 'random-words'
44
import { Login } from '../utils/login'
55
import app from '..'
6+
import { random } from 'lodash'
67

78
const { expect } = chai
89
const user = { email: `${randomWords()}@test.test`, password: `Abcdefg42!`, roles: ["admin"] }
910

10-
describe('User positive tests', () => {
11+
describe('User tests', () => {
1112
const authed = new Login()
1213
let token
1314

@@ -19,6 +20,32 @@ describe('User positive tests', () => {
1920
await authed.destroyToken()
2021
})
2122

23+
it('should not login', (done) => {
24+
request(app)
25+
.post('/user/login')
26+
.send({ email: `${randomWords()}@test.test`, password: `Abcdefg12!` })
27+
.set('Accept', 'application/json')
28+
.expect('Content-Type', 'text/html; charset=utf-8')
29+
.expect(403)
30+
.end((err, res) => {
31+
if (err) return done(err)
32+
expect(res.text).to.equal(`Forbidden`)
33+
done()
34+
})
35+
})
36+
it('should not login with invalid email', (done) => {
37+
request(app)
38+
.post('/user/login')
39+
.send({ email: randomWords(), password: `Abcdefg12!` })
40+
.set('Accept', 'application/json')
41+
.expect('Content-Type', 'text/html; charset=utf-8')
42+
.expect(400)
43+
.end((err, res) => {
44+
if (err) return done(err)
45+
expect(res.text).to.equal(`Bad Request`)
46+
done()
47+
})
48+
})
2249
it('should get all users', (done) => {
2350
request(app)
2451
.get(`/user`)
@@ -61,7 +88,6 @@ describe('User positive tests', () => {
6188
done()
6289
})
6390
})
64-
// Test will need some more work.
6591
it('should update a user', (done) => {
6692
user.displayName = randomWords()
6793
request(app)
@@ -77,6 +103,45 @@ describe('User positive tests', () => {
77103
done()
78104
})
79105
})
106+
it('should request a reset of the password', (done) => {
107+
request(app)
108+
.post(`/user/reset/${user.email}`)
109+
.send(user)
110+
.set('Accept', 'application/json')
111+
.expect('Content-Type', 'text/html; charset=utf-8')
112+
.expect(200)
113+
.end((err, res) => {
114+
if (err) return done(err)
115+
expect(res.text).to.equal(`Password reset email sent`)
116+
done()
117+
})
118+
})
119+
it('should request a reset of the password even with invalid email', (done) => {
120+
request(app)
121+
.post(`/user/reset/${randomWords()}@test.test`)
122+
.send(user)
123+
.set('Accept', 'application/json')
124+
.expect('Content-Type', 'text/html; charset=utf-8')
125+
.expect(200)
126+
.end((err, res) => {
127+
if (err) return done(err)
128+
expect(res.text).to.equal(`Password reset email sent`)
129+
done()
130+
})
131+
})
132+
it('should not request a reset of the password with invalid email format', (done) => {
133+
request(app)
134+
.post(`/user/reset/${randomWords()}`)
135+
.send(user)
136+
.set('Accept', 'application/json')
137+
.expect('Content-Type', 'text/html; charset=utf-8')
138+
.expect(400)
139+
.end((err, res) => {
140+
if (err) return done(err)
141+
expect(res.text).to.equal(`Bad Request`)
142+
done()
143+
})
144+
})
80145
it('should delete a user', (done) => {
81146
request(app)
82147
.delete(`/user/${user.email}`)

0 commit comments

Comments
 (0)