From 55ecaab6051eda0784c612204902ba8c97780445 Mon Sep 17 00:00:00 2001 From: Martin Vysny Date: Thu, 1 Aug 2024 11:30:21 +0300 Subject: [PATCH] Routes: add MockRouteAccessDeniedError to the list of default error routes --- .../mvysny/kaributesting/v10/NavigatorTest.kt | 123 ++++++------------ .../github/mvysny/kaributesting/v10/Routes.kt | 2 +- 2 files changed, 44 insertions(+), 81 deletions(-) diff --git a/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/NavigatorTest.kt b/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/NavigatorTest.kt index d2c49eda..77b6feb1 100644 --- a/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/NavigatorTest.kt +++ b/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/NavigatorTest.kt @@ -220,60 +220,60 @@ internal fun DynaNodeGroup.navigatorTest() { } group("security") { - group("no user logged in") { - test("both mock routes are present") { - expect(true, routes.toString()) { routes.errorRoutes.contains(MockRouteNotFoundError::class.java) } - expect(true, routes.toString()) { routes.errorRoutes.contains(MockRouteAccessDeniedError::class.java) } - } - test("when access is rejected, redirect goes to WelcomeView") { - UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl().apply { - setLoginView(WelcomeView::class.java) - }) - navigateTo() - expectView() + navigatorSecurityTests("com.github") // includes also Karibu-Testing MockRouteNotFoundError and MockRouteAccessDeniedError on classpath + navigatorSecurityTests(null) // includes everything on classpath, including Flow's default RouteNotFoundError and RouteAccessDeniedError and also MyRouteNotFoundError + navigatorSecurityTests("test.app") // includes only MyRouteNotFoundError, but not Mock*Errors nor Flow default routes. + navigatorSecurityTests("nonexisting.pkg") // includes nothing. + } +} + +@Route("navigation-postpone") +class NavigationPostponeView : VerticalLayout(), BeforeLeaveObserver { + override fun beforeLeave(event: BeforeLeaveEvent) { + val action = event.postpone() + Dialog().apply { + span("Are you sure you want to leave such a beautiful view?") + button("Yes") { + onLeftClick { action.proceed(); close() } } - test("when access is rejected and no login view is set, redirects to MockRouteNotFoundError") { - UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl()) - expectThrows("No route found for 'testing': Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed.") { - navigateTo() - } + button("No") { + onLeftClick { close() } } - } - group("user logged in") { - test("when access is rejected, default handler redirects to MockRouteNotFoundError") { - MockVaadin.tearDown() - val routes = Routes().autoDiscoverViews("com.github") - routes.errorRoutes.remove(MockRouteAccessDeniedError::class.java) - MockVaadin.setup(routes) + }.open() + } +} - UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl("admin")) - expectThrows("No route found for 'testing': Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed.") { - navigateTo() - } - } - test("when access is rejected, Karibu's MockRouteAccessDeniedError throws AccessDeniedException") { - UI.getCurrent().addBeforeEnterListener(SimpleNavigationAccessControl("admin")) - expectThrows("Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed") { - navigateTo() - } +class SimpleNavigationAccessControl(val user: String? = null) : NavigationAccessControl() { + override fun getPrincipal(request: VaadinRequest?): Principal? = if (user == null) null else SimplePrincipal(user) + override fun getRolesChecker(request: VaadinRequest?): Predicate = Predicate { false } +} + +data class SimplePrincipal(private val name: String): Principal, Serializable { + override fun getName(): String = name +} + +@DynaTestDsl +internal fun DynaNodeGroup.navigatorSecurityTests(classpathScanPackage: String?) { + group("classpath scan of $classpathScanPackage") { + lateinit var routes: Routes + beforeEach { + MockVaadin.tearDown() + routes = Routes().autoDiscoverViews(classpathScanPackage) + if (routes.errorRoutes.contains(MyRouteNotFoundError::class.java)) { + routes.errorRoutes.remove(MyRouteNotFoundError::class.java) + routes.errorRoutes.add(MockRouteNotFoundError::class.java) } + routes.routes.addAll(setOf(TestingView::class.java, WelcomeView::class.java)) + MockVaadin.setup(routes) } - } - group("security2 - with all routes") { - beforeEach { MockVaadin.tearDown() } afterEach { MockVaadin.tearDown() } group("no user logged in") { test("both mock routes are present") { - val routes = Routes().autoDiscoverViews() + expect(true, routes.toString()) { routes.errorRoutes.contains(MockRouteNotFoundError::class.java) } expect(true, routes.toString()) { routes.errorRoutes.contains(MockRouteAccessDeniedError::class.java) } } 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) }) @@ -281,11 +281,6 @@ internal fun DynaNodeGroup.navigatorTest() { expectView() } 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("No route found for 'testing': Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed.") { navigateTo() @@ -294,9 +289,7 @@ internal fun DynaNodeGroup.navigatorTest() { } 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) + MockVaadin.tearDown() routes.errorRoutes.remove(MockRouteAccessDeniedError::class.java) MockVaadin.setup(routes) @@ -306,11 +299,6 @@ internal fun DynaNodeGroup.navigatorTest() { } } 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("Consider adding one of the following annotations to make the view accessible: @AnonymousAllowed, @PermitAll, @RolesAllowed") { navigateTo() @@ -319,28 +307,3 @@ internal fun DynaNodeGroup.navigatorTest() { } } } - -@Route("navigation-postpone") -class NavigationPostponeView : VerticalLayout(), BeforeLeaveObserver { - override fun beforeLeave(event: BeforeLeaveEvent) { - val action = event.postpone() - Dialog().apply { - span("Are you sure you want to leave such a beautiful view?") - button("Yes") { - onLeftClick { action.proceed(); close() } - } - button("No") { - onLeftClick { close() } - } - }.open() - } -} - -class SimpleNavigationAccessControl(val user: String? = null) : NavigationAccessControl() { - override fun getPrincipal(request: VaadinRequest?): Principal? = if (user == null) null else SimplePrincipal(user) - override fun getRolesChecker(request: VaadinRequest?): Predicate = Predicate { false } -} - -data class SimplePrincipal(private val name: String): Principal, Serializable { - override fun getName(): String = name -} diff --git a/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/Routes.kt b/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/Routes.kt index 1db302a0..cd08b3eb 100644 --- a/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/Routes.kt +++ b/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/Routes.kt @@ -30,7 +30,7 @@ import kotlin.test.expect */ public data class Routes( val routes: MutableSet> = mutableSetOf(), - val errorRoutes: MutableSet>> = mutableSetOf(MockRouteNotFoundError::class.java), + val errorRoutes: MutableSet>> = mutableSetOf(MockRouteNotFoundError::class.java, MockRouteAccessDeniedError::class.java), var skipPwaInit: Boolean = true ) : Serializable {