login-test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.$('#appModal').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. assert.expect(1);
  24. visit('/');
  25. click('.navbar-guest-nav button.btn-default');
  26. click('#appModal .btn-primary');
  27. andThen(function() {
  28. assert.equal(getToastMessage(), 'Fill out both fields.');
  29. });
  30. });
  31. test('backend errored', function(assert) {
  32. assert.expect(1);
  33. Ember.$.mockjax({
  34. url: '/api/auth/',
  35. status: 500
  36. });
  37. visit('/');
  38. click('.navbar-guest-nav .btn-default');
  39. fillIn('#appModal .form-group:first-child input', 'SomeFake');
  40. fillIn('#appModal .form-group:last-child input', 'pass1234');
  41. click('#appModal .btn-primary');
  42. andThen(function() {
  43. assert.equal(getToastMessage(), 'Unknown error has occured.');
  44. });
  45. });
  46. test('login with invalid credentials', function(assert) {
  47. assert.expect(1);
  48. var message = 'Login or password is incorrect.';
  49. Ember.$.mockjax({
  50. url: '/api/auth/',
  51. status: 400,
  52. responseText: {
  53. 'detail': message,
  54. 'code': 'invalid_login'
  55. }
  56. });
  57. visit('/');
  58. click('.navbar-guest-nav .btn-default');
  59. fillIn('#appModal .form-group:first-child input', 'SomeFake');
  60. fillIn('#appModal .form-group:last-child input', 'pass1234');
  61. click('#appModal .btn-primary');
  62. andThen(function() {
  63. assert.equal(getToastMessage(), message);
  64. });
  65. });
  66. test('login to user-activated account', function(assert) {
  67. assert.expect(1);
  68. var message = 'You have to activate your account before you will be able to sign in.';
  69. Ember.$.mockjax({
  70. url: '/api/auth/',
  71. status: 400,
  72. responseText: {
  73. 'detail': message,
  74. 'code': 'inactive_user'
  75. }
  76. });
  77. visit('/');
  78. click('.navbar-guest-nav .btn-default');
  79. fillIn('#appModal .form-group:first-child input', 'SomeFake');
  80. fillIn('#appModal .form-group:last-child input', 'pass1234');
  81. click('#appModal .btn-primary');
  82. andThen(function() {
  83. assert.equal(getToastMessage(), message);
  84. });
  85. });
  86. test('login to admin-activated account', function(assert) {
  87. assert.expect(1);
  88. var message = 'Your account has to be activated by Administrator before you will be able to sign in.';
  89. Ember.$.mockjax({
  90. url: '/api/auth/',
  91. status: 400,
  92. responseText: {
  93. 'detail': message,
  94. 'code': 'inactive_admin'
  95. }
  96. });
  97. visit('/');
  98. click('.navbar-guest-nav .btn-default');
  99. fillIn('#appModal .form-group:first-child input', 'SomeFake');
  100. fillIn('#appModal .form-group:last-child input', 'pass1234');
  101. click('#appModal .btn-primary');
  102. andThen(function() {
  103. assert.equal(getToastMessage(), message);
  104. });
  105. });
  106. test('login to banned account', function(assert) {
  107. assert.expect(3);
  108. Ember.$.mockjax({
  109. url: '/api/auth/',
  110. status: 400,
  111. responseText: {
  112. 'detail': {
  113. 'expires_on': null,
  114. 'message': {
  115. 'plain': 'You are banned for trolling.',
  116. 'html': '<p>You are banned for trolling.</p>',
  117. }
  118. },
  119. 'code': 'banned'
  120. }
  121. });
  122. visit('/');
  123. click('.navbar-guest-nav .btn-default');
  124. fillIn('#appModal .form-group:first-child input', 'SomeFake');
  125. fillIn('#appModal .form-group:last-child input', 'pass1234');
  126. click('#appModal .btn-primary');
  127. andThen(function() {
  128. assert.equal(currentPath(), 'error-banned');
  129. var banMessage = find('.lead p').text();
  130. assert.equal(banMessage, 'You are banned for trolling.');
  131. var expirationMessage = Ember.$.trim(find('.error-message>p').text());
  132. assert.equal(expirationMessage, 'This ban is permanent.');
  133. });
  134. });
  135. test('login successfully', function(assert) {
  136. assert.expect(2);
  137. Ember.$.mockjax({
  138. url: '/api/auth/',
  139. status: 200,
  140. responseText: {
  141. 'username': 'SomeFake'
  142. }
  143. });
  144. visit('/');
  145. click('.navbar-guest-nav .btn-default');
  146. fillIn('#appModal .form-group:first-child input', 'SomeFake');
  147. fillIn('#appModal .form-group:last-child input', 'pass1234');
  148. click('#appModal .btn-primary');
  149. andThen(function() {
  150. assert.equal(Ember.$('#hidden-login-form input[name="username"]').val(), 'SomeFake');
  151. assert.equal(Ember.$('#hidden-login-form input[name="password"]').val(), 'pass1234');
  152. });
  153. });