Search in sources :

Example 21 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class StompBrokerRelayMessageHandlerTests method connectMessage.

private Message<byte[]> connectMessage(String sessionId, String user) {
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
    headers.setSessionId(sessionId);
    headers.setUser(new TestPrincipal(user));
    headers.setLeaveMutable(true);
    return MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal)

Example 22 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class DefaultUserDestinationResolverTests method ignoreMessage.

@Test
public void ignoreMessage() {
    // no destination
    TestPrincipal user = new TestPrincipal("joe");
    Message<?> message = createMessage(SimpMessageType.MESSAGE, user, "123", null);
    UserDestinationResult actual = this.resolver.resolveDestination(message);
    assertThat(actual).isNull();
    // not a user destination
    message = createMessage(SimpMessageType.MESSAGE, user, "123", "/queue/foo");
    actual = this.resolver.resolveDestination(message);
    assertThat(actual).isNull();
    // subscribe + not a user destination
    message = createMessage(SimpMessageType.SUBSCRIBE, user, "123", "/queue/foo");
    actual = this.resolver.resolveDestination(message);
    assertThat(actual).isNull();
    // no match on message type
    message = createMessage(SimpMessageType.CONNECT, user, "123", "user/joe/queue/foo");
    actual = this.resolver.resolveDestination(message);
    assertThat(actual).isNull();
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 23 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class DefaultUserDestinationResolverTests method handleSubscribeForDestinationWithoutLeadingSlash.

// SPR-14044
@Test
public void handleSubscribeForDestinationWithoutLeadingSlash() {
    this.resolver.setRemoveLeadingSlash(true);
    TestPrincipal user = new TestPrincipal("joe");
    String destination = "/user/jms.queue.call";
    Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, user, "123", destination);
    UserDestinationResult actual = this.resolver.resolveDestination(message);
    assertThat(actual.getTargetDestinations().size()).isEqualTo(1);
    assertThat(actual.getTargetDestinations().iterator().next()).isEqualTo("jms.queue.call-user123");
    assertThat(actual.getSubscribeDestination()).isEqualTo(destination);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 24 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class DefaultUserDestinationResolverTests method handleSubscribe.

@Test
public void handleSubscribe() {
    TestPrincipal user = new TestPrincipal("joe");
    String sourceDestination = "/user/queue/foo";
    Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, user, "123", sourceDestination);
    UserDestinationResult actual = this.resolver.resolveDestination(message);
    assertThat(actual.getSourceDestination()).isEqualTo(sourceDestination);
    assertThat(actual.getTargetDestinations().size()).isEqualTo(1);
    assertThat(actual.getTargetDestinations().iterator().next()).isEqualTo("/queue/foo-user123");
    assertThat(actual.getSubscribeDestination()).isEqualTo(sourceDestination);
    assertThat(actual.getUser()).isEqualTo(user.getName());
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 25 with TestPrincipal

use of org.springframework.core.testfixture.security.TestPrincipal in project spring-framework by spring-projects.

the class JettyWebSocketSessionTests method getPrincipalFromNativeSession.

@Test
@SuppressWarnings("resource")
public void getPrincipalFromNativeSession() {
    TestPrincipal user = new TestPrincipal("joe");
    UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
    given(request.getUserPrincipal()).willReturn(user);
    UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
    given(response.getAcceptedSubProtocol()).willReturn(null);
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getUpgradeRequest()).willReturn(request);
    given(nativeSession.getUpgradeResponse()).willReturn(response);
    JettyWebSocketSession session = new JettyWebSocketSession(attributes);
    session.initializeNativeSession(nativeSession);
    reset(nativeSession);
    assertThat(session.getPrincipal()).isSameAs(user);
    verifyNoMoreInteractions(nativeSession);
}
Also used : UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.jupiter.api.Test)

Aggregations

TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)31 Test (org.junit.jupiter.api.Test)27 Message (org.springframework.messaging.Message)4 SimpUser (org.springframework.messaging.simp.user.SimpUser)4 MessageHeaders (org.springframework.messaging.MessageHeaders)2 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)2 TextMessage (org.springframework.web.socket.TextMessage)2 Session (jakarta.websocket.Session)1 HashSet (java.util.HashSet)1 Session (org.eclipse.jetty.websocket.api.Session)1 UpgradeRequest (org.eclipse.jetty.websocket.api.UpgradeRequest)1 UpgradeResponse (org.eclipse.jetty.websocket.api.UpgradeResponse)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 MessageChannel (org.springframework.messaging.MessageChannel)1 SimpSubscription (org.springframework.messaging.simp.user.SimpSubscription)1 BinaryMessage (org.springframework.web.socket.BinaryMessage)1 CloseStatus (org.springframework.web.socket.CloseStatus)1 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)1 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)1