Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Commit

Permalink
Fix the initial superusers not having auth tokens assigned.
Browse files Browse the repository at this point in the history
Fixes NREL/api-umbrella#95

We're no longer wiping the mongodb as part of our test suite startup,
since this would wipe these initial admin accounts. I don't think
wiping the database should actually be necessary in these integration
tests, since before boot, we're actually wiping the whole data directory
that the embedded database would use.
  • Loading branch information
GUI committed Sep 24, 2015
1 parent 6fc65e7 commit fa10065
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
23 changes: 20 additions & 3 deletions lib/models/admin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
'use strict';

var mongoose = global.mongoose || require('mongoose');
var crypto = require('crypto'),
mongoose = global.mongoose || require('mongoose');

module.exports = mongoose.model('Admin', new mongoose.Schema({
var schema = new mongoose.Schema({
_id: mongoose.Schema.Types.Mixed,
username: {
type: String,
index: { unique: true },
},
superuser: Boolean,
}, { collection: 'admins' }));
authentication_token: String,
}, { collection: 'admins' });

schema.pre('validate', function generateApiKey(next) {
if(!this.authentication_token) {
// Create a random api key consisting of only A-Za-z0-9.
this.authentication_token = crypto
.randomBytes(64)
.toString('base64')
.replace(/[+\/=]/g, '')
.slice(0, 40);
}

next();
});

module.exports = mongoose.model('Admin', schema);
3 changes: 3 additions & 0 deletions test/config/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ varnish:
port: 13011
web:
port: 13012
admin:
initial_superusers:
- [email protected]
router:
api_backends:
port: 13011
Expand Down
17 changes: 17 additions & 0 deletions test/factories/admins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

require('../test_helper');

var Factory = require('factory-lady'),
mongoose = require('mongoose'),
uuid = require('node-uuid');

require('../../lib/models/admin');

function generateId(callback) {
callback(uuid.v4());
}

Factory.define('admin', mongoose.testConnection.model('Admin'), {
_id: generateId,
});
17 changes: 17 additions & 0 deletions test/integration/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

require('../test_helper');

var mongoose = require('mongoose');

describe('boot', function() {
it('generates the initial superusers with tokens', function(done) {
this.timeout(9000000);
mongoose.testConnection.model('Admin').find({}, function(error, admins) {
should.not.exist(error);
admins.length.should.eql(1);
admins[0].authentication_token.should.match(/^[a-zA-Z0-9]{40}$/);
done();
});
});
});
10 changes: 3 additions & 7 deletions test/support/database_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ var config = apiUmbrellaConfig.load(path.resolve(__dirname, '../config/test.yml'

mongoose.testConnection = mongoose.createConnection();

// Drop the mongodb database.
// Open the mongodb database.
before(function mongoOpen(done) {
this.timeout(20000);

mongoose.testConnection.on('connected', function() {
// Drop the whole database, since that properly blocks for any active
// connections. The database will get re-created on demand.
mongoose.testConnection.db.dropDatabase(done);
});

// Since we're calling createConnection earlier on, and then opening the
// connection here, we need to explicitly handle whether we should call
// openSet for a replicaset based URL.
Expand All @@ -31,6 +25,8 @@ before(function mongoOpen(done) {
} else {
mongoose.testConnection.open(config.get('mongodb.url'), config.get('mongodb.options'));
}

done();
});

// Wipe the redis data.
Expand Down
1 change: 1 addition & 0 deletions test/support/factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

require('../test_helper');

require('../factories/admins');
require('../factories/api_users');
require('../factories/config_versions');

0 comments on commit fa10065

Please sign in to comment.