Advertisement
Guest User

Untitled

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