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
This commit is contained in:
Dan Siwiec 2015-07-10 13:58:54 -07:00
parent 4f39da5b02
commit 8686148d44
2 changed files with 71 additions and 28 deletions

View File

@ -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);
}
};
});
}
]);
})();

View File

@ -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();
}));
});
});
})();