manage_alert_notifications_controller_test.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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. require('templates/main/alerts/alert_instance/status');
  22. function getController() {
  23. return App.ManageAlertNotificationsController.create({});
  24. }
  25. function getInputFields() {
  26. return Em.Object.create({
  27. name: {
  28. value: ''
  29. },
  30. groups: {
  31. value: []
  32. },
  33. global: {
  34. value: false
  35. },
  36. allGroups: {
  37. value: false
  38. },
  39. method: {
  40. value: ''
  41. },
  42. email: {
  43. value: ''
  44. },
  45. severityFilter: {
  46. value: []
  47. },
  48. description: {
  49. value: ''
  50. },
  51. SMTPServer: {
  52. value: ''
  53. },
  54. SMTPPort: {
  55. value: ''
  56. },
  57. SMTPUseAuthentication: {
  58. value: ''
  59. },
  60. SMTPUsername: {
  61. value: ''
  62. },
  63. SMTPPassword: {
  64. value: ''
  65. },
  66. retypeSMTPPassword: {
  67. value: ''
  68. },
  69. SMTPSTARTTLS: {
  70. value: ''
  71. },
  72. emailFrom: {
  73. value: ''
  74. },
  75. version: {
  76. value: ''
  77. },
  78. OIDs: {
  79. value: ''
  80. },
  81. community: {
  82. value: ''
  83. },
  84. host: {
  85. value: ''
  86. },
  87. port: {
  88. value: ''
  89. }
  90. });
  91. }
  92. var createEditPopupView = getController().showCreateEditPopup();
  93. describe('App.ManageAlertNotificationsController', function () {
  94. beforeEach(function () {
  95. controller = getController();
  96. });
  97. describe('#alertNotifications', function () {
  98. beforeEach(function () {
  99. sinon.stub(App.AlertNotification, 'find', function () {
  100. return [1, 2, 3];
  101. });
  102. });
  103. afterEach(function () {
  104. App.AlertNotification.find.restore();
  105. });
  106. it("should return all alert notifications if controller isLoaded", function () {
  107. controller.set('isLoaded', true);
  108. expect(controller.get('alertNotifications')).to.eql([1, 2, 3]);
  109. });
  110. it("should return [] if controller isLoaded=false", function () {
  111. controller.set('isLoaded', false);
  112. expect(controller.get('alertNotifications')).to.eql([]);
  113. });
  114. });
  115. describe('#loadAlertNotifications()', function () {
  116. it("should send ajax request and set isLoaded to false", function () {
  117. controller.set('isLoaded', true);
  118. controller.loadAlertNotifications();
  119. expect(controller.get('isLoaded')).to.be.false;
  120. });
  121. });
  122. describe('#getAlertNotificationsSuccessCallback()', function () {
  123. beforeEach(function () {
  124. sinon.spy(App.alertNotificationMapper, 'map');
  125. });
  126. afterEach(function () {
  127. App.alertNotificationMapper.map.restore();
  128. });
  129. it("should call mapper and set isLoaded to true", function () {
  130. controller.set('isLoaded', false);
  131. controller.getAlertNotificationsSuccessCallback('test');
  132. expect(controller.get('isLoaded')).to.be.true;
  133. expect(App.alertNotificationMapper.map.calledWith('test')).to.be.true;
  134. });
  135. });
  136. describe('#getAlertNotificationsErrorCallback()', function () {
  137. it("should set isLoaded to true", function () {
  138. controller.set('isLoaded', false);
  139. controller.getAlertNotificationsSuccessCallback('test');
  140. expect(controller.get('isLoaded')).to.be.true;
  141. });
  142. });
  143. describe('#addAlertNotification()', function () {
  144. var inputFields = Em.Object.create({
  145. a: {
  146. value: '',
  147. defaultValue: 'a'
  148. },
  149. b: {
  150. value: '',
  151. defaultValue: 'b'
  152. },
  153. c: {
  154. value: '',
  155. defaultValue: 'c'
  156. },
  157. severityFilter: {
  158. value: [],
  159. defaultValue: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']
  160. },
  161. global: {
  162. value: false
  163. },
  164. allGroups: Em.Object.create({
  165. value: 'custom'
  166. })
  167. });
  168. beforeEach(function () {
  169. sinon.stub(controller, 'showCreateEditPopup');
  170. controller.set('inputFields', inputFields);
  171. controller.addAlertNotification();
  172. });
  173. afterEach(function () {
  174. controller.showCreateEditPopup.restore();
  175. });
  176. Object.keys(inputFields).forEach(function (key) {
  177. it(key, function () {
  178. expect(controller.get('inputFields.' + key + '.value')).to.be.eql(controller.get('inputFields.' + key + '.defaultValue'));
  179. });
  180. });
  181. it("should call showCreateEditPopup", function () {
  182. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  183. });
  184. });
  185. describe('#editAlertNotification()', function () {
  186. beforeEach(function () {
  187. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  188. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  189. });
  190. afterEach(function () {
  191. controller.showCreateEditPopup.restore();
  192. controller.fillEditCreateInputs.restore();
  193. });
  194. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  195. controller.editAlertNotification();
  196. expect(controller.fillEditCreateInputs.calledOnce).to.be.true;
  197. expect(controller.showCreateEditPopup.calledWith(true)).to.be.true;
  198. });
  199. });
  200. describe('#fillEditCreateInputs()', function () {
  201. it("should map properties from selectedAlertNotification to inputFields (ambari.dispatch.recipients ignored) - EMAIL", function () {
  202. controller.set('selectedAlertNotification', Em.Object.create({
  203. name: 'test_name',
  204. global: true,
  205. description: 'test_description',
  206. groups: ['test1', 'test2'],
  207. type: 'EMAIL',
  208. alertStates: ['OK', 'UNKNOWN'],
  209. properties: {
  210. 'ambari.dispatch.recipients': [
  211. 'test1@test.test',
  212. 'test2@test.test'
  213. ],
  214. 'customName': 'customValue',
  215. "mail.smtp.from" : "from",
  216. "ambari.dispatch.credential.username" : "user",
  217. "mail.smtp.host" : "s1",
  218. "mail.smtp.port" : "25",
  219. "mail.smtp.auth" : "true",
  220. "ambari.dispatch.credential.password" : "pass",
  221. "mail.smtp.starttls.enable" : "true"
  222. }
  223. }));
  224. controller.set('inputFields', Em.Object.create({
  225. name: {
  226. value: ''
  227. },
  228. groups: {
  229. value: []
  230. },
  231. global: {
  232. value: false
  233. },
  234. allGroups: {
  235. value: false
  236. },
  237. method: {
  238. value: ''
  239. },
  240. email: {
  241. value: ''
  242. },
  243. severityFilter: {
  244. value: []
  245. },
  246. description: {
  247. value: ''
  248. },
  249. SMTPServer: {
  250. value: ''
  251. },
  252. SMTPPort: {
  253. value: ''
  254. },
  255. SMTPUseAuthentication: {
  256. value: ''
  257. },
  258. SMTPUsername: {
  259. value: ''
  260. },
  261. SMTPPassword: {
  262. value: ''
  263. },
  264. retypeSMTPPassword: {
  265. value: ''
  266. },
  267. SMTPSTARTTLS: {
  268. value: ''
  269. },
  270. emailFrom: {
  271. value: ''
  272. },
  273. version: {
  274. value: ''
  275. },
  276. OIDs: {
  277. value: ''
  278. },
  279. community: {
  280. value: ''
  281. },
  282. host: {
  283. value: ''
  284. },
  285. port: {
  286. value: ''
  287. },
  288. customProperties: [
  289. {name: 'customName', value: 'customValue1', defaultValue: 'customValue1'},
  290. {name: 'customName2', value: 'customValue1', defaultValue: 'customValue1'}
  291. ]
  292. }));
  293. controller.fillEditCreateInputs();
  294. expect(JSON.stringify(controller.get('inputFields'))).to.equal(JSON.stringify({
  295. name: {
  296. value: 'test_name'
  297. },
  298. groups: {
  299. value: ['test1', 'test2']
  300. },
  301. global: {
  302. value: true,
  303. disabled: true
  304. },
  305. allGroups: {
  306. value: 'all'
  307. },
  308. method: {
  309. value: 'EMAIL'
  310. },
  311. email: {
  312. value: 'test1@test.test, test2@test.test'
  313. },
  314. severityFilter: {
  315. value: ['OK', 'UNKNOWN']
  316. },
  317. description: {
  318. value: 'test_description'
  319. },
  320. SMTPServer: {
  321. value: 's1'
  322. },
  323. SMTPPort: {
  324. value: '25'
  325. },
  326. SMTPUseAuthentication: {
  327. value: true
  328. },
  329. SMTPUsername: {
  330. value: 'user'
  331. },
  332. SMTPPassword: {
  333. value: 'pass'
  334. },
  335. retypeSMTPPassword: {
  336. value: 'pass'
  337. },
  338. SMTPSTARTTLS: {
  339. value: true
  340. },
  341. emailFrom: {
  342. value: 'from'
  343. },
  344. version: {},
  345. OIDs: {},
  346. community: {},
  347. host: {
  348. value: 'test1@test.test, test2@test.test'
  349. },
  350. port: {},
  351. customProperties: [
  352. {name: 'customName', value: 'customValue', defaultValue: 'customValue'}
  353. ]
  354. }));
  355. });
  356. it("should map properties from selectedAlertNotification to inputFields (ambari.dispatch.recipients ignored) - SNMP", function () {
  357. controller.set('selectedAlertNotification', Em.Object.create({
  358. name: 'test_SNMP_name',
  359. global: true,
  360. description: 'test_description',
  361. groups: ['test1', 'test2'],
  362. type: 'SNMP',
  363. alertStates: ['OK', 'UNKNOWN'],
  364. properties: {
  365. 'ambari.dispatch.recipients': [
  366. 'c6401.ambari.apache.org',
  367. 'c6402.ambari.apache.org'
  368. ],
  369. 'customName': 'customValue',
  370. 'ambari.dispatch.snmp.version': 'SNMPv1',
  371. 'ambari.dispatch.snmp.oids.trap': '1',
  372. 'ambari.dispatch.snmp.community': 'snmp',
  373. 'ambari.dispatch.snmp.port': 161
  374. }
  375. }));
  376. controller.set('inputFields', Em.Object.create({
  377. name: {
  378. value: ''
  379. },
  380. groups: {
  381. value: []
  382. },
  383. global: {
  384. value: false
  385. },
  386. allGroups: {
  387. value: false
  388. },
  389. method: {
  390. value: ''
  391. },
  392. email: {
  393. value: ''
  394. },
  395. severityFilter: {
  396. value: []
  397. },
  398. description: {
  399. value: ''
  400. },
  401. SMTPServer: {
  402. value: ''
  403. },
  404. SMTPPort: {
  405. value: ''
  406. },
  407. SMTPUseAuthentication: {
  408. value: ''
  409. },
  410. SMTPUsername: {
  411. value: ''
  412. },
  413. SMTPPassword: {
  414. value: ''
  415. },
  416. retypeSMTPPassword: {
  417. value: ''
  418. },
  419. SMTPSTARTTLS: {
  420. value: ''
  421. },
  422. emailFrom: {
  423. value: ''
  424. },
  425. version: {
  426. value: ''
  427. },
  428. OIDs: {
  429. value: ''
  430. },
  431. community: {
  432. value: ''
  433. },
  434. host: {
  435. value: ''
  436. },
  437. port: {
  438. value: ''
  439. },
  440. customProperties: [
  441. {name: 'customName', value: 'customValue1', defaultValue: 'customValue1'},
  442. {name: 'customName2', value: 'customValue1', defaultValue: 'customValue1'}
  443. ]
  444. }));
  445. controller.fillEditCreateInputs();
  446. expect(JSON.stringify(controller.get('inputFields'))).to.equal(JSON.stringify({
  447. name: {
  448. value: 'test_SNMP_name'
  449. },
  450. groups: {
  451. value: ['test1', 'test2']
  452. },
  453. global: {
  454. value: true,
  455. disabled: true
  456. },
  457. allGroups: {
  458. value: 'all'
  459. },
  460. method: {
  461. value: 'SNMP'
  462. },
  463. email: {
  464. value: 'c6401.ambari.apache.org, c6402.ambari.apache.org'
  465. },
  466. severityFilter: {
  467. value: ['OK', 'UNKNOWN']
  468. },
  469. description: {
  470. value: 'test_description'
  471. },
  472. SMTPServer: {},
  473. SMTPPort: {},
  474. SMTPUseAuthentication: {
  475. value: true
  476. },
  477. SMTPUsername: {},
  478. SMTPPassword: {},
  479. retypeSMTPPassword: {},
  480. SMTPSTARTTLS: {
  481. value: true
  482. },
  483. emailFrom: {},
  484. version: {
  485. value:'SNMPv1'
  486. },
  487. OIDs: {
  488. value: '1'
  489. },
  490. community: {
  491. value: 'snmp'
  492. },
  493. host: {
  494. value: 'c6401.ambari.apache.org, c6402.ambari.apache.org'
  495. },
  496. port: {
  497. value: 161
  498. },
  499. customProperties: [
  500. {name: 'customName', value: 'customValue', defaultValue: 'customValue'}
  501. ]
  502. }));
  503. })
  504. });
  505. describe("#showCreateEditPopup()", function () {
  506. beforeEach(function () {
  507. sinon.spy(App.ModalPopup, 'show');
  508. });
  509. afterEach(function () {
  510. App.ModalPopup.show.restore();
  511. });
  512. it("should open popup and set popup object to createEditPopup", function () {
  513. controller.showCreateEditPopup();
  514. expect(App.ModalPopup.show.calledOnce).to.be.true;
  515. });
  516. App.TestAliases.testAsComputedOr(getController().showCreateEditPopup(), 'disablePrimary', ['isSaving', 'hasErrors']);
  517. describe('#bodyClass', function () {
  518. function getBodyClass() {
  519. return createEditPopupView.get('bodyClass').create({
  520. controller: Em.Object.create({
  521. inputFields: {
  522. name: {},
  523. global: {},
  524. allGroups: {},
  525. SMTPUseAuthentication: {},
  526. SMTPUsername: {},
  527. SMTPPassword: {},
  528. retypeSMTPPassword: {},
  529. method: {}
  530. }
  531. }),
  532. groupSelect: Em.Object.create({
  533. selection: [],
  534. content: [{}, {}]
  535. }),
  536. parentView: Em.Object.create({
  537. hasErrors: false
  538. })
  539. });
  540. }
  541. var view;
  542. beforeEach(function () {
  543. view = getBodyClass();
  544. });
  545. App.TestAliases.testAsComputedOr(getBodyClass(), 'someErrorExists', ['nameError', 'emailToError', 'emailFromError', 'smtpPortError', 'hostError', 'portError', 'smtpUsernameError', 'smtpPasswordError', 'passwordError']);
  546. describe('#selectAllGroups', function () {
  547. it('should check inputFields.allGroups.value', function () {
  548. view.set('controller.inputFields.allGroups.value', 'all');
  549. view.selectAllGroups();
  550. expect(view.get('groupSelect.selection')).to.eql([]);
  551. view.set('controller.inputFields.allGroups.value', 'custom');
  552. view.selectAllGroups();
  553. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  554. });
  555. });
  556. describe('#clearAllGroups', function () {
  557. it('should check inputFields.allGroups.value', function () {
  558. view.set('controller.inputFields.allGroups.value', 'custom');
  559. view.selectAllGroups();
  560. view.set('controller.inputFields.allGroups.value', 'all');
  561. view.clearAllGroups();
  562. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  563. view.set('controller.inputFields.allGroups.value', 'custom');
  564. view.clearAllGroups();
  565. expect(view.get('groupSelect.selection')).to.eql([]);
  566. });
  567. });
  568. describe('#nameValidation', function () {
  569. it('should check inputFields.name.value', function () {
  570. view.set('controller.inputFields.name.value', '');
  571. expect(view.get('controller.inputFields.name.errorMsg')).to.equal(Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty'));
  572. expect(view.get('parentView.hasErrors')).to.be.true;
  573. });
  574. it('should check inputFields.name.value (2)', function () {
  575. view.set('controller.inputFields.name.errorMsg', 'error');
  576. view.set('controller.inputFields.name.value', 'test');
  577. expect(view.get('controller.inputFields.name.errorMsg')).to.equal('');
  578. });
  579. it('should check inputFields.name.value (3)', function () {
  580. view.set('isEdit', true);
  581. view.set('controller.inputFields.name.value', '');
  582. expect(view.get('controller.inputFields.name.errorMsg')).to.equal(Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty'));
  583. expect(view.get('parentView.hasErrors')).to.be.true;
  584. });
  585. it('should check inputFields.name.value (4)', function () {
  586. view.set('isEdit', true);
  587. view.set('controller.inputFields.name.errorMsg', 'error');
  588. view.set('controller.inputFields.name.value', 'test');
  589. expect(view.get('controller.inputFields.name.errorMsg')).to.equal('');
  590. });
  591. });
  592. describe('#smtpUsernameValidation', function () {
  593. beforeEach(function () {
  594. view.set('controller.inputFields', getInputFields());
  595. view.set('controller.inputFields.emailFrom.value', '1@2.com');
  596. view.set('controller.inputFields.method.value', 'EMAIL');
  597. });
  598. it('should check inputFields.SMTPUsername.value', function () {
  599. view.set('parentView.hasErrors', false);
  600. view.set('controller.inputFields.SMTPUsername.errorMsg', null);
  601. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  602. view.set('controller.inputFields.SMTPUsername.value', '');
  603. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  604. view.set('controller.inputFields.retypeSMTPPassword.value', 'pass');
  605. expect(view.get('controller.inputFields.SMTPUsername.errorMsg')).to.equal(Em.I18n.t('alerts.notifications.error.SMTPUsername'));
  606. expect(view.get('smtpUsernameError')).to.be.true;
  607. });
  608. it('should check inputFields.SMTPUsername.value (2)', function () {
  609. view.set('parentView.hasErrors', true);
  610. view.set('controller.inputFields.SMTPUsername.errorMsg', 'error');
  611. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  612. view.set('controller.inputFields.SMTPUsername.value', 'test');
  613. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  614. view.set('controller.inputFields.retypeSMTPPassword.value', 'pass');
  615. expect(view.get('controller.inputFields.SMTPUsername.errorMsg')).to.equal(null);
  616. expect(view.get('smtpUsernameError')).to.be.false;
  617. });
  618. it('should check inputFields.SMTPUsername.value (3)', function () {
  619. view.set('parentView.hasErrors', true);
  620. view.set('controller.inputFields.SMTPUsername.errorMsg', 'error');
  621. view.set('controller.inputFields.SMTPUseAuthentication.value', false);
  622. view.set('controller.inputFields.SMTPUsername.value', '');
  623. view.set('controller.inputFields.SMTPPassword.value', '');
  624. view.set('controller.inputFields.retypeSMTPPassword.value', '');
  625. expect(view.get('controller.inputFields.SMTPUsername.errorMsg')).to.equal(null);
  626. expect(view.get('smtpUsernameError')).to.be.false;
  627. });
  628. });
  629. describe('#smtpPasswordValidation', function () {
  630. beforeEach(function () {
  631. view.set('controller.inputFields', getInputFields());
  632. view.set('controller.inputFields.emailFrom.value', '1@2.com');
  633. view.set('controller.inputFields.method.value', 'EMAIL');
  634. });
  635. it('should check inputFields.SMTPPassword.value', function () {
  636. view.set('parentView.hasErrors', false);
  637. view.set('controller.inputFields.SMTPPassword.errorMsg', null);
  638. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  639. view.set('controller.inputFields.SMTPUsername.value', 'user');
  640. view.set('controller.inputFields.SMTPPassword.value', '');
  641. view.set('controller.inputFields.retypeSMTPPassword.value', '');
  642. expect(view.get('controller.inputFields.SMTPPassword.errorMsg')).to.equal(Em.I18n.t('alerts.notifications.error.SMTPPassword'));
  643. expect(view.get('smtpPasswordError')).to.be.true;
  644. });
  645. it('should check inputFields.SMTPPassword.value (2)', function () {
  646. view.set('parentView.hasErrors', true);
  647. view.set('controller.inputFields.SMTPPassword.errorMsg', 'error');
  648. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  649. view.set('controller.inputFields.SMTPUsername.value', 'user');
  650. view.set('controller.inputFields.SMTPPassword.value', 'test');
  651. view.set('controller.inputFields.retypeSMTPPassword.value', 'test');
  652. expect(view.get('controller.inputFields.SMTPPassword.errorMsg')).to.equal(null);
  653. expect(view.get('smtpPasswordError')).to.be.false;
  654. });
  655. it('should check inputFields.SMTPPassword.value (3)', function () {
  656. view.set('parentView.hasErrors', true);
  657. view.set('controller.inputFields.SMTPPassword.errorMsg', 'error');
  658. view.set('controller.inputFields.SMTPUseAuthentication.value', false);
  659. view.set('controller.inputFields.SMTPUsername.value', '');
  660. view.set('controller.inputFields.SMTPPassword.value', '');
  661. view.set('controller.inputFields.retypeSMTPPassword.value', '');
  662. expect(view.get('controller.inputFields.SMTPPassword.errorMsg')).to.equal(null);
  663. expect(view.get('smtpPasswordError')).to.be.false;
  664. });
  665. });
  666. describe('#retypePasswordValidation', function () {
  667. it('should check inputFields.retypeSMTPPassword.value', function () {
  668. view.set('controller.inputFields.retypeSMTPPassword.errorMsg', null);
  669. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  670. view.set('controller.inputFields.retypeSMTPPassword.value', 'pas');
  671. expect(view.get('controller.inputFields.retypeSMTPPassword.errorMsg')).to.equal(Em.I18n.t('alerts.notifications.error.retypePassword'));
  672. expect(view.get('parentView.hasErrors')).to.be.true;
  673. });
  674. it('should check inputFields.retypeSMTPPassword.value (2)', function () {
  675. view.set('parentView.hasErrors', true);
  676. view.set('controller.inputFields.retypeSMTPPassword.errorMsg', 'error');
  677. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  678. view.set('controller.inputFields.retypeSMTPPassword.value', 'pass');
  679. expect(view.get('controller.inputFields.retypeSMTPPassword.errorMsg')).to.equal(null);
  680. expect(view.get('parentView.hasErrors')).to.be.false;
  681. });
  682. });
  683. describe('#methodObserver', function () {
  684. var cases = [
  685. {
  686. method: 'EMAIL',
  687. errors: ['portError', 'hostError'],
  688. validators: ['emailToValidation', 'emailFromValidation', 'smtpPortValidation', 'smtpUsernameValidation', 'smtpPasswordValidation', 'retypePasswordValidation']
  689. },
  690. {
  691. method: 'SNMP',
  692. errors: ['emailToError', 'emailFromError', 'smtpPortError', 'smtpUsernameError', 'smtpPasswordError', 'passwordError'],
  693. validators: ['portValidation', 'hostsValidation']
  694. }
  695. ],
  696. validators = [];
  697. before(function () {
  698. cases.forEach(function (item) {
  699. validators.pushObjects(item.validators);
  700. });
  701. });
  702. beforeEach(function () {
  703. validators.forEach(function (item) {
  704. sinon.stub(view, item, Em.K);
  705. });
  706. });
  707. afterEach(function () {
  708. validators.forEach(function (item) {
  709. view.get(item).restore();
  710. });
  711. });
  712. cases.forEach(function (item) {
  713. describe(item.method, function () {
  714. beforeEach(function () {
  715. item.errors.forEach(function (errorName) {
  716. view.set(errorName, true);
  717. });
  718. view.set('controller.inputFields.method.value', item.method);
  719. });
  720. item.errors.forEach(function (errorName) {
  721. it(errorName + ' is false', function () {
  722. expect(view.get(errorName)).to.be.false;
  723. });
  724. });
  725. validators.forEach(function (validatorName) {
  726. var called = item.validators.contains(validatorName);
  727. it(validatorName + ' ' + (called ? '' : 'not') + ' called', function () {
  728. expect(view.get(validatorName).calledOnce).to.equal(called);
  729. });
  730. });
  731. });
  732. });
  733. });
  734. });
  735. });
  736. describe("#formatNotificationAPIObject()", function () {
  737. var inputFields = Em.Object.create({
  738. name: {
  739. value: 'test_name'
  740. },
  741. groups: {
  742. value: [{id: 1}, {id: 2}, {id: 3}]
  743. },
  744. allGroups: {
  745. value: 'custom'
  746. },
  747. global: {
  748. value: false
  749. },
  750. method: {
  751. value: 'EMAIL'
  752. },
  753. email: {
  754. value: 'test1@test.test, test2@test.test,test3@test.test , test4@test.test'
  755. },
  756. severityFilter: {
  757. value: ['OK', 'CRITICAL']
  758. },
  759. SMTPServer: {
  760. value: 's1'
  761. },
  762. SMTPPort: {
  763. value: '25'
  764. },
  765. SMTPUseAuthentication: {
  766. value: "true"
  767. },
  768. SMTPUsername: {
  769. value: 'user'
  770. },
  771. SMTPPassword: {
  772. value: 'pass'
  773. },
  774. SMTPSTARTTLS: {
  775. value: "true"
  776. },
  777. emailFrom: {
  778. value: 'from'
  779. },
  780. description: {
  781. value: 'test_description'
  782. },
  783. customProperties: [
  784. {name: 'n1', value: 'v1'},
  785. {name: 'n2', value: 'v2'}
  786. ]
  787. });
  788. it("should create object with properties from inputFields values", function () {
  789. controller.set('inputFields', inputFields);
  790. var result = controller.formatNotificationAPIObject();
  791. expect(JSON.stringify(result)).to.eql(JSON.stringify({
  792. AlertTarget: {
  793. name: 'test_name',
  794. description: 'test_description',
  795. global: false,
  796. notification_type: 'EMAIL',
  797. alert_states: ['OK', 'CRITICAL'],
  798. properties: {
  799. 'ambari.dispatch.recipients': [
  800. 'test1@test.test',
  801. 'test2@test.test',
  802. 'test3@test.test',
  803. 'test4@test.test'
  804. ],
  805. "mail.smtp.host" : "s1",
  806. "mail.smtp.port" : "25",
  807. "mail.smtp.from" : "from",
  808. "mail.smtp.auth" : "true",
  809. "ambari.dispatch.credential.username" : "user",
  810. "ambari.dispatch.credential.password" : "pass",
  811. "mail.smtp.starttls.enable" : "true",
  812. 'n1': 'v1',
  813. 'n2': 'v2'
  814. },
  815. groups: [1,2,3]
  816. }
  817. }));
  818. });
  819. it('should ignore groups if global is true', function () {
  820. controller.set('inputFields', inputFields);
  821. controller.set('inputFields.allGroups.value', 'all');
  822. var result = controller.formatNotificationAPIObject();
  823. expect(Em.keys(result.AlertTarget)).to.not.contain('groups');
  824. });
  825. });
  826. describe('#createAlertNotification()', function () {
  827. it("should send ajax request", function () {
  828. controller.createAlertNotification();
  829. var args = helpers.findAjaxRequest('name', 'alerts.create_alert_notification');
  830. expect(args[0]).to.exists;
  831. });
  832. });
  833. describe('#createAlertNotificationSuccessCallback()', function () {
  834. beforeEach(function () {
  835. controller.set('createEditPopup', {
  836. hide: Em.K
  837. });
  838. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  839. sinon.spy(controller.createEditPopup, 'hide');
  840. });
  841. afterEach(function () {
  842. controller.loadAlertNotifications.restore();
  843. controller.createEditPopup.hide.restore();
  844. });
  845. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  846. controller.createAlertNotificationSuccessCallback();
  847. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  848. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  849. });
  850. });
  851. describe('#updateAlertNotification()', function () {
  852. it("should send ajax request", function () {
  853. controller.updateAlertNotification();
  854. var args = helpers.findAjaxRequest('name', 'alerts.update_alert_notification');
  855. expect(args[0]).to.exists;
  856. });
  857. });
  858. describe('#updateAlertNotificationSuccessCallback()', function () {
  859. beforeEach(function () {
  860. controller.set('createEditPopup', {
  861. hide: Em.K
  862. });
  863. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  864. sinon.spy(controller.createEditPopup, 'hide');
  865. });
  866. afterEach(function () {
  867. controller.loadAlertNotifications.restore();
  868. controller.createEditPopup.hide.restore();
  869. });
  870. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  871. controller.updateAlertNotificationSuccessCallback();
  872. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  873. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  874. });
  875. });
  876. describe('#deleteAlertNotification()', function () {
  877. beforeEach(function () {
  878. sinon.spy(App, 'showConfirmationPopup');
  879. });
  880. afterEach(function () {
  881. App.showConfirmationPopup.restore();
  882. });
  883. it("should show popup and send request on confirmation", function () {
  884. var popup = controller.deleteAlertNotification();
  885. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  886. popup.onPrimary();
  887. var args = helpers.findAjaxRequest('name', 'alerts.delete_alert_notification');
  888. expect(args[0]).to.exists;
  889. });
  890. });
  891. describe('#deleteAlertNotificationSuccessCallback()', function () {
  892. var mockSelectedAlertNotification;
  893. beforeEach(function () {
  894. mockSelectedAlertNotification = {
  895. deleteRecord: Em.K
  896. };
  897. controller.set('selectedAlertNotification', mockSelectedAlertNotification);
  898. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  899. sinon.spy(mockSelectedAlertNotification, 'deleteRecord');
  900. controller.deleteAlertNotificationSuccessCallback();
  901. });
  902. afterEach(function () {
  903. controller.loadAlertNotifications.restore();
  904. mockSelectedAlertNotification.deleteRecord.restore();
  905. });
  906. it("should call loadAlertNotifications", function () {
  907. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  908. });
  909. it("should call selectedAlertNotification.deleteRecord", function () {
  910. expect(mockSelectedAlertNotification.deleteRecord.calledOnce).to.be.true;
  911. });
  912. it("should set null to selectedAlertNotification", function () {
  913. expect(controller.get('selectedAlertNotification')).to.equal(null);
  914. });
  915. });
  916. describe('#duplicateAlertNotification()', function () {
  917. beforeEach(function () {
  918. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  919. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  920. });
  921. afterEach(function () {
  922. controller.fillEditCreateInputs.restore();
  923. controller.showCreateEditPopup.restore();
  924. });
  925. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  926. controller.duplicateAlertNotification();
  927. expect(controller.fillEditCreateInputs.calledWith(true)).to.be.true;
  928. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  929. });
  930. });
  931. describe('#addCustomProperty', function () {
  932. beforeEach(function () {
  933. controller.set('inputFields.customProperties', []);
  934. });
  935. /*eslint-disable mocha-cleanup/asserts-limit */
  936. it('should add custom Property to customProperties', function () {
  937. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  938. controller.addCustomProperty();
  939. helpers.nestedExpect([{name: 'n1', value: 'v1', defaultValue: 'v1'}], controller.get('inputFields.customProperties'));
  940. });
  941. /*eslint-enable mocha-cleanup/asserts-limit */
  942. });
  943. describe('#removeCustomPropertyHandler', function () {
  944. var c = {name: 'n2', value: 'v2', defaultValue: 'v2'};
  945. beforeEach(function () {
  946. controller.set('inputFields.customProperties', [
  947. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  948. c,
  949. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  950. ]);
  951. });
  952. /*eslint-disable mocha-cleanup/asserts-limit */
  953. it('should remove selected custom property', function () {
  954. controller.removeCustomPropertyHandler({context: c});
  955. helpers.nestedExpect(
  956. [
  957. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  958. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  959. ],
  960. controller.get('inputFields.customProperties')
  961. );
  962. });
  963. /*eslint-enable mocha-cleanup/asserts-limit */
  964. });
  965. describe('#addCustomPropertyHandler', function () {
  966. it('should clean up newCustomProperty on primary click', function () {
  967. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  968. controller.addCustomPropertyHandler().onPrimary();
  969. expect(controller.get('newCustomProperty')).to.eql({name: '', value: ''});
  970. });
  971. });
  972. });