|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.util.Collections;
|
20 | 20 |
|
21 | 21 | import org.junit.jupiter.api.Test;
|
| 22 | +import reactor.core.publisher.Mono; |
| 23 | +import reactor.test.StepVerifier; |
22 | 24 |
|
23 | 25 | import org.springframework.security.authentication.TestAuthentication;
|
24 | 26 | import org.springframework.security.web.authentication.session.SessionAuthenticationException;
|
25 | 27 | import org.springframework.security.web.server.authentication.MaximumSessionsContext;
|
26 | 28 | import org.springframework.security.web.server.authentication.PreventLoginServerMaximumSessionsExceededHandler;
|
| 29 | +import org.springframework.web.server.WebSession; |
27 | 30 |
|
28 |
| -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.mockito.BDDMockito.given; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.verify; |
29 | 35 |
|
30 | 36 | /**
|
31 | 37 | * Tests for {@link PreventLoginServerMaximumSessionsExceededHandler}.
|
|
35 | 41 | class PreventLoginServerMaximumSessionsExceededHandlerTests {
|
36 | 42 |
|
37 | 43 | @Test
|
38 |
| - void handleWhenInvokedThenThrowsSessionAuthenticationException() { |
| 44 | + void handleWhenInvokedThenInvalidateWebSessionAndThrowsSessionAuthenticationException() { |
39 | 45 | PreventLoginServerMaximumSessionsExceededHandler handler = new PreventLoginServerMaximumSessionsExceededHandler();
|
| 46 | + WebSession webSession = mock(); |
| 47 | + given(webSession.invalidate()).willReturn(Mono.empty()); |
40 | 48 | MaximumSessionsContext context = new MaximumSessionsContext(TestAuthentication.authenticatedUser(),
|
41 |
| - Collections.emptyList(), 1); |
42 |
| - assertThatExceptionOfType(SessionAuthenticationException.class) |
43 |
| - .isThrownBy(() -> handler.handle(context).block()) |
44 |
| - .withMessage("Maximum sessions of 1 for authentication 'user' exceeded"); |
| 49 | + Collections.emptyList(), 1, webSession); |
| 50 | + StepVerifier.create(handler.handle(context)).expectErrorSatisfies((ex) -> { |
| 51 | + assertThat(ex).isInstanceOf(SessionAuthenticationException.class); |
| 52 | + assertThat(ex.getMessage()).isEqualTo("Maximum sessions exceeded"); |
| 53 | + }).verify(); |
| 54 | + verify(webSession).invalidate(); |
45 | 55 | }
|
46 | 56 |
|
47 | 57 | }
|
0 commit comments