manage_alert_notifications_controller_test.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. SMTPUseAuthentication: {},
  459. SMTPUsername: {},
  460. SMTPPassword: {},
  461. retypeSMTPPassword: {},
  462. method: {}
  463. }
  464. }),
  465. groupSelect: Em.Object.create({
  466. selection: [],
  467. content: [{}, {}]
  468. }),
  469. parentView: Em.Object.create({
  470. hasErrors: false
  471. })
  472. });
  473. }
  474. var view;
  475. beforeEach(function () {
  476. view = getBodyClass();
  477. });
  478. App.TestAliases.testAsComputedOr(getBodyClass(), 'someErrorExists', ['nameError', 'emailToError', 'emailFromError', 'smtpPortError', 'hostError', 'portError', 'smtpUsernameError', 'smtpPasswordError', 'passwordError']);
  479. describe('#selectAllGroups', function () {
  480. it('should check inputFields.allGroups.value', function () {
  481. view.set('controller.inputFields.allGroups.value', 'all');
  482. view.selectAllGroups();
  483. expect(view.get('groupSelect.selection')).to.eql([]);
  484. view.set('controller.inputFields.allGroups.value', 'custom');
  485. view.selectAllGroups();
  486. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  487. });
  488. });
  489. describe('#clearAllGroups', function () {
  490. it('should check inputFields.allGroups.value', function () {
  491. view.set('controller.inputFields.allGroups.value', 'custom');
  492. view.selectAllGroups();
  493. view.set('controller.inputFields.allGroups.value', 'all');
  494. view.clearAllGroups();
  495. expect(view.get('groupSelect.selection')).to.eql([{}, {}]);
  496. view.set('controller.inputFields.allGroups.value', 'custom');
  497. view.clearAllGroups();
  498. expect(view.get('groupSelect.selection')).to.eql([]);
  499. });
  500. });
  501. describe('#nameValidation', function () {
  502. it('should check inputFields.name.value', function () {
  503. view.set('controller.inputFields.name.value', '');
  504. expect(view.get('controller.inputFields.name.errorMsg')).to.equal(Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty'));
  505. expect(view.get('parentView.hasErrors')).to.be.true;
  506. });
  507. it('should check inputFields.name.value (2)', function () {
  508. view.set('controller.inputFields.name.errorMsg', 'error');
  509. view.set('controller.inputFields.name.value', 'test');
  510. expect(view.get('controller.inputFields.name.errorMsg')).to.equal('');
  511. });
  512. it('should check inputFields.name.value (3)', function () {
  513. view.set('isEdit', true);
  514. view.set('controller.inputFields.name.value', '');
  515. expect(view.get('controller.inputFields.name.errorMsg')).to.equal(Em.I18n.t('alerts.actions.manage_alert_notifications_popup.error.name.empty'));
  516. expect(view.get('parentView.hasErrors')).to.be.true;
  517. });
  518. it('should check inputFields.name.value (4)', function () {
  519. view.set('isEdit', true);
  520. view.set('controller.inputFields.name.errorMsg', 'error');
  521. view.set('controller.inputFields.name.value', 'test');
  522. expect(view.get('controller.inputFields.name.errorMsg')).to.equal('');
  523. });
  524. });
  525. describe('#smtpUsernameValidation', function () {
  526. it('should check inputFields.SMTPUsername.value', function () {
  527. view.set('parentView.hasErrors', false);
  528. view.set('controller.inputFields.SMTPUsername.errorMsg', null);
  529. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  530. view.set('controller.inputFields.SMTPUsername.value', '');
  531. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  532. view.set('controller.inputFields.retypeSMTPPassword.value', 'pass');
  533. expect(view.get('controller.inputFields.SMTPUsername.errorMsg')).to.equal(Em.I18n.t('alerts.notifications.error.SMTPUsername'));
  534. expect(view.get('parentView.hasErrors')).to.be.true;
  535. });
  536. it('should check inputFields.SMTPUsername.value (2)', function () {
  537. view.set('parentView.hasErrors', true);
  538. view.set('controller.inputFields.SMTPUsername.errorMsg', 'error');
  539. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  540. view.set('controller.inputFields.SMTPUsername.value', 'test');
  541. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  542. view.set('controller.inputFields.retypeSMTPPassword.value', 'pass');
  543. expect(view.get('controller.inputFields.SMTPUsername.errorMsg')).to.equal(null);
  544. expect(view.get('parentView.hasErrors')).to.be.false;
  545. });
  546. it('should check inputFields.SMTPUsername.value (3)', function () {
  547. view.set('parentView.hasErrors', true);
  548. view.set('controller.inputFields.SMTPUsername.errorMsg', 'error');
  549. view.set('controller.inputFields.SMTPUseAuthentication.value', false);
  550. view.set('controller.inputFields.SMTPUsername.value', '');
  551. view.set('controller.inputFields.SMTPPassword.value', '');
  552. view.set('controller.inputFields.retypeSMTPPassword.value', '');
  553. expect(view.get('controller.inputFields.SMTPUsername.errorMsg')).to.equal(null);
  554. expect(view.get('parentView.hasErrors')).to.be.false;
  555. });
  556. });
  557. describe('#smtpPasswordValidation', function () {
  558. it('should check inputFields.SMTPPassword.value', function () {
  559. view.set('parentView.hasErrors', false);
  560. view.set('controller.inputFields.SMTPPassword.errorMsg', null);
  561. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  562. view.set('controller.inputFields.SMTPUsername.value', 'user');
  563. view.set('controller.inputFields.SMTPPassword.value', '');
  564. view.set('controller.inputFields.retypeSMTPPassword.value', '');
  565. expect(view.get('controller.inputFields.SMTPPassword.errorMsg')).to.equal(Em.I18n.t('alerts.notifications.error.SMTPPassword'));
  566. expect(view.get('parentView.hasErrors')).to.be.true;
  567. });
  568. it('should check inputFields.SMTPPassword.value (2)', function () {
  569. view.set('parentView.hasErrors', true);
  570. view.set('controller.inputFields.SMTPPassword.errorMsg', 'error');
  571. view.set('controller.inputFields.SMTPUseAuthentication.value', true);
  572. view.set('controller.inputFields.SMTPUsername.value', 'user');
  573. view.set('controller.inputFields.SMTPPassword.value', 'test');
  574. view.set('controller.inputFields.retypeSMTPPassword.value', 'test');
  575. expect(view.get('controller.inputFields.SMTPPassword.errorMsg')).to.equal(null);
  576. expect(view.get('parentView.hasErrors')).to.be.false;
  577. });
  578. it('should check inputFields.SMTPPassword.value (3)', function () {
  579. view.set('parentView.hasErrors', true);
  580. view.set('controller.inputFields.SMTPPassword.errorMsg', 'error');
  581. view.set('controller.inputFields.SMTPUseAuthentication.value', false);
  582. view.set('controller.inputFields.SMTPUsername.value', '');
  583. view.set('controller.inputFields.SMTPPassword.value', '');
  584. view.set('controller.inputFields.retypeSMTPPassword.value', '');
  585. expect(view.get('controller.inputFields.SMTPPassword.errorMsg')).to.equal(null);
  586. expect(view.get('parentView.hasErrors')).to.be.false;
  587. });
  588. });
  589. describe('#retypePasswordValidation', function () {
  590. it('should check inputFields.retypeSMTPPassword.value', function () {
  591. view.set('controller.inputFields.retypeSMTPPassword.errorMsg', null);
  592. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  593. view.set('controller.inputFields.retypeSMTPPassword.value', 'pas');
  594. expect(view.get('controller.inputFields.retypeSMTPPassword.errorMsg')).to.equal(Em.I18n.t('alerts.notifications.error.retypePassword'));
  595. expect(view.get('parentView.hasErrors')).to.be.true;
  596. });
  597. it('should check inputFields.retypeSMTPPassword.value (2)', function () {
  598. view.set('parentView.hasErrors', true);
  599. view.set('controller.inputFields.retypeSMTPPassword.errorMsg', 'error');
  600. view.set('controller.inputFields.SMTPPassword.value', 'pass');
  601. view.set('controller.inputFields.retypeSMTPPassword.value', 'pass');
  602. expect(view.get('controller.inputFields.retypeSMTPPassword.errorMsg')).to.equal(null);
  603. expect(view.get('parentView.hasErrors')).to.be.false;
  604. });
  605. });
  606. describe('#methodObserver', function () {
  607. var cases = [
  608. {
  609. method: 'EMAIL',
  610. errors: ['portError', 'hostError'],
  611. validators: ['emailToValidation', 'emailFromValidation', 'smtpPortValidation', 'smtpUsernameValidation', 'smtpPasswordValidation', 'retypePasswordValidation']
  612. },
  613. {
  614. method: 'SNMP',
  615. errors: ['emailToError', 'emailFromError', 'smtpPortError', 'smtpUsernameError', 'smtpPasswordError', 'passwordError'],
  616. validators: ['portValidation', 'hostsValidation']
  617. }
  618. ],
  619. validators = [];
  620. before(function () {
  621. cases.forEach(function (item) {
  622. validators.pushObjects(item.validators);
  623. });
  624. });
  625. beforeEach(function () {
  626. validators.forEach(function (item) {
  627. sinon.stub(view, item, Em.K);
  628. });
  629. });
  630. afterEach(function () {
  631. validators.forEach(function (item) {
  632. view.get(item).restore();
  633. });
  634. });
  635. cases.forEach(function (item) {
  636. describe(item.method, function () {
  637. beforeEach(function () {
  638. item.errors.forEach(function (errorName) {
  639. view.set(errorName, true);
  640. });
  641. view.set('controller.inputFields.method.value', item.method);
  642. });
  643. item.errors.forEach(function (errorName) {
  644. it(errorName + ' is false', function () {
  645. expect(view.get(errorName)).to.be.false;
  646. });
  647. });
  648. validators.forEach(function (validatorName) {
  649. var called = item.validators.contains(validatorName);
  650. it(validatorName + ' ' + (called ? '' : 'not') + ' called', function () {
  651. expect(view.get(validatorName).calledOnce).to.equal(called);
  652. });
  653. });
  654. });
  655. });
  656. });
  657. });
  658. });
  659. describe("#formatNotificationAPIObject()", function () {
  660. var inputFields = Em.Object.create({
  661. name: {
  662. value: 'test_name'
  663. },
  664. groups: {
  665. value: [{id: 1}, {id: 2}, {id: 3}]
  666. },
  667. allGroups: {
  668. value: 'custom'
  669. },
  670. global: {
  671. value: false
  672. },
  673. method: {
  674. value: 'EMAIL'
  675. },
  676. email: {
  677. value: 'test1@test.test, test2@test.test,test3@test.test , test4@test.test'
  678. },
  679. severityFilter: {
  680. value: ['OK', 'CRITICAL']
  681. },
  682. SMTPServer: {
  683. value: 's1'
  684. },
  685. SMTPPort: {
  686. value: '25'
  687. },
  688. SMTPUseAuthentication: {
  689. value: "true"
  690. },
  691. SMTPUsername: {
  692. value: 'user'
  693. },
  694. SMTPPassword: {
  695. value: 'pass'
  696. },
  697. SMTPSTARTTLS: {
  698. value: "true"
  699. },
  700. emailFrom: {
  701. value: 'from'
  702. },
  703. description: {
  704. value: 'test_description'
  705. },
  706. customProperties: [
  707. {name: 'n1', value: 'v1'},
  708. {name: 'n2', value: 'v2'}
  709. ]
  710. });
  711. it("should create object with properties from inputFields values", function () {
  712. controller.set('inputFields', inputFields);
  713. var result = controller.formatNotificationAPIObject();
  714. expect(JSON.stringify(result)).to.eql(JSON.stringify({
  715. AlertTarget: {
  716. name: 'test_name',
  717. description: 'test_description',
  718. global: false,
  719. notification_type: 'EMAIL',
  720. alert_states: ['OK', 'CRITICAL'],
  721. properties: {
  722. 'ambari.dispatch.recipients': [
  723. 'test1@test.test',
  724. 'test2@test.test',
  725. 'test3@test.test',
  726. 'test4@test.test'
  727. ],
  728. "mail.smtp.host" : "s1",
  729. "mail.smtp.port" : "25",
  730. "mail.smtp.from" : "from",
  731. "mail.smtp.auth" : "true",
  732. "ambari.dispatch.credential.username" : "user",
  733. "ambari.dispatch.credential.password" : "pass",
  734. "mail.smtp.starttls.enable" : "true",
  735. 'n1': 'v1',
  736. 'n2': 'v2'
  737. },
  738. groups: [1,2,3]
  739. }
  740. }));
  741. });
  742. it('should ignore groups if global is true', function () {
  743. controller.set('inputFields', inputFields);
  744. controller.set('inputFields.allGroups.value', 'all');
  745. var result = controller.formatNotificationAPIObject();
  746. expect(Em.keys(result.AlertTarget)).to.not.contain('groups');
  747. });
  748. });
  749. describe('#createAlertNotification()', function () {
  750. it("should send ajax request", function () {
  751. controller.createAlertNotification();
  752. var args = helpers.findAjaxRequest('name', 'alerts.create_alert_notification');
  753. expect(args[0]).to.exists;
  754. });
  755. });
  756. describe('#createAlertNotificationSuccessCallback()', function () {
  757. beforeEach(function () {
  758. controller.set('createEditPopup', {
  759. hide: Em.K
  760. });
  761. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  762. sinon.spy(controller.createEditPopup, 'hide');
  763. });
  764. afterEach(function () {
  765. controller.loadAlertNotifications.restore();
  766. controller.createEditPopup.hide.restore();
  767. });
  768. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  769. controller.createAlertNotificationSuccessCallback();
  770. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  771. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  772. });
  773. });
  774. describe('#updateAlertNotification()', function () {
  775. it("should send ajax request", function () {
  776. controller.updateAlertNotification();
  777. var args = helpers.findAjaxRequest('name', 'alerts.update_alert_notification');
  778. expect(args[0]).to.exists;
  779. });
  780. });
  781. describe('#updateAlertNotificationSuccessCallback()', function () {
  782. beforeEach(function () {
  783. controller.set('createEditPopup', {
  784. hide: Em.K
  785. });
  786. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  787. sinon.spy(controller.createEditPopup, 'hide');
  788. });
  789. afterEach(function () {
  790. controller.loadAlertNotifications.restore();
  791. controller.createEditPopup.hide.restore();
  792. });
  793. it("should call loadAlertNotifications and createEditPopup.hide", function () {
  794. controller.updateAlertNotificationSuccessCallback();
  795. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  796. expect(controller.createEditPopup.hide.calledOnce).to.be.true;
  797. });
  798. });
  799. describe('#deleteAlertNotification()', function () {
  800. beforeEach(function () {
  801. sinon.spy(App, 'showConfirmationPopup');
  802. });
  803. afterEach(function () {
  804. App.showConfirmationPopup.restore();
  805. });
  806. it("should show popup and send request on confirmation", function () {
  807. var popup = controller.deleteAlertNotification();
  808. expect(App.showConfirmationPopup.calledOnce).to.be.true;
  809. popup.onPrimary();
  810. var args = helpers.findAjaxRequest('name', 'alerts.delete_alert_notification');
  811. expect(args[0]).to.exists;
  812. });
  813. });
  814. describe('#deleteAlertNotificationSuccessCallback()', function () {
  815. var mockSelectedAlertNotification;
  816. beforeEach(function () {
  817. mockSelectedAlertNotification = {
  818. deleteRecord: Em.K
  819. };
  820. controller.set('selectedAlertNotification', mockSelectedAlertNotification);
  821. sinon.stub(controller, 'loadAlertNotifications', Em.K);
  822. sinon.spy(mockSelectedAlertNotification, 'deleteRecord');
  823. controller.deleteAlertNotificationSuccessCallback();
  824. });
  825. afterEach(function () {
  826. controller.loadAlertNotifications.restore();
  827. mockSelectedAlertNotification.deleteRecord.restore();
  828. });
  829. it("should call loadAlertNotifications", function () {
  830. expect(controller.loadAlertNotifications.calledOnce).to.be.true;
  831. });
  832. it("should call selectedAlertNotification.deleteRecord", function () {
  833. expect(mockSelectedAlertNotification.deleteRecord.calledOnce).to.be.true;
  834. });
  835. it("should set null to selectedAlertNotification", function () {
  836. expect(controller.get('selectedAlertNotification')).to.equal(null);
  837. });
  838. });
  839. describe('#duplicateAlertNotification()', function () {
  840. beforeEach(function () {
  841. sinon.stub(controller, 'fillEditCreateInputs', Em.K);
  842. sinon.stub(controller, 'showCreateEditPopup', Em.K);
  843. });
  844. afterEach(function () {
  845. controller.fillEditCreateInputs.restore();
  846. controller.showCreateEditPopup.restore();
  847. });
  848. it("should call fillEditCreateInputs and showCreateEditPopup", function () {
  849. controller.duplicateAlertNotification();
  850. expect(controller.fillEditCreateInputs.calledWith(true)).to.be.true;
  851. expect(controller.showCreateEditPopup.calledOnce).to.be.true;
  852. });
  853. });
  854. describe('#addCustomProperty', function () {
  855. beforeEach(function () {
  856. controller.set('inputFields.customProperties', []);
  857. });
  858. /*eslint-disable mocha-cleanup/asserts-limit */
  859. it('should add custom Property to customProperties', function () {
  860. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  861. controller.addCustomProperty();
  862. helpers.nestedExpect([{name: 'n1', value: 'v1', defaultValue: 'v1'}], controller.get('inputFields.customProperties'));
  863. });
  864. /*eslint-enable mocha-cleanup/asserts-limit */
  865. });
  866. describe('#removeCustomPropertyHandler', function () {
  867. var c = {name: 'n2', value: 'v2', defaultValue: 'v2'};
  868. beforeEach(function () {
  869. controller.set('inputFields.customProperties', [
  870. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  871. c,
  872. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  873. ]);
  874. });
  875. /*eslint-disable mocha-cleanup/asserts-limit */
  876. it('should remove selected custom property', function () {
  877. controller.removeCustomPropertyHandler({context: c});
  878. helpers.nestedExpect(
  879. [
  880. {name: 'n1', value: 'v1', defaultValue: 'v1'},
  881. {name: 'n3', value: 'v3', defaultValue: 'v3'}
  882. ],
  883. controller.get('inputFields.customProperties')
  884. );
  885. });
  886. /*eslint-enable mocha-cleanup/asserts-limit */
  887. });
  888. describe('#addCustomPropertyHandler', function () {
  889. it('should clean up newCustomProperty on primary click', function () {
  890. controller.set('newCustomProperty', {name: 'n1', value: 'v1'});
  891. controller.addCustomPropertyHandler().onPrimary();
  892. expect(controller.get('newCustomProperty')).to.eql({name: '', value: ''});
  893. });
  894. describe('#bodyClass', function () {
  895. var view;
  896. beforeEach(function () {
  897. view = controller.addCustomPropertyHandler().get('bodyClass').create({
  898. parentView: Em.View.create(),
  899. controller: Em.Object.create({
  900. inputFields: Em.Object.create({
  901. customProperties: [
  902. {name: 'n1', value: 'v1', defaultValue: 'v1'}
  903. ]
  904. }),
  905. newCustomProperty: {name: '', value: ''}
  906. })
  907. });
  908. });
  909. describe('#errorHandler', function () {
  910. it('should fire invalid name', function () {
  911. view.set('controller.newCustomProperty.name', '!!');
  912. view.errorsHandler();
  913. expect(view.get('isError')).to.be.true;
  914. expect(view.get('parentView.disablePrimary')).to.be.true;
  915. expect(view.get('errorMessage.length')).to.be.above(0);
  916. });
  917. it('should fire existing property name', function () {
  918. view.set('controller.newCustomProperty.name', 'n1');
  919. view.errorsHandler();
  920. expect(view.get('isError')).to.be.true;
  921. expect(view.get('parentView.disablePrimary')).to.be.true;
  922. expect(view.get('errorMessage.length')).to.be.above(0);
  923. });
  924. });
  925. });
  926. });
  927. });