Skip to content

Commit

Permalink
Add KaribuTest base class for Vaadin testing
Browse files Browse the repository at this point in the history
Introduced a new abstract base class
  • Loading branch information
LiveNathan committed Aug 16, 2024
1 parent a648da3 commit 81ee8ee
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
<artifactId>vaadin-testbench-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.github.mvysny.kaributesting</groupId>
<artifactId>karibu-testing-v10-spring</artifactId>
<version>2.1.8</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/dev/nathanlively/views/clock/ClockMvcTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package dev.nathanlively.views.clock;

import com.vaadin.testbench.unit.UIUnitTest;
import com.vaadin.testbench.unit.ViewPackages;
import com.vaadin.flow.component.UI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static com.github.mvysny.kaributesting.v10.LocatorJ._get;
import static org.assertj.core.api.Assertions.assertThat;

@ViewPackages(classes = {ClockView.class})
@Tag("mvc")
class ClockMvcTest extends UIUnitTest {
class ClockMvcTest extends KaribuTest {

@BeforeEach
public void login() {
UI.getCurrent().getPage().reload();
UI.getCurrent().navigate(ClockView.class);
}

@Test
public void getRequestToIndex() throws Exception {
ClockView view = navigate(ClockView.class);
ClockView view = _get(ClockView.class);

assertThat(view).isNotNull();
}
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/dev/nathanlively/views/clock/KaribuTest.java
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
}
}
}

0 comments on commit 81ee8ee

Please sign in to comment.