-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Parry
committed
Mar 24, 2017
1 parent
ed1ecac
commit f6b5370
Showing
5 changed files
with
214 additions
and
2 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
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,44 @@ | ||
// Copyright 2016, EMC, Inc. | ||
|
||
'use strict'; | ||
|
||
var injector = require('../../index.js').injector; | ||
var tasksApiService = injector.get('Http.Services.Api.Tasks'); | ||
var _ = injector.get('_'); // jshint ignore:line | ||
var Errors = injector.get('Errors'); | ||
var Promise = injector.get('Promise'); | ||
|
||
var getBootstrap = function(call) { | ||
return Promise.try(function() { | ||
var scope = call.request.scope; | ||
var ipAddress = call.request.ipAddress; | ||
var macAddress = call.request.macAddress; | ||
return tasksApiService.getBootstrap(scope, ipAddress, macAddress); | ||
}); | ||
}; | ||
|
||
var getTasksById = function(call) { | ||
return Promise.try(function() { | ||
return tasksApiService.getTasks(call.request.identifier) | ||
}) | ||
.catch(function (err) { | ||
if (err.name === 'NoActiveTaskError') { | ||
return {}; | ||
} | ||
// throw a NotFoundError | ||
throw new Errors.NotFoundError('Not Found'); | ||
}); | ||
}; | ||
|
||
var postTaskById = function(call) { | ||
return Promise.try(function() { | ||
return tasksApiService.postTasksById(call.request.identifier, | ||
JSON.parse(call.request.config)); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
getBootstrap: getBootstrap, | ||
getTasksById: getTasksById, | ||
postTaskById: postTaskById | ||
}; |
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
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,140 @@ | ||
// Copyright 2016, EMC, Inc. | ||
/* jshint node:true */ | ||
|
||
'use strict'; | ||
|
||
describe('Taskgraph.Api.Tasks.Rpc', function () { | ||
var mockery; | ||
var tasksApi; | ||
|
||
before('setup mockery', function () { | ||
this.timeout(10000); | ||
|
||
// setup injector with mock override injecatbles | ||
var injectables = [ | ||
helper.di.simpleWrapper({ | ||
controller: function(opts, cb) { | ||
if (typeof(opts) === 'function') { | ||
cb = opts; | ||
} | ||
return cb; | ||
} | ||
}, 'Http.Services.Swagger'), | ||
helper.di.simpleWrapper({ | ||
getBootstrap: sinon.stub(), | ||
getTasks: sinon.stub(), | ||
postTasksById: sinon.stub() | ||
}, 'Http.Services.Api.Tasks') | ||
]; | ||
helper.setupInjector(injectables); | ||
|
||
// setup mockery such that index.injector is our test injector. | ||
mockery = require('mockery'); | ||
mockery.registerMock('../../index.js', { injector: helper.injector }); | ||
mockery.enable(); | ||
|
||
// Now require file to test | ||
tasksApi = require('../../../api/rpc/tasks'); | ||
}); | ||
|
||
|
||
after('disable mockery', function () { | ||
mockery.deregisterMock('../../index.js'); | ||
mockery.disable(); | ||
}); | ||
|
||
describe('GET /tasks/:id', function () { | ||
it("should get a task by id", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.getTasks.resolves('a task'); | ||
return tasksApi.getTasksById({ request: { identifier: '123' } }) | ||
.should.eventually.equal('a task'); | ||
}); | ||
|
||
it("should reject with not found if getTasks rejects", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
|
||
tasksApiService.getTasks.rejects('Not Found'); | ||
return tasksApi.getTasksById({ request: { identifier: '123' } }) | ||
.should.be.rejectedWith('Not Found'); | ||
}); | ||
|
||
it("should reject with not found if req is invalid", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.getTasks.resolves(); | ||
return tasksApi.getTasksById(undefined) | ||
.should.be.rejectedWith('Not Found'); | ||
}); | ||
}); | ||
|
||
describe("GET /tasks/bootstrap.js", function() { | ||
it("should get bootstrap", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.getBootstrap.resolves('bootstrap'); | ||
return tasksApi.getBootstrap( { request: { scope: '' } }, { request: { ipAddress: '123' } }, | ||
{ request: { macAddress: '10.20.30' } } ) | ||
.should.eventually.equal('bootstrap'); | ||
}); | ||
|
||
it("should reject if getBootstrap rejects", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.getBootstrap.rejects('No Bootstrap'); | ||
return tasksApi.getBootstrap( { request: { scope: '' } }, { request: { ipAddress: '123' } }, | ||
{ request: { macAddress: '10.20.30' } } ) | ||
.should.be.rejectedWith('No Bootstrap'); | ||
}); | ||
|
||
it("should reject if req is invalid", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.getBootstrap.resolves(); | ||
return tasksApi.getBootstrap(undefined) | ||
.should.be.rejectedWith(/undefined/); | ||
}); | ||
}); | ||
|
||
// Since sinon.stub() has no returnsArg method, add a Promise wrapper. | ||
// This will allow a stub to return a promise resolving to one of its call | ||
// arguments. | ||
function _stubPromiseWrapper(stub) { | ||
return function() { | ||
return Promise.resolve(stub.apply(sinon, arguments)); | ||
}; | ||
} | ||
|
||
describe("POST /tasks/:id", function() { | ||
beforeEach(function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.postTasksById = sinon.stub(); | ||
}); | ||
|
||
it("should post a task", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.postTasksById = _stubPromiseWrapper(sinon.stub().returnsArg(1)); | ||
return tasksApi.postTaskById({ request: { identifier: '123' }, | ||
request: { config: '{ "foo": "bar" }' } }) | ||
.then(function(body) { | ||
expect(body).to.deep.equal({ foo: 'bar' }); | ||
}); | ||
}); | ||
|
||
|
||
it("should reject if postTaskById rejects", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.postTasksById.rejects('post error'); | ||
return tasksApi.postTaskById({ request: { identifier: '123' }, | ||
request: { config: '{ "foo": "bar" }' } }) | ||
.should.be.rejectedWith('post error'); | ||
|
||
}); | ||
|
||
|
||
it("should reject if postTaskById rejects", function() { | ||
var tasksApiService = helper.injector.get('Http.Services.Api.Tasks'); | ||
tasksApiService.postTasksById.resolves(); | ||
return tasksApi.postTaskById({ swagger: undefined, | ||
body: { foo: 'bar' } }) | ||
.should.be.rejectedWith(/undefined/); | ||
|
||
}); | ||
}); | ||
}); |