security_test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. require('controllers/main/admin/security');
  20. describe('App.MainAdminSecurityController', function () {
  21. var controller = App.MainAdminSecurityController.create({
  22. getServiceConfigsFromServer: function () {
  23. }
  24. });
  25. describe('#setServiceTagNames()', function () {
  26. var testCases = [
  27. {
  28. title: 'configs is empty object',
  29. content: {
  30. secureService: {},
  31. configs: {}
  32. },
  33. result: undefined
  34. },
  35. {
  36. title: 'secureService.sites is null',
  37. content: {
  38. secureService: {
  39. sites: null
  40. },
  41. configs: {
  42. site1: {}
  43. }
  44. },
  45. result: undefined
  46. },
  47. {
  48. title: 'secureService.sites doesn\'t contain required config tag',
  49. content: {
  50. secureService: {
  51. sites: []
  52. },
  53. configs: {
  54. site1: {}
  55. }
  56. },
  57. result: undefined
  58. },
  59. {
  60. title: 'secureService.sites contains required config tag',
  61. content: {
  62. secureService: {
  63. sites: ['site1']
  64. },
  65. configs: {
  66. site1: {
  67. tag: 'tag1'
  68. }
  69. }
  70. },
  71. result: {
  72. siteName: 'site1',
  73. tagName: 'tag1',
  74. newTagName: null,
  75. configs: {}
  76. }
  77. }
  78. ];
  79. testCases.forEach(function (test) {
  80. it(test.title, function () {
  81. expect(controller.setServiceTagNames(test.content.secureService, test.content.configs)).to.eql(test.result);
  82. });
  83. });
  84. });
  85. describe('#getSecurityStatusFromServerSuccessCallback()', function () {
  86. beforeEach(function () {
  87. sinon.spy(controller, 'showSecurityErrorPopup');
  88. sinon.spy(controller, 'getServiceConfigsFromServer');
  89. });
  90. afterEach(function () {
  91. controller.showSecurityErrorPopup.restore();
  92. controller.getServiceConfigsFromServer.restore();
  93. });
  94. it('desired_configs is empty', function () {
  95. var data = {Clusters: {
  96. desired_configs: {}
  97. }};
  98. controller.getSecurityStatusFromServerSuccessCallback(data);
  99. expect(controller.showSecurityErrorPopup.called).to.equal(true);
  100. });
  101. it('hdfs-site is missing', function () {
  102. var data = {Clusters: {
  103. desired_configs: {
  104. 'global': {}
  105. }
  106. }};
  107. controller.getSecurityStatusFromServerSuccessCallback(data);
  108. expect(controller.showSecurityErrorPopup.called).to.equal(true);
  109. });
  110. it('global is missing', function () {
  111. var data = {Clusters: {
  112. desired_configs: {
  113. 'hdfs-site': {}
  114. }
  115. }};
  116. controller.getSecurityStatusFromServerSuccessCallback(data);
  117. expect(controller.showSecurityErrorPopup.called).to.equal(true);
  118. });
  119. it('global and hdfs-site are correct', function () {
  120. var data = {Clusters: {
  121. desired_configs: {
  122. 'hdfs-site': {
  123. tag: 1
  124. },
  125. 'global': {
  126. tag: 2
  127. }
  128. }
  129. }};
  130. controller.getSecurityStatusFromServerSuccessCallback(data);
  131. expect(controller.get('tag.global')).to.equal(2);
  132. expect(controller.get('tag.hdfs-site')).to.equal(1);
  133. expect(controller.getServiceConfigsFromServer.called).to.equal(true);
  134. });
  135. });
  136. describe('#setNnHaStatus()', function () {
  137. beforeEach(function () {
  138. sinon.stub(App.db, "setIsNameNodeHa", Em.K);
  139. });
  140. afterEach(function () {
  141. App.db.setIsNameNodeHa.restore();
  142. });
  143. it('hdfsConfigs is null', function () {
  144. var hdfsConfigs = null;
  145. controller.setNnHaStatus(hdfsConfigs);
  146. expect(App.db.setIsNameNodeHa.withArgs('false').called).to.equal(true);
  147. });
  148. it('"dfs.nameservices" is absent in hdfsConfigs', function () {
  149. var hdfsConfigs = {};
  150. controller.setNnHaStatus(hdfsConfigs);
  151. expect(App.db.setIsNameNodeHa.withArgs('false').called).to.equal(true);
  152. });
  153. it('namenodesKey is absent in hdfsConfigs', function () {
  154. var hdfsConfigs = {
  155. 'dfs.nameservices': 'key'
  156. };
  157. controller.setNnHaStatus(hdfsConfigs);
  158. expect(App.db.setIsNameNodeHa.withArgs('false').called).to.equal(true);
  159. });
  160. it('namenodesKey is present in hdfsConfigs', function () {
  161. var hdfsConfigs = {
  162. 'dfs.nameservices': 'key',
  163. 'dfs.ha.namenodes.key': 'true'
  164. };
  165. controller.setNnHaStatus(hdfsConfigs);
  166. expect(App.db.setIsNameNodeHa.withArgs('true').called).to.equal(true);
  167. });
  168. });
  169. describe('#loadUsers()', function () {
  170. beforeEach(function () {
  171. sinon.stub(App.db, "setSecureUserInfo", Em.K);
  172. });
  173. afterEach(function () {
  174. App.db.setSecureUserInfo.restore();
  175. });
  176. it('if defaultUserNameMap is empty then serviceUsers stays the same', function () {
  177. var configs = {};
  178. controller.set('serviceUsers', []);
  179. controller.set('defaultUserNameMap', {});
  180. controller.loadUsers(configs);
  181. expect(controller.get('serviceUsers')).to.be.empty;
  182. });
  183. it('if user config value is missing then use default', function () {
  184. var configs = {};
  185. controller.set('serviceUsers', []);
  186. controller.set('defaultUserNameMap', {
  187. 'test_user': 'test'
  188. });
  189. controller.loadUsers(configs);
  190. expect(controller.get('serviceUsers')).to.eql([
  191. {
  192. "id": "puppet var",
  193. "name": "test_user",
  194. "value": "test"
  195. }
  196. ]);
  197. });
  198. it('user config value has value', function () {
  199. var configs = {
  200. 'test_user': 'config-value'
  201. };
  202. controller.set('serviceUsers', []);
  203. controller.set('defaultUserNameMap', {
  204. 'test_user': 'test'
  205. });
  206. controller.loadUsers(configs);
  207. expect(controller.get('serviceUsers')).to.eql([
  208. {
  209. "id": "puppet var",
  210. "name": "test_user",
  211. "value": "config-value"
  212. }
  213. ]);
  214. });
  215. });
  216. });