dengzedong
2024-12-03 22d6c70a50235fb46bd6db500c99406b42d454e6
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.signature.core;
H 2
3 import cn.hutool.core.map.MapUtil;
4 import cn.hutool.core.util.IdUtil;
5 import cn.hutool.crypto.digest.DigestUtil;
6 import com.iailab.framework.signature.core.annotation.ApiSignature;
7 import com.iailab.framework.signature.core.aop.ApiSignatureAspect;
8 import com.iailab.framework.signature.core.redis.ApiSignatureRedisDAO;
9 import org.junit.jupiter.api.Test;
10 import org.junit.jupiter.api.extension.ExtendWith;
11 import org.mockito.InjectMocks;
12 import org.mockito.Mock;
13 import org.mockito.junit.jupiter.MockitoExtension;
14
15 import javax.servlet.http.HttpServletRequest;
16 import java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.StringReader;
19 import java.util.concurrent.TimeUnit;
20
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.*;
24
25 /**
26  * {@link ApiSignatureTest} 的单元测试
27  */
28 @ExtendWith(MockitoExtension.class)
29 public class ApiSignatureTest {
30
31     @InjectMocks
32     private ApiSignatureAspect apiSignatureAspect;
33
34     @Mock
35     private ApiSignatureRedisDAO signatureRedisDAO;
36
37     @Test
38     public void testSignatureGet() throws IOException {
39         // 搞一个签名
40         Long timestamp = System.currentTimeMillis();
41         String nonce = IdUtil.randomUUID();
42         String appId = "xxxxxx";
43         String appSecret = "yyyyyy";
44         String signString = "k1=v1&v1=k1testappId=xxxxxx&nonce=" + nonce + "&timestamp=" + timestamp + "yyyyyy";
45         String sign = DigestUtil.sha256Hex(signString);
46
47         // 准备参数
48         ApiSignature apiSignature = mock(ApiSignature.class);
49         when(apiSignature.appId()).thenReturn("appId");
50         when(apiSignature.timestamp()).thenReturn("timestamp");
51         when(apiSignature.nonce()).thenReturn("nonce");
52         when(apiSignature.sign()).thenReturn("sign");
53         when(apiSignature.timeout()).thenReturn(60);
54         when(apiSignature.timeUnit()).thenReturn(TimeUnit.SECONDS);
55         HttpServletRequest request = mock(HttpServletRequest.class);
56         when(request.getHeader(eq("appId"))).thenReturn(appId);
57         when(request.getHeader(eq("timestamp"))).thenReturn(String.valueOf(timestamp));
58         when(request.getHeader(eq("nonce"))).thenReturn(nonce);
59         when(request.getHeader(eq("sign"))).thenReturn(sign);
60         when(request.getParameterMap()).thenReturn(MapUtil.<String, String[]>builder()
61                 .put("v1", new String[]{"k1"}).put("k1", new String[]{"v1"}).build());
62         when(request.getContentType()).thenReturn("application/json");
63         when(request.getReader()).thenReturn(new BufferedReader(new StringReader("test")));
64         // mock 方法
65         when(signatureRedisDAO.getAppSecret(eq(appId))).thenReturn(appSecret);
66
67         // 调用
68         boolean result = apiSignatureAspect.verifySignature(apiSignature, request);
69         // 断言结果
70         assertTrue(result);
71         // 断言调用
72         verify(signatureRedisDAO).setNonce(eq(nonce), eq(120), eq(TimeUnit.SECONDS));
73     }
74
75 }