register.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import misago from 'misago/index';
  4. import { RegisterForm, RegisterComplete } from 'misago/components/register'; // jshint ignore:line
  5. import modal from 'misago/services/modal';
  6. import snackbar from 'misago/services/snackbar';
  7. import store from 'misago/services/store';
  8. import * as testUtils from 'misago/utils/test-utils';
  9. let component = null;
  10. let snackbarStore = null;
  11. describe("Register Form", function() {
  12. beforeEach(function() {
  13. snackbarStore = testUtils.snackbarStoreMock();
  14. snackbar.init(snackbarStore);
  15. testUtils.initModal(modal);
  16. testUtils.initEmptyStore(store);
  17. window.zxcvbn = function() {
  18. return {
  19. score: 2
  20. };
  21. };
  22. misago._context = {
  23. 'SETTINGS': {
  24. captcha_type: 'no',
  25. username_length_min: 3,
  26. username_length_max: 10,
  27. password_length_min: 3
  28. },
  29. 'USERS_API': '/test-api/users/',
  30. 'TERMS_OF_SERVICE_URL': '/read-rules/'
  31. };
  32. /* jshint ignore:start */
  33. component = testUtils.render(<RegisterForm />);
  34. /* jshint ignore:end */
  35. });
  36. afterEach(function() {
  37. delete window.zxcvbn;
  38. testUtils.unmountComponents();
  39. testUtils.snackbarClear(snackbar);
  40. $.mockjax.clear();
  41. });
  42. it("renders", function() {
  43. let element = $('.modal-register');
  44. assert.ok(element.length, "component renders");
  45. assert.equal(element.find('.modal-footer a').attr('href'), '/read-rules/',
  46. "registration has url to forum TOS");
  47. assert.equal(element.find('.modal-footer a').text().trim(),
  48. "By registering you agree to site's terms and conditions.",
  49. "registration has legal footnote");
  50. });
  51. it("handles empty submit", function(done) {
  52. snackbarStore.callback(function(message) {
  53. assert.deepEqual(message, {
  54. message: "Form contains errors.",
  55. type: "error"
  56. }, "rejests empty submission");
  57. done();
  58. });
  59. testUtils.simulateSubmit('#test-mount form');
  60. });
  61. it("handles invalid submit", function(done) {
  62. snackbarStore.callback(function(message) {
  63. assert.deepEqual(message, {
  64. message: "Form contains errors.",
  65. type: "error"
  66. }, "rejests empty submission");
  67. done();
  68. });
  69. testUtils.simulateChange('#id_username', 'lo');
  70. testUtils.simulateChange('#id_email', 'nope');
  71. testUtils.simulateChange('#id_password', 'sh');
  72. testUtils.simulateSubmit('#test-mount form');
  73. });
  74. it("handles backend error", function(done) {
  75. snackbarStore.callback(function(message) {
  76. assert.deepEqual(message, {
  77. message: "Unknown error has occured.",
  78. type: 'error'
  79. }, "raised alert about backend error");
  80. done();
  81. });
  82. $.mockjax({
  83. url: '/test-api/users/',
  84. status: 500
  85. });
  86. testUtils.simulateChange('#id_username', 'SomeFake');
  87. testUtils.simulateChange('#id_email', 'lorem@ipsum.com');
  88. testUtils.simulateChange('#id_password', 'pass1234');
  89. testUtils.simulateSubmit('#test-mount form');
  90. });
  91. it("handles rejected data", function(done) {
  92. snackbarStore.callback(function(message) {
  93. assert.deepEqual(message, {
  94. message: "Form contains errors.",
  95. type: 'error'
  96. }, "raised alert about backend rejection");
  97. component.forceUpdate(function() {
  98. assert.deepEqual(component.state.errors.username,
  99. ['Dat username tou!'],
  100. "set backend username error to validation");
  101. assert.deepEqual(component.state.errors.password,
  102. ['Dat password tou!'],
  103. "set backend password error to validation");
  104. done();
  105. });
  106. });
  107. $.mockjax({
  108. url: '/test-api/users/',
  109. status: 400,
  110. responseText: {
  111. username: ['Dat username tou!'],
  112. password: ['Dat password tou!']
  113. }
  114. });
  115. testUtils.simulateChange('#id_username', 'SomeFake');
  116. testUtils.simulateChange('#id_email', 'lorem@ipsum.com');
  117. testUtils.simulateChange('#id_password', 'pass1234');
  118. testUtils.simulateSubmit('#test-mount form');
  119. });
  120. it("from banned IP", function(done) {
  121. $.mockjax({
  122. url: '/test-api/users/',
  123. status: 403,
  124. responseText: {
  125. 'ban': {
  126. 'expires_on': null,
  127. 'message': {
  128. 'plain': 'Your ip is banned from registering.',
  129. 'html': '<p>Your ip is banned from registering.</p>',
  130. }
  131. }
  132. }
  133. });
  134. testUtils.simulateChange('#id_username', 'SomeFake');
  135. testUtils.simulateChange('#id_email', 'lorem@ipsum.com');
  136. testUtils.simulateChange('#id_password', 'pass1234');
  137. testUtils.simulateSubmit('#test-mount form');
  138. testUtils.onElement('.page-error-banned .lead', function() {
  139. assert.equal(
  140. $('.page .message-body .lead p').text().trim(),
  141. "Your ip is banned from registering.",
  142. "displayed error banned page with ban message.");
  143. done();
  144. });
  145. });
  146. it("registered account", function(done) { // jshint ignore:line
  147. /* jshint ignore:start */
  148. let callback = function(user) {
  149. assert.deepEqual(user, {
  150. 'activation': 'user',
  151. 'username': 'BobBoberson',
  152. 'email': 'bob@boberson.com'
  153. }, "user data returned from backend is passed to success callback");
  154. done();
  155. };
  156. component = testUtils.render(<RegisterForm callback={callback}/>);
  157. /* jshint ignore:end */
  158. $.mockjax({
  159. url: '/test-api/users/',
  160. status: 200,
  161. responseText: {
  162. 'activation': 'user',
  163. 'username': 'BobBoberson',
  164. 'email': 'bob@boberson.com'
  165. }
  166. });
  167. testUtils.simulateChange('#id_username', 'SomeFake');
  168. testUtils.simulateChange('#id_email', 'lorem@ipsum.com');
  169. testUtils.simulateChange('#id_password', 'pass1234');
  170. testUtils.simulateSubmit('#test-mount form');
  171. });
  172. });
  173. describe("Register Complete", function() {
  174. afterEach(function() {
  175. testUtils.unmountComponents();
  176. });
  177. it("renders user-activated message", function() {
  178. /* jshint ignore:start */
  179. testUtils.render(
  180. <RegisterComplete activation="user"
  181. username="Bob"
  182. email="bob@boberson.com" />
  183. );
  184. /* jshint ignore:end */
  185. let element = $('#test-mount .modal-message');
  186. assert.ok(element.length, "component renders");
  187. assert.equal(element.find('p').first().text().trim(),
  188. "Bob, your account has been created but you need to activate it before you will be able to sign in.",
  189. "component renders valid message");
  190. assert.equal(element.find('p').last().text().trim(),
  191. "We have sent an e-mail to bob@boberson.com with link that you have to click to activate your account.",
  192. "component renders valid activation instruction");
  193. });
  194. it("renders admin-activated message", function() {
  195. /* jshint ignore:start */
  196. testUtils.render(
  197. <RegisterComplete activation="admin"
  198. username="Bob"
  199. email="bob@boberson.com" />
  200. );
  201. /* jshint ignore:end */
  202. let element = $('#test-mount .modal-message');
  203. assert.ok(element.length, "component renders");
  204. assert.equal(element.find('p').first().text().trim(),
  205. "Bob, your account has been created but board administrator will have to activate it before you will be able to sign in.",
  206. "component renders valid message");
  207. assert.equal(element.find('p').last().text().trim(),
  208. "We will send an e-mail to bob@boberson.com when this takes place.",
  209. "component renders valid activation instruction");
  210. });
  211. });