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,
|
2016-10-10 17:51:44 -04:00
|
|
|
$location,
|
|
|
|
|
Notification;
|
2015-07-28 17:52:03 -06:00
|
|
|
|
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.
|
2016-10-10 17:51:44 -04:00
|
|
|
beforeEach(inject(function ($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Notification_) {
|
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_;
|
2016-10-10 17:51:44 -04:00
|
|
|
Notification = _Notification_;
|
|
|
|
|
|
|
|
|
|
// Spy on Notification
|
|
|
|
|
spyOn(Notification, 'error');
|
|
|
|
|
spyOn(Notification, 'success');
|
2015-07-28 17:52:03 -06:00
|
|
|
|
2016-10-05 17:51:53 -07:00
|
|
|
// Ignore parent template get on state transitions
|
|
|
|
|
$httpBackend.whenGET('/modules/core/client/views/home.client.view.html').respond(200);
|
|
|
|
|
$httpBackend.whenGET('/modules/core/client/views/400.client.view.html').respond(200);
|
|
|
|
|
|
2015-07-28 17:52:03 -06:00
|
|
|
// Initialize the Authentication controller
|
2016-02-11 22:34:20 -03:00
|
|
|
AuthenticationController = $controller('AuthenticationController as vm', {
|
2015-07-28 17:52:03 -06:00
|
|
|
$scope: scope
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('$scope.signin()', function () {
|
2016-10-05 17:51:53 -07:00
|
|
|
it('should login with a correct user and password', inject(function ($templateCache) {
|
|
|
|
|
$templateCache.put('/modules/core/client/views/home.client.view.html', '');
|
|
|
|
|
|
2015-07-28 17:52:03 -06:00
|
|
|
// Test expected GET request
|
2016-10-05 16:04:22 -07:00
|
|
|
$httpBackend.when('POST', '/api/auth/signin').respond(200, { username: 'Fred' });
|
2015-07-28 17:52:03 -06:00
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.signin(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// Test scope value
|
2016-09-17 12:05:21 -07:00
|
|
|
expect(scope.vm.authentication.user.username).toEqual('Fred');
|
2015-07-28 17:52:03 -06:00
|
|
|
expect($location.url()).toEqual('/');
|
2016-10-05 17:51:53 -07:00
|
|
|
}));
|
2015-07-28 17:52:03 -06:00
|
|
|
|
2016-10-05 17:51:53 -07:00
|
|
|
it('should login with a correct email and password', inject(function ($templateCache) {
|
|
|
|
|
$templateCache.put('/modules/core/client/views/home.client.view.html', '');
|
2016-10-06 11:34:15 -04:00
|
|
|
// Test expected GET request
|
2016-10-05 17:51:53 -07:00
|
|
|
$httpBackend.when('POST', '/api/auth/signin').respond(200, { email: 'Fred@email.com' });
|
2016-10-06 11:34:15 -04:00
|
|
|
|
|
|
|
|
scope.vm.signin(true);
|
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// Test scope value
|
|
|
|
|
expect(scope.vm.authentication.user.email).toEqual('Fred@email.com');
|
|
|
|
|
expect($location.url()).toEqual('/');
|
2016-10-05 17:51:53 -07:00
|
|
|
}));
|
2016-10-06 11:34:15 -04:00
|
|
|
|
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
|
2016-10-05 16:04:22 -07:00
|
|
|
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');
|
2015-09-09 21:43:55 -03:00
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.signin(true);
|
2015-09-09 21:43:55 -03:00
|
|
|
$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
|
2016-10-05 16:04:22 -07:00
|
|
|
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
|
2015-07-28 17:52:03 -06:00
|
|
|
'message': 'Missing credentials'
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.signin(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
// Test Notification.error is called
|
|
|
|
|
expect(Notification.error).toHaveBeenCalledWith({ message: 'Missing credentials', title: '<i class="glyphicon glyphicon-remove"></i> Signin Error!', delay: 6000 });
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
|
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
|
2016-10-06 11:34:15 -04:00
|
|
|
scope.vm.authentication.user = { username: 'Foo' };
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.credentials = 'Bar';
|
2015-07-28 17:52:03 -06:00
|
|
|
|
|
|
|
|
// Test expected POST request
|
2016-10-05 16:04:22 -07:00
|
|
|
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
|
2015-07-28 17:52:03 -06:00
|
|
|
'message': 'Unknown user'
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.signin(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
// Test Notification.error is called
|
|
|
|
|
expect(Notification.error).toHaveBeenCalledWith({ message: 'Unknown user', title: '<i class="glyphicon glyphicon-remove"></i> Signin Error!', delay: 6000 });
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('$scope.signup()', function () {
|
2016-10-05 17:51:53 -07:00
|
|
|
it('should register with correct data', inject(function ($templateCache) {
|
|
|
|
|
$templateCache.put('/modules/core/client/views/home.client.view.html', '');
|
|
|
|
|
|
2015-07-28 17:52:03 -06:00
|
|
|
// Test expected GET request
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.authentication.user = 'Fred';
|
2016-10-05 16:04:22 -07:00
|
|
|
$httpBackend.when('POST', '/api/auth/signup').respond(200, { username: 'Fred' });
|
2015-07-28 17:52:03 -06:00
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.signup(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
|
|
// test scope value
|
2016-09-17 12:05:21 -07:00
|
|
|
expect(scope.vm.authentication.user.username).toBe('Fred');
|
2016-10-10 17:51:44 -04:00
|
|
|
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Signup successful!' });
|
2015-07-28 17:52:03 -06:00
|
|
|
expect($location.url()).toBe('/');
|
2016-10-05 17:51:53 -07:00
|
|
|
}));
|
2015-07-28 17:52:03 -06:00
|
|
|
|
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
|
2016-10-05 16:04:22 -07:00
|
|
|
$httpBackend.when('POST', '/api/auth/signup').respond(400, {
|
2015-07-28 17:52:03 -06:00
|
|
|
'message': 'Username already exists'
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
scope.vm.signup(true);
|
2015-07-28 17:52:03 -06:00
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
2016-10-10 17:51:44 -04:00
|
|
|
// Test Notification.error is called
|
|
|
|
|
expect(Notification.error).toHaveBeenCalledWith({ message: 'Username already exists', title: '<i class="glyphicon glyphicon-remove"></i> Signup Error!', delay: 6000 });
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
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']
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-11 22:34:20 -03:00
|
|
|
AuthenticationController = $controller('AuthenticationController as vm', {
|
2015-07-28 17:52:03 -06:00
|
|
|
$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
|
|
|
}());
|