回调验证和调试日志
This commit is contained in:
1 parent
f15f4162eb
commit
4818f25d8f
4 files changed
+164
-56
No files matched your search
+69
@@ -0,0 +1,69 @@
|
||||
package com.ruoyi.web.controller.system;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.system.controller.WebhookDebugController;
|
||||
import com.ruoyi.system.rental.RentalCrypto;
|
||||
import com.ruoyi.system.rental.RentalStore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
/** 验证原文签名、代理路径、重放和HTTP方法,不连接真实数据库或回调目标。 */
|
||||
public class WebhookDebugControllerTest {
|
||||
private final RentalStore store = mock(RentalStore.class);
|
||||
private final WebhookDebugController controller = new WebhookDebugController(store, new ObjectMapper());
|
||||
private final String body = "{\"event_no\":\"WH-test\",\"event_type\":\"SUCCESS\"}";
|
||||
private final String externalPath = "/api/api/webhook/debug";
|
||||
|
||||
private void configure() {
|
||||
ReflectionTestUtils.setField(controller, "apiKey", "test-key");
|
||||
ReflectionTestUtils.setField(controller, "apiSecret", "test-secret");
|
||||
ReflectionTestUtils.setField(controller, "signaturePath", externalPath);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest signed(String content, long timestamp, String path) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/webhook/debug");
|
||||
request.setContentType("application/json");
|
||||
request.setContent(content.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
request.addHeader("X-Rent-Key", "test-key");
|
||||
request.addHeader("X-Rent-Timestamp", Long.toString(timestamp));
|
||||
request.addHeader("X-Rent-Nonce", "abcdefghijklmnop");
|
||||
request.addHeader("X-Rent-Signature", RentalCrypto.sign("test-secret", "WEBHOOK", "test-key",
|
||||
timestamp, "abcdefghijklmnop", "POST", path, body));
|
||||
return request;
|
||||
}
|
||||
|
||||
@Test public void verifiedPostUsesExternalPathAndRejectsReplay() throws Exception {
|
||||
configure();
|
||||
MockHttpServletRequest request = signed(body, System.currentTimeMillis(), externalPath);
|
||||
assertEquals("ok", controller.receive(request).getBody());
|
||||
verify(store).insert(eq("nonce"), argThat(values -> "DEBUG_WEBHOOK".equals(values.get("scope"))));
|
||||
when(store.insert(eq("nonce"), anyMap())).thenThrow(new DuplicateKeyException("duplicate"));
|
||||
assertEquals(409, controller.receive(signed(body, System.currentTimeMillis(), externalPath)).getStatusCodeValue());
|
||||
}
|
||||
|
||||
@Test public void tamperedExpiredOrWrongPathCannotReachNonceStore() throws Exception {
|
||||
configure();
|
||||
assertEquals(401, controller.receive(signed(body + " ", System.currentTimeMillis(), externalPath)).getStatusCodeValue());
|
||||
assertEquals(401, controller.receive(signed(body, System.currentTimeMillis() - 120000, externalPath)).getStatusCodeValue());
|
||||
assertEquals(401, controller.receive(signed(body, System.currentTimeMillis(), "/wrong")).getStatusCodeValue());
|
||||
verifyNoInteractions(store);
|
||||
}
|
||||
|
||||
@Test public void postMappingAndMissingConfigurationAreExplicit() throws Exception {
|
||||
MockMvc mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
mvc.perform(get("/api/webhook/debug")).andExpect(status().isMethodNotAllowed());
|
||||
mvc.perform(post("/api/webhook/debug").contentType("application/json").content(body))
|
||||
.andExpect(status().isServiceUnavailable());
|
||||
configure();
|
||||
mvc.perform(post("/api/webhook/debug").contentType("application/json").content(body))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user