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

52 lines
1.8 KiB
TypeScript
Raw Normal View History

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";
import {Message} from "../../../../common/entities/Message";
2016-03-19 11:55:46 +02:00
import "rxjs/Rx";
import {LoginCredential} from "../../../../common/entities/LoginCredential";
2016-05-04 20:10:28 +02:00
import {AuthenticationService} from "./authentication.service";
2016-03-19 11:55:46 +02:00
class MockUserService {
2016-12-27 00:36:38 +02:00
public login(credential: LoginCredential) {
2016-12-27 17:09:47 +02:00
return Promise.resolve(new Message<UserDTO>(null, <UserDTO>{name: "testUserName"}))
2016-03-19 11:55:46 +02:00
}
}
2016-05-04 21:08:05 +02:00
describe('AuthenticationService', () => {
2016-12-27 00:36:38 +02:00
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{provide: UserService, useClass: MockUserService},
AuthenticationService]
});
});
2016-12-27 17:09:47 +02:00
it('should call UserDTO service login', inject([AuthenticationService, UserService], (authService, userService) => {
2016-05-09 17:04:56 +02:00
spyOn(userService, "login").and.callThrough();
expect(userService.login).not.toHaveBeenCalled();
authService.login();
expect(userService.login).toHaveBeenCalled();
}));
2016-05-09 17:04:56 +02:00
it('should have NO Authenticated use', inject([AuthenticationService], (authService) => {
2016-05-04 22:20:54 +02:00
expect(authService.getUser()).toBe(null);
expect(authService.isAuthenticated()).toBe(false);
}));
2016-05-09 17:04:56 +02:00
it('should have Authenticated use', inject([AuthenticationService], (authService) => {
2016-05-16 23:15:03 +02:00
spyOn(authService.OnUserChanged, "trigger").and.callThrough();
2016-05-04 22:20:54 +02:00
authService.login();
2016-05-16 23:15:03 +02:00
authService.OnUserChanged.on(() => {
expect(authService.OnUserChanged.trigger).toHaveBeenCalled();
2016-05-04 22:20:54 +02:00
expect(authService.getUser()).not.toBe(null);
expect(authService.isAuthenticated()).toBe(true);
});
}));
});