Skip to content

Commit cf9839c

Browse files
committed
Integration tests use consistent teardown methods
1 parent c2f0fd5 commit cf9839c

23 files changed

+400
-560
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ var path = require('path'),
628628
// globally, by using a command in the form (replace path to api_tags_spec.js with the test file you want to
629629
// run):
630630
//
631-
// NODE_ENV=testing mocha --timeout=15000 --ui=bdd --reporter=spec core/test/integration/api/api_tags_spec.js`
631+
// `NODE_ENV=testing mocha --timeout=15000 --ui=bdd --reporter=spec core/test/integration/api/api_tags_spec.js`
632632
//
633633
// Their purpose is to test that both the api and models behave as expected when the database layer is involved.
634634
// These tests are run against sqlite3, mysql and pg on travis and ensure that differences between the databases

core/test/integration/api/api_authentication_spec.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
var testUtils = require('../../utils'),
2-
should = require('should'),
3-
when = require('when'),
4-
rewire = require('rewire'),
5-
mail = rewire('../../../server/api/mail'),
1+
/*globals describe, before, beforeEach, afterEach, it */
2+
/*jshint expr:true*/
3+
var testUtils = require('../../utils'),
4+
should = require('should'),
5+
when = require('when'),
6+
rewire = require('rewire'),
7+
8+
// Stuff we are testing
9+
mail = rewire('../../../server/api/mail'),
10+
settings = require('../../../server/api/settings'),
611
permissions = require('../../../server/permissions'),
7-
settings = require('../../../server/api/settings'),
8-
9-
authentication = require('../../../server/api/authentication');
12+
AuthAPI = require('../../../server/api/authentication');
1013

1114
describe('Authentication API', function () {
15+
// Keep the DB clean
16+
before(testUtils.teardown);
17+
afterEach(testUtils.teardown);
1218

13-
before(function (done) {
14-
testUtils.clearData().then(function () {
15-
done();
16-
}).catch(done);
17-
});
19+
should.exist(AuthAPI);
1820

1921
describe('Setup', function () {
2022

2123
describe('Not completed', function () {
2224

2325
beforeEach(function (done) {
24-
testUtils.clearData().then(function () {
25-
return testUtils.initData().then(function () {
26-
return permissions.init().then(function () {
27-
return settings.updateSettingsCache();
28-
});
26+
testUtils.initData().then(function () {
27+
return permissions.init().then(function () {
28+
return settings.updateSettingsCache();
2929
});
3030
}).then(function () {
3131
done();
3232
}).catch(done);
3333
});
3434

3535
it('should report that setup has not been completed', function (done) {
36-
authentication.isSetup().then(function (result) {
36+
AuthAPI.isSetup().then(function (result) {
3737
should.exist(result);
3838
result.setup[0].status.should.be.false;
3939

@@ -55,8 +55,12 @@ describe('Authentication API', function () {
5555
return when.resolve();
5656
});
5757

58-
authentication.setup({ setup: [setupData] }).then(function (result) {
58+
AuthAPI.setup({ setup: [setupData] }).then(function (result) {
5959
should.exist(result);
60+
should.exist(result.users);
61+
result.users.should.have.length(1);
62+
testUtils.API.checkResponse(result, 'users');
63+
testUtils.API.checkResponse(result.users[0], 'user', ['roles']);
6064

6165
var newUser = result.users[0];
6266

@@ -74,12 +78,10 @@ describe('Authentication API', function () {
7478
describe('Completed', function () {
7579

7680
beforeEach(function (done) {
77-
testUtils.clearData().then(function () {
78-
return testUtils.initData().then(function () {
79-
return testUtils.insertDefaultFixtures().then(function () {
80-
return permissions.init().then(function () {
81-
return settings.updateSettingsCache();
82-
});
81+
testUtils.initData().then(function () {
82+
return testUtils.insertDefaultFixtures().then(function () {
83+
return permissions.init().then(function () {
84+
return settings.updateSettingsCache();
8385
});
8486
});
8587
}).then(function () {
@@ -88,7 +90,7 @@ describe('Authentication API', function () {
8890
});
8991

9092
it('should report that setup has been completed', function (done) {
91-
authentication.isSetup().then(function (result) {
93+
AuthAPI.isSetup().then(function (result) {
9294
should.exist(result);
9395
result.setup[0].status.should.be.true;
9496

@@ -104,7 +106,7 @@ describe('Authentication API', function () {
104106
title: 'a test blog'
105107
};
106108

107-
authentication.setup({ setup: [setupData] }).then(function (result) {
109+
AuthAPI.setup({ setup: [setupData] }).then(function () {
108110
done(new Error('Setup was able to be run'));
109111
}).catch(function (err) {
110112
should.exist(err);
@@ -123,19 +125,17 @@ describe('Authentication API', function () {
123125
describe('Setup not completed', function () {
124126

125127
beforeEach(function (done) {
126-
testUtils.clearData().then(function () {
127-
return testUtils.initData().then(function () {
128-
return permissions.init().then(function () {
129-
return settings.updateSettingsCache();
130-
});
128+
return testUtils.initData().then(function () {
129+
return permissions.init().then(function () {
130+
return settings.updateSettingsCache();
131131
});
132132
}).then(function () {
133133
done();
134134
}).catch(done);
135135
});
136136

137137
it('should not allow an invitation to be accepted', function (done) {
138-
authentication.acceptInvitation().then(function () {
138+
AuthAPI.acceptInvitation().then(function () {
139139
done(new Error('Invitation was allowed to be accepted'));
140140
}).catch(function (err) {
141141
should.exist(err);
@@ -148,7 +148,7 @@ describe('Authentication API', function () {
148148
});
149149

150150
it('should not generate a password reset token', function (done) {
151-
authentication.generateResetToken().then(function () {
151+
AuthAPI.generateResetToken().then(function () {
152152
done(new Error('Reset token was generated'));
153153
}).catch(function (err) {
154154
should.exist(err);
@@ -161,7 +161,7 @@ describe('Authentication API', function () {
161161
});
162162

163163
it('should not allow a password reset', function (done) {
164-
authentication.resetPassword().then(function () {
164+
AuthAPI.resetPassword().then(function () {
165165
done(new Error('Password was reset'));
166166
}).catch(function (err) {
167167
should.exist(err);

core/test/integration/api/api_db_spec.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
/*globals describe, before, beforeEach, afterEach, it */
2+
/*jshint expr:true*/
23
var testUtils = require('../../utils'),
34
should = require('should'),
45

56
// Stuff we are testing
67
permissions = require('../../../server/permissions'),
7-
DataGenerator = require('../../utils/fixtures/data-generator'),
88
dbAPI = require('../../../server/api/db'),
99
TagsAPI = require('../../../server/api/tags'),
1010
PostAPI = require('../../../server/api/posts');
1111

1212
describe('DB API', function () {
13-
14-
before(function (done) {
15-
testUtils.clearData().then(function () {
16-
done();
17-
}).catch(done);
18-
});
13+
// Keep the DB clean
14+
before(testUtils.teardown);
15+
afterEach(testUtils.teardown);
1916

2017
beforeEach(function (done) {
2118
testUtils.initData().then(function () {
@@ -29,11 +26,7 @@ describe('DB API', function () {
2926
}).catch(done);
3027
});
3128

32-
afterEach(function (done) {
33-
testUtils.clearData().then(function () {
34-
done();
35-
}).catch(done);
36-
});
29+
should.exist(dbAPI);
3730

3831
it('delete all content', function (done) {
3932
var options = {context: {user: 1}};
@@ -62,17 +55,17 @@ describe('DB API', function () {
6255
permissions.init().then(function () {
6356
return dbAPI.deleteAllContent({context: {user: 2}});
6457
}).then(function (){
65-
done(new Error("Delete all content is not denied for editor."));
58+
done(new Error('Delete all content is not denied for editor.'));
6659
}, function (error) {
6760
error.type.should.eql('NoPermissionError');
6861
return dbAPI.deleteAllContent({context: {user: 3}});
6962
}).then(function (){
70-
done(new Error("Delete all content is not denied for author."));
63+
done(new Error('Delete all content is not denied for author.'));
7164
}, function (error) {
7265
error.type.should.eql('NoPermissionError');
7366
return dbAPI.deleteAllContent();
7467
}).then(function (){
75-
done(new Error("Delete all content is not denied without authentication."));
68+
done(new Error('Delete all content is not denied without authentication.'));
7669
}).catch(function (error) {
7770
error.type.should.eql('NoPermissionError');
7871
done();
@@ -83,17 +76,17 @@ describe('DB API', function () {
8376
permissions.init().then(function () {
8477
return dbAPI.exportContent({context: {user: 2}});
8578
}).then(function (){
86-
done(new Error("Export content is not denied for editor."));
79+
done(new Error('Export content is not denied for editor.'));
8780
}, function (error) {
8881
error.type.should.eql('NoPermissionError');
8982
return dbAPI.exportContent({context: {user: 3}});
9083
}).then(function (){
91-
done(new Error("Export content is not denied for author."));
84+
done(new Error('Export content is not denied for author.'));
9285
}, function (error) {
9386
error.type.should.eql('NoPermissionError');
9487
return dbAPI.exportContent();
9588
}).then(function (){
96-
done(new Error("Export content is not denied without authentication."));
89+
done(new Error('Export content is not denied without authentication.'));
9790
}).catch(function (error) {
9891
error.type.should.eql('NoPermissionError');
9992
done();
@@ -103,18 +96,18 @@ describe('DB API', function () {
10396
it('import content is denied', function (done) {
10497
permissions.init().then(function () {
10598
return dbAPI.importContent({context: {user: 2}});
106-
}).then(function (result) {
107-
done(new Error("Import content is not denied for editor."));
99+
}).then(function () {
100+
done(new Error('Import content is not denied for editor.'));
108101
}, function (error) {
109102
error.type.should.eql('NoPermissionError');
110103
return dbAPI.importContent({context: {user: 3}});
111-
}).then(function (result) {
112-
done(new Error("Import content is not denied for author."));
104+
}).then(function () {
105+
done(new Error('Import content is not denied for author.'));
113106
}, function (error) {
114107
error.type.should.eql('NoPermissionError');
115108
return dbAPI.importContent();
116-
}).then(function (result) {
117-
done(new Error("Import content is not denied without authentication."));
109+
}).then(function () {
110+
done(new Error('Import content is not denied without authentication.'));
118111
}).catch(function (error) {
119112
error.type.should.eql('NoPermissionError');
120113
done();

core/test/integration/api/api_mail_spec.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
/*globals describe, before, beforeEach, afterEach, it */
2+
/*jshint expr:true*/
23
var testUtils = require('../../utils'),
34
should = require('should'),
45

56
// Stuff we are testing
67
permissions = require('../../../server/permissions'),
7-
MailAPI = require('../../../server/api/mail');
8-
8+
MailAPI = require('../../../server/api/mail'),
9+
mailData = {
10+
mail: [{
11+
message: {
12+
to: 'joe@example.com',
13+
subject: 'testemail',
14+
html: '<p>This</p>'
15+
},
16+
options: {}
17+
}]
18+
};
919

1020
describe('Mail API', function () {
11-
var mailData = {
12-
mail: [{
13-
message: {
14-
to: 'joe@example.com',
15-
subject: 'testemail',
16-
html: '<p>This</p>'
17-
},
18-
options: {}
19-
}]
20-
};
21+
// Keep the DB clean
22+
before(testUtils.teardown);
23+
afterEach(testUtils.teardown);
2124

22-
before(function (done) {
25+
beforeEach(function (done) {
2326
testUtils.clearData()
2427
.then(function () {
2528
return testUtils.initData();
@@ -32,13 +35,7 @@ describe('Mail API', function () {
3235
}).catch(done);
3336
});
3437

35-
36-
afterEach(function (done) {
37-
testUtils.clearData().then(function () {
38-
done();
39-
}).catch(done);
40-
});
41-
38+
should.exist(MailAPI);
4239

4340
it('return correct failure message', function (done) {
4441
MailAPI.send(mailData, {context: {internal: true}}).then(function (response) {

core/test/integration/api/api_notifications_spec.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
/*globals describe, before, beforeEach, afterEach, it */
2-
var testUtils = require('../../utils'),
3-
should = require('should'),
2+
/*jshint expr:true*/
3+
var testUtils = require('../../utils'),
4+
should = require('should'),
45

56
// Stuff we are testing
6-
permissions = require('../../../server/permissions'),
7-
DataGenerator = require('../../utils/fixtures/data-generator'),
7+
permissions = require('../../../server/permissions'),
88
NotificationsAPI = require('../../../server/api/notifications');
99

1010
describe('Notifications API', function () {
1111

12-
before(function (done) {
13-
testUtils.clearData().then(function () {
14-
done();
15-
}).catch(done);
16-
});
12+
// Keep the DB clean
13+
before(testUtils.teardown);
14+
afterEach(testUtils.teardown);
1715

1816
beforeEach(function (done) {
1917
testUtils.initData()
@@ -26,11 +24,7 @@ describe('Notifications API', function () {
2624
}).catch(done);
2725
});
2826

29-
afterEach(function (done) {
30-
testUtils.clearData().then(function () {
31-
done();
32-
}).catch(done);
33-
});
27+
should.exist(NotificationsAPI);
3428

3529
it('can add, adds defaults (internal)', function (done) {
3630
var msg = {
@@ -102,7 +96,7 @@ describe('Notifications API', function () {
10296
type: 'error', // this can be 'error', 'success', 'warn' and 'info'
10397
message: 'This is an error' // A string. Should fit in one line.
10498
};
105-
NotificationsAPI.add({ notifications: [msg] }, {context: {internal: true}}).then(function (notification) {
99+
NotificationsAPI.add({ notifications: [msg] }, {context: {internal: true}}).then(function () {
106100
NotificationsAPI.browse({context: {internal: true}}).then(function (results) {
107101
should.exist(results);
108102
should.exist(results.notifications);
@@ -118,7 +112,7 @@ describe('Notifications API', function () {
118112
type: 'error', // this can be 'error', 'success', 'warn' and 'info'
119113
message: 'This is an error' // A string. Should fit in one line.
120114
};
121-
NotificationsAPI.add({ notifications: [msg] }, {context: {internal: true}}).then(function (notification) {
115+
NotificationsAPI.add({ notifications: [msg] }, {context: {internal: true}}).then(function () {
122116
NotificationsAPI.browse({context: {user: 1}}).then(function (results) {
123117
should.exist(results);
124118
should.exist(results.notifications);

0 commit comments

Comments
 (0)