auth_service.spec.ts
2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {AuthService, AUTH_EVENTS} from "./";
import {User, Credentials} from "./../../models/interfaces";
describe("Services", () => {
describe("Auth Service", () => {
let $httpBackend: ng.IHttpBackendService;
let authService: AuthService;
let credentials: Credentials;
let $rootScope: ng.IRootScopeService;
let user: User;
beforeEach(angular.mock.module("noosferoApp"));
beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _$rootScope_: ng.IRootScopeService, _AuthService_: AuthService) => {
$httpBackend = _$httpBackend_;
authService = _AuthService_;
$rootScope = _$rootScope_;
user = <User>{
id: 1,
login: "user"
};
}));
describe("Succesffull login", () => {
beforeEach(() => {
credentials = { username: "user", password: "password" };
$httpBackend.expectPOST("/api/v1/login", "login=user&password=password").respond(200, { user: user });
});
it("should return loggedUser", (done) => {
authService.login(credentials).then((loggedUser) => {
expect(loggedUser).toBeDefined();
done();
});
$httpBackend.flush();
expect($httpBackend.verifyNoOutstandingRequest());
});
it("should emit event loggin successful with user logged data", () => {
authService.login(credentials);
let eventEmmited: boolean = false;
$rootScope.$on(AUTH_EVENTS.loginSuccess, (event: ng.IAngularEvent, userThroughEvent: User) => {
eventEmmited = true;
expect(userThroughEvent).toEqual(user)
});
$httpBackend.flush();
expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!");
});
it("should return the current logged in user", () => {
authService.login(credentials);
$httpBackend.flush();
let actual: User = authService.currentUser();
expect(actual).toEqual(user, "The returned user must be present");
});
it("should not return the current user after logout", () => {
authService.logout();
let actual: any = authService.currentUser();
expect(actual).toEqual(undefined, "The returned user must not be defined");
});
});
});
});