Commit 5dcaf6413b05bcf5d3bed110bc58ff880f6ab4ea
1 parent
494fb34f
Exists in
master
and in
35 other branches
added auth service spec
Showing
2 changed files
with
68 additions
and
1 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,67 @@ |
| 1 | + | |
| 2 | + | |
| 3 | +import {AuthService, AUTH_EVENTS} from "./"; | |
| 4 | +import {User, Credentials} from "./../../models/interfaces"; | |
| 5 | + | |
| 6 | +describe("Auth Service", () => { | |
| 7 | + | |
| 8 | + let $httpBackend: ng.IHttpBackendService; | |
| 9 | + let authService: AuthService; | |
| 10 | + let credentials: Credentials; | |
| 11 | + let $rootScope: ng.IRootScopeService; | |
| 12 | + let user: User; | |
| 13 | + | |
| 14 | + beforeEach(angular.mock.module("noosferoApp")); | |
| 15 | + | |
| 16 | + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _$rootScope_: ng.IRootScopeService, _AuthService_: AuthService) => { | |
| 17 | + $httpBackend = _$httpBackend_; | |
| 18 | + authService = _AuthService_; | |
| 19 | + $rootScope = _$rootScope_; | |
| 20 | + | |
| 21 | + user = <User>{ | |
| 22 | + id: 1, | |
| 23 | + login: "user" | |
| 24 | + }; | |
| 25 | + })); | |
| 26 | + | |
| 27 | + | |
| 28 | + describe("Succesffull login", () => { | |
| 29 | + | |
| 30 | + beforeEach(() => { | |
| 31 | + credentials = { username: "user", password: "password" }; | |
| 32 | + | |
| 33 | + $httpBackend.expectPOST("/api/v1/login", "login=user&password=password").respond(200, { user: user }); | |
| 34 | + }); | |
| 35 | + | |
| 36 | + it("should return loggedUser", (done) => { | |
| 37 | + authService.login(credentials).then((loggedUser) => { | |
| 38 | + expect(loggedUser).toBeDefined(); | |
| 39 | + done(); | |
| 40 | + }); | |
| 41 | + $httpBackend.flush(); | |
| 42 | + expect($httpBackend.verifyNoOutstandingRequest()); | |
| 43 | + }); | |
| 44 | + | |
| 45 | + | |
| 46 | + it("should emit event loggin successful with user logged data", () => { | |
| 47 | + | |
| 48 | + authService.login(credentials); | |
| 49 | + | |
| 50 | + let eventEmmited: boolean = false; | |
| 51 | + $rootScope.$on(AUTH_EVENTS.loginSuccess, (event: ng.IAngularEvent, userThroughEvent: User) => { | |
| 52 | + eventEmmited = true; | |
| 53 | + expect(userThroughEvent).toEqual(user) | |
| 54 | + }); | |
| 55 | + | |
| 56 | + $httpBackend.flush(); | |
| 57 | + | |
| 58 | + expect(eventEmmited).toBeTruthy(AUTH_EVENTS.loginSuccess + " was not emmited!"); | |
| 59 | + }); | |
| 60 | + | |
| 61 | + }); | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | +}); | |
| 67 | + | |
| 0 | 68 | \ No newline at end of file | ... | ... |
src/app/components/auth/auth_service.ts
| ... | ... | @@ -32,7 +32,7 @@ export class AuthService { |
| 32 | 32 | return currentUser; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - login(credentials: Credentials) { | |
| 35 | + login(credentials: Credentials): ng.IPromise<User> { | |
| 36 | 36 | let url = '/api/v1/login'; |
| 37 | 37 | let encodedData = 'login=' + credentials.username + '&password=' + credentials.password; |
| 38 | 38 | return this.$http.post(url, encodedData).then(this.loginSuccessCallback.bind(this), this.loginFailedCallback.bind(this)); | ... | ... |