manage_alert_notifications_controller_test.js 19 KB

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