Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alino committed Sep 5, 2018
1 parent c064442 commit 0907a73
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 17 deletions.
23 changes: 23 additions & 0 deletions api/policies/isResourceOwner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var constants = require('../../constants');

module.exports = function(req, res, next) {

const root_api_key = constants.ROOT_USER_API_KEY;

console.log('checking if is resource owner');
Account.findOne({ token: req.query.access_token || req.get('Authorization').replace('Bearer ', '') })
.then((account) => {
if (!account) { return res.serverError('Auth error: No account found with this token.') }
if (account.token == root_api_key) return next();
return Job.findOne({ ownerId: account.id, id: req.param('id') });
})
.then((job) => {
if (job) return next();
})
.catch((err) => {
return res.forbidden('You are not permitted to perform this action. Only localhost account can do this.');
})



};
13 changes: 13 additions & 0 deletions api/policies/isRootUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var constants = require('../../constants');

module.exports = function(req, res, next) {

const root_api_key = constants.ROOT_USER_API_KEY;

console.log('checking if is localhost account');
if (req.query.access_token == root_api_key || req.get('Authorization') == `Bearer ${root_api_key}`) {
return next();
}

return res.forbidden('You are not permitted to perform this action. Only localhost account can do this.');
};
16 changes: 2 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
*/

require('ts-node/register');
var constants = require('./constants');


var createLocalHostAccount = require('./functions').createLocalHostAccount;

// Ensure we're in the project directory, so cwd-relative paths work as expected
// no matter where we actually lift from.
Expand Down Expand Up @@ -63,16 +61,6 @@ try {
// Start server
sails.lift(rc('sails'));

// create default localhost account.
sails.on('ready', () => {
const defaultLocalHostAcc = {
hostname: constants.ROOT_USER_HOSTNAME,
api_key: constants.ROOT_USER_API_KEY
}
Account.findOrCreate({ hostname: defaultLocalHostAcc.hostname }, defaultLocalHostAcc)
.exec((err, recordFound, newRecord) => {
if (err) console.error(err);
if (recordFound) console.log('default localhost account already exist.');
if (newRecord) console.log('created a default localhost account.');
});
createLocalHostAccount();
});
19 changes: 19 additions & 0 deletions functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var constants = require('./constants');

function createLocalHostAccount(callback) {
const defaultLocalHostAcc = {
hostname: constants.ROOT_USER_HOSTNAME,
api_key: constants.ROOT_USER_API_KEY
}
Account.findOrCreate({ hostname: defaultLocalHostAcc.hostname }, defaultLocalHostAcc)
.exec((err, recordFound, newRecord) => {
if (err) console.error(err);
if (recordFound) console.log('default localhost account already exist.');
if (newRecord) console.log('created a default localhost account.');
if (callback) callback();
});
}

module.exports = {
createLocalHostAccount
}
1 change: 1 addition & 0 deletions test/bootstrap.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var sails = require('sails');
var constants = require('../constants');

before(function(done) {

Expand Down
5 changes: 4 additions & 1 deletion test/integration/controllers/AssetController.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var request = require('supertest');
var expect = require('expect');
var _ = require('lodash');
var constants = require('../../../constants');
var createLocalHostAccount = require('../../../functions').createLocalHostAccount;

describe('AssetController', function () {

Expand All @@ -12,7 +14,7 @@ describe('AssetController', function () {
submitter: 'symfonie.com/123'
}).exec((err, res) => {
if (err) console.error(err);
done();
createLocalHostAccount(done);
});

sails.once('hook:orm:reloaded', cb);
Expand All @@ -22,6 +24,7 @@ describe('AssetController', function () {
it('should handle file upload and asset creation', function (done) {
request(sails.hooks.http.app)
.post('/jobs/1/assets/uploadfile')
.set('Authorization', 'Bearer ' + constants.ROOT_USER_API_KEY)
.field('sourceLanguage', 'en')
.field('encoding', 'utf8')
.attach('asset', 'test/fixtures/testAssetFile.txt')
Expand Down
7 changes: 6 additions & 1 deletion test/integration/controllers/JobController.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
var request = require('supertest');
var expect = require('expect');
var constants = require('../../../constants');
var createLocalHostAccount = require('../../../functions').createLocalHostAccount;

describe('JobController', function() {
beforeEach((done) => {
sails.once('hook:orm:reloaded', done);
sails.once('hook:orm:reloaded', () => {
createLocalHostAccount(done);
});
sails.emit('hook:orm:reload');
})

describe('GET /jobs', function() {
it('should return all Jobs', function (done) {
request(sails.hooks.http.app)
.get('/jobs')
.set('Authorization', 'Bearer ' + constants.ROOT_USER_API_KEY)
.expect(200)
.then((response) => {
expect(response.body).toEqual([]);
Expand Down
5 changes: 4 additions & 1 deletion test/integration/controllers/TaskController.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var request = require('supertest');
var expect = require('expect');
var _ = require('lodash');
var constants = require('../../../constants');
var createLocalHostAccount = require('../../../functions').createLocalHostAccount;

describe('TaskController', function () {
const fixtures = {
Expand All @@ -27,7 +29,7 @@ describe('TaskController', function () {
}).then(() => {
return Task.create(fixtures.tasks[0])
}).then(() => {
done()
createLocalHostAccount(done);
});

sails.once('hook:orm:reloaded', cb);
Expand All @@ -39,6 +41,7 @@ describe('TaskController', function () {

request(sails.hooks.http.app)
.post('/assets/1/tasks/1/uploaddeliverable')
.set('Authorization', 'Bearer ' + constants.ROOT_USER_API_KEY)
.attach('deliverable', 'test/fixtures/testDeliverableFile.txt')
.expect(200)
.then((response) => {
Expand Down

0 comments on commit 0907a73

Please sign in to comment.