sign-in.js 7.2 KB

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