251 lines
8.3 KiB
Java
251 lines
8.3 KiB
Java
package run.webup.platform;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.Mockito.*;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import java.util.*;
|
|
import org.junit.jupiter.api.*;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
|
|
|
@SpringBootTest(properties = "app.encryption-key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
|
@ActiveProfiles("test")
|
|
class IntegrationModerationTest {
|
|
@Autowired IntegrationService integrations;
|
|
@Autowired IdentityService users;
|
|
@Autowired SecretVault vault;
|
|
@Autowired SiteModerationService moderation;
|
|
@Autowired RuntimeOperationService operations;
|
|
@Autowired HostingService hosting;
|
|
@Autowired ObjectMapper json;
|
|
@Autowired StorageService storage;
|
|
@MockitoBean EphemeralStore ephemeral;
|
|
@MockitoBean AliyunGateway aliyun;
|
|
@MockitoBean MailGateway mail;
|
|
@MockitoBean ObjectStorageGateway objects;
|
|
UUID user, project;
|
|
|
|
@BeforeEach
|
|
void setup() {
|
|
TestDatabase.clear(users.db());
|
|
user = users.create("operator@test.com", "Operator", "StrongPassword123", "test");
|
|
project = UUID.randomUUID();
|
|
users
|
|
.db()
|
|
.update(
|
|
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
|
+ " (?,?,?,'','HTML','')",
|
|
project,
|
|
user,
|
|
"Site");
|
|
users
|
|
.db()
|
|
.update("INSERT INTO project_moderation(project_id,status) VALUES (?,'PENDING')", project);
|
|
org.mockito.Mockito.reset(aliyun, mail, objects);
|
|
}
|
|
|
|
Map<String, String> sms() {
|
|
return Map.of(
|
|
"accessKeyId",
|
|
"fictional-ak",
|
|
"accessKeySecret",
|
|
"fictional-secret",
|
|
"regionId",
|
|
"cn-hangzhou",
|
|
"endpoint",
|
|
"dysmsapi.aliyuncs.com",
|
|
"signName",
|
|
"测试签名",
|
|
"templateCode",
|
|
"SMS_TEST");
|
|
}
|
|
|
|
@Test
|
|
void credentialsAreEncryptedAndNeverReturned() {
|
|
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
|
String returned = integrations.list().toString();
|
|
assertFalse(returned.contains("fictional-secret"));
|
|
assertFalse(returned.contains("fictional-ak"));
|
|
String encrypted =
|
|
users
|
|
.db()
|
|
.queryForObject("SELECT encrypted_payload FROM integration_configs", String.class);
|
|
assertFalse(encrypted.contains("fictional-secret"));
|
|
assertEquals(
|
|
"fictional-secret", integrations.secrets("ALIYUN_SMS", true).get("accessKeySecret"));
|
|
}
|
|
|
|
@Test
|
|
void blankSecretsPreserveExistingValues() {
|
|
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
|
var update = new HashMap<>(sms());
|
|
update.put("accessKeySecret", "");
|
|
integrations.save("ALIYUN_SMS", user, update, false, "test");
|
|
assertEquals(
|
|
"fictional-secret", integrations.secrets("ALIYUN_SMS", false).get("accessKeySecret"));
|
|
assertThrows(ApiError.Failure.class, () -> integrations.secrets("ALIYUN_SMS", true));
|
|
}
|
|
|
|
@Test
|
|
void invalidPortAndUnknownConfigRejected() {
|
|
assertThrows(
|
|
ApiError.Failure.class,
|
|
() ->
|
|
integrations.save(
|
|
"SMTP",
|
|
user,
|
|
Map.of(
|
|
"host",
|
|
"localhost",
|
|
"port",
|
|
"99999",
|
|
"fromEmail",
|
|
"test@example.test",
|
|
"fromName",
|
|
"Test",
|
|
"security",
|
|
"TLS"),
|
|
true,
|
|
"test"));
|
|
assertThrows(
|
|
ApiError.Failure.class,
|
|
() -> integrations.save("ALIYUN_SMS", user, Map.of("unknown", "value"), false, "test"));
|
|
}
|
|
|
|
@Test
|
|
void connectionTestsRecordRealAdapterOutcome() {
|
|
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
|
when(aliyun.call(anyMap(), eq("QuerySmsSignList"), anyString(), anyString(), anyMap()))
|
|
.thenReturn(Map.of("Code", "OK", "RequestId", "fixture-id"));
|
|
integrations.test("ALIYUN_SMS", user, "test");
|
|
assertEquals(
|
|
"CONNECTED",
|
|
users
|
|
.db()
|
|
.queryForObject("SELECT connection_status FROM integration_configs", String.class));
|
|
verify(aliyun).call(anyMap(), eq("QuerySmsSignList"), eq("2017-05-25"), eq("POST"), anyMap());
|
|
}
|
|
|
|
@Test
|
|
void failedTestCannotPretendConnected() {
|
|
integrations.save("ALIYUN_SMS", user, sms(), true, "test");
|
|
when(aliyun.call(anyMap(), anyString(), anyString(), anyString(), anyMap()))
|
|
.thenThrow(new ApiError.Failure(502, "fixture failure"));
|
|
assertThrows(ApiError.Failure.class, () -> integrations.test("ALIYUN_SMS", user, "test"));
|
|
assertEquals(
|
|
"ERROR",
|
|
users
|
|
.db()
|
|
.queryForObject("SELECT connection_status FROM integration_configs", String.class));
|
|
assertEquals(
|
|
1,
|
|
users
|
|
.db()
|
|
.queryForObject(
|
|
"SELECT COUNT(*) FROM integration_test_logs WHERE status='ERROR'", Integer.class));
|
|
}
|
|
|
|
@Test
|
|
void banQueuesSuspensionAndBlocksPublish() {
|
|
moderation.review(project, user, "APPROVED", "通过测试审核", "test");
|
|
operations.checkAllowed(project);
|
|
moderation.review(project, user, "BANNED", "违规测试", "test");
|
|
assertThrows(ApiError.Failure.class, () -> operations.checkAllowed(project));
|
|
assertThrows(ApiError.Failure.class, () -> hosting.deploy(user, project, "blocked", null));
|
|
assertEquals(
|
|
"QUEUED", users.db().queryForObject("SELECT status FROM runtime_operations", String.class));
|
|
assertEquals(
|
|
1,
|
|
users
|
|
.db()
|
|
.queryForObject("SELECT COUNT(*) FROM projects WHERE id=?", Integer.class, project));
|
|
}
|
|
|
|
@Test
|
|
void liftBanDoesNotFakeActualRuntimeRecovery() {
|
|
moderation.review(project, user, "BANNED", "test", "test");
|
|
moderation.review(project, user, "APPROVED", "人工复核通过", "test");
|
|
operations.checkAllowed(project);
|
|
assertEquals(
|
|
"QUEUED", users.db().queryForObject("SELECT status FROM runtime_operations", String.class));
|
|
assertEquals(
|
|
2,
|
|
users
|
|
.db()
|
|
.queryForObject(
|
|
"SELECT COUNT(*) FROM admin_operation_logs WHERE action='SITE_MODERATION'",
|
|
Integer.class));
|
|
}
|
|
|
|
@Test
|
|
void objectUploadsUseProjectPrefixAndReplayWithoutDuplicatePut() {
|
|
integrations.save(
|
|
"S3",
|
|
user,
|
|
Map.of(
|
|
"endpoint",
|
|
"http://localhost:9000",
|
|
"region",
|
|
"us-east-1",
|
|
"bucket",
|
|
"test-bucket",
|
|
"accessKey",
|
|
"fixture-access",
|
|
"secretKey",
|
|
"fixture-secret"),
|
|
true,
|
|
"test");
|
|
var first =
|
|
storage.upload(user, project, "file.txt", "text/plain", "hello".getBytes(), "upload");
|
|
var second =
|
|
storage.upload(user, project, "file.txt", "text/plain", "hello".getBytes(), "upload");
|
|
assertEquals(first.get("id"), second.get("id"));
|
|
verify(objects, times(1))
|
|
.put(
|
|
anyMap(),
|
|
startsWith("projects/" + project + "/objects/"),
|
|
any(),
|
|
eq("text/plain"),
|
|
anyString());
|
|
assertFalse(first.containsKey("object_key"));
|
|
}
|
|
|
|
@Test
|
|
void objectDeletionIsSoftAndCanBeRestored() {
|
|
integrations.save(
|
|
"S3",
|
|
user,
|
|
Map.of(
|
|
"endpoint",
|
|
"http://localhost:9000",
|
|
"region",
|
|
"us-east-1",
|
|
"bucket",
|
|
"test-bucket",
|
|
"accessKey",
|
|
"fixture-access",
|
|
"secretKey",
|
|
"fixture-secret"),
|
|
true,
|
|
"test");
|
|
UUID id =
|
|
UUID.fromString(
|
|
storage
|
|
.upload(user, project, "file.txt", "text/plain", "test".getBytes(), "upload")
|
|
.get("id")
|
|
.toString());
|
|
storage.trash(user, project, id);
|
|
assertThrows(ApiError.Failure.class, () -> storage.download(user, project, id));
|
|
storage.restore(user, project, id);
|
|
assertEquals(
|
|
"READY",
|
|
users
|
|
.db()
|
|
.queryForObject("SELECT status FROM storage_objects WHERE id=?", String.class, id));
|
|
verify(objects, never()).purge(anyMap(), anyString());
|
|
}
|
|
}
|