kdc_credentials_controller_mixin_test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. require('mixins/common/kdc_credentials_controller_mixin');
  19. var App = require('app');
  20. var credentialsUtils = require('utils/credentials');
  21. var mixedObject;
  22. describe('App.KDCCredentialsControllerMixin', function() {
  23. beforeEach(function() {
  24. mixedObject = Em.Object.create(App.KDCCredentialsControllerMixin);
  25. });
  26. afterEach(function() {
  27. mixedObject.destroy();
  28. });
  29. describe('#initilizeKDCStoreProperties', function() {
  30. [
  31. {
  32. isStorePersisted: true,
  33. e: {
  34. isEditable: true,
  35. hintMessage: Em.I18n.t('admin.kerberos.credentials.store.hint.supported')
  36. },
  37. message: 'Persistent store available, config should be editable, and appropriate hint shown'
  38. },
  39. {
  40. isStorePersisted: false,
  41. e: {
  42. isEditable: false,
  43. hintMessage: Em.I18n.t('admin.kerberos.credentials.store.hint.not.supported')
  44. },
  45. message: 'Only temporary store available, config should be disabled, and appropriate hint shown'
  46. }
  47. ].forEach(function(test) {
  48. it(test.message, function() {
  49. var configs = [],
  50. config;
  51. mixedObject.reopen({
  52. isStorePersisted: function() {
  53. return test.isStorePersisted;
  54. }.property()
  55. });
  56. mixedObject.initilizeKDCStoreProperties(configs);
  57. config = configs.findProperty('name', 'persist_credentials');
  58. Em.keys(test.e).forEach(function(key) {
  59. assert.equal(Em.get(config, key), test.e[key], 'validate attribute: ' + key);
  60. });
  61. });
  62. });
  63. });
  64. describe('#createKDCCredentials', function() {
  65. beforeEach(function () {
  66. sinon.stub(App, 'get').withArgs('clusterName').returns('testName');
  67. sinon.stub(credentialsUtils, 'createCredentials', function() {
  68. return resolveWith();
  69. });
  70. sinon.stub(credentialsUtils, 'updateCredentials', function() {
  71. return resolveWith();
  72. });
  73. mixedObject.reopen({
  74. isStorePersisted: true
  75. });
  76. });
  77. afterEach(function () {
  78. App.get.restore();
  79. credentialsUtils.createCredentials.restore();
  80. credentialsUtils.updateCredentials.restore();
  81. });
  82. var createConfig = function(name, value) {
  83. return App.ServiceConfigProperty.create({
  84. name: name,
  85. value: value
  86. });
  87. };
  88. var resolveWith = function(data) {
  89. return $.Deferred().resolve(data).promise();
  90. };
  91. var rejectWith = function(data) {
  92. return $.Deferred().reject(data).promise();
  93. };
  94. [
  95. {
  96. configs: [
  97. createConfig('admin_password', 'admin'),
  98. createConfig('admin_principal', 'admin/admin'),
  99. createConfig('persist_credentials', 'true')
  100. ],
  101. credentialsExists: false,
  102. createCredentialFnCalled: true,
  103. updateCredentialFnCalled: false,
  104. e: [
  105. 'testName',
  106. 'kdc.admin.credential',
  107. {
  108. type: 'persisted',
  109. key: 'admin',
  110. principal: 'admin/admin'
  111. }
  112. ],
  113. message: 'Save Admin credentials checkbox checked, credentials already stored and should be updated as `persisted`'
  114. },
  115. {
  116. configs: [
  117. createConfig('admin_password', 'admin'),
  118. createConfig('admin_principal', 'admin/admin'),
  119. createConfig('persist_credentials', 'true')
  120. ],
  121. credentialsExists: true,
  122. createCredentialFnCalled: false,
  123. updateCredentialFnCalled: true,
  124. e: [
  125. 'testName',
  126. 'kdc.admin.credential',
  127. {
  128. type: 'persisted',
  129. key: 'admin',
  130. principal: 'admin/admin'
  131. }
  132. ],
  133. message: 'Save Admin credentials checkbox checked, no stored credentials, should be created as `persisted`'
  134. },
  135. {
  136. configs: [
  137. createConfig('admin_password', 'admin'),
  138. createConfig('admin_principal', 'admin/admin'),
  139. createConfig('persist_credentials', 'false')
  140. ],
  141. credentialsExists: true,
  142. createCredentialFnCalled: false,
  143. updateCredentialFnCalled: true,
  144. e: [
  145. 'testName',
  146. 'kdc.admin.credential',
  147. {
  148. type: 'temporary',
  149. key: 'admin',
  150. principal: 'admin/admin'
  151. }
  152. ],
  153. message: 'Save Admin credentials checkbox unchecked, credentials already stored and should be updated as `temporary`'
  154. },
  155. {
  156. configs: [
  157. createConfig('admin_password', 'admin'),
  158. createConfig('admin_principal', 'admin/admin'),
  159. createConfig('persist_credentials', 'false')
  160. ],
  161. credentialsExists: false,
  162. createCredentialFnCalled: true,
  163. updateCredentialFnCalled: false,
  164. e: [
  165. 'testName',
  166. 'kdc.admin.credential',
  167. {
  168. type: 'temporary',
  169. key: 'admin',
  170. principal: 'admin/admin'
  171. }
  172. ],
  173. message: 'Save Admin credentials checkbox unchecked, credentials already stored and should be updated as `temporary`'
  174. }
  175. ].forEach(function(test) {
  176. describe(test.message, function() {
  177. beforeEach(function () {
  178. sinon.stub(credentialsUtils, 'getCredential', function() {
  179. return test.credentialsExists ? resolveWith() : rejectWith();
  180. });
  181. mixedObject.createKDCCredentials(test.configs);
  182. });
  183. afterEach(function () {
  184. credentialsUtils.getCredential.restore();
  185. });
  186. it('credentialsUtils#createCredentials called', function () {
  187. expect(credentialsUtils.createCredentials.calledOnce).to.equal(test.createCredentialFnCalled);
  188. });
  189. if (test.createCredentialFnCalled) {
  190. it('credentialsUtils#createCredentials called with correct arguments', function () {
  191. expect(credentialsUtils.createCredentials.args[0]).to.eql(test.e);
  192. });
  193. }
  194. it('credentialUtils#updateCredentials called', function () {
  195. expect(credentialsUtils.updateCredentials.calledOnce).to.equal(test.updateCredentialFnCalled);
  196. });
  197. if (test.updateCredentialFnCalled) {
  198. it('credentialUtils#updateCredentials called with correct arguments', function () {
  199. expect(credentialsUtils.updateCredentials.args[0]).to.eql(test.e);
  200. });
  201. }
  202. });
  203. });
  204. });
  205. });