login-test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import Ember from 'ember';
  2. import { module, test } from 'qunit';
  3. import startApp from '../helpers/start-app';
  4. import getToastMessage from '../helpers/toast-message';
  5. var application;
  6. module('Acceptance: Login', {
  7. beforeEach: function() {
  8. Ember.$('#hidden-login-form').on('submit.stopInTest', function(event) {
  9. event.stopPropagation();
  10. return false;
  11. });
  12. application = startApp();
  13. },
  14. afterEach: function() {
  15. Ember.$('#hidden-login-form').off('submit.stopInTest');
  16. Ember.$('#loginModal').off();
  17. Ember.$('body').removeClass('modal-open');
  18. Ember.run(application, 'destroy');
  19. Ember.$.mockjax.clear();
  20. }
  21. });
  22. test('login with empty credentials', function(assert) {
  23. visit('/');
  24. click('.guest-nav button.btn-login');
  25. click('#loginModal .btn-primary');
  26. andThen(function() {
  27. assert.equal(getToastMessage(), 'Fill out both fields.');
  28. });
  29. });
  30. test('login with invalid credentials', function(assert) {
  31. var message = 'Login or password is incorrect.';
  32. Ember.$.mockjax({
  33. url: "/api/auth/login/",
  34. status: 400,
  35. responseText: {
  36. 'detail': message,
  37. 'code': 'invalid_login'
  38. }
  39. });
  40. visit('/');
  41. click('.guest-nav .btn-login');
  42. fillIn('#loginModal .form-group:first-child input', 'SomeFake');
  43. fillIn('#loginModal .form-group:last-child input', 'pass1234');
  44. click('#loginModal .btn-primary');
  45. andThen(function() {
  46. assert.equal(getToastMessage(), message);
  47. });
  48. });
  49. test('login to user-activated account', function(assert) {
  50. var message = 'You have to activate your account before you will be able to sign in.';
  51. Ember.$.mockjax({
  52. url: "/api/auth/login/",
  53. status: 400,
  54. responseText: {
  55. 'detail': message,
  56. 'code': 'inactive_user'
  57. }
  58. });
  59. visit('/');
  60. click('.guest-nav .btn-login');
  61. fillIn('#loginModal .form-group:first-child input', 'SomeFake');
  62. fillIn('#loginModal .form-group:last-child input', 'pass1234');
  63. click('#loginModal .btn-primary');
  64. andThen(function() {
  65. assert.equal(getToastMessage(), message);
  66. });
  67. });
  68. test('login to admin-activated account', function(assert) {
  69. var message = 'Your account has to be activated by Administrator before you will be able to sign in.';
  70. Ember.$.mockjax({
  71. url: "/api/auth/login/",
  72. status: 400,
  73. responseText: {
  74. 'detail': message,
  75. 'code': 'inactive_admin'
  76. }
  77. });
  78. visit('/');
  79. click('.guest-nav .btn-login');
  80. fillIn('#loginModal .form-group:first-child input', 'SomeFake');
  81. fillIn('#loginModal .form-group:last-child input', 'pass1234');
  82. click('#loginModal .btn-primary');
  83. andThen(function() {
  84. assert.equal(getToastMessage(), message);
  85. });
  86. });
  87. test('login to banned account', function(assert) {
  88. var done = assert.async();
  89. Ember.$.mockjax({
  90. url: "/api/auth/login/",
  91. status: 400,
  92. responseText: {
  93. 'detail': {
  94. 'expires_on': null,
  95. 'message': {
  96. 'plain': 'You are banned for trolling.',
  97. 'html': '<p>You are banned for trolling.</p>',
  98. }
  99. },
  100. 'code': 'banned'
  101. }
  102. });
  103. visit('/');
  104. click('.guest-nav .btn-login');
  105. fillIn('#loginModal .form-group:first-child input', 'SomeFake');
  106. fillIn('#loginModal .form-group:last-child input', 'pass1234');
  107. click('#loginModal .btn-primary');
  108. andThen(function() {
  109. var banMessage = find('.lead p').text();
  110. assert.equal(banMessage, 'You are banned for trolling.');
  111. var expirationMessage = find('.error-message>p').text();
  112. assert.equal(expirationMessage, 'This ban is permanent.');
  113. done();
  114. });
  115. });
  116. test('login successfully', function(assert) {
  117. Ember.$.mockjax({
  118. url: "/api/auth/login/",
  119. status: 200,
  120. responseText: {
  121. 'username': 'SomeFake'
  122. }
  123. });
  124. visit('/');
  125. click('.guest-nav .btn-login');
  126. fillIn('#loginModal .form-group:first-child input', 'SomeFake');
  127. fillIn('#loginModal .form-group:last-child input', 'pass1234');
  128. click('#loginModal .btn-primary');
  129. andThen(function() {
  130. assert.equal(Ember.$('#hidden-login-form input[name="username"]').val(), 'SomeFake');
  131. assert.equal(Ember.$('#hidden-login-form input[name="password"]').val(), 'pass1234');
  132. });
  133. });