Skip to content

Commit

Permalink
Merge pull request #18 from OpenLMIS/SELV3-784
Browse files Browse the repository at this point in the history
SELV-784: Login to superset on login to app
  • Loading branch information
saleksandra authored Jan 7, 2025
2 parents 98297a4 + 2353c16 commit fd371f1
Show file tree
Hide file tree
Showing 16 changed files with 1,159 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/openlmis-login/login-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
.controller('LoginController', LoginController);

LoginController.$inject = [
'loginService', 'modalDeferred', 'loadingModalService', '$rootScope'
'loginService', 'modalDeferred', 'loadingModalService', '$rootScope', 'supersetOAuthService'
];

function LoginController(loginService, modalDeferred, loadingModalService, $rootScope) {
function LoginController(loginService, modalDeferred, loadingModalService, $rootScope, supersetOAuthService) {

var vm = this;

Expand All @@ -53,6 +53,7 @@
.then(function() {
$rootScope.$emit('openlmis-auth.login');
modalDeferred.resolve();
loginToSuperset();
})
.catch(function(error) {
vm.loginError = error;
Expand All @@ -61,5 +62,18 @@
.finally(loadingModalService.close);
}

function loginToSuperset() {
supersetOAuthService.checkAuthorizationInSuperset()
.then(function(data) {
vm.supersetOAuthState = data.state;
if (data.isAuthorized === false) {
supersetOAuthService.authorizeInSuperset(vm.username, vm.password, vm.supersetOAuthState)
.then(function() {
$rootScope.$emit('openlmis-auth.authorized-in-superset');
});
}
});
}

}
}());
58 changes: 58 additions & 0 deletions src/openlmis-login/login-controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,33 @@ describe('LoginController', function() {
return {};
});
});
module('openlmis-superset');

inject(function($injector) {
this.$q = $injector.get('$q');
this.$rootScope = $injector.get('$rootScope');
this.$controller = $injector.get('$controller');
this.loginService = $injector.get('loginService');
this.loadingModalService = $injector.get('loadingModalService');
this.supersetOAuthService = $injector.get('supersetOAuthService');

spyOn(this.supersetOAuthService, 'checkAuthorizationInSuperset')
.andReturn(this.$q.resolve(this.isAuthorizedResponse));
spyOn(this.supersetOAuthService, 'authorizeInSuperset').andReturn(this.$q.resolve());
});

this.modalDeferred = this.$q.defer();

this.vm = this.$controller('LoginController', {
modalDeferred: this.modalDeferred
});
this.isAuthorizedResponse = {
isAuthorized: true
};
this.isNotAuthorizedResponse = {
isAuthorized: false,
state: 'test_state'
};
});

describe('doLogin', function() {
Expand Down Expand Up @@ -174,5 +187,50 @@ describe('LoginController', function() {

expect(success).toBe(true);
});

it('should check Superset authorization and not send authorize request after successful login', function() {
this.loginService.login.andReturn(this.$q.resolve());
this.supersetOAuthService.checkAuthorizationInSuperset
.andReturn(this.$q.resolve(this.isAuthorizedResponse));

this.vm.doLogin();
this.$rootScope.$apply();

expect(this.supersetOAuthService.checkAuthorizationInSuperset).toHaveBeenCalled();
expect(this.supersetOAuthService.authorizeInSuperset).not.toHaveBeenCalled();
});

it('should check Superset authorization and send authorize request after successful login', function() {
var success = false;
this.$rootScope.$on('openlmis-auth.authorized-in-superset', function() {
success = true;
});

this.loginService.login.andReturn(this.$q.resolve());
this.supersetOAuthService.checkAuthorizationInSuperset
.andReturn(this.$q.resolve(this.isNotAuthorizedResponse));

this.vm.doLogin();
this.$rootScope.$apply();

expect(this.supersetOAuthService.checkAuthorizationInSuperset).toHaveBeenCalled();
expect(this.supersetOAuthService.authorizeInSuperset).toHaveBeenCalled();
expect(success).toBe(true);
});

it('should not check authorization in Superset after failed login', function() {
var success = false;
this.$rootScope.$on('openlmis-auth.authorized-in-superset', function() {
success = true;
});
this.loginService.login.andReturn(this.$q.reject());

this.vm.doLogin();
this.$rootScope.$apply();

expect(this.supersetOAuthService.checkAuthorizationInSuperset).not.toHaveBeenCalled();
expect(this.supersetOAuthService.authorizeInSuperset).not.toHaveBeenCalled();
expect(success).toBe(false);
});
});
});
3 changes: 2 additions & 1 deletion src/openlmis-login/openlmis-login.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
'openlmis-offline',
'openlmis-locale',
'openlmis-modal',
'ui.router'
'ui.router',
'openlmis-superset'
]);

})();
11 changes: 11 additions & 0 deletions src/openlmis-superset/messages_en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"superset.auth.pageRequiresAuthorization": "This page requires authorization in the reporting stack. If nothing shows up, you still can authorize manually by",
"superset.auth.clickingHere": "clicking here",
"superset.oAuthLogin.header": "Allow Superset to get access to OpenLMIS",
"superset.oAuthLogin.authorize": "Authorize",
"superset.oAuthLogin.cancel": "Cancel",
"superset.oAuthLogin.username": "Username",
"superset.oAuthLogin.password": "Password",
"superset.oAuthLogin.invalidCredentialsOrOAuthRequest": "Invalid credentials or unsuccessful OAuth request",
"superset.oAuthLogin.unsuccessfulApprovingPermissions": "Unsuccessful approving permissions for Superset"
}
30 changes: 30 additions & 0 deletions src/openlmis-superset/modal-cancelled.constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
*/

(function() {
'use strict';

/**
* @ngdoc object
* @name openlmis-superset.MODAL_CANCELLED
*
* @description
* This is the constant describing the rejection of promise in case of cancellation of a modal.
*/
angular
.module('openlmis-superset')
.constant('MODAL_CANCELLED', 'MODAL_CANCELLED');

})();
61 changes: 61 additions & 0 deletions src/openlmis-superset/superset-locale.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
*/

(function() {
'use strict';

/**
* @ngdoc service
* @name superset:supersetLocaleService
* @description
* The service that allows modifing Superset locales.
*/
angular
.module('openlmis-superset')
.service('supersetLocaleService', supersetLocaleService);

supersetLocaleService.$inject = ['SUPERSET_URL', 'SUPERSET_LOCALES', 'DEFAULT_LANGUAGE', '$http'];

function supersetLocaleService(SUPERSET_URL, SUPERSET_LOCALES, DEFAULT_LANGUAGE, $http) {

this.changeLocale = changeLocale;

/**
* @ngdoc method
* @methodOf superset:supersetLocaleService
* @name changeLocale
*
* @description
* The method that changes Superset locales.
*
* @param {String} locale (optional) locale to populate
* @return {Promise} The promise of Superset locale change request.
*/
function changeLocale(locale) {
return $http({
method: 'GET',
url: SUPERSET_URL + '/lang/change/' + getValidLocale(locale),
withCredentials: true
});
}

function getValidLocale(locale) {
if (SUPERSET_LOCALES.indexOf(locale) === -1) {
locale = DEFAULT_LANGUAGE;
}
return locale;
}
}
}());
58 changes: 58 additions & 0 deletions src/openlmis-superset/superset-locale.service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
*/

describe('supersetLocaleService', function() {

var that = this;

beforeEach(function() {
that.SUPERSET_URL = 'http://localhost/superset';
that.DEFAULT_LANGUAGE = 'en';

module('openlmis-superset', function($provide) {
$provide.constant('SUPERSET_URL', that.SUPERSET_URL);
$provide.constant('DEFAULT_LANGUAGE', that.DEFAULT_LANGUAGE);
});

inject(function($injector) {
that.supersetLocaleService = $injector.get('supersetLocaleService');
that.$httpBackend = $injector.get('$httpBackend');
});

});

describe('changeLocale', function() {

// eslint-disable-next-line jasmine/missing-expect
it('should send change language request', function() {
that.$httpBackend.expectGET(that.SUPERSET_URL + '/lang/change/' + that.DEFAULT_LANGUAGE);

that.supersetLocaleService.changeLocale(that.DEFAULT_LANGUAGE);
});

// eslint-disable-next-line jasmine/missing-expect
it('should change the language to default if a not known locale provided', function() {
that.$httpBackend.expectGET(that.SUPERSET_URL + '/lang/change/' + that.DEFAULT_LANGUAGE);

that.supersetLocaleService.changeLocale('not_known_locale');
});

afterEach(function() {
that.$httpBackend.verifyNoOutstandingExpectation();
that.$httpBackend.verifyNoOutstandingRequest();
});
});

});
33 changes: 33 additions & 0 deletions src/openlmis-superset/superset-locales.constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
*/

(function() {
'use strict';

/**
* @ngdoc object
* @name superset.SUPERSET_LOCALES
*
* @description
* This is the constant defining a list of locales available in Superset.
*/
angular
.module('openlmis-superset')
.constant('SUPERSET_LOCALES', locales());

function locales() {
return ['en', 'fr', 'pt'];
}
})();
Loading

0 comments on commit fd371f1

Please sign in to comment.