request-password-reset.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 />, 'test-mount');
  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(
  106. <RequestResetForm showInactivePage={showInactivePage}/>,
  107. 'test-mount'
  108. );
  109. /* jshint ignore:end */
  110. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  111. testUtils.simulateSubmit('#test-mount form');
  112. });
  113. it("from banned IP", function(done) {
  114. $.mockjax({
  115. url: '/test-api/request-password-reset/',
  116. status: 403,
  117. responseText: {
  118. 'ban': {
  119. 'expires_on': null,
  120. 'message': {
  121. 'plain': 'Your ip is banned for spamming.',
  122. 'html': '<p>Your ip is banned for spamming.</p>',
  123. }
  124. }
  125. }
  126. });
  127. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  128. testUtils.simulateSubmit('#test-mount form');
  129. testUtils.onElement('.page-error-banned .lead', function() {
  130. assert.equal(
  131. $('.page .message-body .lead p').text().trim(),
  132. "Your ip is banned for spamming.",
  133. "displayed error banned page with ban message.");
  134. done();
  135. });
  136. });
  137. it("handles success", function(done) { // jshint ignore:line
  138. $.mockjax({
  139. url: '/test-api/request-password-reset/',
  140. status: 200,
  141. responseText: {
  142. 'username': 'Bob',
  143. 'email': 'bob@boberson.com'
  144. }
  145. });
  146. /* jshint ignore:start */
  147. let callback = function(apiResponse) {
  148. assert.deepEqual(apiResponse, {
  149. 'username': 'Bob',
  150. 'email': 'bob@boberson.com'
  151. }, "callback function was called on ajax success");
  152. done();
  153. };
  154. testUtils.render(<RequestResetForm callback={callback} />, 'test-mount');
  155. /* jshint ignore:end */
  156. testUtils.simulateChange('#test-mount input', 'lorem@ipsum.com');
  157. testUtils.simulateSubmit('#test-mount form');
  158. });
  159. });
  160. describe("Reset Link Sent", function() {
  161. afterEach(function() {
  162. testUtils.emptyTestContainers();
  163. });
  164. it("renders message", function(done) { // jshint ignore:line
  165. /* jshint ignore:start */
  166. let callback = function() {
  167. assert.ok(true, "callback function was called on button press");
  168. done();
  169. };
  170. testUtils.render(
  171. <LinkSent user={{email: 'bob@boberson.com' }}
  172. callback={callback} />,
  173. 'test-mount'
  174. );
  175. /* jshint ignore:end */
  176. let element = $('#test-mount .well-done');
  177. assert.ok(element.length, "component renders");
  178. assert.equal(element.find('p').text().trim(),
  179. "Reset password link was sent to bob@boberson.com",
  180. "component renders valid message");
  181. testUtils.simulateClick('#test-mount .btn-primary');
  182. });
  183. });
  184. describe("Account Inactive Page", function() {
  185. beforeEach(function() {
  186. misago._context = {
  187. 'REQUEST_ACTIVATION_URL': '/activate-thy-account/'
  188. };
  189. });
  190. afterEach(function() {
  191. testUtils.emptyTestContainers();
  192. });
  193. it("renders page for user-activated user", function() {
  194. /* jshint ignore:start */
  195. testUtils.render(
  196. <AccountInactivePage activation='inactive_user'
  197. message="Lorem ipsum dolor met." />,
  198. 'test-mount'
  199. );
  200. /* jshint ignore:end */
  201. let element = $('#test-mount .page-forgotten-password-inactive');
  202. assert.ok(element.length, "component renders");
  203. assert.equal(
  204. $('#test-mount .page .message-body p:eq(1)').text().trim(),
  205. "Lorem ipsum dolor met.",
  206. "displayed error inactive page with backend message.");
  207. assert.equal($('#test-mount a').attr('href'), '/activate-thy-account/',
  208. "activate account link is displayed on inactive error page");
  209. });
  210. it("renders page for admin-activated user", function() {
  211. /* jshint ignore:start */
  212. testUtils.render(
  213. <AccountInactivePage activation='inactive_admin'
  214. message="Lorem ipsum dolor met admin." />,
  215. 'test-mount'
  216. );
  217. /* jshint ignore:end */
  218. let element = $('#test-mount .page-forgotten-password-inactive');
  219. assert.ok(element.length, "component renders");
  220. assert.equal(
  221. $('#test-mount .page .message-body p:eq(1)').text().trim(),
  222. "Lorem ipsum dolor met admin.",
  223. "displayed error inactive page with backend message.");
  224. assert.ok(!$('#test-mount a').length,
  225. "activate account link is not displayed on admin-activated error page");
  226. });
  227. });