Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/fittings/rackhd_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var injector = require('../../index.js').injector;
var validator = injector.get('Http.Services.Swagger').validator;
var configuration = injector.get('Services.Configuration');

module.exports = function create(fittingDef) {
var validate = validator();
Expand Down Expand Up @@ -33,6 +34,17 @@ module.exports = function create(fittingDef) {
schemaName = service + suffix;
}
}
var credentials = configuration.get("defaultIbms");
if (credentials !== undefined
&& context.request.method === "PUT"
&& context.request.originalUrl === "/api/2.0/ibms"
&& context.request.body.config !== undefined
&& context.request.body.config.user === undefined
&& context.request.body.config.password === undefined
) {
context.request.body.config.user = credentials.user;
context.request.body.config.password = credentials.password;
}
validate(schemaName, context.request.body, next);
}
};
Expand Down
63 changes: 48 additions & 15 deletions spec/lib/api/2.0/ibms-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@

describe('Http.Api.Ibms', function () {
var waterline, stub, Errors;

var goodSendData =
{
nodeId: '12345678',
service: 'ssh-ibm-service',
config: {
host: '1.1.1.1',
user: 'user',
password: 'passw'
}
};
var configuration;
var defaultCred = {
id: '12341234',
node: '12345678',
service: 'ssh-ibm-service',
config: {
host: '1.1.1.1'
}
};
var goodSendData =
{
nodeId: '12345678',
service: 'ssh-ibm-service',
config: {
host: '1.1.1.1',
user: 'user',
password: 'passw'
}
};

var goodData = [
{
Expand Down Expand Up @@ -61,7 +69,9 @@ describe('Http.Api.Ibms', function () {
this.timeout(5000);
return helper.startServer().then(function() {
waterline = helper.injector.get('Services.Waterline');
Errors = helper.injector.get('Errors');
Errors = helper.injector.get('Errors');helper.injector.get("Services.Configuration");
configuration = helper.injector.get("Services.Configuration");
//sinon.stub(configuration);
});
});

Expand All @@ -75,7 +85,7 @@ describe('Http.Api.Ibms', function () {
after('stop HTTP server', function () {
return helper.stopServer();
});

describe('/api/2.0/ibms/definitions', function () {
it('should return a list of IBM schemas', function () {
return helper.request().get('/api/2.0/ibms/definitions')
Expand All @@ -100,7 +110,7 @@ describe('Http.Api.Ibms', function () {
});

});

describe('/api/2.0/ibms', function () {
it('should return a list of IBM instances', function () {
stub = sinon.stub(waterline.ibms, 'find').resolves(goodData);
Expand All @@ -116,7 +126,7 @@ describe('Http.Api.Ibms', function () {
expect(res.body.config).to.deep.equal(goodData.config);
});
});

it('should put an IBM instance', function () {
stub = sinon.stub(waterline.ibms, 'upsertByNode').resolves(goodData[0]);

Expand All @@ -129,6 +139,29 @@ describe('Http.Api.Ibms', function () {
});
});

it('should PUT an IBM instance with default credential', function () {
this.sandbox = sinon.sandbox.create();
stub = sinon.stub(waterline.ibms, 'upsertByNode').resolves(defaultCred);
configuration.set('defaultIbms', {
"user": "monorail",
"password": "monorail"
});
return helper.request().put('/api/2.0/ibms')
.send(defaultCred)
.expect('Content-Type', /^application\/json/)
.expect(201);
});
it('should fail to PUT an IBM instance with default credential', function () {
this.sandbox.restore();
stub = sinon.stub(waterline.ibms, 'upsertByNode').resolves(defaultCred);
configuration.set('defaultIbms', undefined);

return helper.request().put('/api/2.0/ibms')
.send(defaultCred)
.expect('Content-Type', /^application\/json/)
.expect(400);
});

it('should 400 when put with unloaded schema', function () {
stub = sinon.stub(waterline.ibms, 'upsertByNode');

Expand Down