| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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('/accounts');
- }));
-
- 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');
- }));
- });
- });
|