|
15 | 15 | import org.mockito.Mock; |
16 | 16 | import org.mockito.junit.jupiter.MockitoExtension; |
17 | 17 |
|
| 18 | +import com.opensource.docgrid.domain.collection.entity.DocumentCollection; |
18 | 19 | import com.opensource.docgrid.domain.collection.fixture.CollectionFixture; |
19 | 20 | import com.opensource.docgrid.domain.collection.repository.CollectionRepository; |
20 | 21 | import com.opensource.docgrid.domain.document.entity.Document; |
@@ -310,6 +311,129 @@ void canAdminDocument_noPermission_returnsFalse() { |
310 | 311 | assertThat(result).isFalse(); |
311 | 312 | } |
312 | 313 |
|
| 314 | + // ==================== canReadCollection ==================== |
| 315 | + |
| 316 | + @Test |
| 317 | + @DisplayName("컬렉션 소유자는 canReadCollection이 true다") |
| 318 | + void canReadCollection_owner_returnsTrue() { |
| 319 | + User owner = CollectionFixture.createOwner(); |
| 320 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)) |
| 321 | + .willReturn(Optional.of(CollectionFixture.createCollection(owner))); |
| 322 | + |
| 323 | + boolean result = service.canReadCollection(CollectionFixture.USER_ID, CollectionFixture.COLLECTION_ID); |
| 324 | + |
| 325 | + assertThat(result).isTrue(); |
| 326 | + then(collectionPermissionRepository).should(never()).existsUserReadPermission(CollectionFixture.USER_ID, CollectionFixture.COLLECTION_ID); |
| 327 | + } |
| 328 | + |
| 329 | + @Test |
| 330 | + @DisplayName("PUBLIC 컬렉션은 누구나 canReadCollection이 true다") |
| 331 | + void canReadCollection_public_returnsTrue() { |
| 332 | + User owner = CollectionFixture.createOwner(); |
| 333 | + DocumentCollection collection = CollectionFixture.createCollection(owner); |
| 334 | + org.springframework.test.util.ReflectionTestUtils.setField(collection, "visibility", VisibilityType.PUBLIC); |
| 335 | + Long otherUserId = 99L; |
| 336 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)).willReturn(Optional.of(collection)); |
| 337 | + |
| 338 | + boolean result = service.canReadCollection(otherUserId, CollectionFixture.COLLECTION_ID); |
| 339 | + |
| 340 | + assertThat(result).isTrue(); |
| 341 | + then(collectionPermissionRepository).should(never()).existsUserReadPermission(otherUserId, CollectionFixture.COLLECTION_ID); |
| 342 | + } |
| 343 | + |
| 344 | + @Test |
| 345 | + @DisplayName("USER 권한으로 read가 부여된 경우 canReadCollection이 true다") |
| 346 | + void canReadCollection_userPermission_returnsTrue() { |
| 347 | + User owner = CollectionFixture.createOwner(); |
| 348 | + Long otherUserId = 99L; |
| 349 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)) |
| 350 | + .willReturn(Optional.of(CollectionFixture.createCollection(owner))); |
| 351 | + given(collectionPermissionRepository.existsUserReadPermission(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 352 | + .willReturn(true); |
| 353 | + |
| 354 | + boolean result = service.canReadCollection(otherUserId, CollectionFixture.COLLECTION_ID); |
| 355 | + |
| 356 | + assertThat(result).isTrue(); |
| 357 | + } |
| 358 | + |
| 359 | + @Test |
| 360 | + @DisplayName("ROLE live 권한이 있으면 canReadCollection이 true다") |
| 361 | + void canReadCollection_roleLive_returnsTrue() { |
| 362 | + User owner = CollectionFixture.createOwner(); |
| 363 | + Long otherUserId = 99L; |
| 364 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)) |
| 365 | + .willReturn(Optional.of(CollectionFixture.createCollection(owner))); |
| 366 | + given(collectionPermissionRepository.existsUserReadPermission(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 367 | + .willReturn(false); |
| 368 | + given(collectionPermissionRepository.existsRoleReadPermissionForCollection(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 369 | + .willReturn(true); |
| 370 | + |
| 371 | + boolean result = service.canReadCollection(otherUserId, CollectionFixture.COLLECTION_ID); |
| 372 | + |
| 373 | + assertThat(result).isTrue(); |
| 374 | + } |
| 375 | + |
| 376 | + @Test |
| 377 | + @DisplayName("DEPARTMENT live 권한이 있으면 canReadCollection이 true다") |
| 378 | + void canReadCollection_deptLive_returnsTrue() { |
| 379 | + User owner = CollectionFixture.createOwner(); |
| 380 | + Long otherUserId = 99L; |
| 381 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)) |
| 382 | + .willReturn(Optional.of(CollectionFixture.createCollection(owner))); |
| 383 | + given(collectionPermissionRepository.existsUserReadPermission(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 384 | + .willReturn(false); |
| 385 | + given(collectionPermissionRepository.existsRoleReadPermissionForCollection(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 386 | + .willReturn(false); |
| 387 | + given(collectionPermissionRepository.existsDeptReadPermissionForCollection(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 388 | + .willReturn(true); |
| 389 | + |
| 390 | + boolean result = service.canReadCollection(otherUserId, CollectionFixture.COLLECTION_ID); |
| 391 | + |
| 392 | + assertThat(result).isTrue(); |
| 393 | + } |
| 394 | + |
| 395 | + @Test |
| 396 | + @DisplayName("권한이 없으면 canReadCollection이 false다") |
| 397 | + void canReadCollection_noPermission_returnsFalse() { |
| 398 | + User owner = CollectionFixture.createOwner(); |
| 399 | + Long otherUserId = 99L; |
| 400 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)) |
| 401 | + .willReturn(Optional.of(CollectionFixture.createCollection(owner))); |
| 402 | + given(collectionPermissionRepository.existsUserReadPermission(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 403 | + .willReturn(false); |
| 404 | + given(collectionPermissionRepository.existsRoleReadPermissionForCollection(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 405 | + .willReturn(false); |
| 406 | + given(collectionPermissionRepository.existsDeptReadPermissionForCollection(otherUserId, CollectionFixture.COLLECTION_ID)) |
| 407 | + .willReturn(false); |
| 408 | + |
| 409 | + boolean result = service.canReadCollection(otherUserId, CollectionFixture.COLLECTION_ID); |
| 410 | + |
| 411 | + assertThat(result).isFalse(); |
| 412 | + } |
| 413 | + |
| 414 | + @Test |
| 415 | + @DisplayName("컬렉션이 없으면 canReadCollection 호출 시 COLLECTION_NOT_FOUND 예외가 발생한다") |
| 416 | + void canReadCollection_collectionNotFound_throwsException() { |
| 417 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)).willReturn(Optional.empty()); |
| 418 | + |
| 419 | + assertThatThrownBy(() -> service.canReadCollection(CollectionFixture.USER_ID, CollectionFixture.COLLECTION_ID)) |
| 420 | + .isInstanceOf(DocGridException.class) |
| 421 | + .hasFieldOrPropertyWithValue("errorCode", ErrorCode.COLLECTION_NOT_FOUND); |
| 422 | + } |
| 423 | + |
| 424 | + @Test |
| 425 | + @DisplayName("삭제된 컬렉션이면 canReadCollection 호출 시 COLLECTION_NOT_FOUND 예외가 발생한다") |
| 426 | + void canReadCollection_collectionDeleted_throwsException() { |
| 427 | + User owner = CollectionFixture.createOwner(); |
| 428 | + DocumentCollection collection = CollectionFixture.createCollection(owner); |
| 429 | + org.springframework.test.util.ReflectionTestUtils.setField(collection, "status", com.opensource.docgrid.domain.collection.enums.CollectionStatus.DELETED); |
| 430 | + given(collectionRepository.findById(CollectionFixture.COLLECTION_ID)).willReturn(Optional.of(collection)); |
| 431 | + |
| 432 | + assertThatThrownBy(() -> service.canReadCollection(CollectionFixture.USER_ID, CollectionFixture.COLLECTION_ID)) |
| 433 | + .isInstanceOf(DocGridException.class) |
| 434 | + .hasFieldOrPropertyWithValue("errorCode", ErrorCode.COLLECTION_NOT_FOUND); |
| 435 | + } |
| 436 | + |
313 | 437 | // ==================== canWriteCollection ==================== |
314 | 438 |
|
315 | 439 | @Test |
|
0 commit comments