Search in sources :

Example 1 with CsrfChecker

use of dev.hilla.auth.CsrfChecker in project flow by vaadin.

the class EndpointControllerTest method createVaadinController.

private <T> EndpointController createVaadinController(T endpoint, ObjectMapper vaadinEndpointMapper, EndpointAccessChecker accessChecker, EndpointNameChecker endpointNameChecker, ExplicitNullableTypeChecker explicitNullableTypeChecker, CsrfChecker csrfChecker) {
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    if (vaadinEndpointMapper == null) {
        vaadinEndpointMapper = new ObjectMapper();
    }
    if (accessChecker == null) {
        accessChecker = mock(EndpointAccessChecker.class);
        when(accessChecker.check(TEST_METHOD, requestMock)).thenReturn(null);
    }
    if (csrfChecker == null) {
        csrfChecker = new CsrfChecker(servletContext);
    }
    if (endpointNameChecker == null) {
        endpointNameChecker = mock(EndpointNameChecker.class);
        when(endpointNameChecker.check(TEST_ENDPOINT_NAME)).thenReturn(null);
    }
    if (explicitNullableTypeChecker == null) {
        explicitNullableTypeChecker = mock(ExplicitNullableTypeChecker.class);
        when(explicitNullableTypeChecker.checkValueForType(any(), any())).thenReturn(null);
    }
    ApplicationContext mockApplicationContext = mockApplicationContext(endpoint);
    EndpointRegistry registry = new EndpointRegistry(endpointNameChecker);
    EndpointInvoker invoker = Mockito.spy(new EndpointInvoker(mockApplicationContext, vaadinEndpointMapper, explicitNullableTypeChecker, registry));
    Mockito.doReturn(accessChecker).when(invoker).getAccessChecker(any());
    EndpointController connectController = Mockito.spy(new EndpointController(mockApplicationContext, registry, invoker, csrfChecker));
    return connectController;
}
Also used : CsrfChecker(dev.hilla.auth.CsrfChecker) ApplicationContext(org.springframework.context.ApplicationContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with CsrfChecker

use of dev.hilla.auth.CsrfChecker in project flow by vaadin.

the class EndpointControllerMockBuilder method build.

public EndpointController build() {
    EndpointRegistry registry = new EndpointRegistry(endpointNameChecker);
    CsrfChecker csrfChecker = Mockito.mock(CsrfChecker.class);
    Mockito.when(csrfChecker.validateCsrfTokenInRequest(Mockito.any())).thenReturn(true);
    EndpointInvoker invoker = Mockito.spy(new EndpointInvoker(applicationContext, objectMapper, explicitNullableTypeChecker, registry));
    EndpointController controller = Mockito.spy(new EndpointController(applicationContext, registry, invoker, csrfChecker));
    Mockito.doReturn(mock(EndpointAccessChecker.class)).when(invoker).getAccessChecker(Mockito.any());
    return controller;
}
Also used : CsrfChecker(dev.hilla.auth.CsrfChecker) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker)

Aggregations

CsrfChecker (dev.hilla.auth.CsrfChecker)2 EndpointAccessChecker (dev.hilla.auth.EndpointAccessChecker)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lookup (com.vaadin.flow.di.Lookup)1 ServletContext (javax.servlet.ServletContext)1 ApplicationContext (org.springframework.context.ApplicationContext)1