2016-05-01 00:01:54 +08:00
|
|
|
///<reference path="../../../browser.d.ts"/>
|
2016-03-19 16:58:27 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
import {it, inject, beforeEachProviders} from "@angular/core/testing";
|
|
|
|
import {provide} from "@angular/core";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {UserService} from "./user.service.ts";
|
|
|
|
import {User} from "../../../../common/entities/User";
|
|
|
|
import {Message} from "../../../../common/entities/Message";
|
2016-03-19 17:55:46 +08:00
|
|
|
import "rxjs/Rx";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
2016-05-05 02:10:28 +08:00
|
|
|
import {AuthenticationService} from "./authentication.service";
|
2016-03-19 16:58:27 +08:00
|
|
|
|
2016-03-19 17:55:46 +08:00
|
|
|
class MockUserService {
|
2016-05-09 23:04:56 +08:00
|
|
|
public login(credential:LoginCredential) {
|
|
|
|
return Promise.resolve(new Message<User>(null, new User(0, "testUserName")))
|
2016-03-19 17:55:46 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-19 16:58:27 +08:00
|
|
|
|
2016-05-05 03:08:05 +08:00
|
|
|
describe('AuthenticationService', () => {
|
2016-03-19 16:58:27 +08:00
|
|
|
beforeEachProviders(() => [
|
2016-03-19 17:55:46 +08:00
|
|
|
provide(UserService, {useClass: MockUserService}),
|
2016-03-19 16:58:27 +08:00
|
|
|
AuthenticationService
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
it('should call User service login', inject([AuthenticationService, UserService], (authService, userService) => {
|
|
|
|
spyOn(userService, "login").and.callThrough();
|
|
|
|
|
2016-03-19 16:58:27 +08:00
|
|
|
expect(userService.login).not.toHaveBeenCalled();
|
|
|
|
authService.login();
|
|
|
|
expect(userService.login).toHaveBeenCalled();
|
|
|
|
}));
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
it('should have NO Authenticated use', inject([AuthenticationService], (authService) => {
|
2016-05-05 04:20:54 +08:00
|
|
|
expect(authService.getUser()).toBe(null);
|
|
|
|
expect(authService.isAuthenticated()).toBe(false);
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
it('should have Authenticated use', inject([AuthenticationService], (authService) => {
|
|
|
|
spyOn(authService.OnAuthenticated, "trigger").and.callThrough();
|
2016-05-05 04:20:54 +08:00
|
|
|
authService.login();
|
2016-05-09 23:04:56 +08:00
|
|
|
authService.OnAuthenticated.on(() => {
|
2016-05-06 00:14:11 +08:00
|
|
|
expect(authService.OnAuthenticated.trigger).toHaveBeenCalled();
|
2016-05-05 04:20:54 +08:00
|
|
|
expect(authService.getUser()).not.toBe(null);
|
|
|
|
expect(authService.isAuthenticated()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
2016-03-19 16:58:27 +08:00
|
|
|
|
|
|
|
});
|