1 define(['common/authentification/auth.module'], function () {
2 describe('Auth Module', function () {
3 var _Auth, httpBackend, deferred;
4 beforeEach(module('app.common.auth'));
6 beforeEach(inject(function ($injector) {
7 _Auth = $injector.get('Auth');
8 httpBackend = $injector.get('$httpBackend');
11 it('Should have defined function facilate the authentication process', function () {
12 expect(_Auth.setBasic).toBeDefined();
13 expect(_Auth.unsetBasic).toBeDefined();
14 expect(_Auth.getUser).toBeDefined();
15 expect(_Auth.authorize).toBeDefined();
16 expect(_Auth.isAuthed).toBeDefined();
17 expect(_Auth.isLoggedIn).toBeDefined();
18 expect(_Auth.login).toBeDefined();
19 expect(_Auth.logout).toBeDefined();
22 describe(':: Authentication header', function () {
23 var username = 'john',
27 beforeEach(inject(function ($window) {
31 it('Should set the basic authenticate header', function () {
32 _Auth.setBasic(username, password);
34 expect(_window.localStorage.odlUser).toBeDefined();
35 expect(_window.localStorage.odlUser).toEqual(username);
37 expect(_window.localStorage.odlPass).toBeDefined();
38 expect(_window.localStorage.odlPass).toEqual(password);
41 it('Should unset the basic authenticate header', inject(function ($http) {
42 _Auth.setBasic(username, password);
45 expect(_window.localStorage.odlUser).toBeUndefined();
46 expect(_window.localStorage.odlPass).toBeUndefined();
47 expect($http.defaults.headers.common.Authorization).toBeUndefined();
51 describe(':: Login management', function () {
52 var username = 'john',
55 it('Should return the current user or null otherwise', function () {
56 var user = _Auth.getUser();
57 expect(user).toBeNull();
59 _Auth.setBasic(username, password);
60 expect(user).toEqual(user);
63 it('Should set the authentication header and send a callback if success', function () {
64 httpBackend.expect('GET', /.*/).respond(200, '');
65 var successSpy = jasmine.createSpy("successSpy");
66 var errorSpy = jasmine.createSpy("errorSpy");
67 spyOn(_Auth, 'setBasic');
69 _Auth.login(username, password, successSpy, errorSpy);
72 expect(_Auth.setBasic).toHaveBeenCalledWith(username, password);
73 expect(successSpy).toHaveBeenCalled();
74 expect(errorSpy).not.toHaveBeenCalled();
77 it('Should unset the authentication header and send a callback if error', function () {
78 httpBackend.expect('GET', /.*/).respond(404, '');
79 var successSpy = jasmine.createSpy("successSpy");
80 var errorSpy = jasmine.createSpy("errorSpy");
81 spyOn(_Auth, 'setBasic');
82 spyOn(_Auth, 'unsetBasic');
84 _Auth.login(username, password, successSpy, errorSpy);
87 expect(_Auth.setBasic).toHaveBeenCalledWith(username, password);
88 expect(_Auth.unsetBasic).toHaveBeenCalled();
89 expect(successSpy).not.toHaveBeenCalled();
90 expect(errorSpy).toHaveBeenCalled();
93 it('Should unset the authentication header on logout', function () {
94 var successSpy = jasmine.createSpy("successSpy");
95 spyOn(_Auth, 'unsetBasic');
97 _Auth.logout(successSpy);
99 expect(_Auth.unsetBasic).toHaveBeenCalled();
100 expect(successSpy).toHaveBeenCalled();
103 afterEach(function () {
104 httpBackend.verifyNoOutstandingExpectation();
105 httpBackend.verifyNoOutstandingRequest();