kdc_credentials_controller_mixin_test.js 7.2 KB

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