-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add KaribuTest base class for Vaadin testing
Introduced a new abstract base class
- Loading branch information
1 parent
a648da3
commit 81ee8ee
Showing
3 changed files
with
104 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 11 additions & 5 deletions
16
src/test/java/dev/nathanlively/views/clock/ClockMvcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/test/java/dev/nathanlively/views/clock/KaribuTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package dev.nathanlively.views.clock; | ||
|
||
import com.github.mvysny.fakeservlet.FakeRequest; | ||
import com.github.mvysny.kaributesting.v10.MockVaadin; | ||
import com.github.mvysny.kaributesting.v10.Routes; | ||
import com.github.mvysny.kaributesting.v10.spring.MockSpringServlet; | ||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.server.VaadinServletRequest; | ||
import com.vaadin.flow.spring.SpringServlet; | ||
import kotlin.jvm.functions.Function0; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
@SpringBootTest | ||
public abstract class KaribuTest { | ||
|
||
private static Routes routes; | ||
|
||
@Autowired | ||
protected ApplicationContext ctx; | ||
|
||
@BeforeAll | ||
public static void discoverRoutes() { | ||
Locale.setDefault(Locale.ENGLISH); | ||
routes = new Routes().autoDiscoverViews("dev.nathanlively.views"); | ||
} | ||
|
||
@BeforeEach | ||
public void setup() { | ||
Function0<UI> uiFactory = UI::new; | ||
SpringServlet servlet = new MockSpringServlet(routes, ctx, uiFactory); | ||
MockVaadin.setup(uiFactory, servlet); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
logout(); | ||
MockVaadin.tearDown(); | ||
} | ||
|
||
protected void login(String user, String pass, List<String> roles) { | ||
List<SimpleGrantedAuthority> authorities = | ||
roles.stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role)).toList(); | ||
|
||
UserDetails userDetails = new User(user, pass, authorities); | ||
UsernamePasswordAuthenticationToken authReq = | ||
new UsernamePasswordAuthenticationToken(userDetails, pass, authorities); | ||
SecurityContext sc = SecurityContextHolder.getContext(); | ||
sc.setAuthentication(authReq); | ||
|
||
// however, you also need to make sure that ViewAccessChecker works properly; | ||
// that requires a correct MockRequest.userPrincipal and MockRequest.isUserInRole() | ||
FakeRequest request = (FakeRequest) VaadinServletRequest.getCurrent().getRequest(); | ||
request.setUserPrincipalInt(authReq); | ||
request.setUserInRole((principal, role) -> roles.contains(role)); | ||
} | ||
|
||
protected void logout() { | ||
try { | ||
SecurityContextHolder.getContext().setAuthentication(null); | ||
if (VaadinServletRequest.getCurrent() != null) { | ||
FakeRequest request = (FakeRequest) VaadinServletRequest.getCurrent().getRequest(); | ||
request.setUserPrincipalInt(null); | ||
request.setUserInRole((principal, role) -> false); | ||
} | ||
} catch (IllegalStateException ignore) { | ||
// Ignored | ||
} | ||
} | ||
} |