Skip to content

Commit

Permalink
fix: tests - user setting value persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
jbee committed Sep 26, 2024
1 parent e9babff commit ced0a75
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.hisp.dhis.i18n.I18nManager;
import org.hisp.dhis.i18n.locale.LocaleManager;
import org.hisp.dhis.setting.SystemSettingsProvider;
import org.hisp.dhis.setting.UserSettings;
import org.hisp.dhis.system.velocity.VelocityManager;
import org.hisp.dhis.user.CurrentUserUtil;
import org.hisp.dhis.user.SystemUser;
Expand Down Expand Up @@ -501,12 +502,12 @@ private String getMessageFooter(MessageConversation conversation) {
return StringUtils.EMPTY;
}

UserDetails userDetails =
UserSettings settings =
conversation.getCreatedBy() == null
? CurrentUserUtil.getCurrentUserDetails()
: userService.createUserDetails(conversation.getCreatedBy());
? UserSettings.getCurrentSettings()
: userService.createUserDetails(conversation.getCreatedBy()).getUserSettings();

Locale locale = userDetails.getUserSettings().getUserUiLocale();
Locale locale = settings.getUserUiLocale();

locale = ObjectUtils.firstNonNull(locale, LocaleManager.DEFAULT_LOCALE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,13 @@ private Map<String, Optional<Locale>> toLocaleMap(String hql, Date from, Date to
.collect(
toMap(
(Object[] columns) -> (String) columns[0],
(Object[] columns) -> Optional.ofNullable((Locale) columns[1])));
(Object[] columns) -> Optional.ofNullable(toLocale(columns[1]))));
}

private static Locale toLocale(Object value) {
if (value == null) return null;
if (value instanceof Locale l) return l;
return Locale.forLanguageTag(value.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ class EventAnalyticsServiceTest extends PostgresIntegrationTestBase {

@Autowired private EnrollmentQueryService enrollmentQueryTarget;

@Autowired private EnrollmentAggregateService enrollmentAggregateService;

@Autowired private List<AnalyticsTableService> analyticsTableServices;

@Autowired private DataElementService dataElementService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ void setUp() {
userB = makeUser("B");
userService.addUser(userA);
userService.addUser(userB);

settingsService.saveSystemSetting("keyAcceptanceRequiredForApproval", false);
settingsService.clearCurrentSettings();
}

@AfterEach
Expand Down Expand Up @@ -3572,21 +3575,17 @@ void testGetDataApprovalStatuses() {
// -------------------------------------------------------------------------
@Test
void testApprovalsWithCategories() {
settingsService.saveSystemSetting("keyAcceptanceRequiredForApproval", true);
settingsService.clearCurrentSettings();
Date date = new Date();
transactionTemplate.execute(
status -> {
settingsService.saveSystemSetting("keyAcceptanceRequiredForApproval", true);
setUpCategories();
createUserAndInjectSecurityContext(
singleton(organisationUnitA),
false,
DataApproval.AUTH_APPROVE,
DataApproval.AUTH_APPROVE_LOWER_LEVELS,
DataApproval.AUTH_ACCEPT_LOWER_LEVELS,
AUTH_APPR_LEVEL);
dbmsManager.flushSession();
return null;
});
setUpCategories();
createUserAndInjectSecurityContext(
singleton(organisationUnitA),
false,
DataApproval.AUTH_APPROVE,
DataApproval.AUTH_APPROVE_LOWER_LEVELS,
DataApproval.AUTH_ACCEPT_LOWER_LEVELS,
AUTH_APPR_LEVEL);
// Category A -> Options A,B,C,D
// Category B -> Options E,F,G,H
//
Expand Down Expand Up @@ -4106,15 +4105,11 @@ void testGetApprovedByOfAcceptedHere() {
NOT_ACCEPTED,
date,
userA);
transactionTemplate.execute(
status -> {
settingsService.saveSystemSetting("keyAcceptanceRequiredForApproval", true);
createUserAndInjectSecurityContext(singleton(organisationUnitA), false, AUTH_APPR_LEVEL);
dataApprovalStore.addDataApproval(dataApprovalA);
dataApprovalStore.addDataApproval(dataApprovalB);
dbmsManager.flushSession();
return null;
});
settingsService.saveSystemSetting("keyAcceptanceRequiredForApproval", true);
settingsService.clearCurrentSettings();
createUserAndInjectSecurityContext(singleton(organisationUnitA), false, AUTH_APPR_LEVEL);
dataApprovalStore.addDataApproval(dataApprovalA);
dataApprovalStore.addDataApproval(dataApprovalB);
DataApprovalStatus status =
dataApprovalService.getDataApprovalStatus(
workflow1234, periodA, organisationUnitA, defaultOptionCombo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ void getFavoriteStatisticsTest() {
dataStatisticsService.getFavoriteStatistics(DASHBOARD_UID);

settingsService.saveSystemSetting("keyCountPassiveDashboardViewsInUsageAnalytics", true);
settingsService.clearCurrentSettings();
final FavoriteStatistics activePlusPassiveDashboardStats =
dataStatisticsService.getFavoriteStatistics(DASHBOARD_UID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void setup() {
entry("keyRemoteInstanceUsername", "username"),
entry("keyRemoteInstancePassword", "password"),
entry("keyStopMetadataSync", "true")));
settingsService.clearCurrentSettings();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.hisp.dhis.metadata.version.MetadataVersion;
import org.hisp.dhis.metadata.version.MetadataVersionService;
import org.hisp.dhis.metadata.version.VersionType;
import org.hisp.dhis.setting.SystemSettingsService;
import org.hisp.dhis.test.integration.PostgresIntegrationTestBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -67,6 +68,7 @@ class DefaultMetadataVersionServiceTest extends PostgresIntegrationTestBase {
@Autowired private IdentifiableObjectManager manager;

@Autowired private MetadataSystemSettingService metadataSystemSettingService;
@Autowired private SystemSettingsService systemSettingsService;

@Autowired private DbmsManager dbmsManager;

Expand Down Expand Up @@ -184,6 +186,7 @@ void testShouldSaveVersionAndSnapShot() {
assertEquals("Version_2", versionService.getCurrentVersion().getName());
assertEquals(VersionType.ATOMIC, versionService.getCurrentVersion().getType());

systemSettingsService.clearCurrentSettings();
// testing if correct version name is saved in system setting
assertEquals("Version_2", metadataSystemSettingService.getSystemMetadataVersion());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void getRemoteInstanceTest() {
entry("keyRemoteInstanceUsername", USERNAME),
entry("keyRemoteInstancePassword", PASSWORD),
entry("keyRemoteInstanceUrl", URL)));
settingsService.clearCurrentSettings();
SystemInstance systemInstance =
SyncUtils.getRemoteInstance(
settingsService.getCurrentSettings(), SyncEndpoint.DATA_VALUE_SETS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void setUp() {
@Test
void testUserAuthenticationLockout() {
settingsService.saveSystemSetting("keyLockMultipleFailedLogins", true);
settingsService.clearCurrentSettings();
String username = "dr_evil";
userService.registerFailedLogin(username);
assertFalse(userService.isLocked(username));
Expand All @@ -83,6 +84,7 @@ void testUserAuthenticationLockout() {
@Test
void testRecoveryAttemptLocked() {
settingsService.saveSystemSetting("keyLockMultipleFailedLogins", true);
settingsService.clearCurrentSettings();
String username = "dr_evil";
userService.registerRecoveryAttempt(username);
assertFalse(userService.isRecoveryLocked(username));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() th
throws IOException {
assignProgramRule();
settingsService.saveSystemSetting("ruleEngineAssignOverwrite", true);
settingsService.clearCurrentSettings();
TrackerImportParams params = new TrackerImportParams();
TrackerObjects trackerObjects =
fromJson("tracker/programrule/event_update_datavalue_different_value.json");
Expand All @@ -215,6 +216,7 @@ void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() th
throws IOException {
assignProgramRule();
settingsService.saveSystemSetting("ruleEngineAssignOverwrite", true);
settingsService.clearCurrentSettings();
TrackerImportParams params = new TrackerImportParams();
TrackerObjects trackerObjects =
fromJson("tracker/programrule/event_update_datavalue_empty_value.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

/**
* @author Lars Helge Overland
Expand All @@ -88,6 +89,8 @@ class UserServiceTest extends PostgresIntegrationTestBase {

@Autowired private PasswordManager passwordManager;

@Autowired private TransactionTemplate transactionTemplate;

private OrganisationUnit unitA;

private OrganisationUnit unitB;
Expand Down Expand Up @@ -335,6 +338,7 @@ void testGetUserOrgUnits() {
@Test
void testManagedGroups() {
settingsService.saveSystemSetting("keyCanGrantOwnUserAuthorityGroups", true);
settingsService.clearCurrentSettings();
// TODO find way to override in parameters
User userA = addUser("A");
User userB = addUser("B");
Expand Down Expand Up @@ -379,6 +383,7 @@ void testManagedGroups() {
@Test
void testGetByPhoneNumber() {
settingsService.saveSystemSetting("keyCanGrantOwnUserAuthorityGroups", true);
settingsService.clearCurrentSettings();
addUser("A", user -> user.setPhoneNumber("73647271"));
User userB = addUser("B", user -> user.setPhoneNumber("23452134"));
addUser("C", user -> user.setPhoneNumber("14543232"));
Expand Down Expand Up @@ -455,6 +460,9 @@ void testGetOrdered() {

@Test
void testGetManagedGroupsLessAuthoritiesDisjointRoles() {
settingsService.saveSystemSetting("keyCanGrantOwnUserAuthorityGroups", false);
settingsService.clearCurrentSettings();

User userA = addUser("A", roleA);
User userB = addUser("B", roleB, roleC);
User userC = addUser("C", roleA, roleC);
Expand All @@ -475,7 +483,9 @@ void testGetManagedGroupsLessAuthoritiesDisjointRoles() {
userGroupService.addUserGroup(userGroup2);
UserQueryParams params =
new UserQueryParams().setCanManage(true).setAuthSubset(true).setUser(userA);
assertContainsOnly(List.of(userD, userF), userService.getUsers(params));
assertContainsOnly(
List.of(userD.getUsername(), userF.getUsername()),
userService.getUsers(params).stream().map(User::getUsername).toList());
assertEquals(2, userService.getUserCount(params));
params.setUser(userB);
assertIsEmpty(userService.getUsers(params));
Expand Down Expand Up @@ -611,15 +621,10 @@ void testFindNotifiableUsersWithLastLoginBetween() throws Exception {
});
addUser("C", User::setLastLogin, twentyTwoDaysAgo);
addUser("D");
userSettingService.saveUserSetting("keyUiLocale", Locale.CANADA, userA.getUsername());
// the point of setting this setting is to see that the query does not
// get confused by other setting existing for the same user
userSettingService.saveUserSetting("keyDbLocale", Locale.FRANCE, userA.getUsername());

Map<String, Optional<Locale>> users =
userService.findNotifiableUsersWithLastLoginBetween(threeMonthAgo, twoMonthsAgo);
assertEquals(Set.of("emaila"), users.keySet());
assertEquals(Locale.CANADA, users.values().iterator().next().orElse(null));
assertEquals(
Set.of("emaila"),
userService.findNotifiableUsersWithLastLoginBetween(fourMonthAgo, oneMonthsAgo).keySet());
Expand Down

0 comments on commit ced0a75

Please sign in to comment.