Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make MenuAccessControl availalble in mocking environment #1847

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import com.vaadin.flow.router.RouteConfiguration;
import com.vaadin.flow.server.VaadinServiceInitListener;
import com.vaadin.flow.server.auth.DefaultMenuAccessControl;
import com.vaadin.flow.server.auth.MenuAccessControl;
import com.vaadin.flow.server.auth.NavigationAccessControl;
import com.vaadin.flow.server.auth.ViewAccessChecker;

Expand Down Expand Up @@ -88,5 +90,33 @@ public UserDetails loadUserByUsername(String username)
}
};
}

@Bean
MenuAccessControl menuAccessControl() {
// SpringMenuAccessControl has been introduced in Vaadin 24.5
// but the Testbench codebase currently supports also 24.4
// Using reflection to prevent runtime issues.
Class<? extends MenuAccessControl> clazz = springMenuAccessControlClass();
if (clazz != null) {
try {
return clazz.getConstructor().newInstance();
} catch (Exception e) {
throw new AssertionError(
"Cannot instantiate SpringMenuAccessControl");
}
}
return new DefaultMenuAccessControl();
}
}

@SuppressWarnings("unchecked")
static Class<? extends MenuAccessControl> springMenuAccessControlClass() {
try {
return (Class<? extends MenuAccessControl>) Class.forName(
"com.vaadin.flow.spring.security.SpringMenuAccessControl");
} catch (ClassNotFoundException e) {
// No op
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import com.testapp.security.LoginView;
import com.testapp.security.ProtectedView;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.ContextConfiguration;

import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.auth.MenuAccessControl;

@ContextConfiguration(classes = SecurityTestConfig.NavigationAccessControlConfig.class)
@ViewPackages(packages = "com.testapp.security")
Expand Down Expand Up @@ -101,4 +104,21 @@ void withAnonymousUser_redirectToLogin() {
"Anonymous user should be redirect to login view");
}

@Test
void extendingBaseClass_runTest_menuAccessControlAvailable() {
Class<? extends MenuAccessControl> menuAccessControlClass = SecurityTestConfig
.springMenuAccessControlClass();
Assumptions.assumeTrue(menuAccessControlClass != null,
"SpringMenuAccessControl class not available");
MenuAccessControl menuAccessControl = VaadinService.getCurrent()
.getInstantiator().getMenuAccessControl();
Assertions.assertNotNull(menuAccessControl,
"Expecting MenuAccessControl to be available");
Assertions.assertInstanceOf(menuAccessControlClass, menuAccessControl,
"Expecting menu access control to be "
+ menuAccessControlClass.getName() + " but was "
+ menuAccessControl.getClass().getName());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.vaadin.flow.server.VaadinResponse;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.auth.MenuAccessControl;

class UIUnitBaseClassTest {

Expand Down Expand Up @@ -101,4 +102,17 @@ void customService_availableInLookup() {
}
}

@Nested
class MenuAccessControlTest extends UIUnitTest {

@Test
void menuAccessControl_instanceAvailable() {
MenuAccessControl menuAccessControl = VaadinService.getCurrent()
.getInstantiator().getMenuAccessControl();
Assertions.assertNotNull(menuAccessControl,
"Expecting MenuAccessControl to be available");

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.vaadin.testbench.unit.mocks

import com.vaadin.flow.di.Instantiator
import com.vaadin.flow.i18n.I18NProvider
import com.vaadin.flow.server.auth.MenuAccessControl
import net.bytebuddy.ByteBuddy
import net.bytebuddy.implementation.MethodCall
import net.bytebuddy.matcher.ElementMatchers
Expand All @@ -31,6 +32,8 @@ open class MockInstantiator(val delegate: Instantiator) : Instantiator by delega
else -> delegate.getOrCreate(type)
}

override fun getMenuAccessControl(): MenuAccessControl = delegate.menuAccessControl

override fun getI18NProvider(): I18NProvider? = delegate.i18NProvider

companion object {
Expand Down