manage_alert_notifications_controller_test.js 28 KB

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