帮我把地址上链接口 单独分出来一个接口,同时修改对应文档

This commit is contained in:
lu7213 committed 2026-10-29 21:50:00 -04:00
1 parent fbc72b5b57
commit dc2564b1d4
4 files changed
+90 -9

No files matched your search

@@ -576,6 +576,65 @@ public class RentalBusinessTest {
assertEquals(money("80"), amount(store.get("account", payer, false), "available_amount"));
}
/** 独立接口沿用真实APP验签与账务事务,拒绝跨订单类型调用。 */
@Test
@SuppressWarnings("unchecked")
public void independentOnchainApiAuthenticatesActualPathAndIsolatesTypes() throws Exception {
Map<String, Object> credential = tx.execute(status -> identity.issue("APP", app, 10));
String key = text(credential, "api_key"), secret = text(credential, "api_secret");
RentalApiController controller = new RentalApiController(identity, store, orders, json);
java.lang.reflect.Field field = RentalApiController.class.getDeclaredField("onchain");
field.setAccessible(true);
java.lang.reflect.Field serviceField = RentalOrders.class.getDeclaredField("onchain");
serviceField.setAccessible(true);
field.set(controller, serviceField.get(orders));
String prefix = "/api/rental/v1/address-onchain/";
Map<String, Object> data = onchainRequest("API-ONCHAIN");
data.remove("order_type");
String body = json.writeValueAsString(data);
Map<String, Object> quote = (Map<String, Object>) controller.executeOnchain("quote",
signedRequest(prefix + "quote", body, key, secret, "API")).get("data");
assertEquals("0.173457", quote.get("amount"));
assertFalse(quote.containsKey("supplier_fee_amount"));
javax.servlet.http.HttpServletRequest oldSignature = signedRequest(
"/api/rental/v1/quote", body, key, secret, "API");
javax.servlet.http.HttpServletRequest newPathWithOldSignature =
(javax.servlet.http.HttpServletRequest) java.lang.reflect.Proxy.newProxyInstance(
getClass().getClassLoader(), new Class<?>[] {javax.servlet.http.HttpServletRequest.class},
(proxy, method, args) -> "getRequestURI".equals(method.getName())
? prefix + "quote" : method.invoke(oldSignature, args));
try {
controller.executeOnchain("quote", newPathWithOldSignature);
fail("新路由不能复用旧路径签名");
} catch (IllegalArgumentException expected) { /* HMAC包含实际完整URI */ }
Map<String, Object> created = (Map<String, Object>) tx.execute(status -> {
try {
return controller.executeOnchain("create", signedRequest(prefix + "create", body, key, secret, "API"));
} catch (Exception ex) {
throw new IllegalArgumentException(ex);
}
}).get("data");
assertEquals("ADDRESS_ONCHAIN", text(created, "order_type"));
String queryBody = json.writeValueAsString(map("merchant_order_no", "API-ONCHAIN"));
Map<String, Object> queried = (Map<String, Object>) controller.executeOnchain("query",
signedRequest(prefix + "query", queryBody, key, secret, "API")).get("data");
assertEquals(text(created, "order_no"), text(queried, "order_no"));
try {
controller.execute("query", signedRequest("/api/rental/v1/query", queryBody, key, secret, "API"));
fail("资源接口不能查询地址上链订单");
} catch (IllegalArgumentException expected) { /* 类型隔离 */ }
data.put("order_type", "ADDRESS_ONCHAIN");
try {
controller.execute("create", signedRequest("/api/rental/v1/create", json.writeValueAsString(data), key, secret, "API"));
fail("旧资源接口不得创建地址上链订单");
} catch (IllegalArgumentException expected) { /* 不保留混用入口 */ }
data.put("order_type", "RESOURCE_RENTAL");
try {
controller.executeOnchain("create", signedRequest(prefix + "create", json.writeValueAsString(data), key, secret, "API"));
fail("地址上链接口不得创建资源订单");
} catch (IllegalArgumentException expected) { /* 类型不允许覆盖 */ }
}
/** Servlet代理保留原始字节,无需启动真实HTTP端口。 */
private javax.servlet.http.HttpServletRequest demoRequest(String operation, String body, String secret) {
return signedRequest("/api/rental/v1/demo/callback/" + operation, body, "RKdemo", secret, "WEBHOOK");