diff --git a/app/app/pods/auth/sso/route.js b/app/app/pods/auth/sso/route.js index d6347f42..bc4c426e 100644 --- a/app/app/pods/auth/sso/route.js +++ b/app/app/pods/auth/sso/route.js @@ -7,10 +7,9 @@ export default Ember.Route.extend({ this.get("session").authenticate('authenticator:documize', token) .then(() => { this.transitionTo('folders.folder'); - }) - .catch(() => { + }, () => { this.transitionTo('auth.login'); console.log(">>>>> Documize SSO failure"); }); - } + }, }); diff --git a/app/app/pods/folders/route.js b/app/app/pods/folders/route.js index 8ef4415b..03dbfb60 100644 --- a/app/app/pods/folders/route.js +++ b/app/app/pods/folders/route.js @@ -2,62 +2,67 @@ import Ember from 'ember'; import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin'; export default Ember.Route.extend(AuthenticatedRouteMixin, { - folderService: Ember.inject.service('folder'), - localStorage: Ember.inject.service(), - folder: {}, + folderService: Ember.inject.service('folder'), + localStorage: Ember.inject.service(), + folder: {}, - model: function() { - return this.get('folderService').getAll(); - }, + model: function () { + return this.get('folderService').getAll(); + }, - afterModel: function(model) { - let self = this; + afterModel: function (model) { + // TODO: replace with ES6 features (remove self this) + // TODO: replace is.* with Ember utilities + // TODO: flatten if/else + // TODO: make sure chain is maintained by returning promies - if (is.empty(this.paramsFor('folders.folder'))) { - var lastFolder = this.get('localStorage').getSessionItem("folder"); + if (is.empty(this.paramsFor('folders.folder'))) { + let lastFolder = this.get('localStorage').getSessionItem("folder"); - if (is.not.undefined(lastFolder)) { - this.get('folderService').getFolder(lastFolder).then(function(folder) { - if (is.undefined(folder) || is.null(folder)) { - self.transitionTo('auth.login'); - } - self.folder = folder; - self.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); - }, function() { - if (model.length > 0) { - var folder = model[0]; - self.folder = folder; - self.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); - } else { - self.transitionTo('auth.login'); - } - }); - } else { - if (model.length > 0) { - var folder = model[0]; - self.folder = folder; - self.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); - } else - { - // has no folders, create default folder - this.get('folderService').add({ name: "My Space" }).then(function(folder) { - self.folder = folder; - self.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); - }); - } - } - } else { - var folderId = this.paramsFor('folders.folder').folder_id; - this.get('folderService').getFolder(folderId).then(function(folder) { - self.folder = folder; - }); - } + //If folder lastFolder is defined + if (Ember.isPresent(lastFolder)) { + return this.get('folderService').getFolder(lastFolder).then((folder) => { + //if Response is null or undefined redirect to login else transitionTo dashboard + if (Ember.isNone(folder)) { + this.transitionTo('auth.login'); + } + this.folder = folder; + this.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); + }).catch(() => { + //if there was an error redirect to login + this.transitionTo('auth.login'); + }); + } - this.browser.setMetaDescription(); - }, + // If model has any folders redirect to dashboard + if (model.length > 0) { + let folder = model[0]; + this.folder = folder; + this.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); + } - setupController(controller, model) { - controller.set('model', model); - controller.set('folder', this.folder); - } + // has no folders, create default folder + return this.get('folderService').add({ name: "My Space" }).then((folder) => { + this.folder = folder; + this.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); + }); + } + + //If folder route has params + if (Ember.isPresent(this.paramsFor('folders.folder'))) { + + let folderId = this.paramsFor('folders.folder').folder_id; + + return this.get('folderService').getFolder(folderId).then((folder) => { + this.folder = folder; + }); + } + + this.browser.setMetaDescription(); + }, + + setupController(controller, model) { + controller.set('model', model); + controller.set('folder', this.folder); + } }); diff --git a/app/mirage/config.js b/app/mirage/config.js index 3b9ae31d..8dd23999 100644 --- a/app/mirage/config.js +++ b/app/mirage/config.js @@ -7,6 +7,8 @@ export default function () { this.namespace = 'api'; // make this `api`, for example, if your API is namespaced // this.timing = 400; // delay for each request, automatically set to 0 during testing + this.logging = true; + this.get('/public/meta', function (schema) { return schema.db.meta[0]; }); @@ -128,37 +130,7 @@ export default function () { let expectedAuthorization = "Basic OmJyaXpkaWdpdGFsQGdtYWlsLmNvbTp6aW55YW5kbzEyMw=="; if (expectedAuthorization == authorization) { - return { - "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiIiLCJleHAiOjE0NjQwMjM2NjcsImlzcyI6IkRvY3VtaXplIiwib3JnIjoiVnpNdXlFd18zV3FpYWZjRCIsInN1YiI6IndlYmFwcCIsInVzZXIiOiJWek11eUV3XzNXcWlhZmNFIn0.NXZ6bo8mtvdZF_b9HavbidVUJqhmBA1zr0fSAPvbah0", - "user": { - "id": "VzMuyEw_3WqiafcE", - "created": "2016-05-11T15:08:24Z", - "revised": "2016-05-11T15:08:24Z", - "firstname": "Lennex", - "lastname": "Zinyando", - "email": "brizdigital@gmail.com", - "initials": "LZ", - "active": true, - "editor": true, - "admin": true, - "accounts": [{ - "id": "VzMuyEw_3WqiafcF", - "created": "2016-05-11T15:08:24Z", - "revised": "2016-05-11T15:08:24Z", - "admin": true, - "editor": true, - "userId": "VzMuyEw_3WqiafcE", - "orgId": "VzMuyEw_3WqiafcD", - "company": "EmberSherpa", - "title": "EmberSherpa", - "message": "This Documize instance contains all our team documentation", - "domain": "" - }] - } - }; - } else if (expectedAuthorization != authorization) { - return new Mirage.Response(400); - } else { + console.log("SSO login success"); return { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiIiLCJleHAiOjE0NjQwMjM2NjcsImlzcyI6IkRvY3VtaXplIiwib3JnIjoiVnpNdXlFd18zV3FpYWZjRCIsInN1YiI6IndlYmFwcCIsInVzZXIiOiJWek11eUV3XzNXcWlhZmNFIn0.NXZ6bo8mtvdZF_b9HavbidVUJqhmBA1zr0fSAPvbah0", "user": { @@ -189,6 +161,39 @@ export default function () { }; } + if (expectedAuthorization != authorization) { + return new Mirage.Response(401, { 'Content-Type': 'application/json' }, { message: 'Bad Request' }); + } + + return { + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiIiLCJleHAiOjE0NjQwMjM2NjcsImlzcyI6IkRvY3VtaXplIiwib3JnIjoiVnpNdXlFd18zV3FpYWZjRCIsInN1YiI6IndlYmFwcCIsInVzZXIiOiJWek11eUV3XzNXcWlhZmNFIn0.NXZ6bo8mtvdZF_b9HavbidVUJqhmBA1zr0fSAPvbah0", + "user": { + "id": "VzMuyEw_3WqiafcE", + "created": "2016-05-11T15:08:24Z", + "revised": "2016-05-11T15:08:24Z", + "firstname": "Lennex", + "lastname": "Zinyando", + "email": "brizdigital@gmail.com", + "initials": "LZ", + "active": true, + "editor": true, + "admin": true, + "accounts": [{ + "id": "VzMuyEw_3WqiafcF", + "created": "2016-05-11T15:08:24Z", + "revised": "2016-05-11T15:08:24Z", + "admin": true, + "editor": true, + "userId": "VzMuyEw_3WqiafcE", + "orgId": "VzMuyEw_3WqiafcD", + "company": "EmberSherpa", + "title": "EmberSherpa", + "message": "This Documize instance contains all our team documentation", + "domain": "" + }] + } + }; + }); this.get('/users/VzMuyEw_3WqiafcE/permissions', (schema) => { @@ -440,15 +445,4 @@ export default function () { return {}; }); - /** - very helpful for debugging - */ - this.handledRequest = function (verb, path) { - console.log(`👊${verb} ${path}`); - }; - - this.unhandledRequest = function (verb, path) { - console.log(`🔥${verb} ${path}`); - }; - } diff --git a/app/tests/acceptance/authentication-test.js b/app/tests/acceptance/authentication-test.js index 3386a36c..aeb25ba0 100644 --- a/app/tests/acceptance/authentication-test.js +++ b/app/tests/acceptance/authentication-test.js @@ -34,7 +34,6 @@ test('successful sso login authenticates redirects to dashboard', function (asse server.createList('folder', 2); visit('/auth/sso/OmJyaXpkaWdpdGFsQGdtYWlsLmNvbTp6aW55YW5kbzEyMw=='); - // return pauseTest(); andThen(function () { assert.equal(currentURL(), '/s/VzMuyEw_3WqiafcG/my-project', 'SSO login successful');