Skip to content

Commit

Permalink
#179 readme: mention alternative way of handling AccessDeniedException.
Browse files Browse the repository at this point in the history
Fixes #179
  • Loading branch information
mvysny committed Dec 18, 2024
1 parent b850597 commit 737f29f
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion karibu-testing-v10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,8 @@ RichTextEditorsKt._setHtmlValue(rte, "<b>hello</b>");
It's possible to override `HttpRequest.getUserPrincipal()` and `isUserInRole()`; see the
`MockRequest` class for more details. It's even
possible to provide a custom implementation of `MockRequest` by modifying the
`MockVaadin.mockRequestFactory` closure accordingly.
`MockVaadin.mockRequestFactory` closure accordingly; see [issue #180](https://github.com/mvysny/karibu-testing/issues/180)
for more details.

An example which will fool
Vaadin's `AccessAnnotationChecker` and `ViewAccessChecker`:
Expand All @@ -1999,6 +2000,22 @@ currentRequest.mock.userPrincipalInt = MockPrincipal("admin", listOf("admin"))
currentRequest.mock.isUserInRole = { p, r -> (p as MockPrincipal).isUserInRole(r) }
```

### `AccessDeniedException`

In Unit Testing, the fail-eagerly principle is preferred: if an exception is thrown during testing, the test wants to learn and throw the original exception,
so that the test failure can be analyzed and the cause can be understood quickly. In this case, if an `AccessDeniedException` is thrown by the app,
I'd want the test to fail quickly with the `AccessDeniedException` as well.

This is implemented by Karibu-Testing auto-registering the `MockRouteAccessDeniedError` page to Vaadin,
which simply rethrows the `AccessDeniedException`. This is the default behavior of Karibu-Testing.

However, you may want to emulate the default Vaadin behavior: redirecting to `NotFoundException` in production mode when access is denied.
This can be achieved easily, by removing the `MockRouteAccessDeniedError` from Karibu-Testing `Routes`:
```kotlin
routes.errorRoutes.remove(MockRouteAccessDeniedError::class.java)
```
See [issue #179](https://github.com/mvysny/karibu-testing/issues/179) for more information.

# MPR (Multi-Platform Runtime)

It is possible to use Karibu-Testing to test [Vaadin MPR](https://vaadin.com/docs/v14/mpr/Overview.html)-based apps.
Expand Down

0 comments on commit 737f29f

Please sign in to comment.