diff --git a/bower.json b/bower.json index 8b02643..b01f91e 100644 --- a/bower.json +++ b/bower.json @@ -9,6 +9,7 @@ }, "main": "./src/http-auth-interceptor.js", "dependencies": { - "angular": "^1.2" + "angular": "^1.2", + "angular-mocks": "^1.2.16" } } diff --git a/package.json b/package.json new file mode 100644 index 0000000..a79682b --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "http-auth-interceptor", + "description": "HTTP Authentication helper for angularjs", + "repository": "https://github.com/witoldsz/angular-http-auth", + "license": "MIT", + "devDependencies": { + "karma": "~0.10", + "bower": "^1.3.1", + "karma-junit-reporter": "^0.2.2" + }, + "scripts": { + "postinstall": "bower install", + + "pretest": "npm install", + "test": "karma start spec/karma.conf.js", + "test-single-run": "karma start spec/karma.conf.js --single-run" + } +} diff --git a/spec/http-auth-interceptor.js b/spec/http-auth-interceptor.js new file mode 100644 index 0000000..5cd8397 --- /dev/null +++ b/spec/http-auth-interceptor.js @@ -0,0 +1,73 @@ +'use strict'; + +describe('http auth interceptor', function() { + + var authService, $httpBackend, $http, $scope; + var methods = ['GET', 'POST', 'UPDATE', 'DELETE']; + + beforeEach(function() { + // Load http-auth-interceptor module + module('http-auth-interceptor'); + }); + + beforeEach(function() { + // Get services and make them available + inject(function(_authService_, _$httpBackend_, _$http_, _$rootScope_) { + authService = _authService_; + $httpBackend = _$httpBackend_; + $http = _$http_; + $scope = _$rootScope_; + }); + + // Spy on $emit to detect events + spyOn($scope, '$broadcast'); + }); + + describe('events', function() { + + it('should broadcast "event:auth-loginRequired" on http 401 respones and "event:auth-loginConfirmed" after calling loginConfirmed', function() { + angular.forEach(methods, function(method) { + // require authentication (http 401) + $httpBackend.expect(method, '/myresource').respond(401); + $http({method: method, url: '/myresource'}); + $httpBackend.flush(); + expect($scope.$broadcast).toHaveBeenCalledWith('event:auth-loginRequired', jasmine.any(Object)); + $scope.$broadcast.reset(); + + // confirm auth + $httpBackend.expect(method, '/myresource').respond(200); + authService.loginConfirmed(); + expect($scope.$broadcast).toHaveBeenCalledWith('event:auth-loginConfirmed', undefined); + }); + }); + + it('should broadcast "event:auth-loginRequired" on http 401 respones and "event:auth-loginCancelled" after calling loginConfirmed', function() { + angular.forEach(methods, function(method) { + // require authentication (http 401) + $httpBackend.expect(method, '/myresource').respond(401); + $http({method: method, url: '/myresource'}); + $httpBackend.flush(); + expect($scope.$broadcast).toHaveBeenCalledWith('event:auth-loginRequired', jasmine.any(Object)); + $scope.$broadcast.reset(); + + // confirm auth + authService.loginCancelled(); + expect($scope.$broadcast).toHaveBeenCalledWith('event:auth-loginCancelled', undefined); + }); + }); + + it('should not broadcast any event on responses other than 401', function() { + // most of the following tests don't make sense, but they also don't hurt + for(status = 100; status <= 599; status++) { + angular.forEach(methods, function(method) { + $httpBackend.expect(method, '/myresource').respond(status); + $http({method: method, url: '/myresource'}); + $httpBackend.flush(); + expect($scope.$broadcast).not.toHaveBeenCalled(); + }); + } + }); + + }); + +}); diff --git a/spec/karma.conf.js b/spec/karma.conf.js new file mode 100644 index 0000000..fd946c4 --- /dev/null +++ b/spec/karma.conf.js @@ -0,0 +1,29 @@ +module.exports = function(config) { + config.set({ + basePath: '../', + + files: [ + 'bower_components/angular/angular.js', + 'bower_components/angular-mocks/angular-mocks.js', + 'src/http-auth-interceptor.js', + 'spec/*.js' + ], + + autoWatch: true, + + frameworks: ['jasmine'], + + browsers: ['PhantomJS'], + + plugins: [ + 'karma-jasmine', + 'karma-phantomjs-launcher' + ], + + junitReporter: { + outputFile: 'test_out/unit.xml', + suite: 'unit' + } + + }) +}