Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / dlux / dlux-web / src / common / authentification / auth.spec.js
1 define(['common/authentification/auth.module'], function () {
2   describe('Auth Module', function () {
3     var _Auth, httpBackend, deferred;
4     beforeEach(module('app.common.auth'));
5
6     beforeEach(inject(function ($injector) {
7       _Auth = $injector.get('Auth');
8       httpBackend = $injector.get('$httpBackend');
9     }));
10
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();
20     });
21
22     describe(':: Authentication header', function () {
23       var username = 'john',
24         password = 'abc123',
25         _window = null;
26
27       beforeEach(inject(function ($window) {
28         _window = $window;
29       }));
30
31       it('Should set the basic authenticate header', function () {
32         _Auth.setBasic(username, password);
33
34         expect(_window.localStorage.odlUser).toBeDefined();
35         expect(_window.localStorage.odlUser).toEqual(username);
36
37         expect(_window.localStorage.odlPass).toBeDefined();
38         expect(_window.localStorage.odlPass).toEqual(password);
39       });
40
41       it('Should unset the basic authenticate header', inject(function ($http) {
42         _Auth.setBasic(username, password);
43         _Auth.unsetBasic();
44
45         expect(_window.localStorage.odlUser).toBeUndefined();
46         expect(_window.localStorage.odlPass).toBeUndefined();
47         expect($http.defaults.headers.common.Authorization).toBeUndefined();
48       }));
49     });
50
51     describe(':: Login management', function () {
52       var username = 'john',
53         password = 'abc123';
54
55       it('Should return the current user or null otherwise', function () {
56         var user = _Auth.getUser();
57         expect(user).toBeNull();
58
59         _Auth.setBasic(username, password);
60         expect(user).toEqual(user);
61       });
62
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');
68
69         _Auth.login(username, password, successSpy, errorSpy);
70         httpBackend.flush();
71
72         expect(_Auth.setBasic).toHaveBeenCalledWith(username, password);
73         expect(successSpy).toHaveBeenCalled();
74         expect(errorSpy).not.toHaveBeenCalled();
75       });
76
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');
83
84         _Auth.login(username, password, successSpy, errorSpy);
85         httpBackend.flush();
86
87         expect(_Auth.setBasic).toHaveBeenCalledWith(username, password);
88         expect(_Auth.unsetBasic).toHaveBeenCalled();
89         expect(successSpy).not.toHaveBeenCalled();
90         expect(errorSpy).toHaveBeenCalled();
91       });
92
93       it('Should unset the authentication header on logout', function () {
94         var successSpy = jasmine.createSpy("successSpy");
95         spyOn(_Auth, 'unsetBasic');
96
97         _Auth.logout(successSpy);
98
99         expect(_Auth.unsetBasic).toHaveBeenCalled();
100         expect(successSpy).toHaveBeenCalled();
101       });
102
103       afterEach(function () {
104         httpBackend.verifyNoOutstandingExpectation();
105         httpBackend.verifyNoOutstandingRequest();
106       });
107
108     });
109
110   });
111 });