Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d2lam committed Nov 19, 2018
1 parent c43e3c8 commit 0aeb8f0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 47 deletions.
1 change: 1 addition & 0 deletions bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const pipelineFactory = Models.PipelineFactory.getInstance({

// Setup Executor
const executorConfig = config.get('executor');

executorConfig[executorConfig.plugin].options.pipelineFactory = pipelineFactory;
const ExecutorPlugin = require(`screwdriver-executor-${executorConfig.plugin}`);
const executor = new ExecutorPlugin(Object.assign(
Expand Down
4 changes: 2 additions & 2 deletions lib/registerPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function registerDefaultPlugins(server, callback) {
'../plugins/logging',
'../plugins/swagger',
'../plugins/validator',
'../plugins/isAdmin',
'../plugins/template-validator',
'../plugins/command-validator'
'../plugins/command-validator',
'../plugins/isAdmin'
].map(plugin => require(plugin));

async.eachSeries(plugins, (plugin, next) => {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@
"screwdriver-config-parser": "^4.6.1",
"screwdriver-coverage-bookend": "^1.0.1",
"screwdriver-coverage-sonar": "^1.0.10",
"screwdriver-data-schema": "^18.33.5",
"screwdriver-data-schema": "^18.34.1",
"screwdriver-datastore-sequelize": "^5.3.0",
"screwdriver-executor-docker": "^4.0.0",
"screwdriver-executor-k8s": "^13.2.9",
"screwdriver-executor-k8s-vm": "^2.7.4",
"screwdriver-executor-k8s-vm": "^2.7.5",
"screwdriver-executor-nomad": "^1.0.13",
"screwdriver-executor-queue": "^2.2.8",
"screwdriver-executor-router": "^1.0.7",
"screwdriver-models": "^27.15.6",
"screwdriver-executor-router": "^1.0.8",
"screwdriver-models": "^27.15.8",
"screwdriver-notifications-email": "^1.1.9",
"screwdriver-notifications-slack": "^2.1.8",
"screwdriver-scm-github": "^8.0.10",
"screwdriver-scm-gitlab": "^1.1.0",
"screwdriver-scm-router": "^3.1.1",
"screwdriver-scm-router": "^3.2.0",
"screwdriver-template-validator": "^3.0.5",
"screwdriver-workflow-parser": "^1.6.1",
"sqlite3": "^4.0.3",
Expand Down
43 changes: 20 additions & 23 deletions plugins/isAdmin.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
'use strict';

const boom = require('boom');
// const joi = require('joi');
// const schema = require('screwdriver-data-schema');
// const querySchema = joi.object({
// pipelineId: joi.reach(schema.models.pipeline.base, 'id'),
// eventId: joi.reach(schema.models.event.base, 'id'),
// jobId: joi.reach(schema.models.pipeline.job, 'id')
// }).max(1).min(1);
const joi = require('joi');
const schema = require('screwdriver-data-schema');

exports.register = (server, options, next) => {
server.route({
method: 'GET',
path: '/isAdmin',
config: {
description: 'Check if a user is admin of a single pipeline',
description: 'Check if a user is admin of a pipeline, event, or job',
notes: 'Returns true or false',
tags: ['api', 'pipelines'],
tags: ['api'],
auth: {
strategies: ['token'],
scope: ['user']
Expand Down Expand Up @@ -51,21 +46,23 @@ exports.register = (server, options, next) => {
return Promise.all([
pipelineFactory.get(pid),
userFactory.get({ username, scmContext })
])
// get the pipeline given its ID and the user
.then(([pipeline, user]) => {
if (!pipeline) {
throw boom.notFound(`Pipeline ${pid} does not exist`);
}
]).then(([pipeline, user]) => {
if (!pipeline) {
throw boom.notFound(`Pipeline ${pid} does not exist`);
}

// ask the user for permissions on this repo
return user.getPermissions(pipeline.scmUri)
.then(permissions => reply(permissions.admin));
});
}).catch(err => reply(boom.boomify(err)))
// validate: {
// query: querySchema
// }
// ask the user for permissions on this repo
return user.getPermissions(pipeline.scmUri)
.then(permissions => reply(permissions.admin));
});
}).catch(err => reply(boom.boomify(err))),
validate: {
query: joi.object().keys({
pipelineId: joi.reach(schema.models.pipeline.base, 'id'),
eventId: joi.reach(schema.models.event.base, 'id'),
jobId: joi.reach(schema.models.job.base, 'id')
}).max(1).min(1)
}
}
});

Expand Down
3 changes: 2 additions & 1 deletion test/lib/registerPlugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('Register Unit Test Case', () => {
'../plugins/swagger',
'../plugins/validator',
'../plugins/template-validator',
'../plugins/command-validator'
'../plugins/command-validator',
'../plugins/isAdmin'
];
const resourcePlugins = [
'../plugins/auth',
Expand Down
90 changes: 74 additions & 16 deletions test/plugins/isAdmin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const sinon = require('sinon');
const hapi = require('hapi');
const hoek = require('hoek');
const mockery = require('mockery');
const testPipeline = require('./data/pipeline.json');
const pipelineMock = require('./data/pipeline.json');

sinon.assert.expose(assert, { prefix: '' });

Expand All @@ -17,20 +17,19 @@ const getUserMock = (user) => {
return mock;
};

describe.only('isAdmin plugin test', () => {
const pipelineId = 111;
const eventId = 222;
const jobId = 333;
const username = 'testuser';
const scmContext = testPipeline.scmContext;
describe('isAdmin plugin test', () => {
let server;
let plugin;
let pipelineFactoryMock;
let eventFactoryMock;
let jobFactoryMock;
let userFactoryMock;
let pipelineMock;
let userMock;
let server;
let plugin;
const pipelineId = 111;
const eventId = 222;
const jobId = 333;
const username = 'testuser';
const scmContext = pipelineMock.scmContext;

before(() => {
mockery.enable({
Expand All @@ -40,9 +39,8 @@ describe.only('isAdmin plugin test', () => {
});

beforeEach((done) => {
/* eslint-disable global-require */
// eslint-disable-next-line global-require
plugin = require('../../plugins/isAdmin');
/* eslint-enable global-require */

pipelineFactoryMock = {
get: sinon.stub()
Expand All @@ -57,7 +55,7 @@ describe.only('isAdmin plugin test', () => {
get: sinon.stub()
};

pipelineFactoryMock.get.withArgs(pipelineId).resolves(testPipeline);
pipelineFactoryMock.get.withArgs(pipelineId).resolves(pipelineMock);

eventFactoryMock.get.withArgs(eventId).resolves({
id: eventId,
Expand All @@ -70,7 +68,7 @@ describe.only('isAdmin plugin test', () => {
});

userMock = getUserMock({ username, scmContext });
userMock.getPermissions.withArgs(testPipeline.scmUri).resolves({ admin: true });
userMock.getPermissions.withArgs(pipelineMock.scmUri).resolves({ admin: true });
userFactoryMock.get.withArgs({ username, scmContext }).resolves(userMock);
server = new hapi.Server();
server.app = {
Expand All @@ -79,9 +77,19 @@ describe.only('isAdmin plugin test', () => {
jobFactory: jobFactoryMock,
userFactory: userFactoryMock
};

server.connection({
port: 1234
});
server.auth.scheme('custom', () => ({
authenticate: (request, reply) => reply.continue({
credentials: {
scope: ['build']
}
})
}));
server.auth.strategy('token', 'custom');

server.register([{
register: plugin
}], (err) => {
Expand All @@ -103,7 +111,7 @@ describe.only('isAdmin plugin test', () => {
assert.isOk(server.registrations.isAdmin);
});

describe('GET /pipelines/{id}/isAdmin', () => {
describe('GET /isAdmin?pipelineId=', () => {
let options;

beforeEach(() => {
Expand All @@ -120,6 +128,7 @@ describe.only('isAdmin plugin test', () => {

it('returns true for admin', () =>
server.inject(options).then((reply) => {
assert.calledWith(pipelineFactoryMock.get, pipelineId);
assert.calledWith(userMock.getPermissions, pipelineMock.scmUri);
assert.deepEqual(reply.result, true);
assert.equal(reply.statusCode, 200);
Expand All @@ -130,14 +139,15 @@ describe.only('isAdmin plugin test', () => {
userMock.getPermissions.withArgs(pipelineMock.scmUri).resolves({ admin: false });

return server.inject(options).then((reply) => {
assert.calledWith(pipelineFactoryMock.get, pipelineId);
assert.calledWith(userMock.getPermissions, pipelineMock.scmUri);
assert.deepEqual(reply.result, false);
assert.equal(reply.statusCode, 200);
});
});

it('returns 404 for pipeline that does not exist', () => {
pipelineFactoryMock.get.resolves(null);
pipelineFactoryMock.get.withArgs(pipelineId).resolves(null);

return server.inject(options).then((reply) => {
assert.equal(reply.statusCode, 404);
Expand All @@ -153,5 +163,53 @@ describe.only('isAdmin plugin test', () => {
assert.equal(reply.statusCode, 500);
});
});

it('finds pipeline that the event belongs to', () =>
server.inject({
method: 'GET',
url: `/isAdmin?eventId=${eventId}`,
credentials: {
username,
scmContext,
scope: ['user']
}
}).then((reply) => {
assert.calledWith(eventFactoryMock.get, eventId);
assert.calledWith(pipelineFactoryMock.get, pipelineId);
assert.deepEqual(reply.result, true);
assert.equal(reply.statusCode, 200);
})
);

it('finds pipeline that the job belongs to', () =>
server.inject({
method: 'GET',
url: `/isAdmin?jobId=${jobId}`,
credentials: {
username,
scmContext,
scope: ['user']
}
}).then((reply) => {
assert.calledWith(jobFactoryMock.get, jobId);
assert.calledWith(pipelineFactoryMock.get, pipelineId);
assert.deepEqual(reply.result, true);
assert.equal(reply.statusCode, 200);
})
);

it('returns 400 if passes in multiple query params', () =>
server.inject({
method: 'GET',
url: `/isAdmin?pipelineId=999&jobId=${jobId}`,
credentials: {
username,
scmContext,
scope: ['user']
}
}).then((reply) => {
assert.equal(reply.statusCode, 400);
})
);
});
});

0 comments on commit 0aeb8f0

Please sign in to comment.