1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-16 11:37:13 +02:00
pigallery2/frontend/app/model/network/autehentication.service.spec.ts

67 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-03-30 21:30:30 +02:00
import {inject, TestBed} from '@angular/core/testing';
import {UserService} from './user.service';
import {UserDTO} from '../../../../common/entities/UserDTO';
import 'rxjs/Rx';
import {LoginCredential} from '../../../../common/entities/LoginCredential';
import {AuthenticationService} from './authentication.service';
import {NetworkService} from './network.service';
import {ErrorDTO} from '../../../../common/entities/Error';
2016-03-19 11:55:46 +02:00
class MockUserService {
2017-07-03 19:39:09 +02:00
public login(credential: LoginCredential): Promise<UserDTO> {
2018-03-30 21:30:30 +02:00
return Promise.resolve(<UserDTO>{name: 'testUserName'});
}
public async getSessionUser() {
return null;
}
2016-03-19 11:55:46 +02:00
}
2017-07-19 08:00:51 +02:00
class MockNetworkService {
addGlobalErrorHandler(fn: (error: ErrorDTO) => boolean) {
}
}
2016-05-04 21:08:05 +02:00
describe('AuthenticationService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
2017-07-19 08:00:51 +02:00
{provide: NetworkService, useClass: MockNetworkService},
{provide: UserService, useClass: MockUserService},
2017-07-19 08:00:51 +02:00
AuthenticationService,
]
2016-12-27 00:36:38 +02:00
});
});
it('should call UserDTO service login', inject([AuthenticationService, UserService], (authService, userService) => {
2018-03-30 21:30:30 +02:00
spyOn(userService, 'login').and.callThrough();
2016-05-09 17:04:56 +02:00
expect(userService.login).not.toHaveBeenCalled();
authService.login();
expect(userService.login).toHaveBeenCalled();
}));
2017-07-03 19:39:09 +02:00
it('should have NO Authenticated use', inject([AuthenticationService], (authService: AuthenticationService) => {
expect(authService.user.value).toBe(null);
expect(authService.isAuthenticated()).toBe(false);
}));
2016-05-04 22:20:54 +02:00
2017-07-03 19:39:09 +02:00
it('should have Authenticated use', inject([AuthenticationService], (authService: AuthenticationService) => {
2018-03-30 21:30:30 +02:00
spyOn(authService.user, 'next').and.callThrough();
2017-07-03 19:39:09 +02:00
authService.user.subscribe((user) => {
if (user == null) {
return;
}
expect(authService.user.next).toHaveBeenCalled();
expect(authService.user.value).not.toBe(null);
expect(authService.isAuthenticated()).toBe(true);
});
2017-07-03 19:39:09 +02:00
authService.login(<any>{});
2016-05-04 22:20:54 +02:00
}));
2016-05-04 22:20:54 +02:00
});