Increase test coverage: Add unit test to the authentication module part 2 95/21895/1
authorMaxime Millette-Coulombe <mmcoulombe@inocybe.com>
Thu, 4 Jun 2015 18:27:33 +0000 (14:27 -0400)
committerMaxime Millette-Coulombe <mmcoulombe@inocybe.com>
Thu, 4 Jun 2015 18:27:33 +0000 (14:27 -0400)
Change-Id: I4cc97c522c927814f1a970b171869c011a1b1555
Signed-off-by: Maxime Millette-Coulombe <mmcoulombe@inocybe.com>
modules/common-authentication-resources/src/main/resources/authentification/auth.spec.js

index 548fb907340cc9dd614ebdd90879a749f87c69f4..76d3ac77091378704f4301bc78820a2f536d336a 100644 (file)
@@ -1,10 +1,11 @@
 define(['common/authentification/auth.services'], function() {
   describe('Auth Module', function() {
-    var _Auth;
+    var _Auth, httpBackend, deferred;
     beforeEach(module('app.common.auth'));
 
     beforeEach(inject(function($injector) {
       _Auth = $injector.get('Auth');
+      httpBackend = $injector.get('$httpBackend');
     }));
 
     it('Should have defined function facilate the authentication process', function() {
@@ -17,5 +18,94 @@ define(['common/authentification/auth.services'], function() {
       expect(_Auth.login).toBeDefined();
       expect(_Auth.logout).toBeDefined();
     });
+
+    describe(':: Authentication header', function() {
+      var username = 'john',
+          password = 'abc123',
+          _window = null;
+
+      beforeEach(inject(function($window) {
+        _window = $window;
+      }));
+
+      it('Should set the basic authenticate header', function() {
+        _Auth.setBasic(username, password);
+
+        expect(_window.sessionStorage.odlUser).toBeDefined();
+        expect(_window.sessionStorage.odlUser).toEqual(username);
+
+        expect(_window.sessionStorage.odlPass).toBeDefined();
+        expect(_window.sessionStorage.odlPass).toEqual(password);
+      });
+
+      it('Should unset the basic authenticate header', inject(function($http) {
+        _Auth.setBasic(username, password);
+        _Auth.unsetBasic();
+
+        expect(_window.sessionStorage.odlUser).toBeUndefined();
+        expect(_window.sessionStorage.odlPass).toBeUndefined();
+        expect($http.defaults.headers.common.Authorization).toBeUndefined();
+      }));
+    });
+
+    describe(':: Login management', function() {
+      var username = 'john',
+          password = 'abc123';
+
+      it('Should return the current user or null otherwise', function() {
+        var user = _Auth.getUser();
+        expect(user).toBeNull();
+
+        _Auth.setBasic(username, password);
+        expect(user).toEqual(user);
+      });
+
+      it('Should set the authentication header and send a callback if success', function() {
+        httpBackend.expect('GET',/.*/).respond(200, '');
+        var successSpy = jasmine.createSpy("successSpy");
+        var errorSpy = jasmine.createSpy("errorSpy");
+        spyOn(_Auth, 'setBasic');
+
+        _Auth.login(username, password, successSpy, errorSpy);
+        httpBackend.flush();
+
+        expect(_Auth.setBasic).toHaveBeenCalledWith(username, password);
+        expect(successSpy).toHaveBeenCalled();
+        expect(errorSpy).not.toHaveBeenCalled();
+      });
+
+      it('Should unset the authentication header and send a callback if error', function() {
+        httpBackend.expect('GET',/.*/).respond(404, '');
+        var successSpy = jasmine.createSpy("successSpy");
+        var errorSpy = jasmine.createSpy("errorSpy");
+        spyOn(_Auth, 'setBasic');
+        spyOn(_Auth, 'unsetBasic');
+
+        _Auth.login(username, password, successSpy, errorSpy);
+        httpBackend.flush();
+
+        expect(_Auth.setBasic).toHaveBeenCalledWith(username, password);
+        expect(_Auth.unsetBasic).toHaveBeenCalled();
+        expect(successSpy).not.toHaveBeenCalled();
+        expect(errorSpy).toHaveBeenCalled();
+      });
+
+      it('Should unset the authentication header on logout', function() {
+        var successSpy = jasmine.createSpy("successSpy");
+        spyOn(_Auth, 'unsetBasic');
+
+        _Auth.logout(successSpy);
+
+        expect(_Auth.unsetBasic).toHaveBeenCalled();
+        expect(successSpy).toHaveBeenCalled();
+      });
+
+      afterEach(function() {
+        httpBackend.verifyNoOutstandingExpectation();
+        httpBackend.verifyNoOutstandingRequest();
+      });
+
+    });
+
   });
 });