change-email-test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 E-mail', {
  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-email 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-email/');
  26. andThen(function() {
  27. assert.equal(currentPath(), 'options.email.index');
  28. });
  29. });
  30. test('/options/change-email form handles empty submission', function(assert) {
  31. var user = createUser();
  32. auth.setProperties({
  33. 'isAuthenticated': true,
  34. 'user': user
  35. });
  36. assert.expect(4);
  37. visit('/options/change-email/');
  38. click('.panel-form .panel-footer .btn-primary');
  39. andThen(function() {
  40. assert.equal(currentPath(), 'options.email.index');
  41. assert.equal(getToastMessage(), 'Form contains errors.');
  42. var emailValidation = Ember.$('#id_new_email').parents('.form-group').find('.help-block.errors');
  43. assert.equal(Ember.$.trim(emailValidation.text()), 'Enter new e-mail.');
  44. var passwordValidation = Ember.$('#id_password').parents('.form-group').find('.help-block.errors');
  45. assert.equal(Ember.$.trim(passwordValidation.text()), 'Enter current password.');
  46. });
  47. });
  48. test('/options/change-email form handles invalid submission', function(assert) {
  49. var user = createUser();
  50. auth.setProperties({
  51. 'isAuthenticated': true,
  52. 'user': user
  53. });
  54. assert.expect(3);
  55. visit('/options/change-email/');
  56. fillIn('#id_new_email', 'not-valid-email');
  57. fillIn('#id_password', 'password');
  58. click('.panel-form .panel-footer .btn-primary');
  59. andThen(function() {
  60. assert.equal(currentPath(), 'options.email.index');
  61. assert.equal(getToastMessage(), 'Form contains errors.');
  62. var fieldValidation = Ember.$('#id_new_email').parents('.form-group').find('.help-block.errors');
  63. assert.equal(Ember.$.trim(fieldValidation.text()), 'Invalid e-mail address.');
  64. });
  65. });
  66. test('/options/change-email form handles error 400', function(assert) {
  67. var user = createUser();
  68. auth.setProperties({
  69. 'isAuthenticated': true,
  70. 'user': user
  71. });
  72. Ember.$.mockjax({
  73. url: "/api/users/" + user.get('id') + '/change-email/',
  74. status: 400,
  75. responseText: {
  76. 'new_email': ['E-mail is bad.'],
  77. 'password': ['Password is bad.']
  78. }
  79. });
  80. assert.expect(4);
  81. visit('/options/change-email/');
  82. fillIn('#id_new_email', 'valid@email.com');
  83. fillIn('#id_password', 'password');
  84. click('.panel-form .panel-footer .btn-primary');
  85. andThen(function() {
  86. assert.equal(currentPath(), 'options.email.index');
  87. assert.equal(getToastMessage(), 'Form contains errors.');
  88. var emailValidation = Ember.$('#id_new_email').parents('.form-group').find('.help-block.errors');
  89. assert.equal(Ember.$.trim(emailValidation.text()), 'E-mail is bad.');
  90. var passwordValidation = Ember.$('#id_password').parents('.form-group').find('.help-block.errors');
  91. assert.equal(Ember.$.trim(passwordValidation.text()), 'Password is bad.');
  92. });
  93. });
  94. test('/options/change-email form handles valid submission', function(assert) {
  95. var user = createUser();
  96. auth.setProperties({
  97. 'isAuthenticated': true,
  98. 'user': user
  99. });
  100. Ember.$.mockjax({
  101. url: "/api/users/" + user.get('id') + '/change-email/',
  102. status: 200,
  103. responseText: {
  104. 'detail': 'Success happened!'
  105. }
  106. });
  107. assert.expect(2);
  108. visit('/options/change-email/');
  109. fillIn('#id_new_email', 'valid@email.com');
  110. fillIn('#id_password', 'password');
  111. click('.panel-form .panel-footer .btn-primary');
  112. andThen(function() {
  113. assert.equal(currentPath(), 'options.email.index');
  114. assert.equal(getToastMessage(), 'Success happened!');
  115. });
  116. });
  117. test('/options/change-email/token handles invalid token', function(assert) {
  118. var user = createUser();
  119. auth.setProperties({
  120. 'isAuthenticated': true,
  121. 'user': user
  122. });
  123. Ember.$.mockjax({
  124. url: "/api/users/" + user.get('id') + '/change-email/',
  125. status: 400,
  126. responseText: {
  127. 'detail': 'Token is invalid.'
  128. }
  129. });
  130. assert.expect(2);
  131. visit('/options/change-email/token/');
  132. andThen(function() {
  133. assert.equal(currentPath(), 'options.email.index');
  134. assert.equal(getToastMessage(), 'Token is invalid.');
  135. });
  136. });
  137. test('/options/change-email/token handles valid token', function(assert) {
  138. var user = createUser();
  139. auth.setProperties({
  140. 'isAuthenticated': true,
  141. 'user': user
  142. });
  143. Ember.$.mockjax({
  144. url: "/api/users/" + user.get('id') + '/change-email/',
  145. status: 200,
  146. responseText: {
  147. 'detail': 'E-mail was changed.'
  148. }
  149. });
  150. assert.expect(2);
  151. visit('/options/change-email/token/');
  152. andThen(function() {
  153. assert.equal(currentPath(), 'options.email.index');
  154. assert.equal(getToastMessage(), 'E-mail was changed.');
  155. });
  156. });