manage_alert_notifications_controller_test.js 28 KB

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