manage_alert_notifications_controller_test.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 App = require('app');
  19. var controller;
  20. var helpers = require('test/helpers');
  21. describe('App.ManageAlertNotificationsController', function () {
  22. beforeEach(function () {
  23. controller = App.ManageAlertNotificationsController.create({});
  24. sinon.stub(App.ajax, 'send');
  25. });
  26. afterEach(function () {
  27. App.ajax.send.restore();
  28. });
  29. describe('#alertNotifications', function () {
  30. beforeEach(function () {
  31. sinon.stub(App.AlertNotification, 'find', function () {
  32. return [1, 2, 3];
  33. });
  34. });
  35. afterEach(function () {
  36. App.AlertNotification.find.restore();
  37. });
  38. it("should return all alert notifications if controller isLoaded", function () {
  39. controller.set('isLoaded', true);
  40. expect(controller.get('alertNotifications')).to.eql([1, 2, 3]);
  41. });
  42. it("should return [] if controller isLoaded=false", function () {
  43. controller.set('isLoaded', false);
  44. expect(controller.get('alertNotifications')).to.eql([]);
  45. });
  46. });
  47. describe('#loadAlertNotifications()', function () {
  48. it("should send ajax request and set isLoaded to false", function () {
  49. controller.set('isLoaded', true);
  50. controller.loadAlertNotifications();
  51. expect(controller.get('isLoaded')).to.be.false;
  52. });
  53. });
  54. describe('#getAlertNotificationsSuccessCallback()', function () {
  55. beforeEach(function () {
  56. sinon.spy(App.alertNotificationMapper, 'map');
  57. });
  58. afterEach(function () {
  59. App.alertNotificationMapper.map.restore();
  60. });
  61. it("should call mapper and set isLoaded to true", function () {
  62. controller.set('isLoaded', false);
  63. controller.getAlertNotificationsSuccessCallback('test');
  64. expect(controller.get('isLoaded')).to.be.true;
  65. expect(App.alertNotificationMapper.map.calledWith('test')).to.be.true;
  66. });
  67. });
  68. describe('#getAlertNotificationsErrorCallback()', function () {
  69. it("should set isLoaded to true", function () {
  70. controller.set('isLoaded', false);
  71. controller.getAlertNotificationsSuccessCallback('test');
  72. expect(controller.get('isLoaded')).to.be.true;
  73. });
  74. });
  75. describe('#addAlertNotification()', function () {
  76. beforeEach(function () {
  77. sinon.stub(controller, 'showCreateEditPopup');
  78. });
  79. afterEach(function () {
  80. controller.showCreateEditPopup.restore();
  81. });
  82. it("should set value for inputFields and call showCreateEditPopup", function () {
  83. controller.set('inputFields', Em.Object.create({
  84. a: {
  85. value: '',
  86. defaultValue: 'a'
  87. },
  88. b: {
  89. value: '',
  90. defaultValue: 'b'
  91. },
  92. c: {
  93. value: '',
  94. defaultValue: 'c'
  95. },
  96. severityFilter: {
  97. value: [],
  98. defaultValue: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']
  99. },
  100. global: {
  101. value: false
  102. },
  103. allGroups: Em.Object.create({
  104. value: 'custom'
  105. })
  106. }));
  107. controller.addAlertNotification();
  108. Em.keys(controller.get('inputFields')).forEach(function (key) {
  109. expect(controller.get('inputFields.' + key + '.value')).to.eql(controller.get('inputFields.' + key + '.defaultValue'));
  110. });
  111. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  112. });
  113. });
  114. describe('#editAlertNotification()', function () {
  115. beforeEach(function () {
  116. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  117. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  118. });
  119. afterEach(function () {
  120. controller.showCreateEditPopup.restore();
  121. controller.fillEditCreateInputs.restore();
  122. });
  123. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  124. controller.editAlertNotification();
  125. expect(controller.fillEditCreateInputs.calledOnce).to.be.true;
  126. expect(controller.showCreateEditPopup.calledWith(true)).to.be.true;
  127. });
  128. });
  129. describe('#fillEditCreateInputs()', function () {
  130. it("should map properties from selectedAlertNotification to inputFields (ambari.dispatch.recipients ignored)", function () {
  131. controller.set('selectedAlertNotification', Em.Object.create({
  132. name: 'test_name',
  133. global: true,
  134. description: 'test_description',
  135. groups: ['test1', 'test2'],
  136. type: 'EMAIL',
  137. alertStates: ['OK', 'UNKNOWN'],
  138. properties: {
  139. 'ambari.dispatch.recipients': [
  140. 'test1@test.test',
  141. 'test2@test.test'
  142. ],
  143. 'customName': 'customValue',
  144. "mail.smtp.from" : "from",
  145. "ambari.dispatch.credential.username" : "user",
  146. "mail.smtp.host" : "s1",
  147. "mail.smtp.port" : "25",
  148. "mail.smtp.auth" : "true",
  149. "ambari.dispatch.credential.password" : "pass",
  150. "mail.smtp.starttls.enable" : "true"
  151. }
  152. }));
  153. controller.set('inputFields', Em.Object.create({
  154. name: {
  155. value: ''
  156. },
  157. groups: {
  158. value: []
  159. },
  160. global: {
  161. value: false
  162. },
  163. allGroups: {
  164. value: false
  165. },
  166. method: {
  167. value: ''
  168. },
  169. email: {
  170. value: ''
  171. },
  172. severityFilter: {
  173. value: []
  174. },
  175. description: {
  176. value: ''
  177. },
  178. SMTPServer: {
  179. value: ''
  180. },
  181. SMTPPort: {
  182. value: ''
  183. },
  184. SMTPUseAuthentication: {
  185. value: ''
  186. },
  187. SMTPUsername: {
  188. value: ''
  189. },
  190. SMTPPassword: {
  191. value: ''
  192. },
  193. SMTPSTARTTLS: {
  194. value: ''
  195. },
  196. emailFrom: {
  197. value: ''
  198. },
  199. version: {
  200. value: ''
  201. },
  202. OIDs: {
  203. value: ''
  204. },
  205. community: {
  206. value: ''
  207. },
  208. port: {
  209. value: ''
  210. },
  211. customProperties: [
  212. {name: 'customName', value: 'customValue1', defaultValue: 'customValue1'},
  213. {name: 'customName2', value: 'customValue1', defaultValue: 'customValue1'}
  214. ]
  215. }));
  216. controller.fillEditCreateInputs();
  217. expect(JSON.stringify(controller.get('inputFields'))).to.equal(JSON.stringify({
  218. name: {
  219. value: 'test_name'
  220. },
  221. groups: {
  222. value: ['test1', 'test2']
  223. },
  224. global: {
  225. value: true,
  226. disabled: true
  227. },
  228. allGroups: {
  229. value: 'all',
  230. disabled: true
  231. },
  232. method: {
  233. value: 'EMAIL'
  234. },
  235. email: {
  236. value: 'test1@test.test, test2@test.test'
  237. },
  238. severityFilter: {
  239. value: ['OK', 'UNKNOWN']
  240. },
  241. description: {
  242. value: 'test_description'
  243. },
  244. SMTPServer: {
  245. value: 's1'
  246. },
  247. SMTPPort: {
  248. value: '25'
  249. },
  250. SMTPUseAuthentication: {
  251. value: "true"
  252. },
  253. SMTPUsername: {
  254. value: 'user'
  255. },
  256. SMTPPassword: {
  257. value: 'pass'
  258. },
  259. SMTPSTARTTLS: {
  260. value: "true"
  261. },
  262. emailFrom: {
  263. value: 'from'
  264. },
  265. version: {},
  266. OIDs: {},
  267. community: {},
  268. port: {},
  269. customProperties: [
  270. {name: 'customName', value: 'customValue', defaultValue: 'customValue'}
  271. ]
  272. }));
  273. });
  274. });
  275. describe("#showCreateEditPopup()", function () {
  276. beforeEach(function () {
  277. sinon.spy(App.ModalPopup, 'show');
  278. });
  279. afterEach(function () {
  280. App.ModalPopup.show.restore();
  281. });
  282. it("should open popup and set popup object to createEditPopup", function () {
  283. controller.showCreateEditPopup();
  284. expect(App.ModalPopup.show.calledOnce).to.be.true;
  285. });
  286. describe('#bodyClass', function () {
  287. var view;
  288. beforeEach(function () {
  289. view = controller.showCreateEditPopup().get('bodyClass').create({
  290. controller: Em.Object.create({
  291. inputFields: {
  292. global: {},
  293. allGroups: {}
  294. }
  295. }),
  296. groupSelect: Em.Object.create({
  297. selection: [],
  298. content: [{}, {}]
  299. })
  300. });
  301. });
  302. describe('#selectAllGroups', function () {
  303. it('should check inputFields.allGroups.value', function () {
  304. view.set('controller.inputFields.allGroups.value', 'all');
  305. view.selectAllGroups();
  306. expect(view.get('groupSelect.selection')).to.eql([]);
  307. view.set('controller.inputFields.allGroups.value', 'custom');
  308. view.selectAllGroups();
  309. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  310. });
  311. });
  312. describe('#clearAllGroups', function () {
  313. it('should check inputFields.allGroups.value', function () {
  314. view.set('controller.inputFields.allGroups.value', 'custom');
  315. view.selectAllGroups();
  316. view.set('controller.inputFields.allGroups.value', 'all');
  317. view.clearAllGroups();
  318. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  319. view.set('controller.inputFields.allGroups.value', 'custom');
  320. view.clearAllGroups();
  321. expect(view.get('groupSelect.selection')).to.eql([]);
  322. });
  323. });
  324. });
  325. });
  326. describe("#formatNotificationAPIObject()", function () {
  327. var inputFields = Em.Object.create({
  328. name: {
  329. value: 'test_name'
  330. },
  331. groups: {
  332. value: [{id: 1}, {id: 2}, {id: 3}]
  333. },
  334. allGroups: {
  335. value: 'custom'
  336. },
  337. global: {
  338. value: false
  339. },
  340. method: {
  341. value: 'EMAIL'
  342. },
  343. email: {
  344. value: 'test1@test.test, test2@test.test,test3@test.test , test4@test.test'
  345. },
  346. severityFilter: {
  347. value: ['OK', 'CRITICAL']
  348. },
  349. SMTPServer: {
  350. value: 's1'
  351. },
  352. SMTPPort: {
  353. value: '25'
  354. },
  355. SMTPUseAuthentication: {
  356. value: "true"
  357. },
  358. SMTPUsername: {
  359. value: 'user'
  360. },
  361. SMTPPassword: {
  362. value: 'pass'
  363. },
  364. SMTPSTARTTLS: {
  365. value: "true"
  366. },
  367. emailFrom: {
  368. value: 'from'
  369. },
  370. description: {
  371. value: 'test_description'
  372. },
  373. customProperties: [
  374. {name: 'n1', value: 'v1'},
  375. {name: 'n2', value: 'v2'}
  376. ]
  377. });
  378. it("should create object with properties from inputFields values", function () {
  379. controller.set('inputFields', inputFields);
  380. var result = controller.formatNotificationAPIObject();
  381. expect(JSON.stringify(result)).to.eql(JSON.stringify({
  382. AlertTarget: {
  383. name: 'test_name',
  384. description: 'test_description',
  385. global: false,
  386. notification_type: 'EMAIL',
  387. alert_states: ['OK', 'CRITICAL'],
  388. properties: {
  389. 'ambari.dispatch.recipients': [
  390. 'test1@test.test',
  391. 'test2@test.test',
  392. 'test3@test.test',
  393. 'test4@test.test'
  394. ],
  395. "mail.smtp.host" : "s1",
  396. "mail.smtp.port" : "25",
  397. "mail.smtp.from" : "from",
  398. "mail.smtp.auth" : "true",
  399. "ambari.dispatch.credential.username" : "user",
  400. "ambari.dispatch.credential.password" : "pass",
  401. "mail.smtp.starttls.enable" : "true",
  402. 'n1': 'v1',
  403. 'n2': 'v2'
  404. },
  405. groups: [1,2,3]
  406. }
  407. }));
  408. });
  409. it('should ignore groups if global is true', function () {
  410. controller.set('inputFields', inputFields);
  411. controller.set('inputFields.allGroups.value', 'all');
  412. var result = controller.formatNotificationAPIObject();
  413. expect(Em.keys(result.AlertTarget)).to.not.contain('groups');
  414. });
  415. });
  416. describe('#createAlertNotification()', function () {
  417. it("should send ajax request", function () {
  418. controller.createAlertNotification();
  419. expect(App.ajax.send.calledOnce).to.be.true;
  420. });
  421. });
  422. describe('#createAlertNotificationSuccessCallback()', function () {
  423. beforeEach(function () {
  424. controller.set('createEditPopup', {
  425. hide: Em.K
  426. });
  427. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  428. sinon.spy(controller.createEditPopup, 'hide');
  429. });
  430. afterEach(function () {
  431. controller.loadAlertNotifications.restore();
  432. controller.createEditPopup.hide.restore();
  433. });
  434. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  435. controller.createAlertNotificationSuccessCallback();
  436. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  437. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  438. });
  439. });
  440. describe('#updateAlertNotification()', function () {
  441. it("should send ajax request", function () {
  442. controller.createAlertNotification();
  443. expect(App.ajax.send.calledOnce).to.be.true;
  444. });
  445. });
  446. describe('#updateAlertNotificationSuccessCallback()', function () {
  447. beforeEach(function () {
  448. controller.set('createEditPopup', {
  449. hide: Em.K
  450. });
  451. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  452. sinon.spy(controller.createEditPopup, 'hide');
  453. });
  454. afterEach(function () {
  455. controller.loadAlertNotifications.restore();
  456. controller.createEditPopup.hide.restore();
  457. });
  458. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  459. controller.updateAlertNotificationSuccessCallback();
  460. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  461. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  462. });
  463. });
  464. describe('#deleteAlertNotification()', function () {
  465. beforeEach(function () {
  466. sinon.spy(App, 'showConfirmationPopup');
  467. });
  468. afterEach(function () {
  469. App.showConfirmationPopup.restore();
  470. });
  471. it("should show popup and send request on confirmation", function () {
  472. var popup = controller.deleteAlertNotification();
  473. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  474. popup.onPrimary();
  475. expect(App.ajax.send.calledOnce).to.be.true;
  476. });
  477. });
  478. describe('#deleteAlertNotificationSuccessCallback()', function () {
  479. it("should call loadAlertNotifications, selectedAlertNotification.deleteRecord and set null to selectedAlertNotification", function () {
  480. var mockSelectedAlertNotification = {
  481. deleteRecord: Em.K
  482. };
  483. controller.set('selectedAlertNotification', mockSelectedAlertNotification);
  484. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  485. sinon.spy(mockSelectedAlertNotification, 'deleteRecord');
  486. controller.deleteAlertNotificationSuccessCallback();
  487. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  488. expect(mockSelectedAlertNotification.deleteRecord.calledOnce).to.be.true;
  489. expect(controller.get('selectedAlertNotification')).to.equal(null);
  490. controller.loadAlertNotifications.restore();
  491. mockSelectedAlertNotification.deleteRecord.restore();
  492. });
  493. });
  494. describe('#duplicateAlertNotification()', function () {
  495. beforeEach(function () {
  496. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  497. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  498. });
  499. afterEach(function () {
  500. controller.fillEditCreateInputs.restore();
  501. controller.showCreateEditPopup.restore();
  502. });
  503. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  504. controller.duplicateAlertNotification();
  505. expect(controller.fillEditCreateInputs.calledWith(true)).to.be.true;
  506. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  507. });
  508. });
  509. describe('#addCustomProperty', function () {
  510. beforeEach(function () {
  511. controller.set('inputFields.customProperties', []);
  512. });
  513. it('should add custom Property to customProperties', function () {
  514. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  515. controller.addCustomProperty();
  516. helpers.nestedExpect([{name: 'n1', value: 'v1', defaultValue: 'v1'}], controller.get('inputFields.customProperties'));
  517. });
  518. });
  519. describe('#removeCustomPropertyHandler', function () {
  520. var c = {name: 'n2', value: 'v2', defaultValue: 'v2'};
  521. beforeEach(function () {
  522. controller.set('inputFields.customProperties', [
  523. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  524. c,
  525. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  526. ]);
  527. });
  528. it('should remove selected custom property', function () {
  529. controller.removeCustomPropertyHandler({context: c});
  530. helpers.nestedExpect(
  531. [
  532. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  533. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  534. ],
  535. controller.get('inputFields.customProperties')
  536. );
  537. });
  538. });
  539. describe('#addCustomPropertyHandler', function () {
  540. it('should clean up newCustomProperty on primary click', function () {
  541. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  542. controller.addCustomPropertyHandler().onPrimary();
  543. expect(controller.get('newCustomProperty')).to.eql({name: '', value: ''});
  544. });
  545. describe('#bodyClass', function () {
  546. var view;
  547. beforeEach(function () {
  548. view = controller.addCustomPropertyHandler().get('bodyClass').create({
  549. parentView: Em.View.create(),
  550. controller: Em.Object.create({
  551. inputFields: Em.Object.create({
  552. customProperties: [
  553. {name: 'n1', value: 'v1', defaultValue: 'v1'}
  554. ]
  555. }),
  556. newCustomProperty: {name: '', value: ''}
  557. })
  558. });
  559. });
  560. describe('#errorHandler', function () {
  561. it('should fire invalid name', function () {
  562. view.set('controller.newCustomProperty.name', '!!');
  563. view.errorsHandler();
  564. expect(view.get('isError')).to.be.true;
  565. expect(view.get('parentView.disablePrimary')).to.be.true;
  566. expect(view.get('errorMessage.length') > 0).to.be.true;
  567. });
  568. it('should fire existing property name', function () {
  569. view.set('controller.newCustomProperty.name', 'n1');
  570. view.errorsHandler();
  571. expect(view.get('isError')).to.be.true;
  572. expect(view.get('parentView.disablePrimary')).to.be.true;
  573. expect(view.get('errorMessage.length') > 0).to.be.true;
  574. });
  575. });
  576. });
  577. });
  578. });