manage_alert_notifications_controller_test.js 34 KB

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