diff --git a/src/application/fileserver/src/test/java/dev/caskeleton/application/fileserver/lifecycle/FileLifecycleUseCasesTest.java b/src/application/fileserver/src/test/java/dev/caskeleton/application/fileserver/lifecycle/FileLifecycleUseCasesTest.java index 5849351e..9273681e 100644 --- a/src/application/fileserver/src/test/java/dev/caskeleton/application/fileserver/lifecycle/FileLifecycleUseCasesTest.java +++ b/src/application/fileserver/src/test/java/dev/caskeleton/application/fileserver/lifecycle/FileLifecycleUseCasesTest.java @@ -54,6 +54,15 @@ import org.junit.jupiter.api.Test; class FileLifecycleUseCasesTest { private static final String DIGEST = "f".repeat(64); + + /** + * A well-formed SHA-256 of some other content: what a client holds after the file changed. + * + *
Shaped like a real digest so a refusal can only come from the comparison, never from the + * value failing to parse. + */ + private static final String STALE_DIGEST = "0".repeat(64); + private static final long SIZE = 10; private static final long MAX_FILE = 1024; private static final StorageNamespace TARGET = StorageNamespace.of("tenant-b"); @@ -187,18 +196,26 @@ class FileLifecycleUseCasesTest { } @Test + @DisplayName("내용 해시가 맞지 않는 삭제는 PRECONDITION_FAILED로 거절되고 레코드는 READY로 남는다") void aStaleValidatorPreconditionIsRejectedBeforeAnyTransition() { + // given FileRecord ready = ReadyFileFixture.ready(metadata, SIZE, DIGEST); - assertThatThrownBy( + // when + Throwable failure = + catchThrowable( () -> delete.handle( new DeleteFileCommand( - ready.fileId(), Optional.of("\"stale\""), FileserverFixtures.context()))) - .isInstanceOf(FileserverException.class); + ready.fileId(), Optional.of(STALE_DIGEST), FileserverFixtures.context()))); - assertThat(metadata.find(ready.fileId()).orElseThrow().state()).isEqualTo(FileState.READY); - assertThat(cleanup.queued()).isEmpty(); + // then + assertThat( + List.of( + codeOf(failure), + metadata.find(ready.fileId()).orElseThrow().state(), + cleanup.queued())) + .containsExactly(FileserverErrorCode.PRECONDITION_FAILED, FileState.READY, List.of()); } @Test @@ -274,6 +291,46 @@ class FileLifecycleUseCasesTest { assertThat(copyGateway.copies()).isEmpty(); } + @Test + @DisplayName("내용 해시가 맞지 않는 복사는 PRECONDITION_FAILED로 거절되고 레코드도 바이트도 쿼터도 만들지 않는다") + void copyWithAStaleDigestIsRefusedBeforeAnythingIsWritten() { + // given + FileRecord source = ReadyFileFixture.ready(metadata, SIZE, DIGEST); + copyGateway.copiesContentOf(SIZE, DIGEST); + + // when + Throwable failure = + catchThrowable(() -> copy.handle(copyOf(source, Optional.of(STALE_DIGEST)))); + + // then + assertThat( + List.of( + codeOf(failure), + copyGateway.copies(), + metadata.find(nextCopyId()).isPresent(), + quota.reservationCount())) + .containsExactly(FileserverErrorCode.PRECONDITION_FAILED, List.of(), false, 0); + } + + @Test + @DisplayName("내용 해시가 맞는 복사는 진행되어 대상 namespace에 READY 파일을 만들고 그 크기를 쿼터에 확정한다") + void copyWithTheCurrentDigestProceeds() { + // given + FileRecord source = ReadyFileFixture.ready(metadata, SIZE, DIGEST); + copyGateway.copiesContentOf(SIZE, DIGEST); + + // when + FileView copied = copy.handle(copyOf(source, Optional.of(DIGEST))); + + // then + assertThat( + List.of( + copied.state(), + copied.descriptor().namespace(), + quota.usedBytes(QuotaScope.ofNamespace(TARGET.value())))) + .containsExactly(FileState.READY, TARGET, SIZE); + } + @Test @DisplayName("저장소가 hard high-water를 넘으면 복사는 레코드도 바이트도 만들지 않고 STORAGE_FULL로 거절된다") void copyAtTheHardHighWaterMarkIsRefusedBeforeAnyRecordOrByteIsWritten() { @@ -462,6 +519,42 @@ class FileLifecycleUseCasesTest { .containsExactly(FileserverErrorCode.ACCESS_DENIED, ready.namespace()); } + /** + * A move under a digest the file no longer has is refused and changes nothing. + * + *
The matching side — a current digest lets the move through — is {@link
+ * #moveChangesOnlyTheNamespaceAndNeverTheContentKey}.
+ */
+ @Test
+ @DisplayName("내용 해시가 맞지 않는 이동은 PRECONDITION_FAILED로 거절되고 파일과 사용량은 그대로다")
+ void moveWithAStaleDigestIsRefusedAndTheFileStays() {
+ // given
+ FileRecord ready = ReadyFileFixture.ready(metadata, SIZE, DIGEST);
+ QuotaScope source = QuotaScope.ofNamespace(ready.namespace().value());
+ QuotaScope target = QuotaScope.ofNamespace(TARGET.value());
+ quota.alreadyHolds(source, SIZE);
+
+ // when
+ Throwable failure =
+ catchThrowable(
+ () ->
+ move.handle(
+ new MoveFileCommand(
+ ready.fileId(),
+ TARGET,
+ Optional.of(STALE_DIGEST),
+ FileserverFixtures.context())));
+
+ // then
+ assertThat(
+ List.of(
+ codeOf(failure),
+ metadata.find(ready.fileId()).orElseThrow().namespace(),
+ quota.usedBytes(source),
+ quota.usedBytes(target)))
+ .containsExactly(FileserverErrorCode.PRECONDITION_FAILED, ready.namespace(), SIZE, 0L);
+ }
+
@Test
@DisplayName("이동은 파일의 바이트를 출발 namespace 사용량에서 대상 namespace 사용량으로 옮긴다")
void moveTransfersTheFilesBytesFromTheSourceNamespaceToTheTarget() {
@@ -520,8 +613,12 @@ class FileLifecycleUseCasesTest {
}
private static CopyFileCommand copyOf(FileRecord source) {
+ return copyOf(source, Optional.empty());
+ }
+
+ private static CopyFileCommand copyOf(FileRecord source, Optional The authorization decision belongs to the injected policy, and this proves the wiring
- * actually reaches it: the same request that succeeds above fails here on the roles alone.
+ * The download use case's {@code @RequiresPermission("file:read")} answers before the
+ * fileserver's policy is consulted, so this pins the gate's code, not the policy's. That the
+ * wiring reaches the injected policy is {@link
+ * #downloadByAWriterWhoseReadPermissionComesFromAnotherTierIsRefusedByTheFileserverPolicy}.
*/
@Test
- void downloadWithoutTheReadRoleIsRefused() throws Exception {
+ @DisplayName("file:read 권한이 없는 호출자의 다운로드는 권한 gate가 AUTHZ_INSUFFICIENT_PERMISSION으로 거절한다")
+ void downloadWithoutTheReadPermissionIsRefusedByThePermissionGate() throws Exception {
+ // given
String fileId = knownFileId();
actAs(Set.of("ROLE_UNRELATED"));
+ // when
MvcResult refused = mockMvc.perform(get("/v1/files/{fileId}/content", fileId)).andReturn();
- assertThat(refused.getResponse().getStatus()).isEqualTo(403);
+ // then
+ assertThat(List.