Browse Source

refactored login modal controller

Rafał Pitoń 10 years ago
parent
commit
4cf762de99

+ 39 - 33
misago/emberapp/app/controllers/login-modal.js

@@ -41,7 +41,7 @@ export default Ember.Controller.extend({
       Ember.$('#loginModal').modal('show');
       Ember.$('#loginModal').modal('show');
     },
     },
 
 
-    signIn: function() {
+    submit: function() {
       if (this.get('isLoading')) {
       if (this.get('isLoading')) {
         return;
         return;
       }
       }
@@ -59,44 +59,50 @@ export default Ember.Controller.extend({
       var self = this;
       var self = this;
       rpc(this.get('settings.authApiUrl'), credentials
       rpc(this.get('settings.authApiUrl'), credentials
       ).then(function() {
       ).then(function() {
-        var $form = Ember.$('#hidden-login-form');
-
-        // we need to refresh CSRF token because previous api call changed it
-        $form.find('input[name=csrfmiddlewaretoken]').val(getCsrfToken());
-
-        // fill out form with user credentials and submit it, this will tell
-        // misago to redirect user back to right page, and will trigger browser's
-        // key ring feature
-        $form.find('input[name=redirect_to]').val(window.location.href);
-        $form.find('input[name=username]').val(credentials.username);
-        $form.find('input[name=password]').val(credentials.password);
-        $form.submit();
-
+        self.send('success', credentials);
       }, function(jqXHR) {
       }, function(jqXHR) {
-        var rejection = jqXHR.responseJSON;
-        if (typeof rejection.code === "undefined") {
-          self.send("error", rejection);
-        } else {
-          if (rejection.code === 'inactive_admin') {
-            self.get('toast').info(rejection.detail);
-          } else if (rejection.code === 'inactive_user') {
-            self.get('toast').info(rejection.detail);
-            self.set('showActivation', true);
-          } else if (rejection.code === 'banned') {
-            self.send('showBan', rejection.detail);
-            Ember.run(function() {
-              Ember.$('#loginModal').modal('hide');
-            });
-          } else {
-            self.get('toast').error(rejection.detail);
-          }
-        }
-
+        self.send('error', jqXHR);
       }).finally(function() {
       }).finally(function() {
         self.set('isLoading', false);
         self.set('isLoading', false);
       });
       });
     },
     },
 
 
+    success: function(credentials) {
+      var $form = Ember.$('#hidden-login-form');
+
+      // we need to refresh CSRF token because previous api call changed it
+      $form.find('input[name=csrfmiddlewaretoken]').val(getCsrfToken());
+
+      // fill out form with user credentials and submit it, this will tell
+      // misago to redirect user back to right page, and will trigger browser's
+      // key ring feature
+      $form.find('input[name=redirect_to]').val(window.location.href);
+      $form.find('input[name=username]').val(credentials.username);
+      $form.find('input[name=password]').val(credentials.password);
+      $form.submit();
+    },
+
+    error: function(jqXHR) {
+      var rejection = jqXHR.responseJSON;
+      if (jqXHR.status !== 400) {
+        this.send('toastError', jqXHR);
+      } else if (rejection.code === 'inactive_admin') {
+        this.get('toast').info(rejection.detail);
+      } else if (rejection.code === 'inactive_user') {
+        this.get('toast').info(rejection.detail);
+        this.set('showActivation', true);
+      } else if (rejection.code === 'banned') {
+        this.send('showBan', rejection.detail);
+        Ember.run(function() {
+          Ember.$('#loginModal').modal('hide');
+        });
+      } else {
+        this.get('toast').error(rejection.detail);
+      }
+    },
+
+    // handle go-to links
+
     forgotPassword: function() {
     forgotPassword: function() {
       this.transitionToRoute('forgotten-password');
       this.transitionToRoute('forgotten-password');
       Ember.$('#loginModal').modal('hide');
       Ember.$('#loginModal').modal('hide');

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

@@ -6,7 +6,7 @@
         <h4 class="modal-title" id="loginModalLabel">{{gettext "Sign in"}}</h4>
         <h4 class="modal-title" id="loginModalLabel">{{gettext "Sign in"}}</h4>
       </div>
       </div>
 
 
-      <form {{action "signIn" on="submit"}}>
+      <form {{action "submit" on="submit"}}>
         <div class="modal-body">
         <div class="modal-body">
 
 
           <div class="form-group">
           <div class="form-group">

+ 34 - 3
misago/emberapp/tests/acceptance/login-test.js

@@ -24,6 +24,8 @@ module('Acceptance: Login', {
 });
 });
 
 
 test('login with empty credentials', function(assert) {
 test('login with empty credentials', function(assert) {
+  assert.expect(1);
+
   visit('/');
   visit('/');
   click('.guest-nav button.btn-login');
   click('.guest-nav button.btn-login');
   click('#loginModal .btn-primary');
   click('#loginModal .btn-primary');
@@ -33,7 +35,29 @@ test('login with empty credentials', function(assert) {
   });
   });
 });
 });
 
 
+test('backend errored', function(assert) {
+  assert.expect(1);
+
+  Ember.$.mockjax({
+    url: "/api/auth/login/",
+    status: 500
+  });
+
+  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(getToastMessage(), 'Unknown error has occured.');
+  });
+});
+
 test('login with invalid credentials', function(assert) {
 test('login with invalid credentials', function(assert) {
+  assert.expect(1);
+
   var message = 'Login or password is incorrect.';
   var message = 'Login or password is incorrect.';
   Ember.$.mockjax({
   Ember.$.mockjax({
     url: "/api/auth/login/",
     url: "/api/auth/login/",
@@ -57,6 +81,8 @@ test('login with invalid credentials', function(assert) {
 });
 });
 
 
 test('login to user-activated account', function(assert) {
 test('login to user-activated account', function(assert) {
+  assert.expect(1);
+
   var message = 'You have to activate your account before you will be able to sign in.';
   var message = 'You have to activate your account before you will be able to sign in.';
   Ember.$.mockjax({
   Ember.$.mockjax({
     url: "/api/auth/login/",
     url: "/api/auth/login/",
@@ -80,6 +106,8 @@ test('login to user-activated account', function(assert) {
 });
 });
 
 
 test('login to admin-activated account', function(assert) {
 test('login to admin-activated account', function(assert) {
+  assert.expect(1);
+
   var message = 'Your account has to be activated by Administrator before you will be able to sign in.';
   var message = 'Your account has to be activated by Administrator before you will be able to sign in.';
   Ember.$.mockjax({
   Ember.$.mockjax({
     url: "/api/auth/login/",
     url: "/api/auth/login/",
@@ -103,7 +131,8 @@ test('login to admin-activated account', function(assert) {
 });
 });
 
 
 test('login to banned account', function(assert) {
 test('login to banned account', function(assert) {
-  var done = assert.async();
+  assert.expect(3);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
     url: "/api/auth/login/",
     url: "/api/auth/login/",
     status: 400,
     status: 400,
@@ -127,17 +156,19 @@ test('login to banned account', function(assert) {
   click('#loginModal .btn-primary');
   click('#loginModal .btn-primary');
 
 
   andThen(function() {
   andThen(function() {
+    assert.equal(currentPath(), 'error-banned');
+
     var banMessage = find('.lead p').text();
     var banMessage = find('.lead p').text();
     assert.equal(banMessage, 'You are banned for trolling.');
     assert.equal(banMessage, 'You are banned for trolling.');
 
 
     var expirationMessage = find('.error-message>p').text();
     var expirationMessage = find('.error-message>p').text();
     assert.equal(expirationMessage, 'This ban is permanent.');
     assert.equal(expirationMessage, 'This ban is permanent.');
-
-    done();
   });
   });
 });
 });
 
 
 test('login successfully', function(assert) {
 test('login successfully', function(assert) {
+  assert.expect(2);
+
   Ember.$.mockjax({
   Ember.$.mockjax({
     url: "/api/auth/login/",
     url: "/api/auth/login/",
     status: 200,
     status: 200,