Skip to content

Commit

Permalink
fix: System settings endpoint returns expected Cache-Control header v… (
Browse files Browse the repository at this point in the history
#15888)

* fix: System settings endpoint returns expected Cache-Control header value [DHIS2-15196]

* fix: Rename method to match Spring convention [DHIS2-15196]
  • Loading branch information
david-mackessy authored Dec 11, 2023
1 parent 063fd2d commit df7e1f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <[email protected]>
*/
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";
Expand All @@ -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
Expand All @@ -79,7 +79,7 @@ public void setUp() {

systemSettingActions = new SystemSettingActions();

loginActions = new LoginActions();
LoginActions loginActions = new LoginActions();
loginActions.loginAsDefaultUser();
}

Expand Down Expand Up @@ -115,7 +115,7 @@ private void prepareData() {
}

@Test
public void addSystemSetting() {
void addSystemSetting() {
String specificFooter = "Learn more at ";

QueryParamsBuilder params = new QueryParamsBuilder();
Expand All @@ -129,7 +129,7 @@ public void addSystemSetting() {
}

@Test
public void returnDefaultValueWhenTranslationIsNotAvailable() {
void returnDefaultValueWhenTranslationIsNotAvailable() {
prepareData();

ApiResponse response =
Expand All @@ -143,7 +143,7 @@ public void returnDefaultValueWhenTranslationIsNotAvailable() {
}

@Test
public void returnTranslationForUsersLocale() {
void returnTranslationForUsersLocale() {
prepareData();

ApiResponse response =
Expand All @@ -157,7 +157,7 @@ public void returnTranslationForUsersLocale() {
}

@Test
public void returnTranslationForGivenLocale() {
void returnTranslationForGivenLocale() {
prepareData();

ApiResponse response =
Expand All @@ -171,7 +171,7 @@ public void returnTranslationForGivenLocale() {
}

@Test
public void returnAllSystemSettings() {
void returnAllSystemSettings() {
prepareData();

ApiResponse response = systemSettingActions.get();
Expand All @@ -184,7 +184,7 @@ public void returnAllSystemSettings() {
}

@Test
public void deleteTranslationForGivenLocaleAndSettingKey() {
void deleteTranslationForGivenLocaleAndSettingKey() {
prepareData();

ApiResponse response =
Expand All @@ -204,7 +204,7 @@ public void deleteTranslationForGivenLocaleAndSettingKey() {
}

@Test
public void deleteSystemSetting() {
void deleteSystemSetting() {
prepareData();

ApiResponse response = systemSettingActions.delete(APPLICATION_INTRO_KEY);
Expand All @@ -224,7 +224,7 @@ public void deleteSystemSetting() {
}

@Test
public void getDefaultSystemSettingAsText() {
void getDefaultSystemSettingAsText() {
ApiResponse response =
systemSettingActions.get(
MAX_SYNC_ATTEMPTS_KEY,
Expand Down Expand Up @@ -260,7 +260,7 @@ public void getDefaultSystemSettingAsText() {
}

@Test
public void getDefaultSystemSettingAsJson() {
void getDefaultSystemSettingAsJson() {
ApiResponse response =
systemSettingActions.get(
MAX_SYNC_ATTEMPTS_KEY,
Expand Down Expand Up @@ -296,7 +296,7 @@ public void getDefaultSystemSettingAsJson() {
}

@Test
public void getDefaultSystemSettingWithNonSpecifiedContentTypeAndAccept() {
void getDefaultSystemSettingWithNonSpecifiedContentTypeAndAccept() {
ApiResponse response =
systemSettingActions.get(MAX_SYNC_ATTEMPTS_KEY, "", "", new QueryParamsBuilder());

Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -203,7 +203,7 @@ public WebMessage setSystemSettingV29(@RequestBody Map<String, Object> 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)));
Expand All @@ -223,7 +223,7 @@ public WebMessage setSystemSettingV29(@RequestBody Map<String, Object> settings)
}

return ResponseEntity.ok()
.cacheControl(CacheControl.noCache().cachePrivate())
.cacheControl(noCache().cachePrivate())
.contentType(MediaType.APPLICATION_JSON)
.body(Map.of(key, getSystemSettingOrTranslation(key, locale, currentUser)));
}
Expand Down Expand Up @@ -286,7 +286,7 @@ public ResponseEntity<Map<String, Serializable>> getSystemSettingsJson(
}

return ResponseEntity.ok()
.cacheControl(CacheControl.noCache().cachePrivate())
.headers(ContextUtils.noCacheNoStoreMustRevalidate())
.body(systemSettingManager.getSystemSettings(settingKeysToFetch));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit df7e1f5

Please sign in to comment.