Skip to content

Commit f5f0e6e

Browse files
authored
fix findAll dataset only return datasets of organization (#833)
1 parent f0fc9b4 commit f5f0e6e

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

dataset/src/integrationTest/kotlin/com/cosmotech/dataset/service/DatasetServiceIntegrationTest.kt

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
339339
organizationSaved = organizationApiService.registerOrganization(organization)
340340
val numberOfDatasets = 20
341341
val defaultPageSize = csmPlatformProperties.twincache.dataset.defaultPageSize
342-
val expectedSize = 15
342+
val expectedPageSize = 15
343343
IntRange(1, numberOfDatasets).forEach {
344344
datasetApiService.createDataset(
345345
organizationSaved.id!!, makeDataset("d-dataset-$it", "dataset-$it"))
@@ -351,19 +351,19 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
351351

352352
logger.info("should find all datasets and assert there are $numberOfDatasets")
353353
var datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, null, null)
354-
assertEquals(numberOfDatasets + 1, datasetList.size)
354+
assertEquals(numberOfDatasets, datasetList.size)
355355

356356
logger.info("should find all datasets and assert it equals defaultPageSize: $defaultPageSize")
357357
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, null)
358358
assertEquals(defaultPageSize, datasetList.size)
359359

360-
logger.info("should find all datasets and assert there are expected size: $expectedSize")
361-
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, expectedSize)
362-
assertEquals(expectedSize, datasetList.size)
360+
logger.info("should find all datasets and assert there are expected size: $expectedPageSize")
361+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, expectedPageSize)
362+
assertEquals(expectedPageSize, datasetList.size)
363363

364364
logger.info("should find all solutions and assert it returns the second / last page")
365-
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 1, expectedSize)
366-
assertEquals(numberOfDatasets - expectedSize + 1, datasetList.size)
365+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 1, expectedPageSize)
366+
assertEquals(numberOfDatasets - expectedPageSize, datasetList.size)
367367
}
368368

369369
@Test
@@ -440,33 +440,43 @@ class DatasetServiceIntegrationTest : CsmRedisTestBase() {
440440
}
441441

442442
@Test
443-
fun `test find All Datasets with different pagination params`() {
443+
fun `PROD-12947 - test find All Datasets as Platform Admin`() {
444444
organizationSaved = organizationApiService.registerOrganization(organization)
445+
446+
// Create a dataset that current user should not see because he does not have permission to
445447
val numberOfDatasets = 20
446-
val defaultPageSize = csmPlatformProperties.twincache.dataset.defaultPageSize
447-
val expectedSize = 15
448448
IntRange(1, numberOfDatasets).forEach {
449449
datasetApiService.createDataset(
450-
organizationSaved.id!!, makeDatasetWithRole("d-dataset-$it", "dataset-$it"))
450+
organizationSaved.id!!,
451+
makeDatasetWithRole(
452+
organizationId = organizationSaved.id!!, userName = "[email protected]"))
451453
}
452454

453-
logger.info("should find all datasets and assert there are $numberOfDatasets")
455+
// Explicitly set connected user information
456+
every { getCurrentAccountIdentifier(any()) } returns TEST_USER_MAIL
457+
every { getCurrentAuthenticatedUserName(csmPlatformProperties) } returns "test.user"
458+
every { getCurrentAuthenticatedRoles(any()) } returns listOf(ROLE_PLATFORM_ADMIN)
459+
460+
logger.info("should find all datasets because of admin permission")
454461
var datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, null, null)
455462
assertEquals(numberOfDatasets, datasetList.size)
456463

457-
logger.info("should find all datasets and assert it equals defaultPageSize: $defaultPageSize")
458-
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, null)
459-
assertEquals(defaultPageSize, datasetList.size)
460-
461-
logger.info("should find all datasets and assert there are expected size: $expectedSize")
462-
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 0, expectedSize)
463-
assertEquals(expectedSize, datasetList.size)
464+
// Create a dataset that current user should not see because it has been created under another
465+
// organization
466+
val newOrganization = organizationApiService.registerOrganization(makeOrganizationWithRole())
467+
val datasetNotReachableByCurrentUserBecausePartOfAnotherOrganization =
468+
datasetApiService.createDataset(
469+
newOrganization.id!!, makeDatasetWithRole(organizationId = newOrganization.id!!))
470+
assertNotNull(datasetNotReachableByCurrentUserBecausePartOfAnotherOrganization)
471+
logger.info("should not find the new dataset because it was created in another organization")
472+
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, null, null)
473+
assertEquals(numberOfDatasets, datasetList.size)
464474

465-
logger.info("should find all solutions and assert it returns the second / last page")
466-
datasetList = datasetApiService.findAllDatasets(organizationSaved.id!!, 1, expectedSize)
467-
assertEquals(numberOfDatasets - expectedSize, datasetList.size)
475+
logger.info("should find only one dataset")
476+
datasetList = datasetApiService.findAllDatasets(newOrganization.id!!, null, null)
477+
assertEquals(1, datasetList.size)
478+
assertEquals(datasetNotReachableByCurrentUserBecausePartOfAnotherOrganization, datasetList[0])
468479
}
469-
470480
@Test
471481
fun `test find All Datasets with wrong pagination params`() {
472482
organizationSaved = organizationApiService.registerOrganization(organization)

dataset/src/main/kotlin/com/cosmotech/dataset/service/DatasetServiceImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class DatasetServiceImpl(
155155
val currentUser = getCurrentAccountIdentifier(this.csmPlatformProperties)
156156
datasetRepository.findByOrganizationId(organizationId, currentUser, it).toList()
157157
} else {
158-
datasetRepository.findAll(it).toList()
158+
datasetRepository.findByOrganizationIdNoSecurity(organizationId, it).toList()
159159
}
160160
}
161161
} else {
@@ -164,7 +164,7 @@ class DatasetServiceImpl(
164164
val currentUser = getCurrentAccountIdentifier(this.csmPlatformProperties)
165165
datasetRepository.findByOrganizationId(organizationId, currentUser, pageable).toList()
166166
} else {
167-
datasetRepository.findAll(pageable).toList()
167+
datasetRepository.findByOrganizationIdNoSecurity(organizationId, pageable).toList()
168168
}
169169
}
170170

dataset/src/test/kotlin/com/cosmotech/dataset/service/DatasetServiceImplTests.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import org.junit.jupiter.api.assertThrows
4343
import org.junit.jupiter.api.extension.ExtendWith
4444
import org.springframework.core.io.ByteArrayResource
4545
import org.springframework.data.domain.Page
46-
import org.springframework.data.domain.Pageable
46+
import org.springframework.data.domain.PageRequest
4747
import org.springframework.web.context.request.RequestContextHolder
4848
import org.springframework.web.context.request.ServletRequestAttributes
4949
import redis.clients.jedis.UnifiedJedis
@@ -100,7 +100,8 @@ class DatasetServiceImplTests {
100100
@Test
101101
fun `findAllDatasets should return empty list when no dataset exists`() {
102102
every { organizationService.getVerifiedOrganization(ORGANIZATION_ID) } returns Organization()
103-
every { datasetRepository.findAll(any<Pageable>()) } returns Page.empty()
103+
every { datasetRepository.findByOrganizationIdNoSecurity(any(), any<PageRequest>()) } returns
104+
Page.empty()
104105

105106
val result = datasetService.findAllDatasets(ORGANIZATION_ID, null, null)
106107
assertEquals(emptyList(), result)

0 commit comments

Comments
 (0)