credentials_test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var credentials = require('utils/credentials');
  19. var testHelpers = require('test/helpers');
  20. describe('credentials utils', function () {
  21. var storeTypeStatusMock = function (clusterName, key) {
  22. var result = {};
  23. result[key] = clusterName;
  24. return result;
  25. };
  26. describe('#createCredentials', function () {
  27. it('should send AJAX request', function () {
  28. credentials.createCredentials('c', 'a', {});
  29. expect(testHelpers.findAjaxRequest('name', 'credentials.create')).to.eql([
  30. {
  31. sender: credentials,
  32. name: 'credentials.create',
  33. data: {
  34. clusterName: 'c',
  35. resource: {},
  36. alias: 'a'
  37. },
  38. error: 'createCredentialsErrorCallback'
  39. }
  40. ]);
  41. });
  42. });
  43. describe('#credentialsSuccessCallback', function () {
  44. var params = {
  45. callback: Em.K
  46. },
  47. cases = [
  48. {
  49. items: [],
  50. callbackArgument: [],
  51. title: 'no data returned'
  52. },
  53. {
  54. items: [{}, {}],
  55. callbackArgument: [undefined, undefined],
  56. title: 'empty data returned'
  57. },
  58. {
  59. items: [
  60. {
  61. Credential: {
  62. id: 0
  63. }
  64. },
  65. {
  66. Credential: {
  67. id: 1
  68. }
  69. }
  70. ],
  71. callbackArgument: [
  72. {
  73. id: 0
  74. },
  75. {
  76. id: 1
  77. }
  78. ],
  79. title: 'valid data returned'
  80. }
  81. ];
  82. beforeEach(function () {
  83. sinon.spy(params, 'callback');
  84. });
  85. afterEach(function () {
  86. params.callback.restore();
  87. });
  88. cases.forEach(function (item) {
  89. it(item.title, function () {
  90. credentials.credentialsSuccessCallback({
  91. items: item.items
  92. }, null, params);
  93. expect(params.callback.firstCall.args).to.eql([item.callbackArgument]);
  94. });
  95. });
  96. });
  97. describe('#createOrUpdateCredentials', function () {
  98. var mock = {
  99. dfd: {
  100. getCredential: null,
  101. updateCredentials: null,
  102. createCredentials: null
  103. },
  104. callback: Em.K,
  105. getCredential: function () {
  106. return mock.dfd.getCredential.promise();
  107. },
  108. updateCredentials: function () {
  109. return mock.dfd.updateCredentials.promise();
  110. },
  111. createCredentials: function () {
  112. return mock.dfd.createCredentials.promise();
  113. }
  114. },
  115. cases = [
  116. {
  117. getCredentialResolve: true,
  118. credentialsCallback: 'updateCredentials',
  119. isCredentialsCallbackResolve: true,
  120. status: 'success',
  121. result: {
  122. status: 200
  123. },
  124. callbackArgs: [
  125. true,
  126. {
  127. status: 200
  128. }
  129. ],
  130. title: 'successful credentials update'
  131. },
  132. {
  133. getCredentialResolve: true,
  134. credentialsCallback: 'updateCredentials',
  135. isCredentialsCallbackResolve: false,
  136. status: 'error',
  137. result: {
  138. status: 404
  139. },
  140. callbackArgs: [
  141. false,
  142. {
  143. status: 404
  144. }
  145. ],
  146. title: 'failed credentials update'
  147. },
  148. {
  149. getCredentialResolve: false,
  150. credentialsCallback: 'createCredentials',
  151. isCredentialsCallbackResolve: true,
  152. status: 'success',
  153. result: {
  154. status: 201
  155. },
  156. callbackArgs: [
  157. true,
  158. {
  159. status: 201
  160. }
  161. ],
  162. title: 'successful credentials creation'
  163. },
  164. {
  165. getCredentialResolve: false,
  166. credentialsCallback: 'createCredentials',
  167. isCredentialsCallbackResolve: false,
  168. status: 'error',
  169. result: {
  170. status: 500
  171. },
  172. callbackArgs: [
  173. false,
  174. {
  175. status: 500
  176. }
  177. ],
  178. title: 'failed credentials creation'
  179. }
  180. ];
  181. beforeEach(function () {
  182. sinon.stub(credentials, 'getCredential', mock.getCredential);
  183. sinon.stub(credentials, 'updateCredentials', mock.updateCredentials);
  184. sinon.stub(credentials, 'createCredentials', mock.createCredentials);
  185. sinon.spy(mock, 'callback');
  186. mock.dfd.getCredential = $.Deferred();
  187. mock.dfd.updateCredentials = $.Deferred();
  188. mock.dfd.createCredentials = $.Deferred();
  189. });
  190. afterEach(function () {
  191. credentials.getCredential.restore();
  192. credentials.updateCredentials.restore();
  193. credentials.createCredentials.restore();
  194. mock.callback.restore();
  195. });
  196. cases.forEach(function (item) {
  197. var getCredentialMethod = item.getCredentialResolve ? 'resolve' : 'reject',
  198. credentialsCallbackMethod = item.isCredentialsCallbackResolve ? 'resolve' : 'reject';
  199. it(item.title, function () {
  200. mock.dfd.getCredential[getCredentialMethod]();
  201. mock.dfd[item.credentialsCallback][credentialsCallbackMethod](null, item.status, item.result);
  202. credentials.createOrUpdateCredentials().done(mock.callback);
  203. expect(mock.callback.firstCall.args).to.eql(item.callbackArgs);
  204. });
  205. });
  206. });
  207. describe('#getCredential', function () {
  208. it('should send AJAX request', function () {
  209. credentials.getCredential('c', 'a', Em.K);
  210. expect(testHelpers.findAjaxRequest('name', 'credentials.get')).to.eql([
  211. {
  212. sender: credentials,
  213. name: 'credentials.get',
  214. data: {
  215. clusterName: 'c',
  216. alias: 'a',
  217. callback: Em.K
  218. },
  219. success: 'getCredentialSuccessCallback',
  220. error: 'getCredentialErrorCallback'
  221. }
  222. ]);
  223. });
  224. });
  225. describe('#getCredentialSuccessCallback', function () {
  226. var params = {
  227. callback: Em.K
  228. },
  229. cases = [
  230. {
  231. data: null,
  232. callback: undefined,
  233. callbackCallCount: 0,
  234. title: 'no callback passed'
  235. },
  236. {
  237. data: null,
  238. callback: null,
  239. callbackCallCount: 0,
  240. title: 'invalid callback passed'
  241. },
  242. {
  243. data: null,
  244. callbackCallCount: 1,
  245. callbackArgument: null,
  246. title: 'no data passed'
  247. },
  248. {
  249. data: {},
  250. callbackCallCount: 1,
  251. callbackArgument: null,
  252. title: 'no credential info passed'
  253. },
  254. {
  255. data: {
  256. Credential: 'c'
  257. },
  258. callbackCallCount: 1,
  259. callbackArgument: 'c',
  260. title: 'credential info passed'
  261. }
  262. ];
  263. cases.forEach(function (item) {
  264. describe(item.title, function () {
  265. beforeEach(function () {
  266. sinon.spy(params, 'callback');
  267. credentials.getCredentialSuccessCallback(item.data, null, item.hasOwnProperty('callback') ? {
  268. callback: item.callback
  269. } : params);
  270. });
  271. afterEach(function () {
  272. params.callback.restore();
  273. });
  274. it('callback call count', function () {
  275. expect(params.callback.callCount).to.equal(item.callbackCallCount);
  276. });
  277. if (item.callbackCallCount) {
  278. it('callback argument', function () {
  279. expect(params.callback.firstCall.args).to.eql([item.callbackArgument]);
  280. });
  281. }
  282. });
  283. });
  284. });
  285. describe('#updateCredentials', function () {
  286. it('should send AJAX request', function () {
  287. credentials.updateCredentials('c', 'a', {});
  288. expect(testHelpers.findAjaxRequest('name', 'credentials.update')).to.eql([
  289. {
  290. sender: credentials,
  291. name: 'credentials.update',
  292. data: {
  293. clusterName: 'c',
  294. alias: 'a',
  295. resource: {}
  296. }
  297. }
  298. ]);
  299. });
  300. });
  301. describe('#credentials', function () {
  302. it('should send AJAX request', function () {
  303. credentials.credentials('c', Em.K);
  304. expect(testHelpers.findAjaxRequest('name', 'credentials.list')).to.eql([
  305. {
  306. sender: credentials,
  307. name: 'credentials.list',
  308. data: {
  309. clusterName: 'c',
  310. callback: Em.K
  311. },
  312. success: 'credentialsSuccessCallback'
  313. }
  314. ]);
  315. });
  316. });
  317. describe('#removeCredentials', function () {
  318. it('should send AJAX request', function () {
  319. credentials.removeCredentials('c', 'a');
  320. expect(testHelpers.findAjaxRequest('name', 'credentials.delete')).to.eql([
  321. {
  322. sender: credentials,
  323. name: 'credentials.delete',
  324. data: {
  325. clusterName: 'c',
  326. alias: 'a'
  327. }
  328. }
  329. ]);
  330. });
  331. });
  332. describe('#storageInfo', function () {
  333. it('should send AJAX request', function () {
  334. credentials.storageInfo('c', Em.K);
  335. expect(testHelpers.findAjaxRequest('name', 'credentials.store.info')).to.eql([
  336. {
  337. sender: credentials,
  338. name: 'credentials.store.info',
  339. data: {
  340. clusterName: 'c',
  341. callback: Em.K
  342. },
  343. success: 'storageInfoSuccessCallback'
  344. }
  345. ]);
  346. });
  347. });
  348. describe('#storageInfoSuccessCallback', function () {
  349. var params = {
  350. callback: Em.K
  351. },
  352. cases = [
  353. {
  354. callbackArgument: null,
  355. title: 'no clusters'
  356. },
  357. {
  358. clusters: null,
  359. callbackArgument: null,
  360. title: 'invalid clusters info'
  361. },
  362. {
  363. clusters: {},
  364. callbackArgument: {
  365. persistent: false,
  366. temporary: false
  367. },
  368. title: 'empty clusters info'
  369. },
  370. {
  371. clusters: {
  372. credential_store_properties: {
  373. 'storage.persistent': true,
  374. 'storage.temporary': true
  375. }
  376. },
  377. callbackArgument: {
  378. persistent: false,
  379. temporary: false
  380. },
  381. title: 'invalid storage properties format'
  382. },
  383. {
  384. clusters: {
  385. credential_store_properties: {}
  386. },
  387. callbackArgument: {
  388. persistent: false,
  389. temporary: false
  390. },
  391. title: 'no storage properties'
  392. },
  393. {
  394. clusters: {
  395. credential_store_properties: {
  396. 'storage.persistent': 'true',
  397. 'storage.temporary': 'false'
  398. }
  399. },
  400. callbackArgument: {
  401. persistent: true,
  402. temporary: false
  403. },
  404. title: 'valid storage properties format - persistent storage'
  405. },
  406. {
  407. clusters: {
  408. credential_store_properties: {
  409. 'storage.persistent': 'false',
  410. 'storage.temporary': 'true'
  411. }
  412. },
  413. callbackArgument: {
  414. persistent: false,
  415. temporary: true
  416. },
  417. title: 'valid storage properties format - temporary storage'
  418. },
  419. {
  420. clusters: {
  421. credential_store_properties: {
  422. 'storage.persistent': 'true',
  423. 'storage.temporary': 'true'
  424. }
  425. },
  426. callbackArgument: {
  427. persistent: true,
  428. temporary: true
  429. },
  430. title: 'valid storage properties format - both types'
  431. }
  432. ];
  433. cases.forEach(function (item) {
  434. describe(item.title, function () {
  435. beforeEach(function () {
  436. sinon.spy(params, 'callback');
  437. credentials.storageInfoSuccessCallback({
  438. Clusters: item.clusters
  439. }, null, params);
  440. });
  441. afterEach(function () {
  442. params.callback.restore();
  443. });
  444. it('callback execution', function () {
  445. expect(params.callback.calledOnce).to.be.true;
  446. });
  447. it('callback argument', function () {
  448. expect(params.callback.firstCall.args).to.eql([item.callbackArgument]);
  449. });
  450. });
  451. });
  452. });
  453. describe('#isStorePersisted', function () {
  454. beforeEach(function () {
  455. sinon.stub(credentials, 'storeTypeStatus', storeTypeStatusMock);
  456. });
  457. afterEach(function () {
  458. credentials.storeTypeStatus.restore();
  459. });
  460. it('should return storeTypeStatus result', function () {
  461. expect(credentials.isStorePersisted('c')).to.eql({
  462. persistent: 'c'
  463. });
  464. });
  465. });
  466. describe('#isStoreTemporary', function () {
  467. beforeEach(function () {
  468. sinon.stub(credentials, 'storeTypeStatus', storeTypeStatusMock);
  469. });
  470. afterEach(function () {
  471. credentials.storeTypeStatus.restore();
  472. });
  473. it('should return storeTypeStatus result', function () {
  474. expect(credentials.isStoreTemporary('c')).to.eql({
  475. temporary: 'c'
  476. });
  477. });
  478. });
  479. describe('#storeTypeStatus', function () {
  480. var mock = {
  481. successCallback: Em.K,
  482. errorCallback: Em.K
  483. },
  484. data = {
  485. clusterName: 'c'
  486. },
  487. error = {
  488. status: 404
  489. },
  490. cases = [
  491. {
  492. isSuccess: true,
  493. callbackArgument: data,
  494. title: 'success'
  495. },
  496. {
  497. isSuccess: false,
  498. callbackArgument: error,
  499. title: 'fail'
  500. }
  501. ];
  502. cases.forEach(function (item) {
  503. describe(item.title, function () {
  504. var callbackName = item.isSuccess ? 'successCallback' : 'errorCallback';
  505. beforeEach(function () {
  506. sinon.spy(mock, 'successCallback');
  507. sinon.spy(mock, 'errorCallback');
  508. sinon.stub(credentials, 'storageInfo', function (clusterName, callback) {
  509. var dfd = $.Deferred();
  510. if (item.isSuccess) {
  511. callback({
  512. temporary: data
  513. });
  514. } else {
  515. dfd.reject(error);
  516. }
  517. return dfd.promise();
  518. });
  519. credentials.storeTypeStatus(null, 'temporary').then(mock.successCallback, mock.errorCallback);
  520. });
  521. afterEach(function () {
  522. mock.successCallback.restore();
  523. mock.errorCallback.restore();
  524. credentials.storageInfo.restore();
  525. });
  526. it('success callback', function () {
  527. expect(mock.successCallback.called).to.equal(item.isSuccess);
  528. });
  529. it('error callback', function () {
  530. expect(mock.errorCallback.called).to.not.equal(item.isSuccess);
  531. });
  532. it('callback called once', function () {
  533. expect(mock[callbackName].calledOnce).to.be.true;
  534. });
  535. it('callback arguments', function () {
  536. expect(mock[callbackName].firstCall.args).to.eql([item.callbackArgument]);
  537. });
  538. });
  539. });
  540. });
  541. describe('#createCredentialResource', function () {
  542. it('should return object with arguments', function () {
  543. expect(credentials.createCredentialResource('p', 'c', 't')).to.eql({
  544. principal: 'p',
  545. key: 'c',
  546. type: 't'
  547. });
  548. });
  549. });
  550. describe('#isKDCCredentialsPersisted', function () {
  551. var cases = [
  552. {
  553. credentials: [],
  554. isKDCCredentialsPersisted: false,
  555. title: 'empty array passed'
  556. },
  557. {
  558. credentials: [{}, {}],
  559. isKDCCredentialsPersisted: false,
  560. title: 'no aliases passed'
  561. },
  562. {
  563. credentials: [
  564. {
  565. alias: 'a0'
  566. },
  567. {
  568. alias: 'a1'
  569. }
  570. ],
  571. isKDCCredentialsPersisted: false,
  572. title: 'no KDC admin credentials passed'
  573. },
  574. {
  575. credentials: [
  576. {
  577. alias: 'kdc.admin.credential'
  578. },
  579. {
  580. alias: 'a2'
  581. }
  582. ],
  583. isKDCCredentialsPersisted: false,
  584. title: 'no KDC admin credentials type passed'
  585. },
  586. {
  587. credentials: [
  588. {
  589. alias: 'kdc.admin.credential',
  590. type: 'temporary'
  591. },
  592. {
  593. alias: 'a3'
  594. }
  595. ],
  596. isKDCCredentialsPersisted: false,
  597. title: 'temporary storage'
  598. },
  599. {
  600. credentials: [
  601. {
  602. alias: 'kdc.admin.credential',
  603. type: 'persisted'
  604. },
  605. {
  606. alias: 'kdc.admin.credential'
  607. },
  608. {
  609. alias: 'a4'
  610. }
  611. ],
  612. isKDCCredentialsPersisted: true,
  613. title: 'persistent storage'
  614. }
  615. ];
  616. cases.forEach(function (item) {
  617. it(item.title, function () {
  618. expect(credentials.isKDCCredentialsPersisted(item.credentials)).to.equal(item.isKDCCredentialsPersisted);
  619. });
  620. });
  621. });
  622. });