44 lines
2.5 KiB
Java
44 lines
2.5 KiB
Java
package com.ruoyi.system.rental;
|
|
|
|
import static com.ruoyi.system.rental.RentalValues.*;
|
|
import static org.junit.Assert.*;
|
|
import java.math.BigDecimal;
|
|
import java.util.Map;
|
|
import org.junit.Test;
|
|
|
|
/** 计费与回调签名协议的纯函数回归,不访问外部服务。 */
|
|
public class RentalPricingTest {
|
|
@Test
|
|
public void durationScalesBothPricesWithoutChangingQuantityOrOriginal() {
|
|
Map<String, Object> product = map("duration_seconds", 3600, "sale_amount", "3.000000",
|
|
"supplier_amount", "2.000000", "resource_quantity", 65000);
|
|
Map<String, Object> price = RentalPricing.price(product, map("duration_seconds", 7200));
|
|
assertEquals(new BigDecimal("6.000000"), amount(price, "sale_amount"));
|
|
assertEquals(new BigDecimal("4.000000"), amount(price, "supplier_amount"));
|
|
assertEquals(65000, number(price, "resource_quantity"));
|
|
assertEquals(3600, number(product, "duration_seconds"));
|
|
for (long duration : new long[] {0, -1, 3601, 3600L * 721}) {
|
|
assertThrows(IllegalArgumentException.class,
|
|
() -> RentalPricing.price(product, map("duration_seconds", duration)));
|
|
}
|
|
}
|
|
|
|
@Test
|
|
public void callbackRejectsWrongScopeTamperingPathAndExpiredTime() {
|
|
long timestamp = System.currentTimeMillis();
|
|
String key = "RKdemo", secret = "test-secret", nonce = "1234567890abcdef";
|
|
String path = "/api/rental/v1/demo/callback/sync", body = "{\"event_no\":\"WH1\"}";
|
|
String signature = RentalCrypto.sign(secret, "WEBHOOK", key, timestamp, nonce, "POST", path, body);
|
|
RentalCallbackDemoController.verify(key, secret, key, timestamp, nonce, path, body, signature, timestamp);
|
|
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
|
|
key, secret, key, timestamp, nonce, path, body + " ", signature, timestamp));
|
|
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
|
|
key, secret, key, timestamp, nonce, path + "x", body, signature, timestamp));
|
|
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
|
|
key, secret, key, timestamp, nonce, path, body, signature, timestamp + 61000));
|
|
String apiSignature = RentalCrypto.sign(secret, "API", key, timestamp, nonce, "POST", path, body);
|
|
assertThrows(IllegalArgumentException.class, () -> RentalCallbackDemoController.verify(
|
|
key, secret, key, timestamp, nonce, path, body, apiSignature, timestamp));
|
|
}
|
|
}
|