This repository has been archived by the owner on Jan 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the initial superusers not having auth tokens assigned.
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
Showing
6 changed files
with
61 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ varnish: | |
port: 13011 | ||
web: | ||
port: 13012 | ||
admin: | ||
initial_superusers: | ||
- [email protected] | ||
router: | ||
api_backends: | ||
port: 13011 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters