login-test.js 4.2 KB

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