reset-password-form.js 5.3 KB

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