Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Tests #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"main": "./src/http-auth-interceptor.js",
"dependencies": {
"angular": "^1.2"
"angular": "^1.2",
"angular-mocks": "^1.2.16"
}
}
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
73 changes: 73 additions & 0 deletions spec/http-auth-interceptor.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
});

});

});
29 changes: 29 additions & 0 deletions spec/karma.conf.js
Original file line number Diff line number Diff line change
@@ -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'
}

})
}