From 8686148d44b29e2ba93ecd2edc56e1d92a162172 Mon Sep 17 00:00:00 2001 From: Dan Siwiec Date: Fri, 10 Jul 2015 13:58:54 -0700 Subject: [PATCH] Fix eslint error on framework.module. Using $window instead of window for testability. Fixing angular/ng_di for the framework module. Adding unit test covering 401 redirects. Change-Id: I1f3d359be2093766d995897c4809c816845f11a7 Partially-Implements: blueprint jscs-cleanup --- horizon/static/framework/framework.module.js | 63 ++++++++++--------- .../static/framework/framework.module.spec.js | 36 +++++++++++ 2 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 horizon/static/framework/framework.module.spec.js diff --git a/horizon/static/framework/framework.module.js b/horizon/static/framework/framework.module.js index 2e8068dceb..58f60d3be8 100644 --- a/horizon/static/framework/framework.module.js +++ b/horizon/static/framework/framework.module.js @@ -8,34 +8,41 @@ 'horizon.framework.widgets' ]) .constant('horizon.framework.basePath', '/static/framework/') - .config([ - '$interpolateProvider', - '$httpProvider', - function ($interpolateProvider, $httpProvider) { - // Replacing the default angular symbol - // allow us to mix angular with django templates - $interpolateProvider.startSymbol('{$'); - $interpolateProvider.endSymbol('$}'); + .config(frameworkConfiguration); - // Http global settings for ease of use - $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; - $httpProvider.defaults.xsrfCookieName = 'csrftoken'; - $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; - $httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8'; + frameworkConfiguration.$inject = [ + '$interpolateProvider', + '$httpProvider', + '$windowProvider' + ]; + + function frameworkConfiguration($interpolateProvider, $httpProvider, $windowProvider) { + // Replacing the default angular symbol + // allow us to mix angular with django templates + $interpolateProvider.startSymbol('{$'); + $interpolateProvider.endSymbol('$}'); + + // Http global settings for ease of use + $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; + $httpProvider.defaults.xsrfCookieName = 'csrftoken'; + $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; + $httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8'; + + // Global http error handler + // if user is not authorized, log user out + // this can happen when session expires + $httpProvider.interceptors.push(redirect); + + function redirect($q) { + return { + responseError: function (error) { + if (error.status === 401) { + $windowProvider.$get().location.replace('/auth/logout'); + } + return $q.reject(error); + } + }; + } + } - // Global http error handler - // if user is not authorized, log user out - // this can happen when session expires - $httpProvider.interceptors.push(function ($q) { - return { - responseError: function (error) { - if (error.status === 401) { - window.location.replace('/auth/logout'); - } - return $q.reject(error); - } - }; - }); - } - ]); })(); diff --git a/horizon/static/framework/framework.module.spec.js b/horizon/static/framework/framework.module.spec.js new file mode 100644 index 0000000000..a7a3c1d210 --- /dev/null +++ b/horizon/static/framework/framework.module.spec.js @@ -0,0 +1,36 @@ +/** + * Copyright 2015 ThoughtWorks Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + 'use strict'; + + describe('horizon.framework', function() { + beforeEach(module('horizon.framework')); + + describe('when unauthorized', function() { + it('should redirect to /auth/logout', inject(function($http, $httpBackend, $window) { + + spyOn($window.location, 'replace'); + $httpBackend.when('GET', '/api').respond(401, ''); + + $http.get('/api').error(function() { + expect($window.location.replace).toHaveBeenCalledWith('/auth/logout'); + }); + $httpBackend.flush(); + })); + }); + }); +})();