Skip to content

Commit 771a6bc

Browse files
authored
Fix up committee tests (#104)
* Update chai tests to use return Promise syntax to avoid done() timeout pitfalls (#102) * Exclude officer field from committee model and requests (#103)
1 parent d8856e8 commit 771a6bc

File tree

11 files changed

+235
-355
lines changed

11 files changed

+235
-355
lines changed

models/committee.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default sequelize.define('committees', {
2424
return {
2525
include: [{
2626
model: Officer.scope({ method: ['active', date] }),
27+
attributes: [],
28+
includeIgnoreAttributes: false,
2729
}],
2830
};
2931
},

routes/committees.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ router
1313
Committee
1414
.scope(scopes)
1515
.findAndCountAll()
16-
.then(result => res.send({
17-
total: result.count,
18-
perPage: req.query.perPage,
19-
currentPage: req.query.page,
20-
data: result.rows.map((committee) => {
16+
.then(result => {
17+
const uniqueCommittees = {};
18+
result.rows.forEach((committee) => {
2119
const c = committee.get({ plain: true });
2220
Reflect.deleteProperty(c, 'officer');
23-
return c;
24-
}),
25-
}))
21+
uniqueCommittees[c.name] = c;
22+
});
23+
return res.send({
24+
total: Object.keys(uniqueCommittees).length,
25+
perPage: req.query.perPage,
26+
currentPage: req.query.page,
27+
data: Object.values(uniqueCommittees),
28+
})
29+
})
2630
.catch(err => next(err));
2731
});
2832

routes/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ router
3535
}
3636
})
3737
.post(needs('events', 'create'), (req, res, next) => {
38-
Event.create(req.body, { fields: ['name', 'startDate', 'endDate', 'description', 'location', 'image', 'committeeName'] })
38+
Event.create(req.body, { fields: ['name', 'startDate', 'endDate', 'description', 'location', 'link', 'image', 'committeeName'] })
3939
.then(event => res.status(201).send(event))
4040
.catch((err) => {
4141
err.status = 422;

test/routes/auth.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,54 @@ import { token } from '../helpers';
1010

1111
describe('INTEGRATION TESTS: AUTH', function () {
1212
describe('GET /', function () {
13-
it('Denies Unauthenticated Users', function (done) {
13+
it('Denies Unauthenticated Users', function () {
1414
const expected = {
1515
error: 'not logged in',
1616
};
1717

18-
request(app)
18+
return request(app)
1919
.get('/api/v2/auth')
2020
.expect(401)
2121
.then((response) => {
2222
expect(response.body).to.deep.equal(expected);
23-
done();
2423
});
2524
});
2625

27-
it('Gets the Current Logged In User', function (done) {
26+
it('Gets the Current Logged In User', function () {
2827
const expected = {
2928
firstName: 'Test',
3029
lastName: 'User',
3130
dce: 'abc1234',
3231
};
3332

34-
request(app)
33+
return request(app)
3534
.get('/api/v2/auth')
3635
.set('Authorization', `Bearer ${token}`)
3736
.expect(200)
3837
.then((response) => {
3938
expect(response.body).to.deep.equal(expected);
40-
done();
4139
});
4240
});
4341
});
4442

4543
describe('GET /googleClientID', function () {
46-
it('Gets the Google Client ID', function (done) {
44+
it('Gets the Google Client ID', function () {
4745
const expected = {
4846
token: nconf.get('auth').google.id,
4947
};
5048

51-
request(app)
49+
return request(app)
5250
.get('/api/v2/auth/googleClientID')
5351
.expect(200)
5452
.then((response) => {
5553
expect(response.body).to.deep.equal(expected);
56-
done();
5754
});
5855
});
5956
});
6057

6158
describe('POST /:provider', function () {
62-
it('Sends a Refresh Token', function (done) {
63-
request(app)
59+
it('Sends a Refresh Token', function () {
60+
return request(app)
6461
.post('/api/v2/auth/refresh')
6562
.set('Authorization', `Bearer ${token}`)
6663
.expect(200)
@@ -69,22 +66,20 @@ describe('INTEGRATION TESTS: AUTH', function () {
6966
// not be the same as the original token.
7067
expect(response.body.token).to.not.be.undefined; // eslint-disable-line no-unused-expressions
7168
expect(response.body).to.not.deep.equal({ token });
72-
done();
7369
});
7470
});
7571

76-
it('Does Not Support Unknown Providers', function (done) {
72+
it('Does Not Support Unknown Providers', function () {
7773
const expected = {
7874
error: 'invalid provider',
7975
};
8076

81-
request(app)
77+
return request(app)
8278
.post('/api/v2/auth/unknown')
8379
.set('Authorization', `Bearer ${token}`)
8480
.expect(401)
8581
.then((response) => {
8682
expect(response.body).to.deep.equal(expected);
87-
done();
8883
});
8984
});
9085

test/routes/committees.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import nconf from '../../config';
99

1010
describe('INTEGRATION TESTS: COMMITTEES', function () {
1111
describe('GET /', function () {
12-
it('Returns Committees', function (done) {
12+
it('Returns Committees', function () {
1313
const expected = {
1414
total: 4,
1515
perPage: nconf.get('pagination:perPage'),
@@ -42,16 +42,15 @@ describe('INTEGRATION TESTS: COMMITTEES', function () {
4242
],
4343
};
4444

45-
request(app)
45+
return request(app)
4646
.get('/api/v2/committees')
4747
.expect(200)
4848
.then((response) => {
4949
expect(response.body).to.deep.equal(expected);
50-
done();
5150
});
5251
});
5352

54-
it('Filters by an Existing Name', function (done) {
53+
it('Filters by an Existing Name', function () {
5554
const expected = {
5655
total: 1,
5756
perPage: nconf.get('pagination:perPage'),
@@ -66,33 +65,31 @@ describe('INTEGRATION TESTS: COMMITTEES', function () {
6665
],
6766
};
6867

69-
request(app)
68+
return request(app)
7069
.get('/api/v2/committees?name=Technology')
7170
.expect(200)
7271
.then((response) => {
7372
expect(response.body).to.deep.equal(expected);
74-
done();
7573
});
7674
});
7775

78-
it('Filters by an Non-existent Name', function (done) {
76+
it('Filters by an Non-existent Name', function () {
7977
const expected = {
8078
total: 0,
8179
perPage: nconf.get('pagination:perPage'),
8280
currentPage: 1,
8381
data: [],
8482
};
8583

86-
request(app)
84+
return request(app)
8785
.get('/api/v2/committees?name=Nope')
8886
.expect(200)
8987
.then((response) => {
9088
expect(response.body).to.deep.equal(expected);
91-
done();
9289
});
9390
});
9491

95-
it('Filters by Active', function (done) {
92+
it('Filters by Active', function () {
9693
const expected = {
9794
total: 2,
9895
perPage: nconf.get('pagination:perPage'),
@@ -113,17 +110,16 @@ describe('INTEGRATION TESTS: COMMITTEES', function () {
113110
],
114111
};
115112

116-
request(app)
113+
return request(app)
117114
.get(`/api/v2/committees?active=${encodeURIComponent('2017-12-05T05:00:00.000Z')}`)
118115
.expect(200)
119116
.then((response) => {
120117
expect(response.body).to.deep.equal(expected);
121-
done();
122118
});
123119
});
124120

125121
// See: https://github.com/rit-sse/node-api/issues/67
126-
it('Filters by Active When Multiple Officers Are Assigned to the Same Committee', function (done) {
122+
it('Filters by Active When Multiple Officers Are Assigned to the Same Committee', function () {
127123
const expected = {
128124
total: 2,
129125
perPage: nconf.get('pagination:perPage'),
@@ -144,45 +140,42 @@ describe('INTEGRATION TESTS: COMMITTEES', function () {
144140
],
145141
};
146142

147-
request(app)
143+
return request(app)
148144
.get(`/api/v2/committees?active=${encodeURIComponent('2017-01-05T05:00:00.000Z')}`)
149145
.expect(200)
150146
.then((response) => {
151147
expect(response.body).to.deep.equal(expected);
152-
done();
153148
});
154149
});
155150
});
156151

157152
describe('GET /:id', function () {
158-
it('Finds a Specific Committee', function (done) {
153+
it('Finds a Specific Committee', function () {
159154
const expected = {
160155
name: 'Technology',
161156
description: 'tech',
162157
createdAt: '2017-12-01T05:00:00.000Z',
163158
updatedAt: '2017-12-01T05:00:00.000Z',
164159
};
165160

166-
request(app)
161+
return request(app)
167162
.get('/api/v2/committees/Technology')
168163
.expect(200)
169164
.then((response) => {
170165
expect(response.body).to.deep.equal(expected);
171-
done();
172166
});
173167
});
174168

175-
it('Does Not Find a Committee That Does Not Exist', function (done) {
169+
it('Does Not Find a Committee That Does Not Exist', function () {
176170
const expected = {
177171
error: 'Committee not found',
178172
};
179173

180-
request(app)
174+
return request(app)
181175
.get('/api/v2/committees/Nope')
182176
.expect(404)
183177
.then((response) => {
184178
expect(response.body).to.deep.equal(expected);
185-
done();
186179
});
187180
});
188181
});

0 commit comments

Comments
 (0)