Search in sources :

Example 1 with AccessAnnotationChecker

use of com.vaadin.flow.server.auth.AccessAnnotationChecker in project flow by vaadin.

the class EndpointControllerTest method should_CallMethodAnonymously_When_AnonymousOverridesRoles.

@Test
public void should_CallMethodAnonymously_When_AnonymousOverridesRoles() {
    EndpointController vaadinController = createVaadinController(TEST_ENDPOINT, new EndpointAccessChecker(new AccessAnnotationChecker()));
    ResponseEntity<String> response = vaadinController.serveEndpoint(TEST_ENDPOINT_NAME, "anonymousOverrides", createRequestParameters("{}"), requestMock);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals("\"Hello, no user!\"", response.getBody());
}
Also used : AccessAnnotationChecker(com.vaadin.flow.server.auth.AccessAnnotationChecker) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker) Test(org.junit.Test)

Example 2 with AccessAnnotationChecker

use of com.vaadin.flow.server.auth.AccessAnnotationChecker in project flow by vaadin.

the class EndpointControllerTest method should_bePossibeToGetPrincipalInEndpoint.

@Test
public void should_bePossibeToGetPrincipalInEndpoint() {
    when(principal.getName()).thenReturn("foo");
    EndpointController vaadinController = createVaadinController(TEST_ENDPOINT, new EndpointAccessChecker(new AccessAnnotationChecker()));
    ResponseEntity<String> response = vaadinController.serveEndpoint(TEST_ENDPOINT_NAME, "getUserName", createRequestParameters("{}"), requestMock);
    assertEquals("\"foo\"", response.getBody());
}
Also used : AccessAnnotationChecker(com.vaadin.flow.server.auth.AccessAnnotationChecker) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker) Test(org.junit.Test)

Example 3 with AccessAnnotationChecker

use of com.vaadin.flow.server.auth.AccessAnnotationChecker in project flow by vaadin.

the class EndpointControllerTest method should_CallMethodAnonymously_When_UserPrincipalIsInRole.

@Test
public void should_CallMethodAnonymously_When_UserPrincipalIsInRole() {
    when(requestMock.isUserInRole("FOO_ROLE")).thenReturn(true);
    EndpointController vaadinController = createVaadinController(TEST_ENDPOINT, new EndpointAccessChecker(new AccessAnnotationChecker()));
    ResponseEntity<String> response = vaadinController.serveEndpoint(TEST_ENDPOINT_NAME, "testRoleAllowed", createRequestParameters("{}"), requestMock);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals("\"Hello, user in role!\"", response.getBody());
}
Also used : AccessAnnotationChecker(com.vaadin.flow.server.auth.AccessAnnotationChecker) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker) Test(org.junit.Test)

Example 4 with AccessAnnotationChecker

use of com.vaadin.flow.server.auth.AccessAnnotationChecker in project flow by vaadin.

the class EndpointControllerTest method should_clearVaadinRequestInsntace_after_EndpointCall.

@Test
public void should_clearVaadinRequestInsntace_after_EndpointCall() {
    EndpointController vaadinController = createVaadinController(TEST_ENDPOINT, new EndpointAccessChecker(new AccessAnnotationChecker()));
    vaadinController.serveEndpoint(TEST_ENDPOINT_NAME, "getUserName", createRequestParameters("{}"), requestMock);
    Assert.assertNull(CurrentInstance.get(VaadinRequest.class));
    Assert.assertNull(VaadinRequest.getCurrent());
}
Also used : AccessAnnotationChecker(com.vaadin.flow.server.auth.AccessAnnotationChecker) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Test(org.junit.Test)

Example 5 with AccessAnnotationChecker

use of com.vaadin.flow.server.auth.AccessAnnotationChecker in project flow by vaadin.

the class EndpointControllerTest method should_NotCallMethodAnonymously_When_UserPrincipalIsNotInRole.

@Test
public void should_NotCallMethodAnonymously_When_UserPrincipalIsNotInRole() {
    EndpointController vaadinController = createVaadinController(TEST_ENDPOINT, new EndpointAccessChecker(new AccessAnnotationChecker()));
    ResponseEntity<String> response = vaadinController.serveEndpoint(TEST_ENDPOINT_NAME, "testRoleAllowed", createRequestParameters("{}"), requestMock);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
    assertTrue(response.getBody().contains(EndpointAccessChecker.ACCESS_DENIED_MSG));
}
Also used : AccessAnnotationChecker(com.vaadin.flow.server.auth.AccessAnnotationChecker) EndpointAccessChecker(dev.hilla.auth.EndpointAccessChecker) Test(org.junit.Test)

Aggregations

AccessAnnotationChecker (com.vaadin.flow.server.auth.AccessAnnotationChecker)6 EndpointAccessChecker (dev.hilla.auth.EndpointAccessChecker)5 Test (org.junit.Test)5 VaadinRequest (com.vaadin.flow.server.VaadinRequest)1 Principal (java.security.Principal)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Before (org.junit.Before)1