sign-in.js 7.3 KB

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