register.js 7.5 KB

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