OssConfigTest.java
6.42 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
package com.youjia.oss;
import com.youjia.common.core.ResultCode;
import com.youjia.common.exception.BusinessException;
import com.youjia.framework.oss.OssBucketProperties;
import com.youjia.framework.oss.OssPaths;
import com.youjia.framework.oss.OssProperties;
import com.youjia.framework.oss.OssStsService;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* OSS 配置与路径规则单元测试(不连网、不依赖真实密钥)。
*/
class OssConfigTest {
private static OssBucketProperties bucket(String name, String endpoint) {
OssBucketProperties cfg = new OssBucketProperties();
cfg.setBucket(name);
cfg.setEndpoint(endpoint);
cfg.setRegionId("cn-shenzhen");
cfg.setAccessKeyId("ak-" + name);
cfg.setAccessKeySecret("sk-" + name);
return cfg;
}
private static OssProperties properties(List<OssBucketProperties> buckets, String defaultBucket) {
OssProperties properties = new OssProperties();
properties.setBuckets(buckets);
properties.setDefaultBucket(defaultBucket);
return properties;
}
@Test
void 多套配置按bucket名称取用() {
OssProperties properties = properties(
List.of(bucket("store-a", "oss-cn-shenzhen.aliyuncs.com"),
bucket("store-b", "oss-cn-hangzhou.aliyuncs.com")),
"store-a");
assertEquals(2, properties.bucketNames().size());
assertEquals("oss-cn-shenzhen.aliyuncs.com", properties.resolve("store-a").getEndpoint());
assertEquals("oss-cn-hangzhou.aliyuncs.com", properties.resolve("store-b").getEndpoint());
// 不传 bucket 时取默认
assertEquals("store-a", properties.resolve(null).getBucket());
assertEquals("store-a", properties.resolve(" ").getBucket());
}
@Test
void 只有一套配置时不传bucket也能取到() {
OssProperties properties = properties(List.of(bucket("only", "oss-cn-shenzhen.aliyuncs.com")), null);
assertEquals("only", properties.resolve(null).getBucket());
}
@Test
void 未配置的bucket应明确报错() {
OssProperties properties = properties(List.of(bucket("store-a", "oss-cn-shenzhen.aliyuncs.com")), null);
BusinessException ex = assertThrows(BusinessException.class, () -> properties.resolve("not-exist"));
assertEquals(ResultCode.OSS_BUCKET_NOT_CONFIGURED.getCode(), ex.getCode());
}
@Test
void 关闭开关后直接拒绝() {
OssProperties properties = properties(List.of(bucket("store-a", "oss-cn-shenzhen.aliyuncs.com")), "store-a");
properties.setEnabled(false);
BusinessException ex = assertThrows(BusinessException.class, () -> properties.resolve("store-a"));
assertEquals(ResultCode.OSS_NOT_ENABLED.getCode(), ex.getCode());
}
@Test
void 重复bucket名称应启动失败() {
assertThrows(IllegalStateException.class,
() -> properties(List.of(bucket("dup", "e1"), bucket("dup", "e2")), null));
}
@Test
void 默认bucket不在列表中应启动失败() {
OssProperties properties = properties(List.of(bucket("store-a", "e1")), "store-x");
assertThrows(IllegalStateException.class, properties::validate);
}
@Test
void 启用但缺少密钥应启动失败() {
OssBucketProperties cfg = bucket("bad", "oss-cn-shenzhen.aliyuncs.com");
cfg.setAccessKeyId("");
OssProperties properties = properties(List.of(cfg), null);
assertThrows(IllegalStateException.class, properties::validate);
}
@Test
void 目录拼接会合并baseDir并拒绝越权路径() {
OssBucketProperties cfg = bucket("store-a", "oss-cn-shenzhen.aliyuncs.com");
cfg.setBaseDir("/youjia/dev/");
assertEquals("youjia/dev/store/import", OssPaths.resolveDir(cfg, "/store/import/"));
assertEquals("youjia/dev", OssPaths.resolveDir(cfg, null));
assertThrows(BusinessException.class, () -> OssPaths.resolveDir(cfg, "../secret"));
}
@Test
void objectKey按日期与UUID生成且保留扩展名() {
OssBucketProperties cfg = bucket("store-a", "oss-cn-shenzhen.aliyuncs.com");
cfg.setBaseDir("youjia/dev");
String key = OssPaths.buildObjectKey(cfg, "store/import", "门店导入.XLSX");
assertTrue(key.startsWith("youjia/dev/store/import/"), key);
assertTrue(key.endsWith(".xlsx"), key);
// 中间应含 yyyyMMdd 目录段
String[] segments = key.split("/");
assertEquals(6, segments.length, key);
assertTrue(segments[4].matches("\\d{8}"), key);
assertTrue(segments[5].matches("[0-9a-f]{32}\\.xlsx"), key);
}
@Test
void 文件类型与大小校验() {
OssBucketProperties cfg = bucket("store-a", "oss-cn-shenzhen.aliyuncs.com");
cfg.setAllowedExtensions(List.of("xlsx", "png"));
cfg.setMaxFileSizeMb(1);
OssPaths.validateFile(cfg, "a.xlsx", 1024);
BusinessException typeEx = assertThrows(BusinessException.class,
() -> OssPaths.validateFile(cfg, "a.exe", 1024));
assertEquals(ResultCode.OSS_FILE_TYPE_NOT_ALLOWED.getCode(), typeEx.getCode());
BusinessException sizeEx = assertThrows(BusinessException.class,
() -> OssPaths.validateFile(cfg, "a.xlsx", 2 * 1024 * 1024));
assertEquals(ResultCode.OSS_FILE_TOO_LARGE.getCode(), sizeEx.getCode());
}
@Test
void STS策略只授权指定目录的写操作() {
OssStsService service = new OssStsService(null, null);
String policy = service.buildPolicy("store-a", "youjia/dev/store/import");
assertTrue(policy.contains("acs:oss:*:*:store-a/youjia/dev/store/import/*"), policy);
assertTrue(policy.contains("oss:PutObject"), policy);
assertTrue(policy.contains("oss:CompleteMultipartUpload"), policy);
assertFalse(policy.contains("oss:DeleteObject"), policy);
}
@Test
void 未配置STS角色时不可下发临时密钥() {
OssBucketProperties cfg = bucket("store-a", "oss-cn-shenzhen.aliyuncs.com");
assertFalse(cfg.stsAvailable());
cfg.setRoleArn("acs:ram::123:role/oss-upload");
assertTrue(cfg.stsAvailable());
}
}