Skip to content

Commit 337713c

Browse files
committed
Refactor fixture use in tests
no issue - Refactor all integration tests to specify and load ONLY the fixtures they require to run, rather than initialising the whole kit-and-kaboodle for every single test which takes FOREVER. - Refactor the route tests to share a doAuth function, and also specify additional fixtures required - Move import and export unit tests, which are actually integration tests (they touch the DB) - Comment out most of the permissions unit tests for now as they need more stubs/mocks so as to not touch the DB Still todo: - prevent default DB initialisation in route tests, and specify all fixtures requires as per the integration tests - fix up the unit/permissions_spec
1 parent 63efa54 commit 337713c

34 files changed

+1516
-1796
lines changed

core/server/data/migration/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ backupDatabase = function backupDatabase() {
5353
};
5454

5555
// Check for whether data is needed to be bootstrapped or not
56-
init = function () {
56+
init = function (tablesOnly) {
57+
tablesOnly = tablesOnly || false;
58+
5759
var self = this;
5860
// There are 4 possibilities:
5961
// 1. The database exists and is up-to-date
@@ -92,7 +94,7 @@ init = function () {
9294
// 4. The database has not yet been created
9395
// Bring everything up from initial version.
9496
logInfo('Database initialisation required for version ' + versioning.getDefaultDatabaseVersion());
95-
return self.migrateUpFreshDb();
97+
return self.migrateUpFreshDb(tablesOnly);
9698
}
9799
// 3. The database exists but the currentVersion setting does not or cannot be understood
98100
// In this case the setting was missing or there was some other problem
@@ -113,15 +115,21 @@ reset = function () {
113115
};
114116

115117
// Only do this if we have no database at all
116-
migrateUpFreshDb = function () {
117-
var tables = _.map(schemaTables, function (table) {
118+
migrateUpFreshDb = function (tablesOnly) {
119+
var tableSequence,
120+
tables = _.map(schemaTables, function (table) {
118121
return function () {
119122
logInfo('Creating table: ' + table);
120123
return utils.createTable(table);
121124
};
122125
});
123126
logInfo('Creating tables...');
124-
return sequence(tables).then(function () {
127+
tableSequence = sequence(tables);
128+
129+
if (tablesOnly) {
130+
return tableSequence;
131+
}
132+
return tableSequence.then(function () {
125133
// Load the fixtures
126134
return fixtures.populate();
127135
}).then(function () {

core/test/functional/routes/api/db_test.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*global describe, it, before, after */
2+
/*jshint expr:true*/
23
var supertest = require('supertest'),
34
express = require('express'),
45
should = require('should'),
@@ -11,48 +12,34 @@ var supertest = require('supertest'),
1112

1213

1314
describe('DB API', function () {
14-
var user = testUtils.DataGenerator.forModel.users[0],
15-
accesstoken = '';
15+
var accesstoken = '';
1616

1717
before(function (done) {
1818
var app = express();
1919
app.set('disableLoginLimiter', true);
2020

21+
// starting ghost automatically populates the db
22+
// TODO: prevent db init, and manage bringing up the DB with fixtures ourselves
2123
ghost({app: app}).then(function (_httpServer) {
2224
httpServer = _httpServer;
23-
// request = supertest(app);
2425
request = supertest.agent(app);
2526

26-
testUtils.clearData()
27-
.then(function () {
28-
return testUtils.initData();
29-
})
30-
.then(function () {
31-
return testUtils.insertDefaultFixtures();
32-
})
33-
.then(function () {
34-
request.post('/ghost/api/v0.1/authentication/token/')
35-
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
36-
.expect('Content-Type', /json/)
37-
.expect(200)
38-
.end(function (err, res) {
39-
if (err) {
40-
return done(err);
41-
}
42-
var jsonResponse = res.body;
43-
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
44-
accesstoken = jsonResponse.access_token;
45-
return done();
46-
});
47-
}).catch(done);
27+
}).then(function () {
28+
return testUtils.doAuth(request);
29+
}).then(function (token) {
30+
accesstoken = token;
31+
done();
4832
}).catch(function (e) {
4933
console.log('Ghost Error: ', e);
5034
console.log(e.stack);
5135
});
5236
});
5337

54-
after(function () {
55-
httpServer.close();
38+
after(function (done) {
39+
testUtils.clearData().then(function () {
40+
httpServer.close();
41+
done();
42+
});
5643
});
5744

5845
it('attaches the Content-Disposition header on export', function (done) {

core/test/functional/routes/api/notifications_test.js

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
1-
var supertest = require('supertest'),
1+
/*global describe, it, before, after */
2+
/*jshint expr:true*/
3+
var testUtils = require('../../../utils'),
4+
supertest = require('supertest'),
25
express = require('express'),
36
should = require('should'),
4-
_ = require('lodash'),
5-
testUtils = require('../../../utils'),
67

78
ghost = require('../../../../../core'),
89

910
httpServer,
10-
request,
11-
agent;
11+
request;
1212

1313
describe('Notifications API', function () {
14-
var user = testUtils.DataGenerator.forModel.users[0],
15-
accesstoken = '';
14+
var accesstoken = '';
1615

1716
before(function (done) {
1817
var app = express();
1918
app.set('disableLoginLimiter', true);
2019

20+
// starting ghost automatically populates the db
21+
// TODO: prevent db init, and manage bringing up the DB with fixtures ourselves
2122
ghost({app: app}).then(function (_httpServer) {
2223
httpServer = _httpServer;
23-
2424
request = supertest.agent(app);
2525

26-
testUtils.clearData()
27-
.then(function () {
28-
return testUtils.initData();
29-
})
30-
.then(function () {
31-
return testUtils.insertDefaultFixtures();
32-
})
33-
.then(function () {
34-
request.post('/ghost/api/v0.1/authentication/token/')
35-
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
36-
.expect('Content-Type', /json/)
37-
.expect(200)
38-
.end(function (err, res) {
39-
if (err) {
40-
return done(err);
41-
}
42-
var jsonResponse = res.body;
43-
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
44-
accesstoken = jsonResponse.access_token;
45-
return done();
46-
});
47-
}, done);
48-
}).otherwise(function (e) {
26+
}).then(function () {
27+
return testUtils.doAuth(request);
28+
}).then(function (token) {
29+
accesstoken = token;
30+
done();
31+
}).catch(function (e) {
4932
console.log('Ghost Error: ', e);
5033
console.log(e.stack);
5134
});
52-
});
35+
});
5336

5437
after(function () {
5538
httpServer.close();
@@ -105,7 +88,7 @@ describe('Notifications API', function () {
10588
if (err) {
10689
return done(err);
10790
}
108-
91+
10992
var location = res.headers['location'];
11093

11194
var jsonResponse = res.body;
@@ -116,7 +99,7 @@ describe('Notifications API', function () {
11699
jsonResponse.notifications[0].type.should.equal(newNotification.type);
117100
jsonResponse.notifications[0].message.should.equal(newNotification.message);
118101
jsonResponse.notifications[0].status.should.equal(newNotification.status);
119-
102+
120103
// begin delete test
121104
request.del(location)
122105
.set('Authorization', 'Bearer ' + accesstoken)

core/test/functional/routes/api/posts_test.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,48 @@
11
/*global describe, it, before, after */
2-
var supertest = require('supertest'),
3-
express = require('express'),
2+
/*jshint expr:true*/
3+
var testUtils = require('../../../utils'),
44
should = require('should'),
5+
supertest = require('supertest'),
6+
express = require('express'),
57
_ = require('lodash'),
6-
testUtils = require('../../../utils'),
78

89
ghost = require('../../../../../core'),
910

1011
httpServer,
11-
request,
12-
agent;
12+
request;
1313

1414

1515
describe('Post API', function () {
16-
var user = testUtils.DataGenerator.forModel.users[0],
17-
accesstoken = '';
16+
var accesstoken = '';
1817

1918
before(function (done) {
2019
var app = express();
2120
app.set('disableLoginLimiter', true);
2221

22+
// starting ghost automatically populates the db
23+
// TODO: prevent db init, and manage bringing up the DB with fixtures ourselves
2324
ghost({app: app}).then(function (_httpServer) {
2425
httpServer = _httpServer;
25-
2626
request = supertest.agent(app);
2727

28-
testUtils.clearData()
29-
.then(function () {
30-
return testUtils.initData();
31-
})
32-
.then(function () {
33-
return testUtils.insertDefaultFixtures();
34-
})
35-
.then(function () {
36-
request.post('/ghost/api/v0.1/authentication/token/')
37-
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
38-
.expect('Content-Type', /json/)
39-
.expect(200)
40-
.end(function (err, res) {
41-
if (err) {
42-
return done(err);
43-
}
44-
var jsonResponse = res.body;
45-
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
46-
accesstoken = jsonResponse.access_token;
47-
return done();
48-
});
49-
}).catch(done);
28+
}).then(function () {
29+
return testUtils.doAuth(request, 'posts');
30+
}).then(function (token) {
31+
accesstoken = token;
32+
done();
5033
}).catch(function (e) {
5134
console.log('Ghost Error: ', e);
5235
console.log(e.stack);
5336
});
5437
});
5538

56-
after(function () {
57-
httpServer.close();
39+
after(function (done) {
40+
testUtils.clearData().then(function () {
41+
httpServer.close();
42+
done();
43+
});
5844
});
5945

60-
6146
describe('Browse', function () {
6247

6348
it('retrieves all published posts only by default', function (done) {

core/test/functional/routes/api/settings_test.js

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,44 @@
11
/*global describe, it, before, after */
2-
var supertest = require('supertest'),
3-
express = require('express'),
2+
/*jshint expr:true*/
3+
var testUtils = require('../../../utils'),
44
should = require('should'),
5-
_ = require('lodash'),
6-
testUtils = require('../../../utils'),
5+
supertest = require('supertest'),
6+
express = require('express'),
77

88
ghost = require('../../../../../core'),
99

1010
httpServer,
11-
request,
12-
agent;
13-
11+
request;
1412

1513
describe('Settings API', function () {
16-
var user = testUtils.DataGenerator.forModel.users[0],
17-
accesstoken = '';
14+
var accesstoken = '';
1815

1916
before(function (done) {
2017
var app = express();
21-
app.set('disableLoginLimiter', true);
18+
app.set('disableLoginLimiter', true);
2219

20+
// starting ghost automatically populates the db
21+
// TODO: prevent db init, and manage bringing up the DB with fixtures ourselves
2322
ghost({app: app}).then(function (_httpServer) {
2423
httpServer = _httpServer;
2524
request = supertest.agent(app);
2625

27-
testUtils.clearData()
28-
.then(function () {
29-
return testUtils.initData();
30-
})
31-
.then(function () {
32-
return testUtils.insertDefaultFixtures();
33-
})
34-
.then(function () {
35-
request.post('/ghost/api/v0.1/authentication/token/')
36-
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
37-
.expect('Content-Type', /json/)
38-
.expect(200)
39-
.end(function (err, res) {
40-
if (err) {
41-
return done(err);
42-
}
43-
var jsonResponse = res.body;
44-
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
45-
accesstoken = jsonResponse.access_token;
46-
return done();
47-
});
48-
}).catch(done);
26+
}).then(function () {
27+
return testUtils.doAuth(request);
28+
}).then(function (token) {
29+
accesstoken = token;
30+
done();
4931
}).catch(function (e) {
5032
console.log('Ghost Error: ', e);
5133
console.log(e.stack);
5234
});
5335
});
5436

55-
after(function () {
56-
httpServer.close();
37+
after(function (done) {
38+
testUtils.clearData().then(function () {
39+
httpServer.close();
40+
done();
41+
});
5742
});
5843

5944
// TODO: currently includes values of type=core

0 commit comments

Comments
 (0)