First commit

This commit is contained in:
2015-09-10 14:02:43 +00:00
commit ab624930df
25 changed files with 933 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
'use strict';
describe('AuthenticationService', function() {
var AuthenticationService, httpBackend, rootScope, cookieStore;
beforeEach(module('cloudbudget'));
beforeEach(inject(function(_AuthenticationService_, $httpBackend, $rootScope, $cookieStore) {
AuthenticationService = _AuthenticationService_;
httpBackend = $httpBackend;
rootScope = $rootScope;
cookieStore = $cookieStore;
}));
describe('* login()', function() {
it('should login successfully and return user', function() {
httpBackend
.when('POST', 'api/users/login')
.respond(200, {username: 'test', token: 'tok3n'});
AuthenticationService.login('test', 'password', function(data) {
data.success.should.be.true;
var user = data.user;
should.exist(user);
user.username.should.be.equal('test');
user.token.should.be.equal('tok3n');
should.not.exist(data.message);
});
});
it('should fail to login and return message', function() {
httpBackend
.when('POST', 'api/users/login')
.respond(401);
AuthenticationService.login('test', 'password', function(data) {
data.succes.should.be.false;
var message = data.message;
should.exist(message);
message.should.be.equal('Authentication fail');
should.not.exist(data.user);
})
});
});
describe('* setCredentials', function() {
it('should store the given user in scope and store', function() {
AuthenticationService.setCredentials({
username: 'test',
token: 'tok3n'
});
var globals = rootScope.globals;
should.exist(globals);
should.exist(globals.user);
globals.user.username.should.be.equal('test');
globals.user.token.should.be.equal('tok3n');
should.exist(globals.token);
globals.token.should.be.equal('tok3n');
should.exist(cookieStore.get('globals'));
cookieStore.get('globals').should.eql(globals);
});
});
describe('* clearCredentials()', function() {
it('should clean credentials', function() {
AuthenticationService.clearCredentials();
should.exist(rootScope.globals);
rootScope.globals.should.eql({});
should.not.exist(cookieStore.get('globals'))
});
});
});

23
test/home.controller.js Normal file
View File

@@ -0,0 +1,23 @@
describe('HomeController', function() {
var rootScope;
beforeEach(module('cloudbudget'));
beforeEach(inject(function($rootScope) {
rootScope = $rootScope.$new();
}));
describe('getFullname()', function() {
it('should handle names correctly', inject(function($controller) {
var scope = {};
var homeController = $controller('HomeController', {
'$rootScope': rootScope
});
homeController.firstname = 'George';
homeController.lastname = 'Harrison';
homeController.getFullname().should.be.equal('George Harrison');
}));
});
});

98
test/login.controller.js Normal file
View File

@@ -0,0 +1,98 @@
describe('LoginController', function() {
var scope,
$location,
$timeout,
$rootScope,
AuthenticationServiceMock,
createController,
shouldPass;
beforeEach(module('cloudbudget'));
beforeEach(function(){
module(function($provide) {
$provide.factory('AuthenticationService', ['$q', '$rootScope', function($q, $rootScope) {
function login(username, password ) {
var data;
if(shouldPass){
data = {
success: true,
user: {
username: 'test',
token: 'tok3n'
}
};
} else {
data = {
success: false,
message: 'Authentication fail'
};
}
return $q.when(data);
}
function setCredentials(user) {
$rootScope.globals = {
user: user,
token: user.token
}
}
function clearCredentials() {
$rootScope.globals = {};
}
return{
login: login,
setCredentials: setCredentials,
clearCredentials: clearCredentials
};
}]);
});
});
beforeEach(inject(function ($rootScope, $controller, _$location_, _$timeout_, AuthenticationService) {
$location = _$location_;
scope = $rootScope.$new();
$timeout = _$timeout_;
AuthenticationServiceMock = AuthenticationService;
createController = function() {
return $controller('LoginController', {
'$scope': scope,
AuthenticationService: AuthenticationServiceMock
});
};
}));
describe('login()', function() {
it('should log successfully', inject(function($controller, $location) {
shouldPass = true;
var loginController = createController();
loginController.username = 'fail';
loginController.password = 's3cr3t';
loginController.login();
$timeout.flush();
$location.path().should.be.equal('/');
}));
it('should fail to log', inject(function($controller, $location) {
shouldPass = false;
var loginController = $controller('LoginController');
loginController.username = 'fail';
loginController.password = 'secret';
loginController.login();
$timeout.flush();
$location.path().should.be.equal('/login');
}));
});
});

142
test/register.controller.js Normal file
View File

@@ -0,0 +1,142 @@
describe('RegisterController', function() {
var $location,
$rootScope,
$timeout,
$httpBackend,
UserService,
AuthenticationServiceMock,
FlashService,
createController,
apiRoutes,
shouldPass;
beforeEach(module('cloudbudget'));
beforeEach(function() {
module(function($provide) {
$provide.factory('AuthenticationService', ['$q', '$rootScope', function($q, $rootScope) {
function login(username, password ) {
var data;
if(shouldPass){
data = {
success: true,
user: {
username: 'test',
token: 'tok3n'
}
};
} else {
data = {
success: false,
message: 'Authentication fail'
};
}
return $q.when(data);
}
function setCredentials(user) {
$rootScope.globals = {
user: user,
token: user.token
}
}
function clearCredentials() {
$rootScope.globals = {};
}
return{
login: login,
setCredentials: setCredentials,
clearCredentials: clearCredentials
};
}]);
})
});
beforeEach(inject(function ( _$rootScope_, _$httpBackend_, $controller, _$location_, _$timeout_, AuthenticationService, _UserService_, _FlashService_, _apiRoutes_) {
$location = _$location_;
$httpBackend = $httpBackend;
$rootScope = _$rootScope_.$new();
$timeout = _$timeout_;
AuthenticationServiceMock = AuthenticationService;
UserService = _UserService_;
FlashService = _FlashService_;
apiRoutes = _apiRoutes_;
createController = function() {
return $controller('RegisterController', {
'$rootScope': $rootScope,
AuthenticationService: AuthenticationServiceMock,
UserService: _UserService_,
FlashService: _FlashService_
});
};
}));
describe('register()', function() {
it('should register successfully', inject(function($controller, $httpBackend, $location) {
shouldPass = true;
$httpBackend.expect('POST', apiRoutes.register)
.respond({
username: 'test',
token: 'tok3en'
});
var registerController = createController();
registerController.user = {
username: 'test',
password: 's3cr3t'
};
registerController.register();
$httpBackend.flush();
$timeout.flush();
$location.path().should.be.equal('/');
}));
it('should fail to register on bad parameter', inject(function($controller, $httpBackend, $location) {
shouldPass = false;
$httpBackend.expect('POST', apiRoutes.register)
.respond(400);
var registerController = createController();
registerController.user = {
username: 'test',
password: 'secret'
};
registerController.register();
$httpBackend.flush();
$timeout.flush();
$location.path().should.be.equal('/login');
}));
it('should fail to register on duplicate user', inject(function($controller, $httpBackend, $location) {
shouldPass = false;
$httpBackend.expect('POST', apiRoutes.register)
.respond(409);
var registerController = createController();
registerController.user = {
username: 'test',
password: 'secret'
};
registerController.register();
$httpBackend.flush();
$timeout.flush();
$location.path().should.be.equal('/login');
}));
});
});