register.js 7.4 KB

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