Browse Source

increased ember app tests coverage

Rafał Pitoń 10 years ago
parent
commit
4ffd338af4

+ 5 - 6
misago/emberapp/app/controllers/login-modal.js

@@ -74,20 +74,19 @@ export default Ember.Controller.extend({
 
       }, function(jqXHR) {
         var rejection = jqXHR.responseJSON;
-
         if (typeof rejection.code === "undefined") {
           self.send("error", rejection);
         } else {
           if (rejection.code === 'inactive_admin') {
-            this.send('flashInfo', rejection.detail);
+            self.send('flashInfo', rejection.detail);
           } else if (rejection.code === 'inactive_user') {
-            this.send('flashInfo', rejection.detail);
-            this.set('showActivation', true);
+            self.send('flashInfo', rejection.detail);
+            self.set('showActivation', true);
           } else if (rejection.code === 'banned') {
+            self.send('showBan', rejection.detail);
             Ember.$('#loginModal').modal('hide');
-            this.send('showBan', rejection.detail);
           } else {
-            this.send('flashError', rejection.detail);
+            self.send('flashError', rejection.detail);
           }
         }
 

+ 2 - 2
misago/emberapp/app/templates/guest-nav.hbs

@@ -1,9 +1,9 @@
 <div class="guest-nav navbar-right">
-  <button type="button" class="btn btn-default navbar-btn btn-sm" {{action "openLoginModal"}}>
+  <button type="button" class="btn btn-default btn-login navbar-btn btn-sm" {{action "openLoginModal"}}>
     {{gettext "Sign in"}}
   </button>
 
-  <button type="button" class="btn btn-info navbar-btn btn-sm">
+  <button type="button" class="btn btn-info btn-logout navbar-btn btn-sm">
     {{gettext "Join now"}}
   </button>
 </div>

+ 1 - 1
misago/emberapp/app/templates/index.hbs

@@ -10,7 +10,7 @@
   <h2>{{gettext "Test %(msg)s Message" msg=(gettext "Flash")}}</h2>
 
   <p>
-    {{input type="text" value=newFlash}}
+    {{input class="testinput" type="text" value=newFlash}}
     <button class="btn btn-primary" {{action "testInfo"}}>Info</button>
     <button class="btn btn-success" {{action "testSuccess"}}>Success</button>
     <button class="btn btn-warning" {{action "testWarning"}}>Warning</button>

+ 1 - 1
misago/emberapp/app/templates/login-modal.hbs

@@ -24,7 +24,7 @@
         </div>
         <div class="modal-footer">
           {{#if showActivation}}
-          <button type="button" class="btn btn-info btn-success" {{action "activateAccount"}}>{{gettext "Activate account"}}</button>
+          <button type="button" class="btn btn-block btn-success" {{action "activateAccount"}}>{{gettext "Activate account"}}</button>
           {{else}}
             {{#if isLoading}}
             <button type="button" class="btn btn-block btn-primary" disabled="disabled">

+ 28 - 0
misago/emberapp/tests/acceptance/error-handling-test.js

@@ -96,6 +96,34 @@ test('permission denied with reason', function(assert) {
   });
 });
 
+test('banned', function(assert) {
+  Ember.$.mockjax({
+    url: "/api/legal-pages/privacy-policy/",
+    status: 403,
+    responseText: {
+      'ban': {
+        'expires_on': null,
+        'message': {
+          'plain': 'You are banned.',
+          'html': '<p>You are banned.</p>'
+        }
+      }
+    }
+  });
+
+  visit('/privacy-policy');
+
+  andThen(function() {
+    assert.equal(currentPath(), 'error-banned');
+
+    var errorMessage = find('.lead p').text();
+    assert.equal(errorMessage, 'You are banned.');
+
+    var expirationMessage = find('.error-message>p').text();
+    assert.equal(expirationMessage, 'This ban is permanent.');
+  });
+});
+
 test('not found route', function(assert) {
   visit('/this-url-really-doesnt-exist');
 

+ 137 - 0
misago/emberapp/tests/acceptance/login-test.js

@@ -0,0 +1,137 @@
+import Ember from 'ember';
+import { module, test } from 'qunit';
+import startApp from '../helpers/start-app';
+
+var application;
+
+module('Acceptance: Login', {
+  beforeEach: function() {
+    application = startApp();
+  },
+
+  afterEach: function() {
+    Ember.$('#loginModal').off();
+    Ember.$('body').removeClass('modal-open');
+    Ember.run(application, 'destroy');
+    Ember.$.mockjax.clear();
+  }
+});
+
+test('login with empty credentials', function(assert) {
+  visit('/');
+  click('.guest-nav button.btn-login');
+  click('#loginModal .btn-primary');
+
+  andThen(function() {
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, 'Fill out both fields.');
+  });
+});
+
+test('login with invalid credentials', function(assert) {
+  var message = 'Login or password is incorrect.';
+  Ember.$.mockjax({
+    url: "/api/auth/login/",
+    status: 400,
+    responseText: {
+      'detail': message,
+      'code': 'invalid_login'
+    }
+  });
+
+  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() {
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('login to 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/auth/login/",
+    status: 400,
+    responseText: {
+      'detail': message,
+      'code': 'inactive_user'
+    }
+  });
+
+  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() {
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('login to 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/auth/login/",
+    status: 400,
+    responseText: {
+      'detail': message,
+      'code': 'inactive_admin'
+    }
+  });
+
+  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() {
+    var error = Ember.$.trim(find('.flash-message p').text());
+    assert.equal(error, message);
+  });
+});
+
+test('login to banned account', function(assert) {
+  var done = assert.async();
+
+  Ember.$.mockjax({
+    url: "/api/auth/login/",
+    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('/');
+
+  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() {
+    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();
+  });
+});

+ 2 - 2
misago/emberapp/tests/acceptance/privacy-policy-test.js

@@ -36,7 +36,7 @@ test('visiting set /privacy-policy', function(assert) {
       'id': 'privacy-policy',
       'title': 'Privacy policy',
       'link': '',
-      'body': '<p>Top kek</p>'
+      'body': '<p>Privacy policy is working!</p>'
     }
   });
 
@@ -45,6 +45,6 @@ test('visiting set /privacy-policy', function(assert) {
   andThen(function() {
     assert.equal(currentPath(), 'privacy-policy');
     var $e = find('article');
-    assert.equal(Ember.$.trim($e.html()), '<p>Top kek</p>');
+    assert.equal(Ember.$.trim($e.html()), '<p>Privacy policy is working!</p>');
   });
 });

+ 2 - 2
misago/emberapp/tests/acceptance/terms-of-service-test.js

@@ -36,7 +36,7 @@ test('visiting set /terms-of-service', function(assert) {
       'id': 'terms-of-service',
       'title': 'Terms of service',
       'link': '',
-      'body': '<p>Top kek</p>'
+      'body': '<p>Terms of service are working!</p>'
     }
   });
 
@@ -45,6 +45,6 @@ test('visiting set /terms-of-service', function(assert) {
   andThen(function() {
     assert.equal(currentPath(), 'terms-of-service');
     var $e = find('article');
-    assert.equal(Ember.$.trim($e.html()), '<p>Top kek</p>');
+    assert.equal(Ember.$.trim($e.html()), '<p>Terms of service are working!</p>');
   });
 });

+ 22 - 0
misago/emberapp/tests/unit/initializers/auth-service-test.js

@@ -0,0 +1,22 @@
+import Ember from 'ember';
+import { initialize } from '../../../initializers/auth-service';
+import { module, test } from 'qunit';
+
+var container, application;
+
+module('AuthServiceInitializer', {
+  beforeEach: function() {
+    Ember.run(function() {
+      application = Ember.Application.create();
+      container = application.__container__;
+      application.deferReadiness();
+    });
+  }
+});
+
+test('initializer registered auth and user', function(assert) {
+  initialize(container, application);
+
+  assert.ok(container.has('misago:auth'));
+  assert.ok(container.has('misago:user'));
+});

+ 1 - 0
misago/emberapp/tests/unit/initializers/dev-csrf-tokens-test.js

@@ -29,6 +29,7 @@ module('devCsrfTokensInitializer', {
       application.deferReadiness();
     });
   },
+
   afterEach: function() {
     MisagoPreloadStore.set('csrfCookieName', cookieName);
     $element.remove();

+ 1 - 1
misago/emberapp/tests/unit/initializers/django-location-test.js

@@ -14,7 +14,7 @@ module('DjangoLocationInitializer', {
   }
 });
 
-test('it exists', function(assert) {
+test('initializer registers location api', function(assert) {
   initialize(container, application);
 
   assert.ok(container.has('location:django-location'));

+ 30 - 0
misago/emberapp/tests/unit/initializers/moment-locale-test.js

@@ -0,0 +1,30 @@
+import Ember from 'ember';
+import { initialize } from '../../../initializers/moment-locale';
+import { module, test } from 'qunit';
+
+var container, application;
+var documentLang = null;
+
+module('MomentLocaleInitializer', {
+  beforeEach: function() {
+    documentLang = Ember.$('html').attr('lang');
+
+    Ember.run(function() {
+      application = Ember.Application.create();
+      container = application.__container__;
+      application.deferReadiness();
+    });
+  },
+
+  afterEach: function() {
+    Ember.$('html').attr('lang', documentLang);
+    moment.locale(documentLang);
+  }
+});
+
+test('initializer changes moment.js locale', function(assert) {
+  Ember.$('html').attr('lang', 'pl');
+  initialize(container, application);
+
+  assert.equal(moment.locale(), 'pl');
+});

+ 1 - 1
misago/emberapp/vendor/testutils/misago-preload-data.js

@@ -17,7 +17,7 @@ window.MisagoData = {
     "privacy_policy_title": "Polityka prywatno\u015bci",
     "privacy_policy": true,
 
-    "authApiUrl": "/api/auth/",
+    "authApiUrl": "/api/auth/login/",
 
     "loginUrl": "/login/",
     "loginRedirectUrl": "/",