Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: prevent DauStorage calls in unit tests #20050

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading