@@ -14,14 +14,16 @@ import {
1414 getCollection ,
1515 createCollection ,
1616 getDatasetFiles ,
17- restrictFile
17+ restrictFile ,
18+ deleteFile
1819} from '../../../src'
1920import { ApiConfig } from '../../../src'
2021import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
2122import {
2223 createCollectionDTO ,
2324 createCollectionViaApi ,
2425 deleteCollectionViaApi ,
26+ publishCollectionViaApi ,
2527 ROOT_COLLECTION_ALIAS
2628} from '../../testHelpers/collections/collectionHelper'
2729import { CollectionPayload } from '../../../src/collections/infra/repositories/transformers/CollectionPayload'
@@ -41,6 +43,7 @@ import {
4143import { ROOT_COLLECTION_ID } from '../../../src/collections/domain/models/Collection'
4244import {
4345 createCollectionCustomFeaturedItemViaApi ,
46+ createCollectionDvObjectFeaturedItemViaApi ,
4447 createImageFile ,
4548 deleteCollectionFeaturedItemsViaApi ,
4649 deleteCollectionFeaturedItemViaApi
@@ -1133,54 +1136,36 @@ describe('CollectionsRepository', () => {
11331136 } )
11341137
11351138 describe ( 'getCollectionFeaturedItems' , ( ) => {
1136- let testFeaturedItemId : number
1137-
1138- beforeAll ( async ( ) => {
1139- try {
1140- const featuredItemCreated = await createCollectionCustomFeaturedItemViaApi (
1141- testCollectionAlias ,
1142- {
1143- content : '<p class="rte-paragraph">Test content</p>' ,
1144- displayOrder : 1 ,
1145- withFile : true ,
1146- fileName : 'featured-item-test-image.png'
1147- }
1148- )
1149-
1150- testFeaturedItemId = featuredItemCreated . id
1151- } catch ( error ) {
1152- throw new Error ( `Error while creating collection featured item in ${ testCollectionAlias } ` )
1153- }
1154- } )
1155-
1156- afterAll ( async ( ) => {
1157- try {
1158- await deleteCollectionFeaturedItemViaApi ( testFeaturedItemId )
1159- } catch ( error ) {
1160- throw new Error (
1161- `Tests afterAll(): Error while deleting featured item with id ${ testFeaturedItemId } `
1162- )
1163- }
1164- } )
1165-
11661139 test ( 'should return empty featured items array given a valid collection alias when collection has no featured items' , async ( ) => {
11671140 const featuredItemsResponse = await sut . getCollectionFeaturedItems ( ROOT_COLLECTION_ID )
11681141
11691142 expect ( featuredItemsResponse ) . toStrictEqual ( [ ] )
11701143 } )
11711144
11721145 test ( 'should return featured items array given a valid collection alias when collection has featured items' , async ( ) => {
1146+ const featuredItemCreated = await createCollectionCustomFeaturedItemViaApi (
1147+ testCollectionAlias ,
1148+ {
1149+ content : '<p class="rte-paragraph">Test content</p>' ,
1150+ displayOrder : 1 ,
1151+ withFile : true ,
1152+ fileName : 'featured-item-test-image.png'
1153+ }
1154+ )
1155+
11731156 const featuredItemsResponse = await sut . getCollectionFeaturedItems ( testCollectionAlias )
11741157
11751158 expect ( featuredItemsResponse . length ) . toBe ( 1 )
11761159 const firstFeaturedItem = featuredItemsResponse [ 0 ] as CustomFeaturedItem
1177- expect ( firstFeaturedItem . id ) . toBe ( testFeaturedItemId )
1160+ expect ( firstFeaturedItem . id ) . toBe ( featuredItemCreated . id )
11781161 expect ( firstFeaturedItem . displayOrder ) . toBe ( 1 )
11791162 expect ( firstFeaturedItem . content ) . toBe ( '<p class="rte-paragraph">Test content</p>' )
11801163 expect ( firstFeaturedItem . imageFileUrl ) . toContain (
11811164 `/api/access/dataverseFeaturedItemImage/${ firstFeaturedItem . id } `
11821165 )
11831166 expect ( firstFeaturedItem . imageFileName ) . toBe ( 'featured-item-test-image.png' )
1167+
1168+ await deleteCollectionFeaturedItemViaApi ( featuredItemCreated . id )
11841169 } )
11851170
11861171 test ( 'should return error when collection does not exist' , async ( ) => {
@@ -1193,6 +1178,114 @@ describe('CollectionsRepository', () => {
11931178 expectedError
11941179 )
11951180 } )
1181+
1182+ test ( 'Featured Item type File should not be returned if the file was deleted and the dataset published' , async ( ) => {
1183+ const testFileDeletedCollectionAlias = 'testCollectionFeaturedItemsFileDeletion'
1184+ await createCollectionViaApi ( testFileDeletedCollectionAlias )
1185+ await publishCollectionViaApi ( testFileDeletedCollectionAlias )
1186+ const testDatasetIds = await createDataset . execute (
1187+ TestConstants . TEST_NEW_DATASET_DTO ,
1188+ testFileDeletedCollectionAlias
1189+ )
1190+ await uploadFileViaApi ( testDatasetIds . numericId , 'test-file-1.txt' )
1191+ await publishDatasetViaApi ( testDatasetIds . numericId )
1192+ await waitForNoLocks ( testDatasetIds . numericId , 10 )
1193+
1194+ const datasetFiles = await getDatasetFiles . execute ( testDatasetIds . numericId )
1195+ const fileId = datasetFiles . files [ 0 ] . id
1196+
1197+ await createCollectionDvObjectFeaturedItemViaApi ( testFileDeletedCollectionAlias , {
1198+ type : 'datafile' ,
1199+ dvObjectIdentifier : fileId . toString ( ) ,
1200+ displayOrder : 0
1201+ } )
1202+
1203+ const featuredItemsResponse = await sut . getCollectionFeaturedItems (
1204+ testFileDeletedCollectionAlias
1205+ )
1206+
1207+ expect ( featuredItemsResponse . length ) . toBe ( 1 )
1208+
1209+ // Now we delete the file
1210+ await deleteFile . execute ( fileId )
1211+
1212+ // If we dont publish the dataset the featured item will still be there
1213+ const featuredItemsResponseAfterFileDeletion = await sut . getCollectionFeaturedItems (
1214+ testFileDeletedCollectionAlias
1215+ )
1216+
1217+ expect ( featuredItemsResponseAfterFileDeletion . length ) . toBe ( 1 )
1218+
1219+ // Once we publish the dataset the featured item will not be returned anymore
1220+ await publishDatasetViaApi ( testDatasetIds . numericId )
1221+ await waitForNoLocks ( testDatasetIds . numericId , 10 )
1222+
1223+ const featuredItemsResponseAfterDatasetPublish = await sut . getCollectionFeaturedItems (
1224+ testFileDeletedCollectionAlias
1225+ )
1226+
1227+ expect ( featuredItemsResponseAfterDatasetPublish . length ) . toBe ( 0 )
1228+
1229+ await deletePublishedDatasetViaApi ( testDatasetIds . persistentId )
1230+ await deleteCollectionViaApi ( testFileDeletedCollectionAlias )
1231+ } )
1232+
1233+ // TODO:ME - Maybe this should be Featured Item type File should not be returned if the file was restricted and the dataset published. Waiting for confirmation
1234+
1235+ test ( 'Featured Item type File should not be returned if the file was restricted' , async ( ) => {
1236+ const testFileRestrictedCollectionAlias = 'testCollectionFeaturedItemsFileRestriction'
1237+ await createCollectionViaApi ( testFileRestrictedCollectionAlias )
1238+ await publishCollectionViaApi ( testFileRestrictedCollectionAlias )
1239+ const testDatasetIds = await createDataset . execute (
1240+ TestConstants . TEST_NEW_DATASET_DTO ,
1241+ testFileRestrictedCollectionAlias
1242+ )
1243+ await uploadFileViaApi ( testDatasetIds . numericId , 'test-file-1.txt' )
1244+ await publishDatasetViaApi ( testDatasetIds . numericId )
1245+ await waitForNoLocks ( testDatasetIds . numericId , 10 )
1246+
1247+ const datasetFiles = await getDatasetFiles . execute ( testDatasetIds . numericId )
1248+ const fileId = datasetFiles . files [ 0 ] . id
1249+
1250+ await createCollectionDvObjectFeaturedItemViaApi ( testFileRestrictedCollectionAlias , {
1251+ type : 'datafile' ,
1252+ dvObjectIdentifier : fileId . toString ( ) ,
1253+ displayOrder : 0
1254+ } )
1255+
1256+ const featuredItemsResponse = await sut . getCollectionFeaturedItems (
1257+ testFileRestrictedCollectionAlias
1258+ )
1259+
1260+ expect ( featuredItemsResponse . length ) . toBe ( 1 )
1261+
1262+ // Now we restrict the file
1263+ await restrictFile . execute ( fileId , {
1264+ restrict : true ,
1265+ enableAccessRequest : true ,
1266+ termsOfAccess : 'This file is restricted for testing purposes'
1267+ } )
1268+
1269+ // Restricted file should not be returned as featured item once restricted
1270+ const featuredItemsResponseAfterFileRestriction = await sut . getCollectionFeaturedItems (
1271+ testFileRestrictedCollectionAlias
1272+ )
1273+
1274+ expect ( featuredItemsResponseAfterFileRestriction . length ) . toBe ( 0 )
1275+
1276+ // // Once we publish the dataset the featured item will not be returned anymore
1277+ // await publishDatasetViaApi(testDatasetIds.numericId)
1278+ // await waitForNoLocks(testDatasetIds.numericId, 10)
1279+
1280+ // const featuredItemsResponseAfterDatasetPublish = await sut.getCollectionFeaturedItems(
1281+ // testFileRestrictedCollectionAlias
1282+ // )
1283+
1284+ // expect(featuredItemsResponseAfterDatasetPublish.length).toBe(0)
1285+
1286+ await deletePublishedDatasetViaApi ( testDatasetIds . persistentId )
1287+ await deleteCollectionViaApi ( testFileRestrictedCollectionAlias )
1288+ } )
11961289 } )
11971290
11981291 describe ( 'updateCollectionFeaturedItems' , ( ) => {
@@ -1353,6 +1446,35 @@ describe('CollectionsRepository', () => {
13531446 await deleteUnpublishedDatasetViaApi ( testDatasetIds . numericId )
13541447 } )
13551448
1449+ it ( 'should return error when the file to feature is not published' , async ( ) => {
1450+ const testDatasetIds = await createDataset . execute (
1451+ TestConstants . TEST_NEW_DATASET_DTO ,
1452+ testCollectionAlias
1453+ )
1454+ await uploadFileViaApi ( testDatasetIds . numericId , 'test-file-1.txt' )
1455+
1456+ const datasetFiles = await getDatasetFiles . execute ( testDatasetIds . numericId )
1457+
1458+ const fileId = datasetFiles . files [ 0 ] . id
1459+
1460+ const newFeaturedItems : DvObjectFeaturedItemDTO [ ] = [
1461+ {
1462+ type : FeaturedItemType . FILE ,
1463+ dvObjectIdentifier : fileId . toString ( ) ,
1464+ displayOrder : 0
1465+ }
1466+ ]
1467+
1468+ // TODO:ME - Maybe this should be [400] Datafile must be published to be featured. Waiting for confirmation
1469+ const expectedError = new WriteError ( '[400] Dataset must be published to be featured.' )
1470+
1471+ await expect (
1472+ sut . updateCollectionFeaturedItems ( testCollectionAlias , newFeaturedItems )
1473+ ) . rejects . toThrow ( expectedError )
1474+
1475+ await deleteUnpublishedDatasetViaApi ( testDatasetIds . numericId )
1476+ } )
1477+
13561478 it ( 'should return error when the file to feature is restricted' , async ( ) => {
13571479 const testDatasetIds = await createDataset . execute (
13581480 TestConstants . TEST_NEW_DATASET_DTO ,
0 commit comments