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