Skip to content
Permalink
Browse files

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
  • Loading branch information...
ErisDS committed Jul 21, 2014
1 parent 63efa54 commit 337713ce63759eaf6bb265263d2ce7456e3839b6
Showing with 1,516 additions and 1,796 deletions.
  1. +13 βˆ’5 core/server/data/migration/index.js
  2. +14 βˆ’27 core/test/functional/routes/api/db_test.js
  3. +17 βˆ’34 core/test/functional/routes/api/notifications_test.js
  4. +18 βˆ’33 core/test/functional/routes/api/posts_test.js
  5. +19 βˆ’34 core/test/functional/routes/api/settings_test.js
  6. +23 βˆ’38 core/test/functional/routes/api/slugs_test.js
  7. +19 βˆ’34 core/test/functional/routes/api/tags_test.js
  8. +18 βˆ’31 core/test/functional/routes/api/users_test.js
  9. +5 βˆ’5 core/test/functional/routes/frontend_test.js
  10. +49 βˆ’76 core/test/integration/api/api_authentication_spec.js
  11. +36 βˆ’37 core/test/integration/api/api_db_spec.js
  12. +3 βˆ’16 core/test/integration/api/api_mail_spec.js
  13. +39 βˆ’45 core/test/integration/api/api_notifications_spec.js
  14. +1 βˆ’9 core/test/integration/api/api_posts_spec.js
  15. +1 βˆ’17 core/test/integration/api/api_settings_spec.js
  16. +1 βˆ’10 core/test/integration/api/api_slugs_spec.js
  17. +5 βˆ’22 core/test/integration/api/api_tags_spec.js
  18. +34 βˆ’42 core/test/integration/api/api_themes_spec.js
  19. +186 βˆ’260 core/test/integration/api/api_users_spec.js
  20. +10 βˆ’24 core/test/{unit β†’ integration}/export_spec.js
  21. +127 βˆ’157 core/test/{unit β†’ integration}/import_spec.js
  22. +2 βˆ’11 core/test/integration/model/model_app_fields_spec.js
  23. +2 βˆ’11 core/test/integration/model/model_app_settings_spec.js
  24. +7 βˆ’17 core/test/integration/model/model_apps_spec.js
  25. +91 βˆ’7 core/test/integration/model/model_permissions_spec.js
  26. +11 βˆ’21 core/test/integration/model/model_posts_spec.js
  27. +2 βˆ’6 core/test/integration/model/model_roles_spec.js
  28. +2 βˆ’7 core/test/integration/model/model_settings_spec.js
  29. +6 βˆ’12 core/test/integration/model/model_tags_spec.js
  30. +30 βˆ’47 core/test/integration/model/model_users_spec.js
  31. +14 βˆ’27 core/test/integration/update_check_spec.js
  32. +259 βˆ’417 core/test/unit/permissions_spec.js
  33. +130 βˆ’42 core/test/utils/fixtures/data-generator.js
  34. +322 βˆ’215 core/test/utils/index.js
@@ -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
@@ -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
@@ -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 () {
@@ -1,4 +1,5 @@
/*global describe, it, before, after */
/*jshint expr:true*/
var supertest = require('supertest'),
express = require('express'),
should = require('should'),
@@ -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) {
@@ -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();
@@ -105,7 +88,7 @@ describe('Notifications API', function () {
if (err) {
return done(err);
}

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

var jsonResponse = res.body;
@@ -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)
@@ -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) {
@@ -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

0 comments on commit 337713c

Please sign in to comment.
You can’t perform that action at this time.