diff --git a/src/app/components/auth/auth_service.spec.ts b/src/app/components/auth/auth_service.spec.ts new file mode 100644 index 0000000..5b784ae --- /dev/null +++ b/src/app/components/auth/auth_service.spec.ts @@ -0,0 +1,67 @@ + + +import {AuthService, AUTH_EVENTS} from "./"; +import {User, Credentials} from "./../../models/interfaces"; + +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 = { + 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!"); + }); + + }); + + + + +}); + \ No newline at end of file diff --git a/src/app/components/auth/auth_service.ts b/src/app/components/auth/auth_service.ts index 834fc83..94edc1a 100644 --- a/src/app/components/auth/auth_service.ts +++ b/src/app/components/auth/auth_service.ts @@ -32,7 +32,7 @@ export class AuthService { return currentUser; } - login(credentials: Credentials) { + login(credentials: Credentials): ng.IPromise { let url = '/api/v1/login'; let encodedData = 'login=' + credentials.username + '&password=' + credentials.password; return this.$http.post(url, encodedData).then(this.loginSuccessCallback.bind(this), this.loginFailedCallback.bind(this)); -- libgit2 0.21.2