request-password-reset.js 7.5 KB

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