Search in sources :

Example 16 with TestPrincipal

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

the class DefaultUserDestinationResolverTests method handleMessageEncodedUserName.

@Test
public void handleMessageEncodedUserName() {
    String userName = "https://joe.openid.example.org/";
    TestSimpUser simpUser = new TestSimpUser(userName);
    simpUser.addSessions(new TestSimpSession("openid123"));
    given(this.registry.getUser(userName)).willReturn(simpUser);
    String destination = "/user/" + StringUtils.replace(userName, "/", "%2F") + "/queue/foo";
    Message<?> message = createMessage(SimpMessageType.MESSAGE, new TestPrincipal("joe"), null, destination);
    UserDestinationResult actual = this.resolver.resolveDestination(message);
    assertThat(actual.getTargetDestinations().size()).isEqualTo(1);
    assertThat(actual.getTargetDestinations().iterator().next()).isEqualTo("/queue/foo-useropenid123");
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 17 with TestPrincipal

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

the class DefaultUserDestinationResolverTests method handleMessageForDestinationWithDotSeparator.

// SPR-14044
@Test
public void handleMessageForDestinationWithDotSeparator() {
    this.resolver.setRemoveLeadingSlash(true);
    TestPrincipal user = new TestPrincipal("joe");
    String destination = "/user/joe/jms.queue.call";
    Message<?> message = createMessage(SimpMessageType.MESSAGE, 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("/user/jms.queue.call");
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Test(org.junit.jupiter.api.Test)

Example 18 with TestPrincipal

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

the class DefaultUserDestinationResolverTests method handleUnsubscribe.

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

Example 19 with TestPrincipal

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

the class SimpleBrokerMessageHandlerTests method readInactivity.

@Test
public void readInactivity() throws Exception {
    this.messageHandler.setHeartbeatValue(new long[] { 0, 1 });
    this.messageHandler.setTaskScheduler(this.taskScheduler);
    this.messageHandler.start();
    ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(this.taskScheduler).scheduleWithFixedDelay(taskCaptor.capture(), eq(1L));
    Runnable heartbeatTask = taskCaptor.getValue();
    assertThat(heartbeatTask).isNotNull();
    String id = "sess1";
    TestPrincipal user = new TestPrincipal("joe");
    Message<String> connectMessage = createConnectMessage(id, user, new long[] { 1, 0 });
    this.messageHandler.handleMessage(connectMessage);
    Thread.sleep(10);
    heartbeatTask.run();
    verify(this.clientOutChannel, atLeast(2)).send(this.messageCaptor.capture());
    List<Message<?>> messages = this.messageCaptor.getAllValues();
    assertThat(messages.size()).isEqualTo(2);
    MessageHeaders headers = messages.get(0).getHeaders();
    assertThat(headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER)).isEqualTo(SimpMessageType.CONNECT_ACK);
    headers = messages.get(1).getHeaders();
    assertThat(headers.get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER)).isEqualTo(SimpMessageType.DISCONNECT_ACK);
    assertThat(headers.get(SimpMessageHeaderAccessor.SESSION_ID_HEADER)).isEqualTo(id);
    assertThat(headers.get(SimpMessageHeaderAccessor.USER_HEADER)).isEqualTo(user);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.jupiter.api.Test)

Example 20 with TestPrincipal

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

the class StompBrokerRelayMessageHandlerTests method message.

private Message<byte[]> message(StompCommand command, String sessionId, String user, String destination) {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(command);
    if (sessionId != null) {
        accessor.setSessionId(sessionId);
    }
    if (user != null) {
        accessor.setUser(new TestPrincipal(user));
    }
    if (destination != null) {
        accessor.setDestination(destination);
    }
    accessor.setLeaveMutable(true);
    return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal)

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