sign-in.js 7.2 KB

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