Browse Source

updated auth tests

Rafał Pitoń 10 years ago
parent
commit
bf23edd6c0

+ 3 - 3
misago/emberapp/app/controllers/forgotten-password/request-link.js

@@ -31,10 +31,10 @@ export default Ember.ObjectController.extend({
         if (jqXHR.status === 400){
           var rejection = jqXHR.responseJSON;
           if (rejection.code === 'banned') {
-            this.send('showBan', rejection.detail);
-            this.set('email', '');
+            self.send('showBan', rejection.detail);
+            self.set('email', '');
           } else {
-            this.send('flashError', rejection.detail);
+            self.send('flashError', rejection.detail);
           }
         } else {
           self.send("error", jqXHR);

+ 3 - 1
misago/emberapp/app/controllers/login-modal.js

@@ -84,7 +84,9 @@ export default Ember.Controller.extend({
             self.set('showActivation', true);
           } else if (rejection.code === 'banned') {
             self.send('showBan', rejection.detail);
-            Ember.$('#loginModal').modal('hide');
+            Ember.run(function() {
+              Ember.$('#loginModal').modal('hide');
+            });
           } else {
             self.send('flashError', rejection.detail);
           }

+ 2 - 2
misago/emberapp/app/index.html

@@ -19,14 +19,14 @@
 
     <section id="main"></section>
 
-    <form id='hidden-login-form' method="post" action="/login/" style="display: none;">
+    <form id="hidden-login-form" method="post" action="/login/" style="display: none;">
       <input name="csrfmiddlewaretoken" type="hidden" value="">
       <input name="redirect_to" type="text">
       <input name="username" type="text">
       <input name="password" type="password">
       <input type="submit" id="signin-button" value="Sign in">
     </form>
-    <form id='hidden-logout-form' method="post" action="/logout/" style="display: none;">
+    <form id="hidden-logout-form" method="post" action="/logout/" style="display: none;">
       <input name="csrfmiddlewaretoken" type="hidden" value="">
       <input type="submit" id="signin-button" value="Log out">
     </form>

+ 183 - 0
misago/emberapp/tests/acceptance/forgotten-password-test.js

@@ -0,0 +1,183 @@
+import Ember from 'ember';
+import { module, test } from 'qunit';
+import startApp from '../helpers/start-app';
+
+var application;
+
+module('Acceptance: ForgottenPassword', {
+  beforeEach: function() {
+    application = startApp();
+  },
+
+  afterEach: function() {
+    Ember.run(application, 'destroy');
+    Ember.$.mockjax.clear();
+  }
+});
+
+test('visiting /forgotten-password', function(assert) {
+  visit('/forgotten-password');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.index');
+  });
+});
+
+test('request password change link without entering e-mail', function(assert) {
+  visit('/forgotten-password');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.index');
+
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, 'Enter e-mail address.');
+  });
+});
+
+test('request password change link with invalid e-mail', function(assert) {
+  var message = 'Entered e-mail is invalid.';
+  Ember.$.mockjax({
+    url: "/api/change-password/send-link/",
+    status: 400,
+    responseText: {
+      'detail': message,
+      'code': 'invalid_email'
+    }
+  });
+
+  visit('/forgotten-password');
+  fillIn('.forgotten-password-page form input', 'not-valid-email');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.index');
+
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('request password change link with non-existing e-mail', function(assert) {
+  var message = 'No user with this e-mail exists.';
+  Ember.$.mockjax({
+    url: "/api/change-password/send-link/",
+    status: 400,
+    responseText: {
+      'detail': message,
+      'code': 'not_found'
+    }
+  });
+
+  visit('/forgotten-password');
+  fillIn('.forgotten-password-page form input', 'not-valid-email');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.index');
+
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('request password change link with user-activated account', function(assert) {
+  var message = 'You have to activate your account before you will be able to sign in.';
+  Ember.$.mockjax({
+    url: "/api/change-password/send-link/",
+    status: 400,
+    responseText: {
+      'detail': message,
+      'code': 'inactive_user'
+    }
+  });
+
+  visit('/forgotten-password');
+  fillIn('.forgotten-password-page form input', 'valid@mail.com');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.index');
+
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('request password change link with admin-activated account', function(assert) {
+  var message = 'Your account has to be activated by Administrator before you will be able to sign in.';
+  Ember.$.mockjax({
+    url: "/api/change-password/send-link/",
+    status: 400,
+    responseText: {
+      'detail': message,
+      'code': 'inactive_admin'
+    }
+  });
+
+  visit('/forgotten-password');
+  fillIn('.forgotten-password-page form input', 'valid@mail.com');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'forgotten-password.index');
+
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('request password change link with banned account', function(assert) {
+  var done = assert.async();
+  Ember.$.mockjax({
+    url: "/api/change-password/send-link/",
+    status: 400,
+    responseText: {
+      'detail': {
+        'expires_on': null,
+        'message': {
+          'plain': 'You are banned for trolling.',
+          'html': '<p>You are banned for trolling.</p>',
+        }
+      },
+      'code': 'banned'
+    }
+  });
+
+  visit('/forgotten-password');
+  fillIn('.forgotten-password-page form input', 'valid@mail.com');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    var errorMessage = find('.lead p').text();
+    assert.equal(errorMessage, 'You are banned for trolling.');
+
+    var expirationMessage = find('.error-message>p').text();
+    assert.equal(expirationMessage, 'This ban is permanent.');
+
+    done();
+  });
+});
+
+test('request password change link', function(assert) {
+  var done = assert.async();
+  Ember.$.mockjax({
+    url: "/api/change-password/send-link/",
+    status: 200,
+    responseText: {
+      'username': 'BobBoberson',
+      'email': 'valid@mail.com'
+    }
+  });
+
+  visit('/forgotten-password');
+  fillIn('.forgotten-password-page form input', 'valid@mail.com');
+  click('.forgotten-password-page form .btn-primary');
+
+  andThen(function() {
+    var pageHeader = Ember.$.trim(find('.page-header h1').text());
+    assert.equal(pageHeader, 'Change password form link sent');
+
+    done();
+  });
+});

+ 27 - 1
misago/emberapp/tests/acceptance/login-test.js

@@ -6,10 +6,15 @@ var application;
 
 module('Acceptance: Login', {
   beforeEach: function() {
+    Ember.$('#hidden-login-form').on('submit.stopInTest', function(event) {
+      event.stopPropagation();
+      return false;
+    });
     application = startApp();
   },
 
   afterEach: function() {
+    Ember.$('#hidden-login-form').off('submit.stopInTest');
     Ember.$('#loginModal').off();
     Ember.$('body').removeClass('modal-open');
     Ember.run(application, 'destroy');
@@ -102,7 +107,6 @@ test('login to admin-activated account', function(assert) {
 
 test('login to banned account', function(assert) {
   var done = assert.async();
-
   Ember.$.mockjax({
     url: "/api/auth/login/",
     status: 400,
@@ -135,3 +139,25 @@ test('login to banned account', function(assert) {
     done();
   });
 });
+
+test('login successfully', function(assert) {
+  Ember.$.mockjax({
+    url: "/api/auth/login/",
+    status: 200,
+    responseText: {
+      'username': 'SomeFake'
+    }
+  });
+
+  visit('/');
+
+  click('.guest-nav .btn-login');
+  fillIn('#loginModal .form-group:first-child input', 'SomeFake');
+  fillIn('#loginModal .form-group:last-child input', 'pass1234');
+  click('#loginModal .btn-primary');
+
+  andThen(function() {
+    assert.equal(Ember.$('#hidden-login-form input[name="username"]').val(), 'SomeFake');
+    assert.equal(Ember.$('#hidden-login-form input[name="password"]').val(), 'pass1234');
+  });
+});

+ 14 - 0
misago/emberapp/tests/index.html

@@ -21,6 +21,20 @@
 
     {{content-for 'body'}}
     {{content-for 'test-body'}}
+
+    <form id="hidden-login-form" method="post" action="/login/" style="display: none;">
+      <input name="csrfmiddlewaretoken" type="hidden" value="">
+      <input name="redirect_to" type="text">
+      <input name="username" type="text">
+      <input name="password" type="password">
+      <input type="submit" id="signin-button" value="Sign in">
+    </form>
+
+    <form id="hidden-logout-form" method="post" action="/logout/" style="display: none;">
+      <input name="csrfmiddlewaretoken" type="hidden" value="">
+      <input type="submit" id="signin-button" value="Log out">
+    </form>
+
     <script src="misago/js/vendor.js"></script>
     <script src="assets/test-support.js"></script>
     <script src="misago/js/misago.js"></script>

+ 2 - 2
misago/templates/misago/auth.html

@@ -1,6 +1,6 @@
 {% load i18n %}
 {% if user.is_anonymous %}
-<form id='hidden-login-form' method="post" action="{% url LOGIN_URL %}" style="display: none;">
+<form id="hidden-login-form" method="post" action="{% url LOGIN_URL %}" style="display: none;">
   {% csrf_token %}
   <input name="redirect_to" type="text">
   <input name="username" type="text">
@@ -8,7 +8,7 @@
   <input type="submit" id="signin-button" value="{% trans "Sign in" %}">
 </form>
 {% else %}
-<form id='hidden-logout-form' method="post" action="{% url LOGOUT_URL %}" style="display: none;">
+<form id="hidden-logout-form" method="post" action="{% url LOGOUT_URL %}" style="display: none;">
   {% csrf_token %}
   <input type="submit" id="signin-button" value="{% trans "Log out" %}">
 </form>