request-password-reset.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import misago from 'misago/index';
  4. import { RequestResetForm, LinkSent, AccountInactivePage } from 'misago/components/request-password-reset'; // jshint ignore:line
  5. import snackbar from 'misago/services/snackbar';
  6. import store from 'misago/services/store';
  7. import * as testUtils from 'misago/utils/test-utils';
  8. let snackbarStore = null;
  9. describe("Request Password Reset Form", function() {
  10. beforeEach(function() {
  11. snackbarStore = testUtils.snackbarStoreMock();
  12. snackbar.init(snackbarStore);
  13. testUtils.initEmptyStore(store);
  14. misago._context = {
  15. 'SETTINGS': {
  16. 'forum_name': 'Test forum'
  17. },
  18. 'SEND_PASSWORD_RESET_API': '/test-api/request-password-reset/'
  19. };
  20. /* jshint ignore:start */
  21. testUtils.render(<RequestResetForm />);
  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-request-password-reset');
  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 a valid email address.",
  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: "Enter a valid email address.",
  47. type: 'error'
  48. }, "form brought error about invalid input");
  49. done();
  50. });
  51. testUtils.simulateChange('#test-mount input', 'loremipsum');
  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/request-password-reset/',
  64. status: 500
  65. });
  66. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  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/request-password-reset/',
  79. status: 400,
  80. responseText: {
  81. detail: "Nope nope nope!"
  82. }
  83. });
  84. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  85. testUtils.simulateSubmit('#test-mount form');
  86. });
  87. it("displays activation required message", function(done) { // jshint ignore:line
  88. $.mockjax({
  89. url: '/test-api/request-password-reset/',
  90. status: 400,
  91. responseText: {
  92. code: 'inactive_user',
  93. detail: "Your account is inactive!"
  94. }
  95. });
  96. /* jshint ignore:start */
  97. let showInactivePage = function(apiResponse) {
  98. assert.deepEqual({
  99. code: apiResponse.code,
  100. detail: apiResponse.detail
  101. }, {
  102. code: 'inactive_user',
  103. detail: "Your account is inactive!"
  104. }, "component calls inactive page callback");
  105. done();
  106. };
  107. testUtils.render(<RequestResetForm showInactivePage={showInactivePage} />);
  108. /* jshint ignore:end */
  109. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  110. testUtils.simulateSubmit('#test-mount form');
  111. });
  112. it("from banned IP", function(done) {
  113. $.mockjax({
  114. url: '/test-api/request-password-reset/',
  115. status: 403,
  116. responseText: {
  117. 'ban': {
  118. 'expires_on': null,
  119. 'message': {
  120. 'plain': 'Your ip is banned for spamming.',
  121. 'html': '<p>Your ip is banned for spamming.</p>',
  122. }
  123. }
  124. }
  125. });
  126. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  127. testUtils.simulateSubmit('#test-mount form');
  128. testUtils.onElement('.page-error-banned .lead', function() {
  129. assert.equal(
  130. $('.page .message-body .lead p').text().trim(),
  131. "Your ip is banned for spamming.",
  132. "displayed error banned page with ban message.");
  133. done();
  134. });
  135. });
  136. it("handles success", function(done) { // jshint ignore:line
  137. $.mockjax({
  138. url: '/test-api/request-password-reset/',
  139. status: 200,
  140. responseText: {
  141. 'username': 'Bob',
  142. 'email': 'bob@boberson.com'
  143. }
  144. });
  145. /* jshint ignore:start */
  146. let callback = function(apiResponse) {
  147. assert.deepEqual(apiResponse, {
  148. 'username': 'Bob',
  149. 'email': 'bob@boberson.com'
  150. }, "callback function was called on ajax success");
  151. done();
  152. };
  153. testUtils.render(<RequestResetForm callback={callback} />);
  154. /* jshint ignore:end */
  155. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  156. testUtils.simulateSubmit('#test-mount form');
  157. });
  158. });
  159. describe("Reset Link Sent", function() {
  160. afterEach(function() {
  161. testUtils.unmountComponents();
  162. });
  163. it("renders message", function(done) { // jshint ignore:line
  164. /* jshint ignore:start */
  165. let callback = function() {
  166. assert.ok(true, "callback function was called on button press");
  167. done();
  168. };
  169. testUtils.render(
  170. <LinkSent user={{email: 'bob@boberson.com' }}
  171. callback={callback} />
  172. );
  173. /* jshint ignore:end */
  174. let element = $('#test-mount .well-done');
  175. assert.ok(element.length, "component renders");
  176. assert.equal(element.find('p').text().trim(),
  177. "Reset password link was sent to bob@boberson.com",
  178. "component renders valid message");
  179. testUtils.simulateClick('#test-mount .btn-primary');
  180. });
  181. });
  182. describe("Account Inactive Page", function() {
  183. beforeEach(function() {
  184. misago._context = {
  185. 'REQUEST_ACTIVATION_URL': '/activate-thy-account/'
  186. };
  187. });
  188. afterEach(function() {
  189. testUtils.unmountComponents();
  190. });
  191. it("renders page for user-activated user", function() {
  192. /* jshint ignore:start */
  193. testUtils.render(
  194. <AccountInactivePage activation='inactive_user'
  195. message="Lorem ipsum dolor met." />
  196. );
  197. /* jshint ignore:end */
  198. let element = $('#test-mount .page-forgotten-password-inactive');
  199. assert.ok(element.length, "component renders");
  200. assert.equal(
  201. $('#test-mount .page .message-body p:eq(1)').text().trim(),
  202. "Lorem ipsum dolor met.",
  203. "displayed error inactive page with backend message.");
  204. assert.equal($('#test-mount a').attr('href'), '/activate-thy-account/',
  205. "activate account link is displayed on inactive error page");
  206. });
  207. it("renders page for admin-activated user", function() {
  208. /* jshint ignore:start */
  209. testUtils.render(
  210. <AccountInactivePage activation='inactive_admin'
  211. message="Lorem ipsum dolor met admin." />
  212. );
  213. /* jshint ignore:end */
  214. let element = $('#test-mount .page-forgotten-password-inactive');
  215. assert.ok(element.length, "component renders");
  216. assert.equal(
  217. $('#test-mount .page .message-body p:eq(1)').text().trim(),
  218. "Lorem ipsum dolor met admin.",
  219. "displayed error inactive page with backend message.");
  220. assert.ok(!$('#test-mount a').length,
  221. "activate account link is not displayed on admin-activated error page");
  222. });
  223. });