Skip to content

Commit

Permalink
Refactor fixture use in tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ErisDS committed Jul 23, 2014
1 parent 63efa54 commit 337713c
Show file tree
Hide file tree
Showing 34 changed files with 1,516 additions and 1,796 deletions.
18 changes: 13 additions & 5 deletions core/server/data/migration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ backupDatabase = function backupDatabase() {
};

// Check for whether data is needed to be bootstrapped or not
init = function () {
init = function (tablesOnly) {
tablesOnly = tablesOnly || false;

var self = this;
// There are 4 possibilities:
// 1. The database exists and is up-to-date
Expand Down Expand Up @@ -92,7 +94,7 @@ init = function () {
// 4. The database has not yet been created
// Bring everything up from initial version.
logInfo('Database initialisation required for version ' + versioning.getDefaultDatabaseVersion());
return self.migrateUpFreshDb();
return self.migrateUpFreshDb(tablesOnly);
}
// 3. The database exists but the currentVersion setting does not or cannot be understood
// In this case the setting was missing or there was some other problem
Expand All @@ -113,15 +115,21 @@ reset = function () {
};

// Only do this if we have no database at all
migrateUpFreshDb = function () {
var tables = _.map(schemaTables, function (table) {
migrateUpFreshDb = function (tablesOnly) {
var tableSequence,
tables = _.map(schemaTables, function (table) {
return function () {
logInfo('Creating table: ' + table);
return utils.createTable(table);
};
});
logInfo('Creating tables...');
return sequence(tables).then(function () {
tableSequence = sequence(tables);

if (tablesOnly) {
return tableSequence;
}
return tableSequence.then(function () {
// Load the fixtures
return fixtures.populate();
}).then(function () {
Expand Down
41 changes: 14 additions & 27 deletions core/test/functional/routes/api/db_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*global describe, it, before, after */
/*jshint expr:true*/
var supertest = require('supertest'),
express = require('express'),
should = require('should'),
Expand All @@ -11,48 +12,34 @@ var supertest = require('supertest'),


describe('DB API', function () {
var user = testUtils.DataGenerator.forModel.users[0],
accesstoken = '';
var accesstoken = '';

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

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

testUtils.clearData()
.then(function () {
return testUtils.initData();
})
.then(function () {
return testUtils.insertDefaultFixtures();
})
.then(function () {
request.post('/ghost/api/v0.1/authentication/token/')
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
var jsonResponse = res.body;
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
accesstoken = jsonResponse.access_token;
return done();
});
}).catch(done);
}).then(function () {
return testUtils.doAuth(request);
}).then(function (token) {
accesstoken = token;
done();
}).catch(function (e) {
console.log('Ghost Error: ', e);
console.log(e.stack);
});
});

after(function () {
httpServer.close();
after(function (done) {
testUtils.clearData().then(function () {
httpServer.close();
done();
});
});

it('attaches the Content-Disposition header on export', function (done) {
Expand Down
51 changes: 17 additions & 34 deletions core/test/functional/routes/api/notifications_test.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,38 @@
var supertest = require('supertest'),
/*global describe, it, before, after */
/*jshint expr:true*/
var testUtils = require('../../../utils'),
supertest = require('supertest'),
express = require('express'),
should = require('should'),
_ = require('lodash'),
testUtils = require('../../../utils'),

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

httpServer,
request,
agent;
request;

describe('Notifications API', function () {
var user = testUtils.DataGenerator.forModel.users[0],
accesstoken = '';
var accesstoken = '';

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

// starting ghost automatically populates the db
// TODO: prevent db init, and manage bringing up the DB with fixtures ourselves
ghost({app: app}).then(function (_httpServer) {
httpServer = _httpServer;

request = supertest.agent(app);

testUtils.clearData()
.then(function () {
return testUtils.initData();
})
.then(function () {
return testUtils.insertDefaultFixtures();
})
.then(function () {
request.post('/ghost/api/v0.1/authentication/token/')
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
var jsonResponse = res.body;
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
accesstoken = jsonResponse.access_token;
return done();
});
}, done);
}).otherwise(function (e) {
}).then(function () {
return testUtils.doAuth(request);
}).then(function (token) {
accesstoken = token;
done();
}).catch(function (e) {
console.log('Ghost Error: ', e);
console.log(e.stack);
});
});
});

after(function () {
httpServer.close();
Expand Down Expand Up @@ -105,7 +88,7 @@ describe('Notifications API', function () {
if (err) {
return done(err);
}

var location = res.headers['location'];

var jsonResponse = res.body;
Expand All @@ -116,7 +99,7 @@ describe('Notifications API', function () {
jsonResponse.notifications[0].type.should.equal(newNotification.type);
jsonResponse.notifications[0].message.should.equal(newNotification.message);
jsonResponse.notifications[0].status.should.equal(newNotification.status);

// begin delete test
request.del(location)
.set('Authorization', 'Bearer ' + accesstoken)
Expand Down
51 changes: 18 additions & 33 deletions core/test/functional/routes/api/posts_test.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,48 @@
/*global describe, it, before, after */
var supertest = require('supertest'),
express = require('express'),
/*jshint expr:true*/
var testUtils = require('../../../utils'),
should = require('should'),
supertest = require('supertest'),
express = require('express'),
_ = require('lodash'),
testUtils = require('../../../utils'),

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

httpServer,
request,
agent;
request;


describe('Post API', function () {
var user = testUtils.DataGenerator.forModel.users[0],
accesstoken = '';
var accesstoken = '';

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

// starting ghost automatically populates the db
// TODO: prevent db init, and manage bringing up the DB with fixtures ourselves
ghost({app: app}).then(function (_httpServer) {
httpServer = _httpServer;

request = supertest.agent(app);

testUtils.clearData()
.then(function () {
return testUtils.initData();
})
.then(function () {
return testUtils.insertDefaultFixtures();
})
.then(function () {
request.post('/ghost/api/v0.1/authentication/token/')
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
var jsonResponse = res.body;
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
accesstoken = jsonResponse.access_token;
return done();
});
}).catch(done);
}).then(function () {
return testUtils.doAuth(request, 'posts');
}).then(function (token) {
accesstoken = token;
done();
}).catch(function (e) {
console.log('Ghost Error: ', e);
console.log(e.stack);
});
});

after(function () {
httpServer.close();
after(function (done) {
testUtils.clearData().then(function () {
httpServer.close();
done();
});
});


describe('Browse', function () {

it('retrieves all published posts only by default', function (done) {
Expand Down
53 changes: 19 additions & 34 deletions core/test/functional/routes/api/settings_test.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
/*global describe, it, before, after */
var supertest = require('supertest'),
express = require('express'),
/*jshint expr:true*/
var testUtils = require('../../../utils'),
should = require('should'),
_ = require('lodash'),
testUtils = require('../../../utils'),
supertest = require('supertest'),
express = require('express'),

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

httpServer,
request,
agent;

request;

describe('Settings API', function () {
var user = testUtils.DataGenerator.forModel.users[0],
accesstoken = '';
var accesstoken = '';

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

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

testUtils.clearData()
.then(function () {
return testUtils.initData();
})
.then(function () {
return testUtils.insertDefaultFixtures();
})
.then(function () {
request.post('/ghost/api/v0.1/authentication/token/')
.send({ grant_type: "password", username: user.email, password: user.password, client_id: "ghost-admin"})
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
var jsonResponse = res.body;
testUtils.API.checkResponse(jsonResponse, 'accesstoken');
accesstoken = jsonResponse.access_token;
return done();
});
}).catch(done);
}).then(function () {
return testUtils.doAuth(request);
}).then(function (token) {
accesstoken = token;
done();
}).catch(function (e) {
console.log('Ghost Error: ', e);
console.log(e.stack);
});
});

after(function () {
httpServer.close();
after(function (done) {
testUtils.clearData().then(function () {
httpServer.close();
done();
});
});

// TODO: currently includes values of type=core
Expand Down
Loading

0 comments on commit 337713c

Please sign in to comment.