Skip to content

Commit

Permalink
Routes.kt: drop Vaadin @DefaultHandlers for RouteNotFoundError and Ac…
Browse files Browse the repository at this point in the history
…cessDeniedError
  • Loading branch information
mvysny committed Aug 1, 2024
1 parent 526a114 commit 47c9bf8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.vaadin.flow.router.Route
import com.vaadin.flow.router.RouteNotFoundError
import com.vaadin.flow.server.VaadinRequest
import com.vaadin.flow.server.auth.NavigationAccessControl
import test.app.MyRouteNotFoundError
import java.io.Serializable
import java.security.Principal
import java.util.function.Predicate
Expand Down Expand Up @@ -254,6 +255,61 @@ internal fun DynaNodeGroup.navigatorTest() {
}
}
}
group("security2 - with all routes") {
beforeEach { MockVaadin.tearDown() }
afterEach { MockVaadin.tearDown() }

group("no user logged in") {
test("when access is rejected, redirect goes to WelcomeView") {
val routes = Routes().autoDiscoverViews()
routes.errorRoutes.remove(MyRouteNotFoundError::class.java)
routes.errorRoutes.add(MockRouteNotFoundError::class.java)
MockVaadin.setup(routes)

UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl().apply {
setLoginView(WelcomeView::class.java)
})
navigateTo<TestingView>()
expectView<WelcomeView>()
}
test("when access is rejected and no login view is set, redirects to MockRouteNotFoundError") {
val routes = Routes().autoDiscoverViews()
routes.errorRoutes.remove(MyRouteNotFoundError::class.java)
routes.errorRoutes.add(MockRouteNotFoundError::class.java)
MockVaadin.setup(routes)

UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl())
expectThrows<NotFoundException>("No route found for 'testing': Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed.") {
navigateTo<TestingView>()
}
}
}
group("user logged in") {
test("when access is rejected, default handler redirects to MockRouteNotFoundError") {
val routes = Routes().autoDiscoverViews()
routes.errorRoutes.remove(MyRouteNotFoundError::class.java)
routes.errorRoutes.add(MockRouteNotFoundError::class.java)
routes.errorRoutes.remove(MockRouteAccessDeniedError::class.java)
MockVaadin.setup(routes)

UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl("admin"))
expectThrows<NotFoundException>("No route found for 'testing': Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed.") {
navigateTo<TestingView>()
}
}
test("when access is rejected, Karibu's MockRouteAccessDeniedError throws AccessDeniedException") {
val routes = Routes().autoDiscoverViews()
routes.errorRoutes.remove(MyRouteNotFoundError::class.java)
routes.errorRoutes.add(MockRouteNotFoundError::class.java)
MockVaadin.setup(routes)

UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl("admin"))
expectThrows<AccessDeniedException>("Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed") {
navigateTo<TestingView>()
}
}
}
}
}

@Route("navigation-postpone")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ fun DynaNodeGroup.routesTestBatch() {
// https://github.com/mvysny/karibu-testing/issues/50
test("app-specific NotFoundException handler removes MockRouteNotFoundError") {
val routes: Routes = Routes().autoDiscoverViews()
// Vaadin 24.3+ introduces additional route
val routeAccessDeniedError = if (VaadinVersion.get.isAtLeast(24, 3)) Class.forName("com.vaadin.flow.router.RouteAccessDeniedError") else null
val expectedRouteClasses =
setOf(ErrorView::class.java, InternalServerError::class.java, MyRouteNotFoundError::class.java, RouteNotFoundError::class.java, routeAccessDeniedError)
.filterNotNull().toSet()
val expectedRouteClasses = setOf(ErrorView::class.java, InternalServerError::class.java, MyRouteNotFoundError::class.java, MockRouteAccessDeniedError::class.java)
expect(expectedRouteClasses) { routes.errorRoutes.toSet() }
// make sure that Vaadin initializes properly with this set of views
MockVaadin.setup(routes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.mvysny.kaributesting.v10

import com.github.mvysny.kaributesting.v10.mock.MockVaadin19
import com.github.mvysny.kaributools.VaadinVersion
import com.vaadin.flow.component.Component
import com.vaadin.flow.component.Tag
import com.vaadin.flow.router.*
Expand Down Expand Up @@ -83,6 +84,13 @@ public data class Routes(
}
}

// remove @DefaultErrorHandler RouteAccessDeniedError and RouteNotFoundError so that they're replaced with Karibu's Mock counterparts
// which perform better logging
errorRoutes.remove(RouteNotFoundError::class.java)
if (VaadinVersion.get.isAtLeast(24, 3)) {
errorRoutes.remove(Class.forName("com.vaadin.flow.router.RouteAccessDeniedError"))
}

// https://github.com/mvysny/karibu-testing/issues/50
// if the app defines its own NotFoundException handler, remove MockRouteNotFoundError
if (errorRoutes.any { it != MockRouteNotFoundError::class.java && it.isRouteNotFound }) {
Expand Down

0 comments on commit 47c9bf8

Please sign in to comment.