Skip to content

Commit

Permalink
test: prevent DauStorage calls in unit tests (#20050) (#20053)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Collovati <marco@vaadin.com>
  • Loading branch information
vaadin-bot and mcollovati authored Sep 25, 2024
1 parent 69168e4 commit c5ef12d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,33 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.server.dau.DAUCustomizer;
import com.vaadin.flow.server.dau.DAUVaadinRequestInterceptor;
import com.vaadin.flow.server.dau.UserIdentitySupplier;
import com.vaadin.pro.licensechecker.LicenseException;
import com.vaadin.pro.licensechecker.dau.DauIntegration;
import com.vaadin.tests.util.MockDeploymentConfiguration;

import static org.mockito.ArgumentMatchers.anyString;

public class VaadinServiceDauTest {

private static final Predicate<VaadinRequestInterceptor> IS_DAU_INTERCEPTOR = interceptor -> interceptor instanceof DAUVaadinRequestInterceptor
|| (interceptor instanceof VaadinService.VaadinSessionOnRequestStartInterceptorWrapper wrapper
&& wrapper.delegate instanceof DAUVaadinRequestInterceptor);

private String subscriptionKey;
private MockedStatic<DauIntegration> dauIntegrationMock;

@Before
public void setUp() throws Exception {
subscriptionKey = System.getProperty("vaadin.subscriptionKey");
System.setProperty("vaadin.subscriptionKey", "sub-1234");
dauIntegrationMock = Mockito.mockStatic(DauIntegration.class);
}

@After
Expand All @@ -39,6 +46,7 @@ public void tearDown() throws Exception {
} else {
System.clearProperty("vaadin.subscriptionKey");
}
dauIntegrationMock.close();
}

@Test
Expand All @@ -50,6 +58,7 @@ public void init_developmentMode_dauNotEnabled() {
MockVaadinServletService service = new MockVaadinServletService(config);
Assert.assertTrue("Expecting DAU interceptor not to be installed",
vaadinInterceptors(service).noneMatch(IS_DAU_INTERCEPTOR));
dauIntegrationMock.verifyNoInteractions();
}

@Test
Expand All @@ -61,6 +70,7 @@ public void init_productionMode_notDauBuild_dauNotEnabled() {
MockVaadinServletService service = new MockVaadinServletService(config);
Assert.assertTrue("Expecting DAU interceptor not to be installed",
vaadinInterceptors(service).noneMatch(IS_DAU_INTERCEPTOR));
dauIntegrationMock.verifyNoInteractions();
}

@Test
Expand All @@ -72,10 +82,15 @@ public void init_productionMode_dauBuild_dauEnabled() {
MockVaadinServletService service = new MockVaadinServletService(config);
Assert.assertTrue("Expecting DAU interceptor to be installed",
vaadinInterceptors(service).anyMatch(IS_DAU_INTERCEPTOR));
dauIntegrationMock
.verify(() -> DauIntegration.startTracking(anyString()));
}

@Test
public void init_productionMode_dauBuild_subscriptionKeyNotAvailable_throws() {
dauIntegrationMock.reset();
dauIntegrationMock.when(() -> DauIntegration.startTracking(anyString()))
.thenCallRealMethod();
System.clearProperty("vaadin.subscriptionKey");
MockDeploymentConfiguration config = new MockDeploymentConfiguration();
config.setProductionMode(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -123,51 +124,65 @@ public void trackUser_notUidlRequest_track() {

@Test
public void jsonEnforcementResponse_noDauCustomizer_defaultMessages() {
VaadinService service = VaadinServiceDauTest.vaadinServiceWithDau(null);
VaadinRequest request = Mockito.mock(VaadinRequest.class);
Mockito.when(request.getService()).thenReturn(service);
String response = DAUUtils.jsonEnforcementResponse(request,
new DauEnforcementException(new EnforcementException("STOP")));

// remove JSON wrap
response = response.replace("for(;;);[", "").replaceFirst("]$", "");
JsonObject json = Json.parse(response).getObject("meta")
.getObject("appError");

EnforcementNotificationMessages expectedMessages = EnforcementNotificationMessages.DEFAULT;
assertJsonErrorProperty("caption", expectedMessages.caption(), json);
assertJsonErrorProperty("message", expectedMessages.message(), json);
assertJsonErrorProperty("details", expectedMessages.details(), json);
assertJsonErrorProperty("url", expectedMessages.url(), json);
try (MockedStatic<DauIntegration> dauIntegrationMock = Mockito
.mockStatic(DauIntegration.class)) {
VaadinService service = VaadinServiceDauTest
.vaadinServiceWithDau(null);
VaadinRequest request = Mockito.mock(VaadinRequest.class);
Mockito.when(request.getService()).thenReturn(service);
String response = DAUUtils.jsonEnforcementResponse(request,
new DauEnforcementException(
new EnforcementException("STOP")));

// remove JSON wrap
response = response.replace("for(;;);[", "").replaceFirst("]$", "");
JsonObject json = Json.parse(response).getObject("meta")
.getObject("appError");

EnforcementNotificationMessages expectedMessages = EnforcementNotificationMessages.DEFAULT;
assertJsonErrorProperty("caption", expectedMessages.caption(),
json);
assertJsonErrorProperty("message", expectedMessages.message(),
json);
assertJsonErrorProperty("details", expectedMessages.details(),
json);
assertJsonErrorProperty("url", expectedMessages.url(), json);
}
}

@Test
public void jsonEnforcementResponse_customMessages() {
EnforcementNotificationMessages expectedMessages = new EnforcementNotificationMessages(
"caption", "message", "details", "url");
DAUCustomizer customizer = new DAUCustomizer() {
@Override
public EnforcementNotificationMessages getEnforcementNotificationMessages(
SystemMessagesInfo systemMessagesInfo) {
return expectedMessages;
}
};
VaadinService service = VaadinServiceDauTest
.vaadinServiceWithDau(customizer);
VaadinRequest request = Mockito.mock(VaadinRequest.class);
Mockito.when(request.getService()).thenReturn(service);

String response = DAUUtils.jsonEnforcementResponse(request,
new DauEnforcementException(new EnforcementException("STOP")));
response = response.replace("for(;;);[", "").replaceFirst("]$", "");
JsonObject json = Json.parse(response).getObject("meta")
.getObject("appError");

assertJsonErrorProperty("caption", expectedMessages.caption(), json);
assertJsonErrorProperty("message", expectedMessages.message(), json);
assertJsonErrorProperty("details", expectedMessages.details(), json);
assertJsonErrorProperty("url", expectedMessages.url(), json);

try (MockedStatic<DauIntegration> dauIntegrationMock = Mockito
.mockStatic(DauIntegration.class)) {
EnforcementNotificationMessages expectedMessages = new EnforcementNotificationMessages(
"caption", "message", "details", "url");
DAUCustomizer customizer = new DAUCustomizer() {
@Override
public EnforcementNotificationMessages getEnforcementNotificationMessages(
SystemMessagesInfo systemMessagesInfo) {
return expectedMessages;
}
};
VaadinService service = VaadinServiceDauTest
.vaadinServiceWithDau(customizer);
VaadinRequest request = Mockito.mock(VaadinRequest.class);
Mockito.when(request.getService()).thenReturn(service);

String response = DAUUtils.jsonEnforcementResponse(request,
new DauEnforcementException(
new EnforcementException("STOP")));
response = response.replace("for(;;);[", "").replaceFirst("]$", "");
JsonObject json = Json.parse(response).getObject("meta")
.getObject("appError");

assertJsonErrorProperty("caption", expectedMessages.caption(),
json);
assertJsonErrorProperty("message", expectedMessages.message(),
json);
assertJsonErrorProperty("details", expectedMessages.details(),
json);
assertJsonErrorProperty("url", expectedMessages.url(), json);
}
}

@Test
Expand Down

0 comments on commit c5ef12d

Please sign in to comment.