reset-password-form.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import misago from 'misago/index';
  4. import { ResetPasswordForm, PasswordChangedPage } from 'misago/components/reset-password-form'; // jshint ignore:line
  5. import modal from 'misago/services/modal';
  6. import snackbar from 'misago/services/snackbar';
  7. import store from 'misago/services/store';
  8. import * as testUtils from 'misago/utils/test-utils';
  9. let snackbarStore = null;
  10. describe("Reset Password Form", function() {
  11. beforeEach(function() {
  12. snackbarStore = testUtils.snackbarStoreMock();
  13. snackbar.init(snackbarStore);
  14. testUtils.initEmptyStore(store);
  15. misago._context = {
  16. 'SETTINGS': {
  17. 'forum_name': 'Test forum',
  18. 'password_length_min': 4
  19. },
  20. 'CHANGE_PASSWORD_API': '/test-api/change-password/1/s0m3-t0k3n/'
  21. };
  22. /* jshint ignore:start */
  23. testUtils.render(<ResetPasswordForm />);
  24. /* jshint ignore:end */
  25. });
  26. afterEach(function() {
  27. testUtils.unmountComponents();
  28. testUtils.snackbarClear(snackbar);
  29. $.mockjax.clear();
  30. });
  31. it("renders", function() {
  32. let element = $('#test-mount .well-form-reset-password');
  33. assert.ok(element.length, "component renders");
  34. });
  35. it("handles empty submit", function(done) {
  36. snackbarStore.callback(function(message) {
  37. assert.deepEqual(message, {
  38. message: "Enter new password.",
  39. type: 'error'
  40. }, "form brought error about no input");
  41. done();
  42. });
  43. testUtils.simulateSubmit('#test-mount form');
  44. });
  45. it("handles invalid submit", function(done) {
  46. snackbarStore.callback(function(message) {
  47. assert.deepEqual(message, {
  48. message: "Valid password must be at least 4 characters long.",
  49. type: 'error'
  50. }, "form brought error about invalid input");
  51. done();
  52. });
  53. testUtils.simulateChange('#test-mount input', 'abc');
  54. testUtils.simulateSubmit('#test-mount form');
  55. });
  56. it("handles backend error", function(done) {
  57. snackbarStore.callback(function(message) {
  58. assert.deepEqual(message, {
  59. message: "Unknown error has occured.",
  60. type: 'error'
  61. }, "form raised alert about backend error");
  62. done();
  63. });
  64. $.mockjax({
  65. url: '/test-api/change-password/1/s0m3-t0k3n/',
  66. status: 500
  67. });
  68. testUtils.simulateChange('#test-mount input', 'Som3L33tP455');
  69. testUtils.simulateSubmit('#test-mount form');
  70. });
  71. it("handles backend rejection", function(done) {
  72. snackbarStore.callback(function(message) {
  73. assert.deepEqual(message, {
  74. message: "Nope nope nope!",
  75. type: 'error'
  76. }, "form raised alert about backend rejection");
  77. done();
  78. });
  79. $.mockjax({
  80. url: '/test-api/change-password/1/s0m3-t0k3n/',
  81. status: 400,
  82. responseText: {
  83. detail: "Nope nope nope!"
  84. }
  85. });
  86. testUtils.simulateChange('#test-mount input', 'Som3L33tP455');
  87. testUtils.simulateSubmit('#test-mount form');
  88. });
  89. it("from banned IP", function(done) {
  90. $.mockjax({
  91. url: '/test-api/change-password/1/s0m3-t0k3n/',
  92. status: 403,
  93. responseText: {
  94. 'ban': {
  95. 'expires_on': null,
  96. 'message': {
  97. 'plain': 'Your ip is banned for spamming.',
  98. 'html': '<p>Your ip is banned for spamming.</p>',
  99. }
  100. }
  101. }
  102. });
  103. testUtils.simulateChange('#test-mount input', 'Som3L33tP455');
  104. testUtils.simulateSubmit('#test-mount form');
  105. testUtils.onElement('.page-error-banned .lead', function() {
  106. assert.equal(
  107. $('.page .message-body .lead p').text().trim(),
  108. "Your ip is banned for spamming.",
  109. "displayed error banned page with ban message.");
  110. done();
  111. });
  112. });
  113. it("handles success", function(done) { // jshint ignore:line
  114. $.mockjax({
  115. url: '/test-api/change-password/1/s0m3-t0k3n/',
  116. status: 200,
  117. responseText: {
  118. 'username': 'Bob'
  119. }
  120. });
  121. /* jshint ignore:start */
  122. let callback = function(apiResponse) {
  123. assert.deepEqual(apiResponse, {
  124. 'username': 'Bob'
  125. }, "callback function was called on ajax success");
  126. done();
  127. };
  128. testUtils.render(<ResetPasswordForm callback={callback} />);
  129. /* jshint ignore:end */
  130. testUtils.simulateChange('#test-mount input', 'Som3L33tP455');
  131. testUtils.simulateSubmit('#test-mount form');
  132. });
  133. });
  134. describe("Password Changed Page", function() {
  135. beforeEach(function() {
  136. testUtils.initModal(modal);
  137. misago._context = {
  138. 'FORGOTTEN_PASSWORD_URL': '/forgotten-password/'
  139. };
  140. /* jshint ignore:start */
  141. testUtils.render(<PasswordChangedPage user={{username: 'BobBoberson'}} />);
  142. /* jshint ignore:end */
  143. });
  144. afterEach(function() {
  145. testUtils.unmountComponents();
  146. });
  147. it("renders", function() {
  148. let element = $('#test-mount .page-forgotten-password-changed');
  149. assert.ok(element.length, "component renders");
  150. assert.equal(
  151. $('#test-mount .page .message-body p.lead').text().trim(),
  152. "BobBoberson, your password has been changed successfully.",
  153. "displayed password changed page with valid message.");
  154. });
  155. it('opens sign in modal on click', function(done) {
  156. testUtils.simulateClick('#test-mount .btn-primary');
  157. testUtils.onElement('#modal-mount .modal-sign-in', function() {
  158. assert.ok(true, "sign in modal was displayed");
  159. done();
  160. });
  161. });
  162. });