Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.43 KB | None | 0 0
  1. const request = require('request-promise-native');
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const sinon = require('sinon');
  5.  
  6. const appliancesStorageFacade = require('../../modules/appliancesStorageFacade/appliancesStorageFacade.js');
  7.  
  8. describe('[[[ APPLIANCES STORAGE FACADE UNIT TEST ]]]', () => {
  9.  
  10. let getStub, postStub, putStub, deleteStub;
  11. let getTokenStub, getCartIDStub, checkTenantStub, getTenantInfoStub;
  12.  
  13. const token = {
  14. token_type: 'test_',
  15. access_token: 'test'
  16. };
  17. const tokenAuthorization = `${token.token_type} ${token.access_token}`;
  18.  
  19. const originalYaasConfig = {
  20. url: 'https://api.us.yaas.io/hybris/document/v1',
  21. tenant: 'inficart',
  22. client: 'inficart.appliancedoc',
  23. type: 'savedAppliances'
  24. };
  25.  
  26. const originalYaasData = {
  27. client: {
  28. clientID: 'ecxEHXeKZJdo4xhVvEvZPX9uyccRMqCl',
  29. clientSecret: 'rIITEf40Hh8lIbXK'
  30. },
  31. scopes: ['hybris.document_manage', 'hybris.document_view', 'thack.infinitecart_manage']
  32. };
  33.  
  34. const originalYaasConfigBuilderModule = {
  35. url: 'https://api.us.yaas.io/hybris/document/v1',
  36. tenant: 'inficart',
  37. client: 'inficart.appliancedoc',
  38. type: 'builderModule'
  39. };
  40.  
  41. describe('Get token', () => {
  42. const yaasData = {
  43. client: {
  44. clientID: 'clientID',
  45. clientSecret: 'clientSecret'
  46. },
  47. tenant: 'tenant',
  48. scopes: ['scope1', 'scope2']
  49. };
  50.  
  51. let requestedScopes = [];
  52. requestedScopes.push(`hybris.tenant=${yaasData.tenant}`);
  53. requestedScopes = requestedScopes.concat(yaasData.scopes);
  54.  
  55. const requestOptions = {
  56. auth: {
  57. username: yaasData.client.clientID,
  58. password: yaasData.client.clientSecret
  59. },
  60. form: {
  61. grant_type: 'client_credentials',
  62. scope: requestedScopes.join(' ')
  63. }
  64. };
  65. const url = 'https://api.us.yaas.io/hybris/oauth2/v1/token';
  66.  
  67. before(() => {
  68. postStub = sinon.stub(request, 'post');
  69. });
  70.  
  71. after(() => {
  72. postStub.restore();
  73. });
  74.  
  75. beforeEach(() => {
  76. postStub.reset();
  77. });
  78.  
  79. it('should return token', async () => {
  80. postStub.returns(JSON.stringify(token));
  81.  
  82. const response = await appliancesStorageFacade.getToken(yaasData);
  83.  
  84. expect(postStub.calledOnce).to.be.true;
  85. expect(postStub.calledWith(url, requestOptions)).to.be.true;
  86. });
  87.  
  88. it('should return error if token has not been found', async () => {
  89. postStub.throws({
  90. error: {
  91. status: '400',
  92. message: 'Token has not been found'
  93. }
  94. });
  95. try {
  96. const response = await appliancesStorageFacade.getToken(yaasData);
  97. }
  98. catch (err) {
  99. expect(err).to.deep.equal({
  100. status: '400',
  101. message: 'Token has not been found'
  102. });
  103. }
  104. expect(postStub.calledOnce).to.be.true;
  105. expect(postStub.calledWith(url, requestOptions)).to.be.true;
  106. });
  107. });
  108.  
  109.  
  110. describe('Get Cart ID', () => {
  111. const deviceID = 'deviceID';
  112. const appliances = {
  113. cartID: 'cartID'
  114. };
  115.  
  116. const options = {
  117. uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}`,
  118. headers: {
  119. Authorization: tokenAuthorization
  120. },
  121. json: true
  122. };
  123.  
  124. before(() => {
  125. getStub = sinon.stub(request, 'get');
  126. });
  127.  
  128. after(() => {
  129. getStub.restore();
  130. });
  131.  
  132. beforeEach(() => {
  133. getStub.reset();
  134. });
  135.  
  136. it('should return cartID for known device', async () => {
  137. getStub.returns(appliances);
  138.  
  139. const response = await appliancesStorageFacade.getCartID(deviceID, token);
  140.  
  141. expect(response).to.deep.equal(appliances.cartID);
  142. expect(getStub.calledOnce).to.be.true;
  143. expect(getStub.calledWith(options)).to.be.true;
  144. });
  145.  
  146. it('should return error if cartID has not been received', async () => {
  147. getStub.throws({
  148. error: {
  149. status: '404',
  150. message: 'couldnt get the appliance'
  151. }
  152. });
  153. try {
  154. const response = await appliancesStorageFacade.getCartID(deviceID, token);
  155. }
  156. catch (err) {
  157. expect(err).to.deep.equal({
  158. status: '404',
  159. message: 'couldnt get the appliance'
  160. });
  161. }
  162.  
  163. expect(getStub.calledOnce).to.be.true;
  164. expect(getStub.calledWith(options)).to.be.true;
  165. });
  166. });
  167.  
  168.  
  169. describe('Register Devices', () => {
  170. const dev = {
  171. name: 'name',
  172. kind: 'cart',
  173. applianceOwner: 'applianceOwner',
  174. id: 'id'
  175. };
  176.  
  177. const cartID = 'cartID';
  178. const tempDev = dev;
  179. tempDev.cartID = cartID;
  180.  
  181. const options = {
  182. body: dev,
  183. uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/`,
  184. headers: {
  185. Authorization: tokenAuthorization
  186. },
  187. json: true
  188. };
  189.  
  190. before(() => {
  191. getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
  192. postStub = sinon.stub(request, 'post');
  193. });
  194.  
  195. after(() => {
  196. getTokenStub.restore();
  197. postStub.restore();
  198. });
  199.  
  200. beforeEach(() => {
  201. getTokenStub.reset();
  202. postStub.reset();
  203. });
  204.  
  205. it('should return status code 201, the device is created in DOCs REPO', async () => {
  206. getTokenStub.returns(token);
  207. postStub.returns({
  208. status: '201',
  209. message: 'Device registered successfully'
  210. });
  211.  
  212. const result = await appliancesStorageFacade.registerDevice(dev, cartID);
  213.  
  214. expect(result).to.deep.equal({
  215. status: '201',
  216. message: 'Device registered successfully'
  217. });
  218.  
  219. expect(getTokenStub.calledOnce).to.be.true;
  220. expect(postStub.calledOnce).to.be.true;
  221. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  222. expect(postStub.calledWith(options)).to.be.true;
  223. });
  224.  
  225. it('should return status code 409, the device can not be created in DOCs REPO', async () => {
  226. getTokenStub.returns(token);
  227. postStub.throws({
  228. error: {
  229. status: '409',
  230. message: 'Device with that ID already exists'
  231. }
  232. });
  233.  
  234. try {
  235. const result = await appliancesStorageFacade.registerDevice(dev, cartID);
  236. }
  237. catch (err) {
  238. expect(err).to.deep.equal({
  239. status: '409',
  240. message: 'Device with that ID already exists'
  241. });
  242. }
  243.  
  244. expect(getTokenStub.calledOnce).to.be.true;
  245. expect(postStub.calledOnce).to.be.true;
  246. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  247. expect(postStub.calledWith(options)).to.be.true;
  248. });
  249.  
  250. it('should catch error, if there is no token access', async () => {
  251. getTokenStub.throws({
  252. error: {
  253. status: '404',
  254. message: 'Token error'
  255. }
  256. });
  257.  
  258. try {
  259. const result = await appliancesStorageFacade.registerDevice(dev, cartID);
  260. }
  261. catch (err) {
  262. expect(err).to.deep.equal({
  263. status: '404',
  264. message: 'Token error'
  265. });
  266. }
  267.  
  268. expect(getTokenStub.calledOnce).to.be.true;
  269. expect(postStub.calledOnce).to.be.false;
  270. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  271. expect(postStub.calledWith(options)).to.be.false;
  272. });
  273. });
  274.  
  275.  
  276. describe('Unregister Device', () => {
  277. const deviceID = 'deviceID';
  278.  
  279. const options = {
  280. uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}`,
  281. headers: {
  282. Authorization: tokenAuthorization
  283. },
  284. json: true
  285. };
  286.  
  287. before(() => {
  288. getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
  289. deleteStub = sinon.stub(request, 'delete');
  290. });
  291.  
  292. after(() => {
  293. getTokenStub.restore();
  294. deleteStub.restore();
  295. });
  296.  
  297. beforeEach(() => {
  298. getTokenStub.reset();
  299. deleteStub.reset();
  300. });
  301.  
  302. it('should return status 204 and the proper message', async () => {
  303. getTokenStub.returns(token);
  304. deleteStub.returns({
  305. status: '204',
  306. message: 'The request succeeded'
  307. });
  308.  
  309. const response = await appliancesStorageFacade.unregisterDevice(deviceID);
  310.  
  311. expect(response).to.deep.equal({
  312. status: '204',
  313. message: 'The request succeeded'
  314. });
  315.  
  316. expect(getTokenStub.calledOnce).to.be.true;
  317. expect(deleteStub.calledOnce).to.be.true;
  318. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  319. expect(deleteStub.calledWith(options)).to.be.true;
  320. });
  321.  
  322. it('should return error if token is fine and the request is about to delete a non-existing object', async () => {
  323. getTokenStub.returns(token);
  324. deleteStub.throws({
  325. error: {
  326. status: '404',
  327. message: 'The request is about to delete a non-existing resource/object.'
  328. }
  329. });
  330. try {
  331. const response = await appliancesStorageFacade.unregisterDevice(deviceID);
  332. }
  333. catch (err) {
  334. expect(err).to.deep.equal({
  335. status: '404',
  336. message: 'The request is about to delete a non-existing resource/object.'
  337. });
  338. }
  339.  
  340. expect(getTokenStub.calledOnce).to.be.true;
  341. expect(deleteStub.calledOnce).to.be.true;
  342. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  343. expect(deleteStub.calledWith(options)).to.be.true;
  344. });
  345.  
  346. it('should return error if token have not been received', async () => {
  347. getTokenStub.throws({
  348. error: {
  349. status: '404',
  350. message: 'Token error'
  351. }
  352. });
  353. try {
  354. const response = await appliancesStorageFacade.unregisterDevice(deviceID);
  355. }
  356. catch (err) {
  357. expect(err).to.deep.equal({
  358. status: '404',
  359. message: 'Token error'
  360. });
  361. }
  362.  
  363. expect(getTokenStub.calledOnce).to.be.true;
  364. expect(deleteStub.calledOnce).to.be.false;
  365. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  366. expect(deleteStub.calledWith(options)).to.be.false;
  367. });
  368. });
  369.  
  370.  
  371. describe('Get Device', () => {
  372.  
  373. const deviceID = 'deviceID';
  374. const appliance = {
  375. device: {
  376. name: 'name',
  377. kind: 'cart',
  378. applianceOwner: 'applianceOwner',
  379. cartID: 'cartID',
  380. id: 'id'
  381. }
  382. };
  383.  
  384. const options = {
  385. uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}`,
  386. headers: {
  387. Authorization: tokenAuthorization
  388. },
  389. json: true
  390. };
  391.  
  392. before(() => {
  393. getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
  394. getStub = sinon.stub(request, 'get');
  395. });
  396.  
  397. after(() => {
  398. getTokenStub.restore();
  399. getStub.restore();
  400. });
  401.  
  402. beforeEach(() => {
  403. getTokenStub.reset();
  404. getStub.reset();
  405. });
  406.  
  407. it('should return cartID for known device', async () => {
  408. getTokenStub.returns(token);
  409. getStub.returns({
  410. device: {
  411. name: 'name',
  412. kind: 'cart',
  413. applianceOwner: 'applianceOwner',
  414. cartID: 'cartID',
  415. id: 'id'
  416. }
  417. });
  418.  
  419. const response = await appliancesStorageFacade.getDevice(deviceID);
  420.  
  421. expect(response).to.deep.equal({ status: '200', device: appliance });
  422. expect(getTokenStub.calledOnce).to.be.true;
  423. expect(getStub.calledOnce).to.be.true;
  424. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  425. expect(getStub.calledWith(options)).to.be.true;
  426. });
  427.  
  428. it('should return error if token has been received but cartID hasnt', async () => {
  429. getTokenStub.returns(token);
  430. getStub.throws({
  431. error: {
  432. status: '404',
  433. message: 'Not Found. The server did not find anything matching the Request-URI.'
  434. }
  435. });
  436. try {
  437. const response = await appliancesStorageFacade.getDevice(deviceID);
  438. }
  439. catch (err) {
  440. expect(err).to.deep.equal({
  441. status: '404',
  442. message: 'Not Found. The server did not find anything matching the Request-URI.'
  443. });
  444. }
  445.  
  446. expect(getTokenStub.calledOnce).to.be.true;
  447. expect(getStub.calledOnce).to.be.true;
  448. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  449. expect(getStub.calledWith(options)).to.be.true;
  450. });
  451.  
  452. it('should return error if token and cartID have not been received', async () => {
  453. getTokenStub.throws({
  454. error: {
  455. status: '404',
  456. message: 'Token error'
  457. }
  458. });
  459. try {
  460. const response = await appliancesStorageFacade.getDevice(deviceID);
  461. }
  462. catch (err) {
  463. expect(err).to.deep.equal({
  464. status: '404',
  465. message: 'Token error'
  466. });
  467. }
  468.  
  469. expect(getTokenStub.calledOnce).to.be.true;
  470. expect(getStub.calledOnce).to.be.false;
  471. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  472. expect(getStub.calledWith(options)).to.be.false;
  473. });
  474. });
  475.  
  476. describe('Get Devices', () => {
  477. const applianceOwner = 'applianceOwner';
  478. const listOfDevices = ['appliance1', 'appliance2'];
  479.  
  480. const options = {
  481. uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/?fetchAll=true&q=applianceOwner:"${applianceOwner}"`,
  482. headers: {
  483. Authorization: tokenAuthorization
  484. },
  485. json: true
  486. };
  487.  
  488. before(() => {
  489. getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
  490. getStub = sinon.stub(request, 'get');
  491. });
  492.  
  493. after(() => {
  494. getTokenStub.restore();
  495. getStub.restore();
  496. });
  497.  
  498. beforeEach(() => {
  499. getTokenStub.reset();
  500. getStub.reset();
  501. });
  502.  
  503. it('should return status 200 and list of devices for known applianceOwner/tenant', async () => {
  504. getTokenStub.returns(token);
  505. getStub.returns(
  506. listOfDevices
  507. );
  508.  
  509. const response = await appliancesStorageFacade.getDevices(applianceOwner);
  510.  
  511. expect(response).to.deep.equal({ status: '200', message: listOfDevices });
  512.  
  513. expect(getTokenStub.calledOnce).to.be.true;
  514. expect(getStub.calledOnce).to.be.true;
  515. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  516. expect(getStub.calledWith(options)).to.be.true;
  517. });
  518.  
  519. it('should return error if token has been received but cartID hasnt', async () => {
  520. getTokenStub.returns(token);
  521. getStub.throws({
  522. error: {
  523. status: '404',
  524. message: 'Not Found. The server did not find anything matching the Request-URI.'
  525. }
  526. });
  527. try {
  528. const response = await appliancesStorageFacade.getDevices(applianceOwner);
  529. }
  530. catch (err) {
  531. expect(err).to.deep.equal({
  532. status: '404',
  533. message: 'Not Found. The server did not find anything matching the Request-URI.'
  534. });
  535. }
  536.  
  537. expect(getTokenStub.calledOnce).to.be.true;
  538. expect(getStub.calledOnce).to.be.true;
  539. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  540. expect(getStub.calledWith(options)).to.be.true;
  541. });
  542.  
  543. it('should return error if token and cartID have not been received', async () => {
  544. getTokenStub.throws({
  545. error: {
  546. status: '404',
  547. message: 'Token error'
  548. }
  549. });
  550. try {
  551. const response = await appliancesStorageFacade.getDevices(applianceOwner);
  552. }
  553. catch (err) {
  554. expect(err).to.deep.equal({
  555. status: '404',
  556. message: 'Token error'
  557. });
  558. }
  559.  
  560. expect(getTokenStub.calledOnce).to.be.true;
  561. expect(getStub.calledOnce).to.be.false;
  562. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  563. expect(getStub.calledWith(options)).to.be.false;
  564. });
  565. });
  566.  
  567. describe('Update Device', () => {
  568.  
  569. const deviceID = 'deviceID';
  570. const cartID = 'cartID';
  571. const appliances = { status: '200', message: ['appliance1', 'appliance2'] };
  572.  
  573. const options = {
  574. uri: `${originalYaasConfig.url}/${originalYaasConfig.tenant}/${originalYaasConfig.client}/data/${originalYaasConfig.type}/${deviceID}/?&path=true&partial=true`,
  575. body: {
  576. cartID: cartID
  577. },
  578. headers: {
  579. Authorization: tokenAuthorization
  580. },
  581. json: true
  582. };
  583.  
  584. before(() => {
  585. getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
  586. getCartIDStub = sinon.stub(appliancesStorageFacade, 'getCartID');
  587. putStub = sinon.stub(request, 'put');
  588. });
  589.  
  590. after(() => {
  591. getTokenStub.restore();
  592. getCartIDStub.restore();
  593. putStub.restore();
  594. });
  595.  
  596. beforeEach(() => {
  597. getTokenStub.reset();
  598. getCartIDStub.reset();
  599. putStub.reset();
  600. });
  601.  
  602. it('should return status 200 and oldCartID', async () => {
  603. getTokenStub.returns(token);
  604. getCartIDStub.returns('oldCartID');
  605. putStub.returns(appliances);
  606.  
  607. const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
  608.  
  609. expect(response).to.deep.equal({
  610. status: '200',
  611. cartID: 'oldCartID'
  612. });
  613.  
  614. expect(getTokenStub.calledOnce).to.be.true;
  615. expect(getCartIDStub.calledOnce).to.be.true;
  616. expect(putStub.calledOnce).to.be.true;
  617.  
  618. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  619. expect(getCartIDStub.calledWith(deviceID, token)).to.be.true;
  620. expect(putStub.calledWith(options)).to.be.true;
  621. });
  622.  
  623. it('should return error if token and oldCartID have been received but device has not been updated', async () => {
  624. getTokenStub.returns(token);
  625. getCartIDStub.returns('oldCartID');
  626. putStub.throws({
  627. error: {
  628. status: '404',
  629. message: 'The request is about to change a non-existing resource/object'
  630. }
  631. });
  632. try {
  633. const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
  634. }
  635. catch (err) {
  636. expect(err).to.deep.equal({
  637. status: '404',
  638. message: 'The request is about to change a non-existing resource/object'
  639. });
  640. }
  641.  
  642. expect(getTokenStub.calledOnce).to.be.true;
  643. expect(getCartIDStub.calledOnce).to.be.true;
  644. expect(putStub.calledOnce).to.be.true;
  645.  
  646. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  647. expect(getCartIDStub.calledWith(deviceID, token)).to.be.true;
  648. expect(putStub.calledWith(options)).to.be.true;
  649. });
  650.  
  651. it('should return error if token has been received but oldCartID hasnt and device has not been updated', async () => {
  652. getTokenStub.returns(token);
  653. getCartIDStub.throws({
  654. error: {
  655. status: '404',
  656. message: 'Can not find device with that ID'
  657. }
  658. });
  659. try {
  660. const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
  661. }
  662. catch (err) {
  663. expect(err).to.deep.equal({
  664. status: '404',
  665. message: 'Can not find device with that ID'
  666. });
  667. }
  668.  
  669. expect(getTokenStub.calledOnce).to.be.true;
  670. expect(getCartIDStub.calledOnce).to.be.true;
  671. expect(putStub.calledOnce).to.be.false;
  672.  
  673. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  674. expect(getCartIDStub.calledWith(deviceID, token)).to.be.true;
  675. expect(putStub.calledWith(options)).to.be.false;
  676. });
  677.  
  678. it('should return error if token and oldCartID have not been received adn device has not been updated', async () => {
  679. getTokenStub.throws({
  680. error: {
  681. status: '404',
  682. message: 'Token error'
  683. }
  684. });
  685. try {
  686. const response = await appliancesStorageFacade.updateDevice(deviceID, cartID);
  687. }
  688. catch (err) {
  689. expect(err).to.deep.equal({
  690. status: '404',
  691. message: 'Token error'
  692. });
  693. }
  694.  
  695. expect(getTokenStub.calledOnce).to.be.true;
  696. expect(getCartIDStub.calledOnce).to.be.false;
  697. expect(putStub.calledOnce).to.be.false;
  698.  
  699. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  700. expect(getCartIDStub.calledWith(deviceID, token)).to.be.false;
  701. expect(putStub.calledWith(options)).to.be.false;
  702. });
  703. });
  704.  
  705.  
  706.  
  707. // check tenant //
  708. describe('Check Tenant', () => {
  709.  
  710. const JSONInfo = {
  711. tenant: 'tenant',
  712. host: 'host',
  713. baseSiteId: 'baseSiteId'
  714. };
  715.  
  716. const tenantInfo = {
  717. status: '200',
  718. message: [{ tenant: 'tenant', id: 'id' }]
  719.  
  720. };
  721.  
  722. const options = {
  723. uri: `${originalYaasConfigBuilderModule.url}/${originalYaasConfigBuilderModule.tenant}/${originalYaasConfigBuilderModule.client}/data/${originalYaasConfigBuilderModule.type}/${tenantInfo.message[0].id}`,
  724. body: JSONInfo,
  725. headers: {
  726. Authorization: tokenAuthorization
  727. },
  728. json: true
  729. };
  730.  
  731. before(() => {
  732. getTenantInfoStub = sinon.stub(appliancesStorageFacade, 'getTenantInfo');
  733. putStub = sinon.stub(request, 'put');
  734. });
  735.  
  736. after(() => {
  737. getTenantInfoStub.restore();
  738. putStub.restore();
  739. });
  740.  
  741. beforeEach(() => {
  742. getTenantInfoStub.reset();
  743. putStub.reset();
  744. });
  745.  
  746. it('should return status 200 and message if tenant exists on DOCS REPO so tenant has been updated properly', async () => {
  747. getTenantInfoStub.returns(tenantInfo);
  748. putStub.returns({ status: '200', message: 'Tenant has been updated properly' });
  749.  
  750. const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
  751.  
  752. expect(response).to.deep.equal({ status: '200', message: 'Tenant has been updated properly' });
  753.  
  754. expect(getTenantInfoStub.calledOnce).to.be.true;
  755. expect(putStub.calledOnce).to.be.true;
  756. expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
  757. expect(putStub.calledWith(options)).to.be.true;
  758. });
  759.  
  760. it('should return error if tenant exists on DOCS REPO but tenant hasnt been updated properly', async () => {
  761. getTenantInfoStub.returns(tenantInfo);
  762. putStub.throws({
  763. error: {
  764. status: '403',
  765. message: 'Tenant can not be update'
  766. }
  767. });
  768.  
  769. try {
  770. const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
  771. }
  772. catch (err) {
  773. expect(err).to.deep.equal({ status: '403', message: 'Tenant can not be update' });
  774. }
  775. expect(getTenantInfoStub.calledOnce).to.be.true;
  776. expect(putStub.calledOnce).to.be.true;
  777. expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
  778. expect(putStub.calledWith(options)).to.be.true;
  779. });
  780.  
  781. it('should return false if tenant doesnt exist on DOCS REPO so tenant wont be update', async () => {
  782. getTenantInfoStub.returns({
  783. status: '200',
  784. message: []
  785. });
  786.  
  787. const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
  788.  
  789. expect(response).to.deep.equal(false);
  790.  
  791. expect(getTenantInfoStub.calledOnce).to.be.true;
  792. expect(putStub.calledOnce).to.be.false;
  793. expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
  794. expect(putStub.calledWith(options)).to.be.false;
  795. });
  796.  
  797. it('should return error if tenant hasnt been received from DOCS REPO so tenant wont be update', async () => {
  798. getTenantInfoStub.throws({
  799. error: {
  800. status: '403',
  801. message: 'Tenant can not be received'
  802. }
  803. });
  804.  
  805. try {
  806. const response = await appliancesStorageFacade.checkTenant(JSONInfo, token);
  807. }
  808. catch (err) {
  809. expect(err).to.deep.equal({status: '403', message: 'Tenant can not be received'});
  810. }
  811.  
  812. expect(getTenantInfoStub.calledOnce).to.be.true;
  813. expect(putStub.calledOnce).to.be.false;
  814. expect(getTenantInfoStub.calledWith(JSONInfo.tenant)).to.be.true;
  815. expect(putStub.calledWith(options)).to.be.false;
  816. });
  817. });
  818.  
  819. describe('Save Tenant Info', () => {
  820.  
  821. const tenantInfo = {
  822. tenant: 'tenant',
  823. host: 'host',
  824. baseSiteId: 'baseSiteId'
  825. };
  826.  
  827. const options = {
  828. body: tenantInfo,
  829. uri: `${originalYaasConfigBuilderModule.url}/${originalYaasConfigBuilderModule.tenant}/${originalYaasConfigBuilderModule.client}/data/${originalYaasConfigBuilderModule.type}/`,
  830. headers: {
  831. Authorization: tokenAuthorization
  832. },
  833. json: true
  834. };
  835.  
  836. before(() => {
  837. checkTenantStub = sinon.stub(appliancesStorageFacade, 'checkTenant');
  838. getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
  839. postStub = sinon.stub(request, 'post');
  840. });
  841.  
  842. after(() => {
  843. checkTenantStub.restore();
  844. getTokenStub.restore();
  845. postStub.restore();
  846. });
  847.  
  848. beforeEach(() => {
  849. checkTenantStub.reset();
  850. getTokenStub.reset();
  851. postStub.reset();
  852. });
  853.  
  854. it('should return status 201 and message if token has been received properly and tenant will be save for the first time', async () => {
  855. getTokenStub.returns(token);
  856. checkTenantStub.returns(false);
  857. postStub.returns({ status: '201', message: 'Tenant info saved properly' });
  858.  
  859. const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
  860.  
  861. expect(response).to.deep.equal({ status: '201', message: 'Tenant info saved properly' });
  862.  
  863. expect(getTokenStub.calledOnce).to.be.true;
  864. expect(checkTenantStub.calledOnce).to.be.true;
  865. expect(postStub.calledOnce).to.be.true;
  866. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  867. expect(checkTenantStub.calledWith(tenantInfo)).to.be.true;
  868. expect(postStub.calledWith(options)).to.be.true;
  869. });
  870.  
  871. it('should return error if tenant has been received but has been error during saving tenant for the first time', async () => {
  872. getTokenStub.returns(token);
  873. checkTenantStub.returns(false);
  874. postStub.throws({
  875. error: {
  876. status: '400',
  877. message: 'Tenant info can not be saved.'
  878. }
  879. });
  880.  
  881. try {
  882. const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
  883. }
  884. catch (err) {
  885. expect(err).to.deep.equal({
  886. status: '400',
  887. message: 'Tenant info can not be saved.'
  888. });
  889. }
  890.  
  891. expect(getTokenStub.calledOnce).to.be.true;
  892. expect(checkTenantStub.calledOnce).to.be.true;
  893. expect(postStub.calledOnce).to.be.true;
  894. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  895. expect(checkTenantStub.calledWith(tenantInfo)).to.be.true;
  896. expect(postStub.calledWith(options)).to.be.true;
  897. });
  898.  
  899. it('should return true if token has been received and tenant already had been existing in DOCS REPO so just has been updated', async () => {
  900. getTokenStub.returns(token);
  901. checkTenantStub.returns(true);
  902.  
  903. const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
  904.  
  905. expect(response).to.deep.equal(true);
  906.  
  907. expect(getTokenStub.calledOnce).to.be.true;
  908. expect(checkTenantStub.calledOnce).to.be.true;
  909. expect(postStub.calledOnce).to.be.false;
  910. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  911. expect(checkTenantStub.calledWith(tenantInfo)).to.be.true;
  912. expect(postStub.calledWith(options)).to.be.false;
  913.  
  914. });
  915.  
  916. it('should return error if token hasnt been received so tenant havent been saved', async () => {
  917. getTokenStub.throws({
  918. error: {
  919. status: '404',
  920. message: 'Token error'
  921. }
  922. });
  923.  
  924. try {
  925. const response = await appliancesStorageFacade.saveTenantInfo(tenantInfo);
  926. }
  927. catch (err) {
  928. expect(err).to.deep.equal({
  929. status: '404',
  930. message: 'Token error'
  931. });
  932. }
  933.  
  934. expect(getTokenStub.calledOnce).to.be.true;
  935. expect(checkTenantStub.calledOnce).to.be.false;
  936. expect(postStub.calledOnce).to.be.false;
  937. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  938. expect(checkTenantStub.calledWith(tenantInfo)).to.be.false;
  939. expect(postStub.calledWith(options)).to.be.false;
  940. });
  941. });
  942.  
  943. describe('Get Tenant Info', () => {
  944. const tenant = 'tenant';
  945. const tenantInfo = {
  946. host: 'host',
  947. baseSiteId: 'baseSiteId'
  948. };
  949.  
  950. const options = {
  951. uri: `${originalYaasConfigBuilderModule.url}/${originalYaasConfigBuilderModule.tenant}/${originalYaasConfigBuilderModule.client}/data/${originalYaasConfigBuilderModule.type}/?fetchAll=true&q=tenant:"${tenant}"`,
  952. headers: {
  953. Authorization: tokenAuthorization
  954. },
  955. json: true
  956. };
  957.  
  958. before(() => {
  959. getTokenStub = sinon.stub(appliancesStorageFacade, 'getToken');
  960. getStub = sinon.stub(request, 'get');
  961. });
  962.  
  963. after(() => {
  964. getTokenStub.restore();
  965. getStub.restore();
  966. });
  967.  
  968. beforeEach(() => {
  969. getTokenStub.reset();
  970. getStub.reset();
  971. });
  972.  
  973. it('should return status 200 and message if token and tenant info received properly', async () => {
  974. getTokenStub.returns(token);
  975. getStub.returns(tenantInfo);
  976.  
  977. const response = await appliancesStorageFacade.getTenantInfo(tenant);
  978.  
  979. expect(response).to.deep.equal({ status: '200', message: tenantInfo });
  980.  
  981. expect(getTokenStub.calledOnce).to.be.true;
  982. expect(getStub.calledOnce).to.be.true;
  983. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  984. expect(getStub.calledWith(options)).to.be.true;
  985. });
  986.  
  987. it('should return error if tenant has been received but data havent been gotten', async () => {
  988. getTokenStub.returns(token);
  989. getStub.throws({
  990. error: {
  991. status: '400',
  992. message: 'Tenant info can not be gotten.'
  993. }
  994. });
  995.  
  996. try {
  997. const response = await appliancesStorageFacade.getTenantInfo(tenant);
  998. }
  999. catch (err) {
  1000. expect(err).to.deep.equal({
  1001. status: '400',
  1002. message: 'Tenant info can not be gotten.'
  1003. });
  1004. }
  1005.  
  1006. expect(getTokenStub.calledOnce).to.be.true;
  1007. expect(getStub.calledOnce).to.be.true;
  1008. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  1009. expect(getStub.calledWith(options)).to.be.true;
  1010. });
  1011.  
  1012. it('should return error if token hasnt been received so data havent been gotten', async () => {
  1013. getTokenStub.throws({
  1014. error: {
  1015. status: '404',
  1016. message: 'Token error'
  1017. }
  1018. });
  1019.  
  1020. try {
  1021. const response = await appliancesStorageFacade.getTenantInfo(tenant);
  1022. }
  1023. catch (err) {
  1024. expect(err).to.deep.equal({
  1025. status: '404',
  1026. message: 'Token error'
  1027. });
  1028. }
  1029.  
  1030. expect(getTokenStub.calledOnce).to.be.true;
  1031. expect(getStub.calledOnce).to.be.false;
  1032. expect(getTokenStub.calledWith(originalYaasData)).to.be.true;
  1033. expect(getStub.calledWith(options)).to.be.false;
  1034. });
  1035. });
  1036. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement