manage_alert_notifications_controller_test.js 28 KB

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