|
@@ -0,0 +1,66 @@
|
|
|
|
|
+package com.payment.platform.module.payment.stream;
|
|
|
|
|
+
|
|
|
|
|
+import com.payment.platform.common.exception.BusinessException;
|
|
|
|
|
+import com.payment.platform.core.security.LoginUser;
|
|
|
|
|
+import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+
|
|
|
|
|
+class OrderStreamControllerTest {
|
|
|
|
|
+
|
|
|
|
|
+ private final OrderEventHub hub = new OrderEventHub();
|
|
|
|
|
+ private final OrderStreamController controller = new OrderStreamController(hub);
|
|
|
|
|
+ private final MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
+
|
|
|
|
|
+ @AfterEach
|
|
|
|
|
+ void tearDown() {
|
|
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
|
|
+ hub.shutdown();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void login(Long tenantId, boolean superuser) {
|
|
|
|
|
+ LoginUser user = new LoginUser();
|
|
|
|
|
+ user.setTenantId(tenantId);
|
|
|
|
|
+ user.setIsSuperuser(superuser);
|
|
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(
|
|
|
|
|
+ new UsernamePasswordAuthenticationToken(user, null, List.of()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void orders_tenantUser_opensOneTenantConnectionAndDeclaresNoBuffering() {
|
|
|
|
|
+ login(2L, false);
|
|
|
|
|
+
|
|
|
|
|
+ SseEmitter emitter = controller.orders(response);
|
|
|
|
|
+
|
|
|
|
|
+ assertNotNull(emitter);
|
|
|
|
|
+ assertEquals("no", response.getHeader("X-Accel-Buffering"),
|
|
|
|
|
+ "不写这条头,nginx 会把 SSE 帧攒着转发,页面看起来「不实时」");
|
|
|
|
|
+ assertEquals(1, hub.connectionCount());
|
|
|
|
|
+ assertEquals(1, hub.tenantConnectionCount(2L));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void orders_superAdmin_goesToSuperAdminBucketOnly() {
|
|
|
|
|
+ login(2L, true);
|
|
|
|
|
+
|
|
|
|
|
+ controller.orders(response);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(1, hub.connectionCount());
|
|
|
|
|
+ assertEquals(0, hub.tenantConnectionCount(2L), "超管不进租户桶 —— 否则同帧收两份");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void orders_anonymous_throws403AndOpensNothing() {
|
|
|
|
|
+ BusinessException ex = assertThrows(BusinessException.class, () -> controller.orders(response));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(ex.getMessage().contains("未登录"), "实际文案=" + ex.getMessage());
|
|
|
|
|
+ assertEquals(0, hub.connectionCount(), "匿名不许往桶里挂空连接");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|