package ru.mtuci.messenger; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import ru.mtuci.messenger.storage.ObjectStorage; import ru.mtuci.messenger.storage.OrphanSweeper; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import ru.mtuci.messenger.support.ApiClient; import ru.mtuci.messenger.support.LabContainers; import tools.jackson.databind.JsonNode; import tools.jackson.databind.json.JsonMapper; import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.security.MessageDigest; import java.util.Base64; import java.util.HexFormat; import java.util.List; import java.util.Map; import java.time.Instant; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class AttachmentIntegrationTest { @LocalServerPort int port; @Autowired JsonMapper jsonMapper; @Autowired ObjectStorage storage; @Autowired OrphanSweeper sweeper; private ApiClient api; private final HttpClient http = HttpClient.newHttpClient(); @DynamicPropertySource static void containers(DynamicPropertyRegistry registry) { LabContainers.register(registry); } @BeforeEach void client() { api = new ApiClient(port, jsonMapper); } @Test void uploadIsPrivateAndReadableByAnotherMember() throws Exception { String password = "Example-only-not-a-real-secret-123"; JsonNode alice = register("at", password); JsonNode bob = register("bt", password); JsonNode eve = register("et", password); String aliceToken = login(alice.get("username").asString(), password); String bobToken = login(bob.get("username").asString(), password); String eveToken = login(eve.get("username").asString(), password); String chatId = chat(aliceToken, List.of(alice.get("id").asString(), bob.get("id").asString())); String otherChat = chat(aliceToken, List.of(alice.get("id").asString())); byte[] png = Files.readAllBytes(Path.of("../tests/fixtures/sample.png")); HttpResponse created = upload(aliceToken, chatId, "sample.png", "image/png", png); assertEquals(202, created.statusCode()); JsonNode attachment = jsonMapper.readTree(created.body()); assertEquals("queued", attachment.get("status").asString()); assertEquals(sha256(png), attachment.get("sha256").asString()); assertEquals(png.length, attachment.get("size_bytes").asInt()); assertEquals(false, attachment.has("object_key")); String attachmentId = attachment.get("id").asString(); ApiClient.Result message = api.post("/api/v1/chats/" + chatId + "/messages", aliceToken, Map.of("text", "", "attachment_ids", List.of(attachmentId)), Map.of("Idempotency-Key", UUID.randomUUID().toString())); assertEquals(201, message.status()); assertEquals(attachmentId, message.body().get("attachment_ids").get(0).asString()); JsonNode ready = waitReady(bobToken, attachmentId); JsonNode thumbnail = variant(ready, "thumbnail"); assertEquals(256, thumbnail.get("width").asInt()); assertEquals(160, thumbnail.get("height").asInt()); assertEquals("image/jpeg", thumbnail.get("content_type").asString()); JsonNode crop = variant(ready, "crop"); assertEquals(128, crop.get("width").asInt()); assertEquals(128, crop.get("height").asInt()); JsonNode optimized = variant(ready, "optimized"); assertEquals("image/webp", optimized.get("content_type").asString()); assertEquals(320, optimized.get("width").asInt()); assertEquals(200, optimized.get("height").asInt()); HttpResponse download = download(bobToken, attachmentId); assertEquals(200, download.statusCode()); assertEquals("image/png", download.headers().firstValue("Content-Type").orElse("")); assertArrayEquals(png, download.body()); assertEquals(404, api.get("/api/v1/attachments/" + attachmentId, eveToken).status()); assertEquals(404, download(eveToken, attachmentId).statusCode()); HttpResponse badName = upload(aliceToken, chatId, "../secret.png", "image/png", png); assertEquals(400, badName.statusCode()); HttpResponse spoofed = upload(aliceToken, chatId, "photo.png", "image/png", new byte[] {1, 2, 3, 4}); assertEquals(415, spoofed.statusCode()); ApiClient.Result foreign = api.post("/api/v1/chats/" + otherChat + "/messages", aliceToken, Map.of("text", "", "attachment_ids", List.of(attachmentId)), Map.of("Idempotency-Key", UUID.randomUUID().toString())); assertEquals(404, foreign.status()); assertEquals("not_found", foreign.body().get("error").get("code").asString()); ApiClient.Result missing = api.post("/api/v1/chats/" + chatId + "/messages", aliceToken, Map.of("text", "", "attachment_ids", List.of(UUID.randomUUID().toString())), Map.of("Idempotency-Key", UUID.randomUUID().toString())); assertEquals(404, missing.status()); HttpResponse corrupt = upload(aliceToken, chatId, "broken.png", "image/png", new byte[] {(byte) 137, 80, 78, 71, 13, 10, 26, 10, 0, 1, 2}); assertEquals(415, corrupt.statusCode()); assertEquals("unsupported_media_type", jsonMapper.readTree(corrupt.body()).get("error").get("code").asString()); byte[] jpeg = new byte[] { (byte) 0xff, (byte) 0xd8, (byte) 0xff, (byte) 0xc0, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00, (byte) 0xff, (byte) 0xd9 }; HttpResponse mime = upload(aliceToken, chatId, "photo.png", "image/png", jpeg); assertEquals(415, mime.statusCode()); HttpResponse tooBig = upload(aliceToken, chatId, "big.png", "image/png", new byte[10_485_761]); assertEquals(413, tooBig.statusCode()); assertEquals("payload_too_large", jsonMapper.readTree(tooBig.body()).get("error").get("code").asString()); assertTrue(tooBig.statusCode() < 500); } @Test void sweepDropsOrphanAndKeepsReferencedObject() throws Exception { String password = "Example-only-not-a-real-secret-123"; JsonNode alice = register("sw", password); String token = login(alice.get("username").asString(), password); String chatId = chat(token, List.of(alice.get("id").asString())); byte[] png = Files.readAllBytes(Path.of("../tests/fixtures/sample.png")); HttpResponse created = upload(token, chatId, "sample.png", "image/png", png); assertEquals(202, created.statusCode()); String id = jsonMapper.readTree(created.body()).get("id").asString(); waitReady(token, id); storage.put("originals/orphan-" + UUID.randomUUID(), new byte[] {9, 9, 9}, "application/octet-stream"); int removed = sweeper.sweepOlderThan(Instant.now().plusSeconds(120)); assertTrue(removed >= 1); assertEquals(200, download(token, id).statusCode()); assertTrue(storage.list("originals/").stream().noneMatch(object -> object.key().contains("orphan-"))); } private JsonNode waitReady(String token, String id) throws Exception { long deadline = System.nanoTime() + java.time.Duration.ofSeconds(40).toNanos(); JsonNode last = null; while (System.nanoTime() < deadline) { ApiClient.Result result = api.get("/api/v1/attachments/" + id, token); assertEquals(200, result.status()); last = result.body(); String status = last.get("status").asString(); if ("ready".equals(status)) { return last; } if ("failed".equals(status)) { fail("attachment failed: " + last.get("error_code")); } Thread.sleep(200); } fail("attachment did not become ready: " + last); return last; } private static JsonNode variant(JsonNode attachment, String name) { for (JsonNode variant : attachment.get("variants")) { if (name.equals(variant.get("name").asString())) { return variant; } } fail("missing variant " + name); return null; } private HttpResponse upload(String token, String chatId, String filename, String type, byte[] body) throws Exception { String name = URLEncoder.encode(filename, StandardCharsets.UTF_8); HttpRequest request = HttpRequest.newBuilder(URI.create(base() + "/api/v1/chats/" + chatId + "/attachments?filename=" + name)) .header("Authorization", "Bearer " + token) .header("Content-Type", type) .POST(HttpRequest.BodyPublishers.ofByteArray(body)) .build(); return http.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); } private HttpResponse download(String token, String attachmentId) throws Exception { HttpRequest request = HttpRequest.newBuilder(URI.create(base() + "/api/v1/attachments/" + attachmentId + "/content")) .header("Authorization", "Bearer " + token) .GET() .build(); return http.send(request, HttpResponse.BodyHandlers.ofByteArray()); } private String base() { return "http://127.0.0.1:" + port; } private String chat(String token, List members) { return api.post("/api/v1/chats", token, Map.of("title", "Вложения", "member_ids", members)).body().get("id").asString(); } private JsonNode register(String prefix, String password) { String username = (prefix + UUID.randomUUID().toString().replace("-", "")).substring(0, 20); ApiClient.Result result = api.post("/api/v1/users", api.user(username, "Алиса", password)); assertEquals(201, result.status()); return result.body(); } private String login(String username, String password) { String basic = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8)); ApiClient.Result session = api.postBasic("/api/v1/auth/sessions", basic); assertEquals(201, session.status()); return session.body().get("token").asString(); } private static String sha256(byte[] bytes) throws Exception { return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)); } }