From df7e1f5b7b1e24431fa6e1a1c12ecd3e4cb5f065 Mon Sep 17 00:00:00 2001 From: David Mackessy <131455290+david-mackessy@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:08:19 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20System=20settings=20endpoint=20returns?= =?UTF-8?q?=20expected=20Cache-Control=20header=20v=E2=80=A6=20(#15888)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: System settings endpoint returns expected Cache-Control header value [DHIS2-15196] * fix: Rename method to match Spring convention [DHIS2-15196] --- .../systemsettings/SystemSettingsTests.java | 37 ++++++++++++------- .../controller/SystemSettingController.java | 8 ++-- .../hisp/dhis/webapi/utils/ContextUtils.java | 6 +++ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/systemsettings/SystemSettingsTests.java b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/systemsettings/SystemSettingsTests.java index e7e8841327d2..f041c8f7327b 100644 --- a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/systemsettings/SystemSettingsTests.java +++ b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/systemsettings/SystemSettingsTests.java @@ -33,6 +33,7 @@ import com.google.gson.JsonObject; import io.restassured.http.ContentType; +import io.restassured.response.ValidatableResponse; import org.apache.commons.lang3.StringUtils; import org.hisp.dhis.ApiTest; import org.hisp.dhis.actions.LoginActions; @@ -41,12 +42,13 @@ import org.hisp.dhis.helpers.QueryParamsBuilder; import org.hisp.dhis.helpers.TestCleanUp; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; /** * @author David Katuscak */ -public class SystemSettingsTests extends ApiTest { +class SystemSettingsTests extends ApiTest { private static final String APPLICATION_INTRO_KEY = "keyApplicationIntro"; private static final String APPLICATION_FOOTER_KEY = "keyApplicationFooter"; @@ -69,8 +71,6 @@ public class SystemSettingsTests extends ApiTest { private static final String FRENCH_INTRO = "French - Welcome to the DHIS2"; - private LoginActions loginActions; - private SystemSettingActions systemSettingActions; @BeforeEach @@ -79,7 +79,7 @@ public void setUp() { systemSettingActions = new SystemSettingActions(); - loginActions = new LoginActions(); + LoginActions loginActions = new LoginActions(); loginActions.loginAsDefaultUser(); } @@ -115,7 +115,7 @@ private void prepareData() { } @Test - public void addSystemSetting() { + void addSystemSetting() { String specificFooter = "Learn more at "; QueryParamsBuilder params = new QueryParamsBuilder(); @@ -129,7 +129,7 @@ public void addSystemSetting() { } @Test - public void returnDefaultValueWhenTranslationIsNotAvailable() { + void returnDefaultValueWhenTranslationIsNotAvailable() { prepareData(); ApiResponse response = @@ -143,7 +143,7 @@ public void returnDefaultValueWhenTranslationIsNotAvailable() { } @Test - public void returnTranslationForUsersLocale() { + void returnTranslationForUsersLocale() { prepareData(); ApiResponse response = @@ -157,7 +157,7 @@ public void returnTranslationForUsersLocale() { } @Test - public void returnTranslationForGivenLocale() { + void returnTranslationForGivenLocale() { prepareData(); ApiResponse response = @@ -171,7 +171,7 @@ public void returnTranslationForGivenLocale() { } @Test - public void returnAllSystemSettings() { + void returnAllSystemSettings() { prepareData(); ApiResponse response = systemSettingActions.get(); @@ -184,7 +184,7 @@ public void returnAllSystemSettings() { } @Test - public void deleteTranslationForGivenLocaleAndSettingKey() { + void deleteTranslationForGivenLocaleAndSettingKey() { prepareData(); ApiResponse response = @@ -204,7 +204,7 @@ public void deleteTranslationForGivenLocaleAndSettingKey() { } @Test - public void deleteSystemSetting() { + void deleteSystemSetting() { prepareData(); ApiResponse response = systemSettingActions.delete(APPLICATION_INTRO_KEY); @@ -224,7 +224,7 @@ public void deleteSystemSetting() { } @Test - public void getDefaultSystemSettingAsText() { + void getDefaultSystemSettingAsText() { ApiResponse response = systemSettingActions.get( MAX_SYNC_ATTEMPTS_KEY, @@ -260,7 +260,7 @@ public void getDefaultSystemSettingAsText() { } @Test - public void getDefaultSystemSettingAsJson() { + void getDefaultSystemSettingAsJson() { ApiResponse response = systemSettingActions.get( MAX_SYNC_ATTEMPTS_KEY, @@ -296,7 +296,7 @@ public void getDefaultSystemSettingAsJson() { } @Test - public void getDefaultSystemSettingWithNonSpecifiedContentTypeAndAccept() { + void getDefaultSystemSettingWithNonSpecifiedContentTypeAndAccept() { ApiResponse response = systemSettingActions.get(MAX_SYNC_ATTEMPTS_KEY, "", "", new QueryParamsBuilder()); @@ -316,4 +316,13 @@ public void getDefaultSystemSettingWithNonSpecifiedContentTypeAndAccept() { // ----------------------------------------- response = systemSettingActions.get(EMAIL_SENDER_KEY, "", "", new QueryParamsBuilder()); } + + @Test + @DisplayName( + "A call to the system settings endpoint returns expected cache control header values") + void getSystemSettingsCheckCacheControl() { + ValidatableResponse validate = systemSettingActions.get().validate(); + validate.statusCode(200); + validate.header("Cache-Control", "no-cache, no-store, must-revalidate"); + } } diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java index e1d67a45df88..691c3d25b6b4 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java @@ -29,6 +29,7 @@ import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict; import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok; +import static org.springframework.http.CacheControl.noCache; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import java.io.Serializable; @@ -59,7 +60,6 @@ import org.hisp.dhis.util.ObjectUtils; import org.hisp.dhis.webapi.mvc.annotation.ApiVersion; import org.hisp.dhis.webapi.utils.ContextUtils; -import org.springframework.http.CacheControl; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -203,7 +203,7 @@ public WebMessage setSystemSettingV29(@RequestBody Map settings) return ResponseEntity.status(404).body(SETTING_DOESNT_EXIST_OR_CONFIDENTIAL); } response.setHeader( - ContextUtils.HEADER_CACHE_CONTROL, CacheControl.noCache().cachePrivate().getHeaderValue()); + ContextUtils.HEADER_CACHE_CONTROL, noCache().cachePrivate().getHeaderValue()); return ResponseEntity.ok() .body(String.valueOf(getSystemSettingOrTranslation(key, locale, currentUser))); @@ -223,7 +223,7 @@ public WebMessage setSystemSettingV29(@RequestBody Map settings) } return ResponseEntity.ok() - .cacheControl(CacheControl.noCache().cachePrivate()) + .cacheControl(noCache().cachePrivate()) .contentType(MediaType.APPLICATION_JSON) .body(Map.of(key, getSystemSettingOrTranslation(key, locale, currentUser))); } @@ -286,7 +286,7 @@ public ResponseEntity> getSystemSettingsJson( } return ResponseEntity.ok() - .cacheControl(CacheControl.noCache().cachePrivate()) + .headers(ContextUtils.noCacheNoStoreMustRevalidate()) .body(systemSettingManager.getSystemSettings(settingKeysToFetch)); } diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java index e5c376741cd1..a6e1f6d3e110 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextUtils.java @@ -189,6 +189,12 @@ private void configureResponse( } } + public static HttpHeaders noCacheNoStoreMustRevalidate() { + HttpHeaders headers = new HttpHeaders(); + headers.setCacheControl("no-cache, no-store, must-revalidate"); + return headers; + } + public static HttpServletResponse setCacheControl( HttpServletResponse response, CacheControl value) { response.setHeader(HEADER_CACHE_CONTROL, value.getHeaderValue());