change-password-test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import Ember from 'ember';
  2. import { module, test } from 'qunit';
  3. import startApp from '../helpers/start-app';
  4. import getToastMessage from '../helpers/toast-message';
  5. import createUser from '../helpers/create-user';
  6. var application, container, auth;
  7. module('Acceptance: Change Password', {
  8. beforeEach: function() {
  9. application = startApp();
  10. container = application.__container__;
  11. auth = container.lookup('service:auth');
  12. },
  13. afterEach: function() {
  14. Ember.run(application, 'destroy');
  15. Ember.$.mockjax.clear();
  16. }
  17. });
  18. test('/options/change-password form can be accessed', function(assert) {
  19. var user = createUser();
  20. auth.setProperties({
  21. 'isAuthenticated': true,
  22. 'user': user
  23. });
  24. assert.expect(1);
  25. visit('/options/change-password/');
  26. andThen(function() {
  27. assert.equal(currentPath(), 'options.password.index');
  28. });
  29. });
  30. test('/options/change-password form handles empty submission', function(assert) {
  31. var user = createUser();
  32. auth.setProperties({
  33. 'isAuthenticated': true,
  34. 'user': user
  35. });
  36. assert.expect(5);
  37. visit('/options/change-password/');
  38. click('.panel-form .panel-footer .btn-primary');
  39. andThen(function() {
  40. assert.equal(currentPath(), 'options.password.index');
  41. assert.equal(getToastMessage(), 'Form contains errors.');
  42. var newPasswordValidation = Ember.$('#id_new_password').parents('.form-group').find('.help-block.errors');
  43. assert.equal(Ember.$.trim(newPasswordValidation.text()), 'Enter new password.');
  44. var repeatPasswordValidation = Ember.$('#id_repeat_password').parents('.form-group').find('.help-block.errors');
  45. assert.equal(Ember.$.trim(repeatPasswordValidation.text()), 'Repeat new password.');
  46. var passwordValidation = Ember.$('#id_password').parents('.form-group').find('.help-block.errors');
  47. assert.equal(Ember.$.trim(passwordValidation.text()), 'Enter current password.');
  48. });
  49. });
  50. test('/options/change-password form handles invalid submission', function(assert) {
  51. var user = createUser();
  52. auth.setProperties({
  53. 'isAuthenticated': true,
  54. 'user': user
  55. });
  56. assert.expect(3);
  57. visit('/options/change-password/');
  58. fillIn('#id_new_password', 'not-valid-password');
  59. fillIn('#id_password', 'password');
  60. click('.panel-form .panel-footer .btn-primary');
  61. andThen(function() {
  62. assert.equal(currentPath(), 'options.password.index');
  63. assert.equal(getToastMessage(), 'Form contains errors.');
  64. var repeatPasswordValidation = Ember.$('#id_repeat_password').parents('.form-group').find('.help-block.errors');
  65. assert.equal(Ember.$.trim(repeatPasswordValidation.text()), 'Repeat new password.');
  66. });
  67. });
  68. test('/options/change-password form handles error 400', function(assert) {
  69. var user = createUser();
  70. auth.setProperties({
  71. 'isAuthenticated': true,
  72. 'user': user
  73. });
  74. Ember.$.mockjax({
  75. url: "/api/users/" + user.get('id') + '/change-password/',
  76. status: 400,
  77. responseText: {
  78. 'new_password': ['New password is bad.'],
  79. 'password': ['Password is bad.']
  80. }
  81. });
  82. assert.expect(4);
  83. visit('/options/change-password/');
  84. fillIn('#id_new_password', 'V4lidPassword');
  85. fillIn('#id_repeat_password', 'V4lidPassword');
  86. fillIn('#id_password', 'password');
  87. click('.panel-form .panel-footer .btn-primary');
  88. andThen(function() {
  89. assert.equal(currentPath(), 'options.password.index');
  90. assert.equal(getToastMessage(), 'Form contains errors.');
  91. var newPasswordValidation = Ember.$('#id_new_password').parents('.form-group').find('.help-block.errors');
  92. assert.equal(Ember.$.trim(newPasswordValidation.text()), 'New password is bad.');
  93. var passwordValidation = Ember.$('#id_password').parents('.form-group').find('.help-block.errors');
  94. assert.equal(Ember.$.trim(passwordValidation.text()), 'Password is bad.');
  95. });
  96. });
  97. test('/options/change-password form handles valid submission', function(assert) {
  98. var user = createUser();
  99. auth.setProperties({
  100. 'isAuthenticated': true,
  101. 'user': user
  102. });
  103. Ember.$.mockjax({
  104. url: "/api/users/" + user.get('id') + '/change-password/',
  105. status: 200,
  106. responseText: {
  107. 'detail': 'Success happened!'
  108. }
  109. });
  110. assert.expect(2);
  111. visit('/options/change-password/');
  112. fillIn('#id_new_password', 'V4lidPassword');
  113. fillIn('#id_repeat_password', 'V4lidPassword');
  114. fillIn('#id_password', 'password');
  115. click('.panel-form .panel-footer .btn-primary');
  116. andThen(function() {
  117. assert.equal(currentPath(), 'options.password.index');
  118. assert.equal(getToastMessage(), 'Success happened!');
  119. });
  120. });
  121. test('/options/change-password/token handles invalid token', function(assert) {
  122. var user = createUser();
  123. auth.setProperties({
  124. 'isAuthenticated': true,
  125. 'user': user
  126. });
  127. Ember.$.mockjax({
  128. url: "/api/users/" + user.get('id') + '/change-password/',
  129. status: 400,
  130. responseText: {
  131. 'detail': 'Token is invalid.'
  132. }
  133. });
  134. assert.expect(2);
  135. visit('/options/change-password/token/');
  136. andThen(function() {
  137. assert.equal(currentPath(), 'options.password.index');
  138. assert.equal(getToastMessage(), 'Token is invalid.');
  139. });
  140. });
  141. test('/options/change-password/token handles valid token', function(assert) {
  142. var user = createUser();
  143. auth.setProperties({
  144. 'isAuthenticated': true,
  145. 'user': user
  146. });
  147. Ember.$.mockjax({
  148. url: "/api/users/" + user.get('id') + '/change-password/',
  149. status: 200,
  150. responseText: {
  151. 'detail': 'E-mail was changed.'
  152. }
  153. });
  154. assert.expect(2);
  155. visit('/options/change-password/token/');
  156. andThen(function() {
  157. assert.equal(currentPath(), 'options.password.index');
  158. assert.equal(getToastMessage(), 'E-mail was changed.');
  159. });
  160. });