user_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 modelSetup = require('test/init_model_test');
  20. require('models/user');
  21. var user,
  22. form,
  23. userNameField,
  24. userData = {
  25. id: 'user'
  26. },
  27. objectData = Em.Object.create({
  28. userName: 'name',
  29. isLdap: true
  30. });
  31. describe('App.User', function () {
  32. beforeEach(function () {
  33. user = App.User.createRecord(userData);
  34. });
  35. afterEach(function () {
  36. modelSetup.deleteRecord(user);
  37. });
  38. describe('#id', function () {
  39. it('should take value from userName', function () {
  40. user.set('userName', 'name');
  41. expect(user.get('id')).to.equal('name');
  42. });
  43. });
  44. describe('#type', function () {
  45. it('should be LDAP', function () {
  46. user.set('isLdap', true);
  47. expect(user.get('type')).to.equal('LDAP');
  48. });
  49. it('should be Local', function () {
  50. user.set('isLdap', false);
  51. expect(user.get('type')).to.equal('Local');
  52. });
  53. });
  54. });
  55. describe('App.EditUserForm', function () {
  56. beforeEach(function () {
  57. form = App.EditUserForm.create();
  58. });
  59. describe('#object', function () {
  60. before(function () {
  61. sinon.stub(App.router, 'get', function (k) {
  62. if (k === 'mainAdminUserEditController.content') return userData;
  63. return Em.get(App.router, k);
  64. });
  65. });
  66. after(function () {
  67. App.router.get.restore();
  68. });
  69. it('should take data from controller', function () {
  70. expect(form.get('object')).to.eql(userData);
  71. });
  72. });
  73. describe('#disableUsername', function () {
  74. it('should update userName field', function () {
  75. form.set('object', userData);
  76. expect(form.get('field.userName.disabled')).to.equal('disabled');
  77. });
  78. });
  79. describe('#disableAdminCheckbox', function () {
  80. before(function () {
  81. sinon.stub(App, 'get', function(k) {
  82. switch (k) {
  83. case 'router':
  84. return {
  85. getLoginName: Em.K
  86. };
  87. case 'supports.ldapGroupMapping':
  88. return true;
  89. default:
  90. return Em.get(App, k);
  91. }
  92. });
  93. sinon.stub(App.router, 'get', function (k) {
  94. if (k === 'mainAdminUserEditController.content') return objectData;
  95. return Em.get(App.router, k);
  96. });
  97. });
  98. after(function () {
  99. App.get.restore();
  100. App.router.get.restore();
  101. });
  102. it('should not disable', function () {
  103. expect(form.get('field.admin.disabled')).to.be.false;
  104. });
  105. it('should disable', function () {
  106. form.set('object', objectData);
  107. expect(form.get('field.admin.disabled')).to.be.true;
  108. });
  109. });
  110. describe('#isValid', function () {
  111. it('should be true as default', function () {
  112. expect(form.isValid()).to.be.true;
  113. });
  114. it('should be false', function () {
  115. form.set('field.new_password.isRequired', true);
  116. expect(form.isValid()).to.be.false;
  117. });
  118. });
  119. describe('#save', function () {
  120. before(function () {
  121. sinon.stub(App.router, 'get', function (k) {
  122. if (k === 'mainAdminUserEditController.content') return objectData;
  123. return Em.get(App.router, k);
  124. });
  125. });
  126. after(function () {
  127. App.router.get.restore();
  128. });
  129. it('should record form values to object', function () {
  130. form.set('field.userName.value', 'name');
  131. form.save();
  132. expect(form.get('object.userName')).to.equal('name');
  133. });
  134. });
  135. });
  136. describe('App.CreateUserForm', function () {
  137. beforeEach(function () {
  138. form = App.CreateUserForm.create();
  139. });
  140. describe('#object', function () {
  141. before(function () {
  142. sinon.stub(App.router, 'get', function (k) {
  143. if (k === 'mainAdminUserCreateController.content') return userData;
  144. return Em.get(App, k);
  145. });
  146. });
  147. after(function () {
  148. App.router.get.restore();
  149. });
  150. it('should take data from controller', function () {
  151. expect(form.get('object')).to.eql(userData);
  152. });
  153. });
  154. describe('#field.userName.toLowerCase', function () {
  155. it('should convert userName into lower case', function () {
  156. userNameField = form.getField('userName');
  157. userNameField.set('value', 'NAME');
  158. expect(userNameField.get('value')).to.equal('name');
  159. });
  160. });
  161. describe('#isValid', function () {
  162. it('should be false as default', function () {
  163. expect(form.isValid()).to.be.false;
  164. });
  165. it('should be true', function () {
  166. form.get('fields').forEach(function (item) {
  167. if (item.get('isRequired')) {
  168. item.set('value', 'value');
  169. }
  170. });
  171. expect(form.isValid()).to.be.true;
  172. });
  173. });
  174. describe('#isWarn', function () {
  175. it('should be false as default', function () {
  176. expect(form.isWarn()).to.be.false;
  177. });
  178. it('should be true', function () {
  179. form.getField('userName').set('value', '1');
  180. expect(form.isWarn()).to.be.true;
  181. });
  182. it('should be false', function () {
  183. form.getField('userName').set('value', 'name');
  184. expect(form.isWarn()).to.be.false;
  185. });
  186. });
  187. });