CarDataApiControllerTest.java
6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.bs.cardata.controller;
import com.bs.cardata.BsApplication;
import com.bs.cardata.constant.ServiceConstants;
import com.bs.common.core.redis.RedisService;
import com.bs.common.utils.sign.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.UUID;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* CarDataApiController clearCache 接口测试
*/
@Slf4j
@SpringBootTest(classes = BsApplication.class, properties = "spring.application.name=bs-car-data")
@AutoConfigureMockMvc
@ActiveProfiles("local")
public class CarDataApiControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private RedisService redisService;
/**
* 生成合法加密参数
*/
private String[] generateValidParams() {
String nonce = UUID.randomUUID().toString();
String timestamp = String.valueOf(System.currentTimeMillis());
String plainText = nonce + timestamp + ServiceConstants.CLEAR_CACHE_SECRET;
String token = Md5Utils.getMD532Str(plainText);
return new String[]{token, timestamp, nonce};
}
/**
* 测试正常清除缓存
*/
@Test
public void testClearCache_success() throws Exception {
String[] params = generateValidParams();
mockMvc.perform(get("/common/api/clearCache")
.param("token", params[0])
.param("timestamp", params[1])
.param("nonce", params[2]))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.msg").value("缓存清除成功"));
log.info("testClearCache_success 通过");
}
/**
* 测试时间戳格式错误
*/
@Test
public void testClearCache_invalidTimestamp() throws Exception {
String nonce = UUID.randomUUID().toString();
String token = Md5Utils.getMD532Str(nonce + "abc" + ServiceConstants.CLEAR_CACHE_SECRET);
mockMvc.perform(get("/common/api/clearCache")
.param("token", token)
.param("timestamp", "abc")
.param("nonce", nonce))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.msg").value("时间戳格式不正确"));
log.info("testClearCache_invalidTimestamp 通过");
}
/**
* 测试时间戳过期(超出5分钟窗口)
*/
@Test
public void testClearCache_expiredTimestamp() throws Exception {
String nonce = UUID.randomUUID().toString();
// 构造一个6分钟前的时间戳
String timestamp = String.valueOf(System.currentTimeMillis() - 6 * 60 * 1000L);
String plainText = nonce + timestamp + ServiceConstants.CLEAR_CACHE_SECRET;
String token = Md5Utils.getMD532Str(plainText);
mockMvc.perform(get("/common/api/clearCache")
.param("token", token)
.param("timestamp", timestamp)
.param("nonce", nonce))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.msg").value("请求已过期,时间戳超出有效窗口"));
log.info("testClearCache_expiredTimestamp 通过");
}
/**
* 测试签名校验失败(错误的token)
*/
@Test
public void testClearCache_invalidToken() throws Exception {
String nonce = UUID.randomUUID().toString();
String timestamp = String.valueOf(System.currentTimeMillis());
mockMvc.perform(get("/common/api/clearCache")
.param("token", "invalid_token_123456")
.param("timestamp", timestamp)
.param("nonce", nonce))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.msg").value("加密参数校验失败"));
log.info("testClearCache_invalidToken 通过");
}
/**
* 测试加密参数只能使用一次(重放攻击)
*/
@Test
public void testClearCache_nonceReuse() throws Exception {
String[] params = generateValidParams();
// 第一次调用:应成功
mockMvc.perform(get("/common/api/clearCache")
.param("token", params[0])
.param("timestamp", params[1])
.param("nonce", params[2]))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200));
// 第二次使用相同参数:应被拒绝
mockMvc.perform(get("/common/api/clearCache")
.param("token", params[0])
.param("timestamp", params[1])
.param("nonce", params[2]))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.msg").value("该加密参数已被使用,请重新生成"));
log.info("testClearCache_nonceReuse 通过");
}
/**
* 测试缺少必要参数
*/
@Test
public void testClearCache_missingParams() throws Exception {
mockMvc.perform(get("/common/api/clearCache"))
.andDo(print())
.andExpect(status().isBadRequest());
log.info("testClearCache_missingParams 通过");
}
}