user_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. default:
  88. return Em.get(App, k);
  89. }
  90. });
  91. sinon.stub(App.router, 'get', function (k) {
  92. if (k === 'mainAdminUserEditController.content') return objectData;
  93. return Em.get(App.router, k);
  94. });
  95. });
  96. after(function () {
  97. App.get.restore();
  98. App.router.get.restore();
  99. });
  100. it('should not disable', function () {
  101. expect(form.get('field.admin.disabled')).to.be.false;
  102. });
  103. it('should disable', function () {
  104. form.set('object', objectData);
  105. expect(form.get('field.admin.disabled')).to.be.false;
  106. });
  107. });
  108. describe('#isValid', function () {
  109. it('should be true as default', function () {
  110. expect(form.isValid()).to.be.true;
  111. });
  112. it('should be false', function () {
  113. form.set('field.new_password.isRequired', true);
  114. expect(form.isValid()).to.be.false;
  115. });
  116. });
  117. describe('#save', function () {
  118. before(function () {
  119. sinon.stub(App.router, 'get', function (k) {
  120. if (k === 'mainAdminUserEditController.content') return objectData;
  121. return Em.get(App.router, k);
  122. });
  123. });
  124. after(function () {
  125. App.router.get.restore();
  126. });
  127. it('should record form values to object', function () {
  128. form.set('field.userName.value', 'name');
  129. form.save();
  130. expect(form.get('object.userName')).to.equal('name');
  131. });
  132. });
  133. });
  134. describe('App.CreateUserForm', function () {
  135. beforeEach(function () {
  136. form = App.CreateUserForm.create();
  137. });
  138. describe('#object', function () {
  139. before(function () {
  140. sinon.stub(App.router, 'get', function (k) {
  141. if (k === 'mainAdminUserCreateController.content') return userData;
  142. return Em.get(App, k);
  143. });
  144. });
  145. after(function () {
  146. App.router.get.restore();
  147. });
  148. it('should take data from controller', function () {
  149. expect(form.get('object')).to.eql(userData);
  150. });
  151. });
  152. describe('#field.userName.toLowerCase', function () {
  153. it('should convert userName into lower case', function () {
  154. userNameField = form.getField('userName');
  155. userNameField.set('value', 'NAME');
  156. expect(userNameField.get('value')).to.equal('name');
  157. });
  158. });
  159. describe('#isValid', function () {
  160. it('should be false as default', function () {
  161. expect(form.isValid()).to.be.false;
  162. });
  163. it('should be true', function () {
  164. form.get('fields').forEach(function (item) {
  165. if (item.get('isRequired')) {
  166. item.set('value', 'value');
  167. }
  168. });
  169. expect(form.isValid()).to.be.true;
  170. });
  171. });
  172. describe('#isWarn', function () {
  173. it('should be false as default', function () {
  174. expect(form.isWarn()).to.be.false;
  175. });
  176. it('should be true', function () {
  177. form.getField('userName').set('value', '1');
  178. expect(form.isWarn()).to.be.true;
  179. });
  180. it('should be false', function () {
  181. form.getField('userName').set('value', 'name');
  182. expect(form.isWarn()).to.be.false;
  183. });
  184. });
  185. });