2014-05-19 15:39:58 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
(function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Authentication controller Spec
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('AuthenticationController', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Initialize global variables
|
|
|
|
|
var AuthenticationController,
|
|
|
|
|
scope,
|
|
|
|
|
$httpBackend,
|
|
|
|
|
$stateParams,
|
2015-09-09 21:43:55 -03:00
|
|
|
$state,
|
2015-07-28 17:52:03 -06:00
|
|
|
$location;
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
beforeEach(function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
jasmine.addMatchers({
|
2015-07-25 16:53:11 -04:00
|
|
|
toEqualData: function (util, customEqualityTesters) {
|
2015-07-28 17:52:03 -06:00
|
|
|
return {
|
2015-07-25 16:53:11 -04:00
|
|
|
compare: function (actual, expected) {
|
2015-07-28 17:52:03 -06:00
|
|
|
return {
|
|
|
|
|
pass: angular.equals(actual, expected)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Load the main application module
|
|
|
|
|
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('Logged out user', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
|
|
|
|
|
// This allows us to inject a service but then attach it to a variable
|
|
|
|
|
// with the same name as the service.
|
2015-07-25 16:53:11 -04:00
|
|
|
beforeEach(inject(function ($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Set a new global scope
|
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
|
|
|
|
|
|
// Point global variables to injected services
|
|
|
|
|
$stateParams = _$stateParams_;
|
|
|
|
|
$httpBackend = _$httpBackend_;
|
|
|
|
|
$location = _$location_;
|
|
|
|
|
|
|
|
|
|
// Initialize the Authentication controller
|
|
|
|
|
AuthenticationController = $controller('AuthenticationController', {
|
|
|
|
|
$scope: scope
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('$scope.signin()', function () {
|
|
|
|
|
it('should login with a correct user and password', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Test expected GET request
|
|
|
|
|
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');
|
|
|
|
|
|
2015-08-05 00:40:54 -04:00
|
|
|
scope.signin(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// Test scope value
|
|
|
|
|
expect(scope.authentication.user).toEqual('Fred');
|
|
|
|
|
expect($location.url()).toEqual('/');
|
|
|
|
|
});
|
|
|
|
|
|
2015-09-09 21:43:55 -03:00
|
|
|
it('should be redirected to previous state after successful login',
|
|
|
|
|
inject(function (_$state_) {
|
|
|
|
|
$state = _$state_;
|
|
|
|
|
$state.previous = {
|
|
|
|
|
state: {
|
|
|
|
|
name: 'articles.create'
|
|
|
|
|
},
|
|
|
|
|
params: {},
|
|
|
|
|
href: '/articles/create'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
spyOn($state, 'transitionTo');
|
|
|
|
|
spyOn($state, 'go');
|
|
|
|
|
|
|
|
|
|
// Test expected GET request
|
|
|
|
|
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');
|
|
|
|
|
|
|
|
|
|
scope.signin(true);
|
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// Test scope value
|
|
|
|
|
expect($state.go).toHaveBeenCalled();
|
|
|
|
|
expect($state.go).toHaveBeenCalledWith($state.previous.state.name, $state.previous.params);
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should fail to log in with nothing', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Test expected POST request
|
|
|
|
|
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
|
|
|
|
|
'message': 'Missing credentials'
|
|
|
|
|
});
|
|
|
|
|
|
2015-08-05 00:40:54 -04:00
|
|
|
scope.signin(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// Test scope value
|
|
|
|
|
expect(scope.error).toEqual('Missing credentials');
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should fail to log in with wrong credentials', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Foo/Bar combo assumed to not exist
|
|
|
|
|
scope.authentication.user = 'Foo';
|
|
|
|
|
scope.credentials = 'Bar';
|
|
|
|
|
|
|
|
|
|
// Test expected POST request
|
|
|
|
|
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
|
|
|
|
|
'message': 'Unknown user'
|
|
|
|
|
});
|
|
|
|
|
|
2015-08-05 00:40:54 -04:00
|
|
|
scope.signin(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// Test scope value
|
|
|
|
|
expect(scope.error).toEqual('Unknown user');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('$scope.signup()', function () {
|
|
|
|
|
it('should register with correct data', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Test expected GET request
|
|
|
|
|
scope.authentication.user = 'Fred';
|
|
|
|
|
$httpBackend.when('POST', '/api/auth/signup').respond(200, 'Fred');
|
|
|
|
|
|
2015-08-05 00:40:54 -04:00
|
|
|
scope.signup(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// test scope value
|
|
|
|
|
expect(scope.authentication.user).toBe('Fred');
|
2015-08-05 00:40:54 -04:00
|
|
|
expect(scope.error).toEqual(null);
|
2015-07-28 17:52:03 -06:00
|
|
|
expect($location.url()).toBe('/');
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should fail to register with duplicate Username', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
// Test expected POST request
|
|
|
|
|
$httpBackend.when('POST', '/api/auth/signup').respond(400, {
|
|
|
|
|
'message': 'Username already exists'
|
|
|
|
|
});
|
|
|
|
|
|
2015-08-05 00:40:54 -04:00
|
|
|
scope.signup(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// Test scope value
|
|
|
|
|
expect(scope.error).toBe('Username already exists');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('Logged in user', function () {
|
|
|
|
|
beforeEach(inject(function ($controller, $rootScope, _$location_, _Authentication_) {
|
2015-07-28 17:52:03 -06:00
|
|
|
scope = $rootScope.$new();
|
|
|
|
|
|
|
|
|
|
$location = _$location_;
|
|
|
|
|
$location.path = jasmine.createSpy().and.returnValue(true);
|
|
|
|
|
|
|
|
|
|
// Mock logged in user
|
|
|
|
|
_Authentication_.user = {
|
|
|
|
|
username: 'test',
|
|
|
|
|
roles: ['user']
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AuthenticationController = $controller('AuthenticationController', {
|
|
|
|
|
$scope: scope
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should be redirected to home', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
expect($location.path).toHaveBeenCalledWith('/');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-02-16 21:35:33 +01:00
|
|
|
}());
|