Increase test coverage: Add unit tests to the login module part 2 23/21823/2
authorMaxime Millette-Coulombe <mmcoulombe@inocybe.com>
Wed, 3 Jun 2015 20:39:39 +0000 (16:39 -0400)
committerHarman Singh <harmasin@cisco.com>
Thu, 4 Jun 2015 17:26:46 +0000 (17:26 +0000)
Change-Id: I02ffbec59100992d709e0de6eb666813fef29f04
Signed-off-by: Maxime Millette-Coulombe <mmcoulombe@inocybe.com>
modules/common-login-resources/src/main/resources/login/login.spec.js

index 23ab183a146cf18dd576257344d4c4447ec82edd..f16c990785d279c912acb6b7b4a85a319fce3a97 100644 (file)
@@ -1,17 +1,23 @@
 define(['common/login/login.controller'], function() {
   describe('Login Module', function() {
-    var scope, state, controller, AuthMock;
+    var scope, state, controller, location, _AuthMock;
+    var url = '/test';
 
     beforeEach(module('ui.router'));
+    beforeEach(module('app.common.layout'));
     beforeEach(module('app.common.login', function($provide) {
-      AuthMock = jasmine.createSpyObj('AuthMock', ['isAuthed']);
+      AuthMock = {
+        isAuthed: function() {},
+        login: function() {}
+      };
       $provide.value('Auth', AuthMock);
     }));
 
-    beforeEach(inject( function($rootScope, $controller, $state) {
+    beforeEach(inject( function($rootScope, $controller, $state, $location) {
       scope = $rootScope.$new();
       controller = $controller;
       state = $state;
+      location = $location;
     }));
 
     it('Should load the login state', function() {
@@ -20,5 +26,36 @@ define(['common/login/login.controller'], function() {
       controller('LoginCtrl', {$scope: scope, $state: state});
       expect(state.href(stateName, {})).toBe('#/login');
     });
+
+    it('Should redirect any url to login if not logged', function() {
+      var stateName = 'login';
+      spyOn(AuthMock,'isAuthed').andReturn(false);
+      location.url(url);
+      controller('LoginCtrl', {$scope: scope, $state: state});
+      state.go('main');
+
+      expect(AuthMock.isAuthed).toHaveBeenCalled();
+      expect(state.is("login"));
+      expect(location.url()).toEqual('/login');
+    });
+
+    it('Should not redirect if logged', function() {
+      spyOn(AuthMock,'isAuthed').andReturn(true);
+      location.url(url);
+      controller('LoginCtrl', {$scope: scope, $state: state});
+      state.go('main');
+
+      expect(AuthMock.isAuthed).toHaveBeenCalled();
+      expect(state.is("main"));
+      expect(location.url()).toEqual(url);
+    });
+
+    it('Should call the Auth module', function() {
+      spyOn(AuthMock,'login');
+      controller('LoginCtrl', {$scope: scope, $state: state});
+
+      scope.sendLogin();
+      expect(AuthMock.login).toHaveBeenCalled();
+    });
   });
 });