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
64 changes: 64 additions & 0 deletions src/app/api/serviceInstance/serviceInstance.api.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
(function () {
'use strict';

describe('service instance API', function () {
var $httpBackend, serviceInstanceApi;

beforeEach(module('green-box-console'));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');

var apiManager = $injector.get('app.api.apiManager');
serviceInstanceApi = apiManager.retrieve('app.api.serviceInstance');
}));

afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});

it('should be defined', function () {
expect(serviceInstanceApi).toBeDefined();
});

it('should have `$http` property defined', function () {
expect(serviceInstanceApi.$http).toBeDefined();
});

it('should return service instances for specified user', function () {
var data = {
items: ['x','y','z']
};
$httpBackend.when('GET', '/api/service-instances?username=dev').respond(200, data);

serviceInstanceApi.list('dev').then(function (response) {
expect(response.data).toEqual({items: ['x','y','z']});
});

$httpBackend.flush();
});

it('should send POST request for register', function () {
var data = {
username: 'user',
name: 'service',
service_user: 'username',
service_password: 'password'
};
$httpBackend.expectPOST('/api/service-instances/register', data).respond(200, '');
serviceInstanceApi.register('user', 'service', 'username', 'password');
$httpBackend.flush();
});

it('should send POST request for unregister', function () {
var data = {
username: 'user',
name: 'service'
};
$httpBackend.expectPOST('/api/service-instances/unregister', data).respond(200, '');
serviceInstanceApi.unregister('user', 'service');
$httpBackend.flush();
});
});

})();
100 changes: 100 additions & 0 deletions src/app/model/serviceInstance/serviceInstance.model.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
(function () {
'use strict';

describe('service instance model', function () {
var $httpBackend, serviceInstance, mockData;

beforeEach(module('green-box-console'));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');

var modelManager = $injector.get('app.model.modelManager');
var account = modelManager.retrieve('app.model.account');
account.username = 'dev';
serviceInstance = modelManager.retrieve('app.model.serviceInstance');

mockData = {
items: [
{ name: 'cluster1', url:' cluster1_url' },
{ name: 'cluster2', url:' cluster2_url' }
]
};
}));

afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});

it('should be defined', function () {
expect(serviceInstance).toBeDefined();
});

it('should have initial properties defined', function () {
expect(serviceInstance.apiManager).toBeDefined();
expect(serviceInstance.account).toBeDefined();
expect(serviceInstance.serviceInstanceApi).toBeDefined();
expect(serviceInstance.serviceInstances).toEqual([]);
expect(serviceInstance.numRegistered).toBe(0);
});

it('should set `serviceInstances` on list()', function () {
var expectedData = [
{ name: 'cluster1', url:' cluster1_url' },
{ name: 'cluster2', url:' cluster2_url' }
];

$httpBackend.when('GET', '/api/service-instances?username=dev')
.respond(200, mockData);

serviceInstance.list().then(function (response) {
expect(response).toEqual({ serviceInstances: expectedData, numRegistered: 0 });
expect(serviceInstance.serviceInstances).toEqual(expectedData);
expect(serviceInstance.numRegistered).toBe(0);
});

$httpBackend.flush();
});

it('should not set `serviceInstances` on list() and error', function () {
$httpBackend.when('GET', '/api/service-instances?username=dev')
.respond(403, {});

serviceInstance.list().then(function () {}, function (error) {
expect(error.status).toBe(403);
expect(error.data).toEqual({});
expect(serviceInstance.serviceInstances).toEqual([]);
expect(serviceInstance.numRegistered).toBe(0);
});

$httpBackend.flush();
});

it('should POST correct data on register()', function () {
var data = {
username: 'dev',
name: 'service',
service_user: 'username',
service_password: 'password'
};
$httpBackend.expectPOST('/api/service-instances/register', data).respond(200, '');

serviceInstance.register('service', 'username', 'password');

$httpBackend.flush();
});

it('should POST correct data on unregister()', function () {
var data = {
username: 'dev',
name: 'service'
};
$httpBackend.expectPOST('/api/service-instances/unregister', data).respond(200, '');

serviceInstance.unregister('service');

$httpBackend.flush();
});
});

})();