sign-in.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 SignIn from 'misago/components/sign-in'; // 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("Sign In", function() {
  11. beforeEach(function() {
  12. snackbarStore = window.snackbarStoreMock();
  13. snackbar.init(snackbarStore);
  14. window.initModal(modal);
  15. misago._context = {
  16. 'SETTINGS': {
  17. forum_name: 'Test Forum'
  18. },
  19. 'AUTH_API': '/test-api/auth/',
  20. 'REQUEST_ACTIVATION_URL': '/request-activation/',
  21. 'FORGOTTEN_PASSWORD_URL': '/forgotten-password/'
  22. };
  23. /* jshint ignore:start */
  24. component = ReactDOM.render(
  25. <SignIn />,
  26. document.getElementById('test-mount')
  27. );
  28. /* jshint ignore:end */
  29. });
  30. afterEach(function() {
  31. window.emptyTestContainers();
  32. $.mockjax.clear();
  33. });
  34. it("renders", function() {
  35. assert.ok($('#test-mount .modal-sign-in #id_username').length,
  36. "username input rendered");
  37. assert.ok($('#test-mount .modal-sign-in #id_password').length,
  38. "password input rendered");
  39. assert.equal(
  40. $('#test-mount .modal-footer .btn-default').attr('href'),
  41. misago.get('FORGOTTEN_PASSWORD_URL'),
  42. "forgotten password form url is valid");
  43. });
  44. it("handles empty submit", function() {
  45. window.simulateSubmit('#test-mount form');
  46. assert.deepEqual(snackbarStore.message, {
  47. message: "Fill out both fields.",
  48. type: 'error'
  49. }, "form validation rejected empty form");
  50. });
  51. it("handles partial submit", function() {
  52. window.simulateChange('#id_username', 'loremipsum');
  53. window.simulateSubmit('#test-mount form');
  54. assert.deepEqual(snackbarStore.message, {
  55. message: "Fill out both fields.",
  56. type: 'error'
  57. }, "form validation rejected empty form");
  58. });
  59. it("handles backend error", function(done) {
  60. $.mockjax({
  61. url: '/test-api/auth/',
  62. status: 500
  63. });
  64. window.simulateChange('#id_username', 'SomeFake');
  65. window.simulateChange('#id_password', 'pass1234');
  66. window.simulateSubmit('#test-mount form');
  67. window.afterAjax(component, function() {
  68. assert.deepEqual(snackbarStore.message, {
  69. message: "Unknown error has occured.",
  70. type: 'error'
  71. }, "form raised alert about backend error");
  72. done();
  73. });
  74. });
  75. it("handles invalid credentials", function(done) {
  76. let message = 'Login or password is incorrect.';
  77. $.mockjax({
  78. url: '/test-api/auth/',
  79. status: 400,
  80. responseText: {
  81. 'detail': message,
  82. 'code': 'invalid_login'
  83. }
  84. });
  85. window.simulateChange('#id_username', 'SomeFake');
  86. window.simulateChange('#id_password', 'pass1234');
  87. window.simulateSubmit('#test-mount form');
  88. window.afterAjax(component, function() {
  89. assert.deepEqual(snackbarStore.message, {
  90. message: message,
  91. type: 'error'
  92. }, "form raised alert about invalid credentials");
  93. done();
  94. });
  95. });
  96. it("to admin-activated account", function(done) {
  97. let message = "This account has to be activated by admin.";
  98. $.mockjax({
  99. url: '/test-api/auth/',
  100. status: 400,
  101. responseText: {
  102. 'detail': message,
  103. 'code': 'inactive_admin'
  104. }
  105. });
  106. window.simulateChange('#id_username', 'SomeFake');
  107. window.simulateChange('#id_password', 'pass1234');
  108. window.simulateSubmit('#test-mount form');
  109. window.afterAjax(component, function() {
  110. assert.deepEqual(snackbarStore.message, {
  111. message: message,
  112. type: 'info'
  113. }, "form raised alert about admin-activated account");
  114. done();
  115. });
  116. });
  117. it("to user-activated account", function(done) {
  118. let message = "This account has to be activated.";
  119. $.mockjax({
  120. url: '/test-api/auth/',
  121. status: 400,
  122. responseText: {
  123. 'detail': message,
  124. 'code': 'inactive_user'
  125. }
  126. });
  127. window.simulateChange('#id_username', 'SomeFake');
  128. window.simulateChange('#id_password', 'pass1234');
  129. window.simulateSubmit('#test-mount form');
  130. window.afterAjax(component, function() {
  131. assert.deepEqual(snackbarStore.message, {
  132. message: message,
  133. type: 'info'
  134. }, "form raised alert about user-activated account");
  135. let activateButton = $('#test-mount .modal-footer .btn-success');
  136. assert.ok(activateButton.length, "activation button displayed");
  137. assert.equal(
  138. activateButton.attr('href'), misago.get('REQUEST_ACTIVATION_URL'),
  139. "button to activation form has valid url");
  140. done();
  141. });
  142. });
  143. it("from banned IP", function(done) {
  144. $.mockjax({
  145. url: '/test-api/auth/',
  146. status: 403,
  147. responseText: {
  148. 'ban': {
  149. 'expires_on': null,
  150. 'message': {
  151. 'plain': 'Your ip is banned for spamming.',
  152. 'html': '<p>Your ip is banned for spamming.</p>',
  153. }
  154. }
  155. }
  156. });
  157. window.simulateChange('#id_username', 'SomeFake');
  158. window.simulateChange('#id_password', 'pass1234');
  159. window.simulateSubmit('#test-mount form');
  160. window.onElement('.page-error-banned .lead', function() {
  161. assert.equal(
  162. $('.page .message-body .lead p').text().trim(),
  163. "Your ip is banned for spamming.",
  164. "displayed error banned page with ban message.");
  165. done();
  166. });
  167. });
  168. it("to banned account", function(done) {
  169. $.mockjax({
  170. url: '/test-api/auth/',
  171. status: 400,
  172. responseText: {
  173. 'detail': {
  174. 'expires_on': null,
  175. 'message': {
  176. 'plain': 'You are banned for trolling.',
  177. 'html': '<p>You are banned for trolling.</p>',
  178. }
  179. },
  180. 'code': 'banned'
  181. }
  182. });
  183. window.simulateChange('#id_username', 'SomeFake');
  184. window.simulateChange('#id_password', 'pass1234');
  185. window.simulateSubmit('#test-mount form');
  186. window.onElement('.page-error-banned .lead', function() {
  187. assert.equal(
  188. $('.page .message-body .lead p').text().trim(),
  189. "You are banned for trolling.",
  190. "displayed error banned page with ban message.");
  191. done();
  192. });
  193. });
  194. it("login successfully", function(done) {
  195. $('body').append('<div id="hidden-login-form"></div>');
  196. $.mockjax({
  197. url: '/test-api/auth/',
  198. status: 200,
  199. responseText: {
  200. 'detail': 'ok'
  201. }
  202. });
  203. let form = $('#hidden-login-form');
  204. form.on('submit', function(e) {
  205. e.stopPropagation();
  206. assert.equal(form.find('input[name="username"]').val(), 'SomeFake',
  207. "form was filled with valid username.");
  208. assert.equal(form.find('input[name="password"]').val(), 'pass1234',
  209. "form was filled with valid password.");
  210. form.remove();
  211. done();
  212. return false;
  213. });
  214. window.simulateChange('#id_username', 'SomeFake');
  215. window.simulateChange('#id_password', 'pass1234');
  216. window.simulateSubmit('#test-mount form');
  217. });
  218. });