|
| 1 | +package run.halo.app.security.switchuser; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | +import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf; |
| 6 | +import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.security.core.userdetails.ReactiveUserDetailsService; |
| 13 | +import org.springframework.security.core.userdetails.User; |
| 14 | +import org.springframework.security.test.context.support.WithMockUser; |
| 15 | +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
| 16 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | +import run.halo.app.security.authorization.AuthorityUtils; |
| 19 | + |
| 20 | + |
| 21 | +@SpringBootTest |
| 22 | +@AutoConfigureWebTestClient |
| 23 | +class SwitchUserConfigurerTest { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + WebTestClient webClient; |
| 27 | + |
| 28 | + @MockitoSpyBean |
| 29 | + ReactiveUserDetailsService userDetailsService; |
| 30 | + |
| 31 | + @Test |
| 32 | + void shouldSwitchWithSuperRole() { |
| 33 | + when(userDetailsService.findByUsername("faker")) |
| 34 | + .thenReturn(Mono.fromSupplier(() -> User.withUsername("faker") |
| 35 | + .password("password") |
| 36 | + .roles("user") |
| 37 | + .build())); |
| 38 | + var result = webClient.mutateWith(csrf()) |
| 39 | + .mutateWith(mockUser("admin").roles(AuthorityUtils.SUPER_ROLE_NAME)) |
| 40 | + .post() |
| 41 | + .uri("/login/impersonate?username={username}&redirect_uri={redirect_uri}", |
| 42 | + "faker", "/fake-success" |
| 43 | + ) |
| 44 | + .exchange() |
| 45 | + .expectStatus().isFound() |
| 46 | + .expectHeader().location("/fake-success") |
| 47 | + .expectCookie().exists("SESSION") |
| 48 | + .expectBody().returnResult(); |
| 49 | + var session = result.getResponseCookies().getFirst("SESSION"); |
| 50 | + assertNotNull(session); |
| 51 | + |
| 52 | + webClient.mutateWith(csrf()) |
| 53 | + .post().uri("/logout/impersonate?redirect_uri={redirect_uri}", "/fake-logout-success") |
| 54 | + .cookie(session.getName(), session.getValue()) |
| 55 | + .exchange() |
| 56 | + .expectStatus().isFound() |
| 57 | + .expectHeader().location("/fake-logout-success"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @WithMockUser(username = "admin", roles = "non-super-role") |
| 62 | + void shouldNotSwitchWithoutSuperRole() { |
| 63 | + webClient.mutateWith(csrf()) |
| 64 | + .post() |
| 65 | + .uri("/login/impersonate?username={username}&redirect_uri={redirect_uri}", |
| 66 | + "faker", "/fake-success" |
| 67 | + ) |
| 68 | + .exchange() |
| 69 | + .expectStatus().isForbidden(); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments