fix: verify payment callbacks
This commit is contained in:
1 parent
6d868552eb
commit
a75869342c
3 files changed
+229
-21
No files matched your search
+89
@@ -0,0 +1,89 @@
|
||||
package com.ruoyi.web.controller.system;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.ruoyi.system.service.PaymentSignatureService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SysUsdtDemoControllerTest {
|
||||
private static final String API_KEY = "demo-api-key";
|
||||
private static final String API_SECRET = "demo-api-secret-value";
|
||||
private SysUsdtDemoController controller;
|
||||
private Map<String, Object> values;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
controller = new SysUsdtDemoController() {
|
||||
@Override
|
||||
protected String requiredEnv(String name) {
|
||||
return API_SECRET;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String requiredEnvWithAlias(String name, String alias) {
|
||||
return API_KEY;
|
||||
}
|
||||
};
|
||||
values = new HashMap<String, Object>();
|
||||
values.put("order_id", "merchant-1001");
|
||||
values.put("trade_id", "trade-1001");
|
||||
values.put("status", 2);
|
||||
values.put("transaction_id", "tx-1001");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notifyShouldAcceptValidSignature() {
|
||||
String signature = PaymentSignatureService.sign(values, API_SECRET);
|
||||
|
||||
ResponseEntity<String> response = controller.notify(API_KEY, signature,
|
||||
JSON.toJSONString(values));
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals("ok", response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notifyShouldRejectForgedSignature() {
|
||||
ResponseEntity<String> response = controller.notify(API_KEY, "forged",
|
||||
JSON.toJSONString(values));
|
||||
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||
assertEquals("invalid signature", response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notifyShouldRejectWrongApiKey() {
|
||||
String signature = PaymentSignatureService.sign(values, API_SECRET);
|
||||
|
||||
ResponseEntity<String> response = controller.notify("wrong-key", signature,
|
||||
JSON.toJSONString(values));
|
||||
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void redirectShouldAcceptValidSignature() {
|
||||
String signature = PaymentSignatureService.sign(values, API_SECRET);
|
||||
|
||||
ResponseEntity<String> response = controller.redirect("merchant-1001", "trade-1001",
|
||||
2, "tx-1001", API_KEY, signature);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals("redirect SUCCESS", response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void redirectShouldRejectMissingOrInvalidSignature() {
|
||||
ResponseEntity<String> response = controller.redirect("merchant-1001", "trade-1001",
|
||||
2, "tx-1001", API_KEY, null);
|
||||
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user