Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const request = require('request-promise-native');
- const chai = require('chai');
- const expect = chai.expect;
- const sinon = require('sinon');
- const appliancesStorageFacade = require('../../modules/appliancesStorageFacade/appliancesStorageFacade.js');
- describe('[[[ APPLIANCES STORAGE FACADE UNIT TEST ]]]', () => {
- let getStub, postStub, putStub, deleteStub;
- let getTokenStub, getCartIDStub, checkTenantStub, getTenantInfoStub;
- const token = {
- token_type: 'test_',
- access_token: 'test'
- };
- const tokenAuthorization = `${token.token_type} ${token.access_token}`;
- const originalYaasConfig = {
- url: 'https://api.us.yaas.io/hybris/document/v1',
- tenant: 'inficart',
- client: 'inficart.appliancedoc',
- type: 'savedAppliances'
- };
- const originalYaasData = {
- client: {
- clientID: 'ecxEHXeKZJdo4xhVvEvZPX9uyccRMqCl',
- clientSecret: 'rIITEf40Hh8lIbXK'
- },
- scopes: ['hybris.document_manage', 'hybris.document_view', 'thack.infinitecart_manage']
- };
- const originalYaasConfigBuilderModule = {
- url: 'https://api.us.yaas.io/hybris/document/v1',
- tenant: 'inficart',
- client: 'inficart.appliancedoc',
- type: 'builderModule'
- };
- describe('Get token', () => {
- const yaasData = {
- client: {
- clientID: 'clientID',
- clientSecret: 'clientSecret'
- },
- tenant: 'tenant',
- scopes: ['scope1', 'scope2']
- };
- let requestedScopes = [];
- requestedScopes.push(`hybris.tenant=${yaasData.tenant}`);
- requestedScopes = requestedScopes.concat(yaasData.scopes);
- const requestOptions = {
- auth: {
- username: yaasData.client.clientID,
- password: yaasData.client.clientSecret
- },
- form: {
- grant_type: 'client_credentials',
- scope: requestedScopes.join(' ')
- }
- };
- const url = 'https://api.us.yaas.io/hybris/oauth2/v1/token';
- before(() => {
- postStub = sinon.stub(request, 'post');
- });
- after(() => {
- postStub.restore();
- });
- beforeEach(() => {
- postStub.reset();
- });
- it('should return token', async () => {
- postStub.returns(JSON.stringify(token));
- const response = await appliancesStorageFacade.getToken(yaasData);
- expect(postStub.calledOnce).to.be.true;
- expect(postStub.calledWith(url, requestOptions)).to.be.true;
- });
- it('should return error if token has not been found', async () => {
- postStub.throws({
- error: {
- status: '400',
- message: 'Token has not been found'
- }
- });
- try {
- const response = await appliancesStorageFacade.getToken(yaasData);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '400',
- message: 'Token has not been found'
- });
- }
- expect(postStub.calledOnce).to.be.true;
- expect(postStub.calledWith(url, requestOptions)).to.be.true;
- });
- });
- describe('Get Cart ID', () => {
- const deviceID = 'deviceID';
- const appliances = {
- cartID: 'cartID'
- };
- const options = {
- uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getStub = sinon.stub(request, 'get');
- });
- after(() => {
- getStub.restore();
- });
- beforeEach(() => {
- getStub.reset();
- });
- it('should return cartID for known device', async () => {
- getStub.returns(appliances);
- const response = await appliancesStorageFacade.getCartID(deviceID, token);
- expect(response).to.deep.equal(appliances.cartID);
- expect(getStub.calledOnce).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- it('should return error if cartID has not been received', async () => {
- getStub.throws({
- error: {
- status: '404',
- message: 'couldnt get the appliance'
- }
- });
- try {
- const response = await appliancesStorageFacade.getCartID(deviceID, token);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'couldnt get the appliance'
- });
- }
- expect(getStub.calledOnce).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- });
- describe('Register Devices', () => {
- const dev = {
- name: 'name',
- kind: 'cart',
- applianceOwner: 'applianceOwner',
- id: 'id'
- };
- const cartID = 'cartID';
- const tempDev = dev;
- tempDev.cartID = cartID;
- const options = {
- body: dev,
- uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
- postStub = sinon.stub(request, 'post');
- });
- after(() => {
- getTokenStub.restore();
- postStub.restore();
- });
- beforeEach(() => {
- getTokenStub.reset();
- postStub.reset();
- });
- it('should return status code 201, the device is created in DOCs REPO', async () => {
- getTokenStub.returns(token);
- postStub.returns({
- status: '201',
- message: 'Device registered successfully'
- });
- const result = await appliancesStorageFacade.registerDevice(dev, cartID);
- expect(result).to.deep.equal({
- status: '201',
- message: 'Device registered successfully'
- });
- expect(getTokenStub.calledOnce).to.be.true;
- expect(postStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(postStub.calledWith(options)).to.be.true;
- });
- it('should return status code 409, the device can not be created in DOCs REPO', async () => {
- getTokenStub.returns(token);
- postStub.throws({
- error: {
- status: '409',
- message: 'Device with that ID already exists'
- }
- });
- try {
- const result = await appliancesStorageFacade.registerDevice(dev, cartID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '409',
- message: 'Device with that ID already exists'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(postStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(postStub.calledWith(options)).to.be.true;
- });
- it('should catch error, if there is no token access', async () => {
- getTokenStub.throws({
- error: {
- status: '404',
- message: 'Token error'
- }
- });
- try {
- const result = await appliancesStorageFacade.registerDevice(dev, cartID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Token error'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(postStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(postStub.calledWith(options)).to.be.false;
- });
- });
- describe('Unregister Device', () => {
- const deviceID = 'deviceID';
- const options = {
- uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
- deleteStub = sinon.stub(request, 'delete');
- });
- after(() => {
- getTokenStub.restore();
- deleteStub.restore();
- });
- beforeEach(() => {
- getTokenStub.reset();
- deleteStub.reset();
- });
- it('should return status 204 and the proper message', async () => {
- getTokenStub.returns(token);
- deleteStub.returns({
- status: '204',
- message: 'The request succeeded'
- });
- const response = await appliancesStorageFacade.unregisterDevice(deviceID);
- expect(response).to.deep.equal({
- status: '204',
- message: 'The request succeeded'
- });
- expect(getTokenStub.calledOnce).to.be.true;
- expect(deleteStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(deleteStub.calledWith(options)).to.be.true;
- });
- it('should return error if token is fine and the request is about to delete a non-existing object', async () => {
- getTokenStub.returns(token);
- deleteStub.throws({
- error: {
- status: '404',
- message: 'The request is about to delete a non-existing resource/object.'
- }
- });
- try {
- const response = await appliancesStorageFacade.unregisterDevice(deviceID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'The request is about to delete a non-existing resource/object.'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(deleteStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(deleteStub.calledWith(options)).to.be.true;
- });
- it('should return error if token have not been received', async () => {
- getTokenStub.throws({
- error: {
- status: '404',
- message: 'Token error'
- }
- });
- try {
- const response = await appliancesStorageFacade.unregisterDevice(deviceID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Token error'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(deleteStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(deleteStub.calledWith(options)).to.be.false;
- });
- });
- describe('Get Device', () => {
- const deviceID = 'deviceID';
- const appliance = {
- device: {
- name: 'name',
- kind: 'cart',
- applianceOwner: 'applianceOwner',
- cartID: 'cartID',
- id: 'id'
- }
- };
- const options = {
- uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
- getStub = sinon.stub(request, 'get');
- });
- after(() => {
- getTokenStub.restore();
- getStub.restore();
- });
- beforeEach(() => {
- getTokenStub.reset();
- getStub.reset();
- });
- it('should return cartID for known device', async () => {
- getTokenStub.returns(token);
- getStub.returns({
- device: {
- name: 'name',
- kind: 'cart',
- applianceOwner: 'applianceOwner',
- cartID: 'cartID',
- id: 'id'
- }
- });
- const response = await appliancesStorageFacade.getDevice(deviceID);
- expect(response).to.deep.equal({ status: '200', device: appliance });
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- it('should return error if token has been received but cartID hasnt', async () => {
- getTokenStub.returns(token);
- getStub.throws({
- error: {
- status: '404',
- message: 'Not Found. The server did not find anything matching the Request-URI.'
- }
- });
- try {
- const response = await appliancesStorageFacade.getDevice(deviceID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Not Found. The server did not find anything matching the Request-URI.'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- it('should return error if token and cartID have not been received', async () => {
- getTokenStub.throws({
- error: {
- status: '404',
- message: 'Token error'
- }
- });
- try {
- const response = await appliancesStorageFacade.getDevice(deviceID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Token error'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.false;
- });
- });
- describe('Get Devices', () => {
- const applianceOwner = 'applianceOwner';
- const listOfDevices = ['appliance1', 'appliance2'];
- const options = {
- uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/?fetchAll=true&q=applianceOwner:"${applianceOwner}"`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
- getStub = sinon.stub(request, 'get');
- });
- after(() => {
- getTokenStub.restore();
- getStub.restore();
- });
- beforeEach(() => {
- getTokenStub.reset();
- getStub.reset();
- });
- it('should return status 200 and list of devices for known applianceOwner/tenant', async () => {
- getTokenStub.returns(token);
- getStub.returns(
- listOfDevices
- );
- const response = await appliancesStorageFacade.getDevices(applianceOwner);
- expect(response).to.deep.equal({ status: '200', message: listOfDevices });
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- it('should return error if token has been received but cartID hasnt', async () => {
- getTokenStub.returns(token);
- getStub.throws({
- error: {
- status: '404',
- message: 'Not Found. The server did not find anything matching the Request-URI.'
- }
- });
- try {
- const response = await appliancesStorageFacade.getDevices(applianceOwner);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Not Found. The server did not find anything matching the Request-URI.'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- it('should return error if token and cartID have not been received', async () => {
- getTokenStub.throws({
- error: {
- status: '404',
- message: 'Token error'
- }
- });
- try {
- const response = await appliancesStorageFacade.getDevices(applianceOwner);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Token error'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.false;
- });
- });
- describe('Update Device', () => {
- const deviceID = 'deviceID';
- const cartID = 'cartID';
- const appliances = { status: '200', message: ['appliance1', 'appliance2'] };
- const options = {
- uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}/?&path=true&partial=true`,
- body: {
- cartID: cartID
- },
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
- getCartIDStub = sinon.stub(appliancesStorageFacade, 'getCartID');
- putStub = sinon.stub(request, 'put');
- });
- after(() => {
- getTokenStub.restore();
- getCartIDStub.restore();
- putStub.restore();
- });
- beforeEach(() => {
- getTokenStub.reset();
- getCartIDStub.reset();
- putStub.reset();
- });
- it('should return status 200 and oldCartID', async () => {
- getTokenStub.returns(token);
- getCartIDStub.returns('oldCartID');
- putStub.returns(appliances);
- const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
- expect(response).to.deep.equal({
- status: '200',
- cartID: 'oldCartID'
- });
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getCartIDStub.calledOnce).to.be.true;
- expect(putStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getCartIDStub.calledWith(deviceID, token)).to.be.true;
- expect(putStub.calledWith(options)).to.be.true;
- });
- it('should return error if token and oldCartID have been received but device has not been updated', async () => {
- getTokenStub.returns(token);
- getCartIDStub.returns('oldCartID');
- putStub.throws({
- error: {
- status: '404',
- message: 'The request is about to change a non-existing resource/object'
- }
- });
- try {
- const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'The request is about to change a non-existing resource/object'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getCartIDStub.calledOnce).to.be.true;
- expect(putStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getCartIDStub.calledWith(deviceID, token)).to.be.true;
- expect(putStub.calledWith(options)).to.be.true;
- });
- it('should return error if token has been received but oldCartID hasnt and device has not been updated', async () => {
- getTokenStub.returns(token);
- getCartIDStub.throws({
- error: {
- status: '404',
- message: 'Can not find device with that ID'
- }
- });
- try {
- const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Can not find device with that ID'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getCartIDStub.calledOnce).to.be.true;
- expect(putStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getCartIDStub.calledWith(deviceID, token)).to.be.true;
- expect(putStub.calledWith(options)).to.be.false;
- });
- it('should return error if token and oldCartID have not been received adn device has not been updated', async () => {
- getTokenStub.throws({
- error: {
- status: '404',
- message: 'Token error'
- }
- });
- try {
- const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Token error'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getCartIDStub.calledOnce).to.be.false;
- expect(putStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getCartIDStub.calledWith(deviceID, token)).to.be.false;
- expect(putStub.calledWith(options)).to.be.false;
- });
- });
- // check tenant //
- describe('Check Tenant', () => {
- const JSONInfo = {
- tenant: 'tenant',
- host: 'host',
- baseSiteId: 'baseSiteId'
- };
- const tenantInfo = {
- status: '200',
- message: [{ tenant: 'tenant', id: 'id' }]
- };
- const options = {
- uri: `${originalYaasConfigBuilderModule.url}/${originalYaasConfigBuilderModule.tenant}/${originalYaasConfigBuilderModule.client}/data/${originalYaasConfigBuilderModule.type}/${tenantInfo.message[0].id}`,
- body: JSONInfo,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getTenantInfoStub = sinon.stub(appliancesStorageFacade, 'getTenantInfo');
- putStub = sinon.stub(request, 'put');
- });
- after(() => {
- getTenantInfoStub.restore();
- putStub.restore();
- });
- beforeEach(() => {
- getTenantInfoStub.reset();
- putStub.reset();
- });
- it('should return status 200 and message if tenant exists on DOCS REPO so tenant has been updated properly', async () => {
- getTenantInfoStub.returns(tenantInfo);
- putStub.returns({ status: '200', message: 'Tenant has been updated properly' });
- const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
- expect(response).to.deep.equal({ status: '200', message: 'Tenant has been updated properly' });
- expect(getTenantInfoStub.calledOnce).to.be.true;
- expect(putStub.calledOnce).to.be.true;
- expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
- expect(putStub.calledWith(options)).to.be.true;
- });
- it('should return error if tenant exists on DOCS REPO but tenant hasnt been updated properly', async () => {
- getTenantInfoStub.returns(tenantInfo);
- putStub.throws({
- error: {
- status: '403',
- message: 'Tenant can not be update'
- }
- });
- try {
- const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
- }
- catch (err) {
- expect(err).to.deep.equal({ status: '403', message: 'Tenant can not be update' });
- }
- expect(getTenantInfoStub.calledOnce).to.be.true;
- expect(putStub.calledOnce).to.be.true;
- expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
- expect(putStub.calledWith(options)).to.be.true;
- });
- it('should return false if tenant doesnt exist on DOCS REPO so tenant wont be update', async () => {
- getTenantInfoStub.returns({
- status: '200',
- message: []
- });
- const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
- expect(response).to.deep.equal(false);
- expect(getTenantInfoStub.calledOnce).to.be.true;
- expect(putStub.calledOnce).to.be.false;
- expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
- expect(putStub.calledWith(options)).to.be.false;
- });
- it('should return error if tenant hasnt been received from DOCS REPO so tenant wont be update', async () => {
- getTenantInfoStub.throws({
- error: {
- status: '403',
- message: 'Tenant can not be received'
- }
- });
- try {
- const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
- }
- catch (err) {
- expect(err).to.deep.equal({status: '403', message: 'Tenant can not be received'});
- }
- expect(getTenantInfoStub.calledOnce).to.be.true;
- expect(putStub.calledOnce).to.be.false;
- expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
- expect(putStub.calledWith(options)).to.be.false;
- });
- });
- describe('Save Tenant Info', () => {
- const tenantInfo = {
- tenant: 'tenant',
- host: 'host',
- baseSiteId: 'baseSiteId'
- };
- const options = {
- body: tenantInfo,
- uri: `${originalYaasConfigBuilderModule.url}/${originalYaasConfigBuilderModule.tenant}/${originalYaasConfigBuilderModule.client}/data/${originalYaasConfigBuilderModule.type}/`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- checkTenantStub = sinon.stub(appliancesStorageFacade, 'checkTenant');
- getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
- postStub = sinon.stub(request, 'post');
- });
- after(() => {
- checkTenantStub.restore();
- getTokenStub.restore();
- postStub.restore();
- });
- beforeEach(() => {
- checkTenantStub.reset();
- getTokenStub.reset();
- postStub.reset();
- });
- it('should return status 201 and message if token has been received properly and tenant will be save for the first time', async () => {
- getTokenStub.returns(token);
- checkTenantStub.returns(false);
- postStub.returns({ status: '201', message: 'Tenant info saved properly' });
- const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
- expect(response).to.deep.equal({ status: '201', message: 'Tenant info saved properly' });
- expect(getTokenStub.calledOnce).to.be.true;
- expect(checkTenantStub.calledOnce).to.be.true;
- expect(postStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(checkTenantStub.calledWith(tenantInfo)).to.be.true;
- expect(postStub.calledWith(options)).to.be.true;
- });
- it('should return error if tenant has been received but has been error during saving tenant for the first time', async () => {
- getTokenStub.returns(token);
- checkTenantStub.returns(false);
- postStub.throws({
- error: {
- status: '400',
- message: 'Tenant info can not be saved.'
- }
- });
- try {
- const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '400',
- message: 'Tenant info can not be saved.'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(checkTenantStub.calledOnce).to.be.true;
- expect(postStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(checkTenantStub.calledWith(tenantInfo)).to.be.true;
- expect(postStub.calledWith(options)).to.be.true;
- });
- it('should return true if token has been received and tenant already had been existing in DOCS REPO so just has been updated', async () => {
- getTokenStub.returns(token);
- checkTenantStub.returns(true);
- const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
- expect(response).to.deep.equal(true);
- expect(getTokenStub.calledOnce).to.be.true;
- expect(checkTenantStub.calledOnce).to.be.true;
- expect(postStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(checkTenantStub.calledWith(tenantInfo)).to.be.true;
- expect(postStub.calledWith(options)).to.be.false;
- });
- it('should return error if token hasnt been received so tenant havent been saved', async () => {
- getTokenStub.throws({
- error: {
- status: '404',
- message: 'Token error'
- }
- });
- try {
- const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Token error'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(checkTenantStub.calledOnce).to.be.false;
- expect(postStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(checkTenantStub.calledWith(tenantInfo)).to.be.false;
- expect(postStub.calledWith(options)).to.be.false;
- });
- });
- describe('Get Tenant Info', () => {
- const tenant = 'tenant';
- const tenantInfo = {
- host: 'host',
- baseSiteId: 'baseSiteId'
- };
- const options = {
- uri: `${originalYaasConfigBuilderModule.url}/${originalYaasConfigBuilderModule.tenant}/${originalYaasConfigBuilderModule.client}/data/${originalYaasConfigBuilderModule.type}/?fetchAll=true&q=tenant:"${tenant}"`,
- headers: {
- Authorization: tokenAuthorization
- },
- json: true
- };
- before(() => {
- getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
- getStub = sinon.stub(request, 'get');
- });
- after(() => {
- getTokenStub.restore();
- getStub.restore();
- });
- beforeEach(() => {
- getTokenStub.reset();
- getStub.reset();
- });
- it('should return status 200 and message if token and tenant info received properly', async () => {
- getTokenStub.returns(token);
- getStub.returns(tenantInfo);
- const response = await appliancesStorageFacade.getTenantInfo(tenant);
- expect(response).to.deep.equal({ status: '200', message: tenantInfo });
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- it('should return error if tenant has been received but data havent been gotten', async () => {
- getTokenStub.returns(token);
- getStub.throws({
- error: {
- status: '400',
- message: 'Tenant info can not be gotten.'
- }
- });
- try {
- const response = await appliancesStorageFacade.getTenantInfo(tenant);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '400',
- message: 'Tenant info can not be gotten.'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.true;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.true;
- });
- it('should return error if token hasnt been received so data havent been gotten', async () => {
- getTokenStub.throws({
- error: {
- status: '404',
- message: 'Token error'
- }
- });
- try {
- const response = await appliancesStorageFacade.getTenantInfo(tenant);
- }
- catch (err) {
- expect(err).to.deep.equal({
- status: '404',
- message: 'Token error'
- });
- }
- expect(getTokenStub.calledOnce).to.be.true;
- expect(getStub.calledOnce).to.be.false;
- expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
- expect(getStub.calledWith(options)).to.be.false;
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement