reset-password-form.js 5.4 KB

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