feat(auth): return Sa-Token on login and add tenant/user list APIs

This commit is contained in:
2026-07-07 15:06:13 +08:00
parent f50a3fd0c9
commit dfce1be9b2
12 changed files with 199 additions and 49 deletions

View File

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.UUID;
import static com.sino.mci.test.AuthTestHelper.createTenantAndLogin;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -26,17 +27,12 @@ public class ChannelControllerTest {
@Test
void shouldCreateChannelSubjectAndAccount() throws Exception {
String tenantCode = "channel-" + UUID.randomUUID().toString().substring(0, 8);
String tenantPayload = String.format("{\"tenantCode\":\"%s\",\"tenantName\":\"渠道测试\"}", tenantCode);
mockMvc.perform(post("/msg-platform/api/v1/account/tenant")
.contextPath("/msg-platform")
.contentType(MediaType.APPLICATION_JSON)
.content(tenantPayload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
String authToken = createTenantAndLogin(mockMvc, tenantCode, "渠道测试");
String subjectPayload = "{\"channelType\":\"wecom\",\"subjectCode\":\"sino\",\"subjectName\":\"Sino\"}";
mockMvc.perform(post("/msg-platform/api/v1/channel-subject")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(subjectPayload))
@@ -47,6 +43,7 @@ public class ChannelControllerTest {
String accountPayload = "{\"accountType\":1,\"channelType\":\"wecom\",\"accountId\":\"ZhangSan\",\"accountName\":\"张三\"}";
mockMvc.perform(post("/msg-platform/api/v1/channel-account")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(accountPayload))

View File

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.UUID;
import static com.sino.mci.test.AuthTestHelper.createTenantAndLogin;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -26,17 +27,12 @@ public class CrmControllerTest {
@Test
void shouldCreatePersonTagAndConversation() throws Exception {
String tenantCode = "crm-" + UUID.randomUUID().toString().substring(0, 8);
String tenantPayload = String.format("{\"tenantCode\":\"%s\",\"tenantName\":\"CRM测试\"}", tenantCode);
mockMvc.perform(post("/msg-platform/api/v1/account/tenant")
.contextPath("/msg-platform")
.contentType(MediaType.APPLICATION_JSON)
.content(tenantPayload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
String authToken = createTenantAndLogin(mockMvc, tenantCode, "CRM测试");
String personPayload = "{\"personCode\":\"P001\",\"personName\":\"张三\",\"phone\":\"13800138000\",\"personType\":3}";
mockMvc.perform(post("/msg-platform/api/v1/person")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(personPayload))
@@ -47,6 +43,7 @@ public class CrmControllerTest {
String tagPayload = "{\"parentId\":0,\"tagName\":\"客户\"}";
mockMvc.perform(post("/msg-platform/api/v1/tag")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(tagPayload))
@@ -57,6 +54,7 @@ public class CrmControllerTest {
String conversationPayload = "{\"conversationType\":2,\"channelType\":\"wecom\",\"channelConversationId\":\"roomid_123\",\"conversationName\":\"测试群\"}";
mockMvc.perform(post("/msg-platform/api/v1/conversation")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(conversationPayload))
@@ -66,6 +64,7 @@ public class CrmControllerTest {
mockMvc.perform(get("/msg-platform/api/v1/tag/list")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0))

View File

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.UUID;
import static com.sino.mci.test.AuthTestHelper.createTenantAndLogin;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -26,17 +27,12 @@ public class InboundFlowControllerTest {
@Test
void shouldCreateFlowAndReceiveMessage() throws Exception {
String tenantCode = "flow-" + UUID.randomUUID().toString().substring(0, 8);
String tenantPayload = String.format("{\"tenantCode\":\"%s\",\"tenantName\":\"流程测试\",\"inboundWebhookUrl\":\"http://localhost:9999/webhook\"}", tenantCode);
mockMvc.perform(post("/msg-platform/api/v1/account/tenant")
.contextPath("/msg-platform")
.contentType(MediaType.APPLICATION_JSON)
.content(tenantPayload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
String authToken = createTenantAndLogin(mockMvc, tenantCode, "流程测试");
String flowPayload = "{\"flowCode\":\"inbound_default\",\"flowName\":\"默认入站流程\",\"flowType\":\"inbound\",\"definitionJson\":{\"nodes\":[{\"id\":\"receive\",\"type\":\"receive\"},{\"id\":\"intent\",\"type\":\"intent\"},{\"id\":\"webhook\",\"type\":\"webhook\"}]}}";
mockMvc.perform(post("/msg-platform/api/v1/inbound/flow")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(flowPayload))
@@ -45,12 +41,14 @@ public class InboundFlowControllerTest {
mockMvc.perform(post("/msg-platform/api/v1/inbound/flow/inbound_default/publish")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
mockMvc.perform(get("/msg-platform/api/v1/inbound/flow/inbound_default")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0))
@@ -62,6 +60,7 @@ public class InboundFlowControllerTest {
eventId);
mockMvc.perform(post("/msg-platform/api/v1/inbound/receive")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(messagePayload))

View File

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.UUID;
import static com.sino.mci.test.AuthTestHelper.createTenantAndLogin;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -26,18 +27,12 @@ public class SendControllerTest {
@Test
void shouldTestSendToConversationAndQueryRecords() throws Exception {
String tenantCode = "send-" + UUID.randomUUID().toString().substring(0, 8);
String tenantPayload = String.format("{\"tenantCode\":\"%s\",\"tenantName\":\"发送测试\"}", tenantCode);
mockMvc.perform(post("/msg-platform/api/v1/account/tenant")
.contextPath("/msg-platform")
.contentType(MediaType.APPLICATION_JSON)
.content(tenantPayload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
String authToken = createTenantAndLogin(mockMvc, tenantCode, "发送测试");
String accountPayload = "{\"accountType\":1,\"channelType\":\"wecom\",\"accountId\":\"ZhangSan\",\"accountName\":\"张三\"}";
String accountResponse = mockMvc.perform(post("/msg-platform/api/v1/channel-account")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(accountPayload))
@@ -49,6 +44,7 @@ public class SendControllerTest {
String conversationPayload = "{\"conversationType\":2,\"channelType\":\"wecom\",\"channelConversationId\":\"room-001\",\"conversationName\":\"测试群\"}";
String conversationResponse = mockMvc.perform(post("/msg-platform/api/v1/conversation")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(conversationPayload))
@@ -60,6 +56,7 @@ public class SendControllerTest {
String bindPayload = String.format("{\"accountId\":%d,\"bindingType\":1,\"sortOrder\":1}", accountId);
mockMvc.perform(post("/msg-platform/api/v1/conversation/" + conversationId + "/bind-account")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(bindPayload))
@@ -71,6 +68,7 @@ public class SendControllerTest {
conversationId);
String sendResponse = mockMvc.perform(post("/msg-platform/api/v1/send/test")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(sendPayload))
@@ -83,6 +81,7 @@ public class SendControllerTest {
mockMvc.perform(get("/msg-platform/api/v1/send/" + taskId + "/records")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0))
@@ -94,17 +93,12 @@ public class SendControllerTest {
@Test
void shouldRejectUnsupportedTargetType() throws Exception {
String tenantCode = "send-" + UUID.randomUUID().toString().substring(0, 8);
String tenantPayload = String.format("{\"tenantCode\":\"%s\",\"tenantName\":\"发送测试\"}", tenantCode);
mockMvc.perform(post("/msg-platform/api/v1/account/tenant")
.contextPath("/msg-platform")
.contentType(MediaType.APPLICATION_JSON)
.content(tenantPayload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
String authToken = createTenantAndLogin(mockMvc, tenantCode, "发送测试");
String sendPayload = "{\"targetType\":\"rule\",\"targetValue\":{},\"contentType\":\"text\",\"content\":{\"text\":\"hello\"}}";
mockMvc.perform(post("/msg-platform/api/v1/send/test")
.contextPath("/msg-platform")
.header("Authorization", authToken)
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(sendPayload))

View File

@@ -0,0 +1,64 @@
package com.sino.mci.test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import java.util.UUID;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class AuthTestHelper {
public static final String ADMIN_USERNAME = "admin";
public static final String ADMIN_PASSWORD = "admin123";
public static String createTenantAndLogin(MockMvc mockMvc) throws Exception {
String tenantCode = "test-" + UUID.randomUUID().toString().substring(0, 8);
return createTenantAndLogin(mockMvc, tenantCode, "测试租户");
}
public static String createTenantAndLogin(MockMvc mockMvc, String tenantCode, String tenantName) throws Exception {
createTenant(mockMvc, tenantCode, tenantName);
createAdminUser(mockMvc, tenantCode);
return login(mockMvc, tenantCode);
}
public static void createTenant(MockMvc mockMvc, String tenantCode, String tenantName) throws Exception {
String payload = String.format("{\"tenantCode\":\"%s\",\"tenantName\":\"%s\"}", tenantCode, tenantName);
mockMvc.perform(post("/msg-platform/api/v1/account/tenant")
.contextPath("/msg-platform")
.contentType(MediaType.APPLICATION_JSON)
.content(payload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
}
public static void createAdminUser(MockMvc mockMvc, String tenantCode) throws Exception {
String payload = String.format(
"{\"username\":\"%s\",\"password\":\"%s\",\"realName\":\"管理员\",\"phone\":\"13800138000\",\"roleCodes\":[\"platform_admin\"]}",
ADMIN_USERNAME, ADMIN_PASSWORD);
mockMvc.perform(post("/msg-platform/api/v1/account/user")
.contextPath("/msg-platform")
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(payload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
}
public static String login(MockMvc mockMvc, String tenantCode) throws Exception {
String payload = String.format("{\"username\":\"%s\",\"password\":\"%s\"}", ADMIN_USERNAME, ADMIN_PASSWORD);
MvcResult result = mockMvc.perform(post("/msg-platform/api/v1/account/login")
.contextPath("/msg-platform")
.header("X-Tenant-Code", tenantCode)
.contentType(MediaType.APPLICATION_JSON)
.content(payload))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0))
.andReturn();
return "Bearer " + com.jayway.jsonpath.JsonPath.read(result.getResponse().getContentAsString(), "$.data.token");
}
}