diff --git a/ui/app/clinical/common/controllers/patientListHeaderController.js b/ui/app/clinical/common/controllers/patientListHeaderController.js index c757a9c0fb..39a696df19 100644 --- a/ui/app/clinical/common/controllers/patientListHeaderController.js +++ b/ui/app/clinical/common/controllers/patientListHeaderController.js @@ -107,12 +107,14 @@ angular.module('bahmni.clinical') $scope.date = retrospectiveDate ? new Date(retrospectiveDate) : new Date($scope.maxStartDate); $scope.encounterProvider = getCurrentProvider(); selectedProvider = getCurrentProvider(); - + const loginLocations = localStorage.getItem("loginLocations"); + if (loginLocations) { + $scope.locations = JSON.parse(loginLocations); + return; + } return locationService.getAllByTag("Login Location").then(function (response) { $scope.locations = response.data.results; - $scope.selectedLocationUuid = getCurrentCookieLocation().uuid; - } - ); + }); }; return init(); diff --git a/ui/app/common/auth/authentication.js b/ui/app/common/auth/authentication.js index 55cb2f5a0a..af1ae84434 100644 --- a/ui/app/common/auth/authentication.js +++ b/ui/app/common/auth/authentication.js @@ -134,7 +134,8 @@ angular.module('authentication') userService.getProviderForUser(data.results[0].uuid).then(function (providers) { if (!_.isEmpty(providers.results) && hasAnyActiveProvider(providers.results)) { $rootScope.currentUser = new Bahmni.Auth.User(data.results[0]); - $rootScope.currentUser.currentLocation = $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName).name; + $rootScope.currentUser.provider = providers.results[0]; + $rootScope.currentUser.currentLocation = null; $rootScope.$broadcast('event:user-credentialsLoaded', data.results[0]); deferrable.resolve(data.results[0]); } else { diff --git a/ui/app/common/auth/userService.js b/ui/app/common/auth/userService.js index dc357bc04c..eee047fa84 100644 --- a/ui/app/common/auth/userService.js +++ b/ui/app/common/auth/userService.js @@ -40,7 +40,8 @@ angular.module('authentication') return $http.get(Bahmni.Common.Constants.providerUrl, { method: "GET", params: { - user: uuid + user: uuid, + v: 'custom:(uuid,display,attributes)' }, cache: false }); diff --git a/ui/app/home/app.js b/ui/app/home/app.js index bb0b33b480..8635881f2b 100644 --- a/ui/app/home/app.js +++ b/ui/app/home/app.js @@ -41,6 +41,16 @@ angular.module('bahmni.home', ['ui.router', 'httpErrorInterceptor', 'bahmni.comm } } }) + .state('loginLocation', { + url: '/loginLocation', + controller: 'LoginLocationController', + templateUrl: 'views/loginLocation.html', + resolve: { + initialData: function (loginInitialization) { + return loginInitialization(); + } + } + }) .state('errorLog', { url: '/errorLog', controller: 'ErrorLogController', diff --git a/ui/app/home/controllers/dashboardController.js b/ui/app/home/controllers/dashboardController.js index e2da637591..398d6b608c 100644 --- a/ui/app/home/controllers/dashboardController.js +++ b/ui/app/home/controllers/dashboardController.js @@ -18,12 +18,26 @@ angular.module('bahmni.home') return $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName) ? $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName) : null; }; + var setCurrentLoginLocationForUser = function () { + const currentLoginLocation = getCurrentLocation(); + if (currentLoginLocation) { + $scope.selectedLocationUuid = getCurrentLocation().uuid; + } else { + $scope.selectedLocationUuid = null; + } + }; + var init = function () { + const loginLocations = localStorage.getItem("loginLocations"); + if (loginLocations) { + $scope.locations = JSON.parse(loginLocations); + setCurrentLoginLocationForUser(); + return; + } return locationService.getAllByTag("Login Location").then(function (response) { $scope.locations = response.data.results; - $scope.selectedLocationUuid = getCurrentLocation().uuid; - } - ); + setCurrentLoginLocationForUser(); + }); }; var getLocationFor = function (uuid) { @@ -33,7 +47,12 @@ angular.module('bahmni.home') }; $scope.isCurrentLocation = function (location) { - return getCurrentLocation().uuid === location.uuid; + const currentLocation = getCurrentLocation(); + if (currentLocation) { + return getCurrentLocation().uuid === location.uuid; + } else { + return false; + } }; $scope.onLocationChange = function () { diff --git a/ui/app/home/controllers/loginController.js b/ui/app/home/controllers/loginController.js index a3b2f6004f..9d649ff9e1 100644 --- a/ui/app/home/controllers/loginController.js +++ b/ui/app/home/controllers/loginController.js @@ -1,8 +1,8 @@ 'use strict'; angular.module('bahmni.home') - .controller('LoginController', ['$rootScope', '$scope', '$window', '$location', 'sessionService', 'initialData', 'spinner', '$q', '$stateParams', '$bahmniCookieStore', 'localeService', '$translate', 'userService', 'auditLogService', - function ($rootScope, $scope, $window, $location, sessionService, initialData, spinner, $q, $stateParams, $bahmniCookieStore, localeService, $translate, userService, auditLogService) { + .controller('LoginController', ['$rootScope', '$scope', '$window', '$location', 'sessionService', 'initialData', 'spinner', '$q', '$stateParams', '$bahmniCookieStore', 'localeService', '$translate', 'userService', 'auditLogService', '$state', + function ($rootScope, $scope, $window, $location, sessionService, initialData, spinner, $q, $stateParams, $bahmniCookieStore, localeService, $translate, userService, auditLogService, $state) { var redirectUrl = $location.search()['from']; var landingPagePath = "/dashboard"; var loginPagePath = "/login"; @@ -118,6 +118,26 @@ angular.module('bahmni.home') $scope.loginInfo.currentLocation = getLastLoggedinLocation(); }; + var checkIfUserHasProviderAttributes = function () { + return $rootScope.currentUser.provider && $rootScope.currentUser.provider.attributes && $rootScope.currentUser.provider.attributes.length > 0; + }; + + var saveUserAssignedLocationsToLocalStorage = function () { + var userAssignedLocations = $rootScope.currentUser.provider.attributes + .filter(function (attribute) { + return attribute.attributeType.display === "Login Locations"; + }) + .map(function (attribute) { + return { display: attribute.value.name, uuid: attribute.value.uuid }; + }); + + if (userAssignedLocations.length > 0) { + localStorage.setItem("loginLocations", JSON.stringify(userAssignedLocations)); + } else { + localStorage.removeItem("loginLocations"); + } + }; + $scope.login = function () { $scope.errorMessageTranslateKey = null; var deferrable = $q.defer(); @@ -140,20 +160,18 @@ angular.module('bahmni.home') deferrable.resolve(data); return; } - sessionService.updateSession($scope.loginInfo.currentLocation, null).then(function () { - sessionService.loadCredentials().then(function () { - onSuccessfulAuthentication(); - $rootScope.currentUser.addDefaultLocale($scope.selectedLocale); - userService.savePreferences().then( + sessionService.loadCredentials().then(function () { + onSuccessfulAuthentication(); + $rootScope.currentUser.addDefaultLocale($scope.selectedLocale); + userService.savePreferences().then( function () { deferrable.resolve(); }, function (error) { deferrable.reject(error); } ); - logAuditForLoginAttempts("USER_LOGIN_SUCCESS"); - }, function (error) { - $scope.errorMessageTranslateKey = error; - deferrable.reject(error); - logAuditForLoginAttempts("USER_LOGIN_FAILED", true); - }); + logAuditForLoginAttempts("USER_LOGIN_SUCCESS"); + }, function (error) { + $scope.errorMessageTranslateKey = error; + deferrable.reject(error); + logAuditForLoginAttempts("USER_LOGIN_FAILED", true); }); }, function (error) { @@ -211,7 +229,12 @@ angular.module('bahmni.home') } }); } else { - $location.url(landingPagePath); + if (checkIfUserHasProviderAttributes()) { + saveUserAssignedLocationsToLocalStorage(); + } else { + localStorage.removeItem("loginLocations"); + } + $state.go('loginLocation', {}); } } ); diff --git a/ui/app/home/controllers/loginLocationController.js b/ui/app/home/controllers/loginLocationController.js new file mode 100644 index 0000000000..bfd1bb45a4 --- /dev/null +++ b/ui/app/home/controllers/loginLocationController.js @@ -0,0 +1,145 @@ +'use strict'; + +angular.module('bahmni.home') + .controller('LoginLocationController', ['$rootScope', '$scope', '$window', '$location', 'sessionService', 'initialData', 'spinner', '$q', '$stateParams', '$bahmniCookieStore', 'localeService', '$translate', 'userService', 'auditLogService', + function ($rootScope, $scope, $window, $location, sessionService, initialData, spinner, $q, $stateParams, $bahmniCookieStore, localeService, $translate, userService, auditLogService) { + var redirectUrl = $location.search()['from']; + var landingPagePath = "/dashboard"; + var loginPagePath = "/login"; + const LOGIN_LOCATIONS = "Login Locations"; + $scope.loginInfo = {}; + var localeLanguages = []; + + var getLocalTimeZone = function () { + var currentLocalTime = new Date().toString(); + var localTimeZoneList = currentLocalTime.split(" "); + var localTimeZone = localTimeZoneList[localTimeZoneList.length - 1]; + localTimeZone = localTimeZone.substring(1, localTimeZone.length - 1); + return localTimeZone; + }; + + var userLoginLocations = function () { + var loginLocations = localStorage.getItem("loginLocations"); + return loginLocations ? JSON.parse(loginLocations) : []; + }; + + var identifyLoginLocations = function (allLocations) { + var loginLocations = userLoginLocations(); + if (loginLocations.length === 0) { + return allLocations; + } + return loginLocations; + }; + + $scope.locations = identifyLoginLocations(initialData.locations); + + var findLanguageByLocale = function (localeCode) { + return _.find(localeLanguages, function (localeLanguage) { + return localeLanguage.code == localeCode; + }); + }; + + var logAuditForLoginAttempts = function (eventType, isFailedEvent) { + if ($scope.loginInfo.username) { + var messageParams = isFailedEvent ? {userName: $scope.loginInfo.username} : undefined; + auditLogService.log(undefined, eventType, messageParams, 'MODULE_LABEL_LOGIN_KEY'); + } + }; + + var promise = localeService.allowedLocalesList(); + localeService.serverDateTime().then(function (response) { + var serverTime = response.data.date; + var offset = response.data.offset; + var localTime = new Date().toLocaleString(); + var localtimeZone = getLocalTimeZone(); + var localeTimeZone = localTime + " " + localtimeZone; + $scope.timeZoneObject = { serverTime: serverTime, localeTimeZone: localeTimeZone}; + if (offset && !new Date().toString().includes(offset)) { + $scope.warning = "Warning"; + $scope.warningMessage = "WARNING_SERVER_TIME_ZONE_MISMATCH"; + } + }); + + localeService.getLoginText().then(function (response) { + $scope.logo = response.data.loginPage.logo; + $scope.bottomLogos = response.data.loginPage.bottomLogos; + $scope.headerText = response.data.loginPage.showHeaderText; + $scope.titleText = response.data.loginPage.showTitleText; + $scope.helpLink = response.data.helpLink.url; + }); + + localeService.getLocalesLangs().then(function (response) { + localeLanguages = response.data.locales; + }).finally(function () { + promise.then(function (response) { + var localeList = response.data.replace(/\s+/g, '').split(','); + $scope.locales = []; + _.forEach(localeList, function (locale) { + var localeLanguage = findLanguageByLocale(locale); + if (_.isUndefined(localeLanguage)) { + $scope.locales.push({"code": locale, "nativeName": locale}); + } else { + $scope.locales.push(localeLanguage); + } + }); + $scope.selectedLocale = $translate.use() ? $translate.use() : $scope.locales[0].code; + }); + }); + + var getLoginLocationUuid = function () { return $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName) ? $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName).uuid : null; }; + + var getLastLoggedinLocation = function () { + return _.find(initialData.locations, function (location) { + return location.uuid === getLoginLocationUuid(); + }); + }; + + $scope.loginInfo.currentLocation = getLastLoggedinLocation(); + + if ($stateParams.showLoginMessage) { + $scope.errorMessageTranslateKey = "LOGIN_LABEL_LOGIN_ERROR_MESSAGE_KEY"; + } + + var redirectToLandingPageIfAlreadyAuthenticated = function () { + sessionService.get().then(function (data) { + if (data.authenticated) { + $location.path(landingPagePath); + } + }); + }; + + if ($location.path() === loginPagePath) { + redirectToLandingPageIfAlreadyAuthenticated(); + } + var onSuccessfulAuthentication = function () { + $scope.loginInfo.currentLocation = getLastLoggedinLocation(); + $rootScope.$broadcast('event:auth-loggedin'); + }; + + $scope.updateSessionLocation = function () { + $scope.errorMessageTranslateKey = null; + var deferrable = $q.defer(); + + sessionService.updateSession($scope.loginInfo.currentLocation, null).then(function () { + sessionService.loadCredentials().then(function () { + onSuccessfulAuthentication(); + userService.savePreferences().then( + function () { deferrable.resolve(); }, + function (error) { deferrable.reject(error); } + ); + logAuditForLoginAttempts("USER_LOGIN_LOCATION_SUCCESS"); + }, function (error) { + $scope.errorMessageTranslateKey = error; + deferrable.reject(error); + logAuditForLoginAttempts("USER_LOGIN_LOCATION_FAILED", true); + }); + }); + + spinner.forPromise(deferrable.promise).then( + function (data) { + if (data) return; + $location.url(landingPagePath); + } + ); + }; + }]); diff --git a/ui/app/home/index.html b/ui/app/home/index.html index 69def74123..8f3f372cb2 100644 --- a/ui/app/home/index.html +++ b/ui/app/home/index.html @@ -99,6 +99,7 @@ + diff --git a/ui/app/home/views/login.html b/ui/app/home/views/login.html index dba56c3751..09dd7ab299 100644 --- a/ui/app/home/views/login.html +++ b/ui/app/home/views/login.html @@ -53,20 +53,6 @@ -
-
- -
-
- -
-
- diff --git a/ui/app/home/views/loginLocation.html b/ui/app/home/views/loginLocation.html new file mode 100644 index 0000000000..f50eb55dc4 --- /dev/null +++ b/ui/app/home/views/loginLocation.html @@ -0,0 +1,48 @@ +
+ + {{'LOGIN_LOCATION_PAGE_TITLE_TEXT' | translate }} +
+ +
+
+
{{'LOGIN_LOCATION_PAGE_TITLE_TEXT' | translate}}
+
+
+ + +
+ +
+ {{warning}} +

{{warningMessage | translate :timeZoneObject}}

+
+ +
+ +
+ +
+ + +
+ Bahmni + Bahmni Help +
diff --git a/ui/app/i18n/home/locale_en.json b/ui/app/i18n/home/locale_en.json index c243d5c3bd..6ecb01b652 100644 --- a/ui/app/i18n/home/locale_en.json +++ b/ui/app/i18n/home/locale_en.json @@ -2,6 +2,7 @@ "BAHMNI_PAGE_TITLE_KEY": "Bahmni Home", "LOGIN_LABEL_LOGIN_KEY": "Login", "LOGIN_LABEL_SUBMIT_KEY": "Submit", + "LOGIN_LOCATION_SELECTION_LABEL_KEY": "Submit Location", "LOGIN_LABEL_ENTER_OTP_KEY": "Enter OTP", "LOGIN_LABEL_USERNAME_KEY": "Username", "LOGIN_LABEL_PASSWORD_KEY": "Password", @@ -47,6 +48,7 @@ "WARNING_SERVER_TIME_ZONE_MISMATCH": "The server timezone ({{serverTime}}) is different from your browser's time/timezone ({{localeTimeZone}}). This can cause some data or reports to appear incorrect. Please contact the administrator for further assistance.", "LOGIN_PAGE_HEADER_TEXT": "BAHMNI EMR LOGIN", "LOGIN_PAGE_TITLE_TEXT": "TITLE TEXT", + "LOGIN_LOCATION_PAGE_TITLE_TEXT": "Select login location", "LOGIN_UNSUPPORTED_BROWSER_KEY" : "Bahmni is supported on Chrome/Firefox Browser", "DOB_LABEL": "Date of Birth", "NO_RESULTS_FOUND": "No results found", diff --git a/ui/app/i18n/home/locale_es.json b/ui/app/i18n/home/locale_es.json index b03e8962ac..34543ef4d8 100644 --- a/ui/app/i18n/home/locale_es.json +++ b/ui/app/i18n/home/locale_es.json @@ -2,6 +2,7 @@ "BAHMNI_PAGE_TITLE_KEY": "Inicio Bahmni", "LOGIN_LABEL_LOGIN_KEY": "Iniciar sesión", "LOGIN_LABEL_SUBMIT_KEY": "Enviar", + "LOGIN_LOCATION_SELECTION_LABEL_KEY": "Submit Location es", "LOGIN_LABEL_ENTER_OTP_KEY": "Ingresar OTP", "LOGIN_LABEL_USERNAME_KEY": "Nombre de usuario", "LOGIN_LABEL_PASSWORD_KEY": "Contraseña", @@ -54,5 +55,6 @@ "NO_FULFILMENT_MESSAGE": "No se han captado observaciones para esta orden.", "NO_NAVIGATION_LINKS_AVAILABLE_MESSAGE": "No hay enlaces de navegación disponibles.", "ACCEPT": "Aceptar", - "SELECT_VALUE_FROM_AUTOCOMPLETE_DEFAULT_MESSAGE": "Por favor seleccione un valor de la lista de autocompletar" -} \ No newline at end of file + "SELECT_VALUE_FROM_AUTOCOMPLETE_DEFAULT_MESSAGE": "Por favor seleccione un valor de la lista de autocompletar", + "LOGIN_LOCATION_PAGE_TITLE_TEXT": "Select login location es" +} diff --git a/ui/app/i18n/home/locale_fr.json b/ui/app/i18n/home/locale_fr.json index 512c93747a..0f3d23fe13 100644 --- a/ui/app/i18n/home/locale_fr.json +++ b/ui/app/i18n/home/locale_fr.json @@ -2,6 +2,7 @@ "BAHMNI_PAGE_TITLE_KEY": "Accueil Bahmni", "LOGIN_LABEL_LOGIN_KEY": "S'identifier", "LOGIN_LABEL_SUBMIT_KEY": "Envoyer", + "LOGIN_LOCATION_SELECTION_LABEL_KEY": "Soumettre l'emplacement", "LOGIN_LABEL_ENTER_OTP_KEY": "Entrez OTP", "LOGIN_LABEL_USERNAME_KEY": "nom d'utilisateur", "LOGIN_LABEL_PASSWORD_KEY": "Mot de passe", @@ -54,5 +55,6 @@ "NO_FULFILMENT_MESSAGE": "Aucune observation capturée pour cette commande.", "NO_NAVIGATION_LINKS_AVAILABLE_MESSAGE": "Aucun lien de navigation disponible.", "ACCEPT": "Accepter", - "SELECT_VALUE_FROM_AUTOCOMPLETE_DEFAULT_MESSAGE": "Veuillez sélectionner une valeur de auto complète" + "SELECT_VALUE_FROM_AUTOCOMPLETE_DEFAULT_MESSAGE": "Veuillez sélectionner une valeur de auto complète", + "LOGIN_LOCATION_PAGE_TITLE_TEXT": "Sélectionnez l'emplacement de connexion" } \ No newline at end of file diff --git a/ui/app/i18n/home/locale_pt_BR.json b/ui/app/i18n/home/locale_pt_BR.json index fbee209042..0d5b745104 100644 --- a/ui/app/i18n/home/locale_pt_BR.json +++ b/ui/app/i18n/home/locale_pt_BR.json @@ -2,6 +2,7 @@ "BAHMNI_PAGE_TITLE_KEY": "Início do Bahmni", "LOGIN_LABEL_LOGIN_KEY": "Entrar", "LOGIN_LABEL_SUBMIT_KEY": "Enviar", + "LOGIN_LOCATION_SELECTION_LABEL_KEY": "Submit Location br", "LOGIN_LABEL_ENTER_OTP_KEY": "Inserir OTP", "LOGIN_LABEL_USERNAME_KEY": "Nome de Usuário", "LOGIN_LABEL_PASSWORD_KEY": "Senha", @@ -54,5 +55,6 @@ "NO_FULFILMENT_MESSAGE": "No observations captured for this order.", "NO_NAVIGATION_LINKS_AVAILABLE_MESSAGE": "No navigation links available.", "ACCEPT": "Accept", - "SELECT_VALUE_FROM_AUTOCOMPLETE_DEFAULT_MESSAGE": "Please select a value from auto complete" + "SELECT_VALUE_FROM_AUTOCOMPLETE_DEFAULT_MESSAGE": "Please select a value from auto complete", + "LOGIN_LOCATION_PAGE_TITLE_TEXT": "Select login location br" } \ No newline at end of file diff --git a/ui/test/unit/home/controllers/loginLocationController.spec.js b/ui/test/unit/home/controllers/loginLocationController.spec.js new file mode 100644 index 0000000000..bbf781bc6e --- /dev/null +++ b/ui/test/unit/home/controllers/loginLocationController.spec.js @@ -0,0 +1,149 @@ +'use strict'; + +describe("LoginLocationController", function () { + var translate, + userService, + localeService, + $controller, + rootScopeMock, + window, + $q, + state, + _spinner, + initialData, + scopeMock, + sessionService, + $bahmniCookieStore, + currentUser, + mockLocation, + auditLogService; + var mockLocation = { path: jasmine.createSpy(), url: jasmine.createSpy(), search: jasmine.createSpy().and.returnValue({}) }; + + + beforeEach(module("bahmni.home")); + + var myUser = { + provider: { + attributes: [ + { + attributeType: { + display: "some attribute", + }, + }, + ], + }, + }; + + beforeEach(inject(function ($rootScope, _$controller_, $window, $state, _$q_) { + $controller = _$controller_; + rootScopeMock = $rootScope; + window = $window; + $q = _$q_; + state = $state; + scopeMock = rootScopeMock.$new(); + })); + + var loginLocationController = function () { + return $controller("LoginLocationController", { + $rootScope: rootScopeMock, + $scope: scopeMock, + $window: window, + $location: mockLocation, + $translate: translate, + userService: userService, + sessionService: sessionService, + initialData: initialData, + spinner: _spinner, + $q: $q, + $stateParams: {}, + $bahmniCookieStore: $bahmniCookieStore, + localeService: localeService, + auditLogService: auditLogService, + }); + }; + + beforeEach(function () { + translate = jasmine.createSpyObj("$translate", ["use"]); + userService = jasmine.createSpyObj("userService", ["savePreferences"]); + localeService = jasmine.createSpyObj("localeService", ["getLoginText", "allowedLocalesList", "serverDateTime", "getLocalesLangs"]); + sessionService = jasmine.createSpyObj("sessionService", ["loginUser", "loadCredentials", "updateSession", "get"]); + auditLogService = jasmine.createSpyObj("auditLogService", ["log"]); + currentUser = jasmine.createSpyObj("currentUser", ["addDefaultLocale", "toContract"]); + _spinner = jasmine.createSpyObj("spinner", ["forPromise"]); + + sessionService.loginUser.and.returnValue(specUtil.simplePromise()); + sessionService.loadCredentials.and.returnValue(specUtil.simplePromise()); + sessionService.updateSession.and.returnValue(specUtil.simplePromise()); + sessionService.get.and.returnValue(specUtil.simplePromise({ authenticated: true })); + currentUser.addDefaultLocale.and.returnValue(specUtil.simplePromise({ data: "" })); + currentUser.toContract.and.returnValue(specUtil.simplePromise({ data: "" })); + localeService.allowedLocalesList.and.returnValue(specUtil.simplePromise({ data: "" })); + localeService.serverDateTime.and.returnValue(specUtil.simplePromise({ data: { date: "Wed Aug 16 15:31:55 NZST 2017", offset: "+1200" } })); + localeService.getLoginText.and.returnValue( + specUtil.simplePromise({ + data: { homePage: { logo: "bahmni logo" }, loginPage: { showHeaderText: "bahmni emr", logo: "bahmni logo" }, helpLink: { url: "192.168.33.10/homepage" } }, + }) + ); + localeService.getLocalesLangs.and.returnValue( + specUtil.createFakePromise({ + locales: [ + { code: "en", nativeName: "English" }, + { code: "es", nativeName: "Español" }, + ], + }) + ); + $bahmniCookieStore = jasmine.createSpyObj("$bahmniCookieStore", ["get", "remove", "put"]); + $bahmniCookieStore.get.and.callFake(function () { + return {}; + }); + initialData = { + locations: [ + { display: "Location1", uuid: "uuid1" }, + { display: "Location2", uuid: "uuid2" }, + { display: "Location3", uuid: "uuid3" }, + ], + }; + rootScopeMock.currentUser = myUser; + scopeMock.loginInfo = {}; + _spinner.forPromise.and.returnValue(specUtil.simplePromise({})); + localStorage.setItem('loginLocations', JSON.stringify([ + { display: 'Location 1', uuid: 'uuid1' }, + { display: 'Location 2', uuid: 'uuid2' } + ])); + }); + + afterEach(function () { + rootScopeMock.currentUser = null; + }); + + it("should return all locations if no login locations for the user exist", function () { + localStorage.clear(); + loginLocationController(); + + var allLocations = initialData.locations; + + expect(scopeMock.locations).toEqual(allLocations); + }); + + it("should return login locations of the user if they exist", function () { + loginLocationController(); + + var expectedLocations = [{ display: 'Location 1', uuid: 'uuid1' },{ display: 'Location 2', uuid: 'uuid2' }]; + + expect(scopeMock.locations).toEqual(expectedLocations); + }); + + it("should update the session location based on user selected location", function () { + loginLocationController(); + + var selectedLocation = { display: "Location1", uuid: "uuid1" }; + scopeMock.locations = selectedLocation; + scopeMock.loginInfo.currentLocation = { display: "Location1", uuid: "uuid1" }; + + spyOn(scopeMock, "updateSessionLocation").and.returnValue($q.when({})); + + scopeMock.updateSessionLocation(selectedLocation); + + expect(scopeMock.loginInfo.currentLocation).toEqual(selectedLocation); + }); +});