Skip to content

Commit e7246c9

Browse files
committed
test: integration cases
1 parent c611295 commit e7246c9

File tree

4 files changed

+152
-5
lines changed

4 files changed

+152
-5
lines changed

src/collections/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { GetCollectionUserPermissions } from './domain/useCases/GetCollectionUse
55
import { GetCollectionItems } from './domain/useCases/GetCollectionItems'
66
import { PublishCollection } from './domain/useCases/PublishCollection'
77
import { UpdateCollection } from './domain/useCases/UpdateCollection'
8-
8+
import { GetCollectionFeaturedItems } from './domain/useCases/GetCollectionFeaturedItems'
99
import { CollectionsRepository } from './infra/repositories/CollectionsRepository'
1010

1111
const collectionsRepository = new CollectionsRepository()
@@ -17,6 +17,7 @@ const getCollectionUserPermissions = new GetCollectionUserPermissions(collection
1717
const getCollectionItems = new GetCollectionItems(collectionsRepository)
1818
const publishCollection = new PublishCollection(collectionsRepository)
1919
const updateCollection = new UpdateCollection(collectionsRepository)
20+
const getCollectionFeaturedItems = new GetCollectionFeaturedItems(collectionsRepository)
2021

2122
export {
2223
getCollection,
@@ -25,7 +26,8 @@ export {
2526
getCollectionUserPermissions,
2627
getCollectionItems,
2728
publishCollection,
28-
updateCollection
29+
updateCollection,
30+
getCollectionFeaturedItems
2931
}
3032
export { Collection, CollectionInputLevel } from './domain/models/Collection'
3133
export { CollectionFacet } from './domain/models/CollectionFacet'
@@ -34,3 +36,4 @@ export { CollectionDTO, CollectionInputLevelDTO } from './domain/dtos/Collection
3436
export { CollectionPreview } from './domain/models/CollectionPreview'
3537
export { CollectionItemType } from './domain/models/CollectionItemType'
3638
export { CollectionSearchCriteria } from './domain/models/CollectionSearchCriteria'
39+
export { CollectionFeaturedItem } from './domain/models/CollectionFeaturedItem'

test/environment/.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
POSTGRES_VERSION=13
22
DATAVERSE_DB_USER=dataverse
33
SOLR_VERSION=9.3.0
4-
DATAVERSE_IMAGE_REGISTRY=docker.io
5-
DATAVERSE_IMAGE_TAG=unstable
4+
DATAVERSE_IMAGE_REGISTRY=ghcr.io
5+
DATAVERSE_IMAGE_TAG=10943-featured-items
66
DATAVERSE_BOOTSTRAP_TIMEOUT=5m

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import {
2828
OrderType,
2929
SortType
3030
} from '../../../src/collections/domain/models/CollectionSearchCriteria'
31+
import { ROOT_COLLECTION_ID } from '../../../src/collections/domain/models/Collection'
32+
import {
33+
createCollectionFeaturedItemViaApi,
34+
deleteCollectionFeaturedItemViaApi
35+
} from '../../testHelpers/collections/collectionFeaturedItemsHelper'
3136

3237
describe('CollectionsRepository', () => {
3338
const testCollectionAlias = 'collectionsRepositoryTestCollection'
@@ -293,7 +298,7 @@ describe('CollectionsRepository', () => {
293298

294299
const expectedFileMd5 = '68b22040025784da775f55cfcb6dee2e'
295300
const expectedDatasetCitationFragment =
296-
'Admin, Dataverse; Owner, Dataverse, 2024, "Dataset created using the createDataset use case'
301+
'Admin, Dataverse; Owner, Dataverse, 2025, "Dataset created using the createDataset use case'
297302
const expectedDatasetDescription = 'Dataset created using the createDataset use case'
298303
const expectedFileName = 'test-file-1.txt'
299304
const expectedCollectionsName = 'Scientific Research'
@@ -720,4 +725,64 @@ describe('CollectionsRepository', () => {
720725
).rejects.toThrow(expectedError)
721726
})
722727
})
728+
729+
describe('getCollectionFeaturedItems', () => {
730+
let tetFeaturedItemId: number
731+
732+
beforeAll(async () => {
733+
try {
734+
const featuredItemCreated = await createCollectionFeaturedItemViaApi(testCollectionAlias, {
735+
content: '<p class="rte-paragraph">Test content</p>',
736+
displayOrder: 1,
737+
withFile: true,
738+
fileName: 'featured-item-test-image.png'
739+
})
740+
741+
tetFeaturedItemId = featuredItemCreated.id
742+
} catch (error) {
743+
throw new Error(`Error while creating collection featured item in ${testCollectionAlias}`)
744+
}
745+
})
746+
747+
afterAll(async () => {
748+
try {
749+
await deleteCollectionFeaturedItemViaApi(tetFeaturedItemId)
750+
} catch (error) {
751+
throw new Error(
752+
`Tests afterAll(): Error while deleting test dataset with id ${tetFeaturedItemId}`
753+
)
754+
}
755+
})
756+
757+
test('should return empty featured items array given a valid collection alias when collection has no featured items', async () => {
758+
const featuredItemsResponse = await sut.getCollectionFeaturedItems(ROOT_COLLECTION_ID)
759+
760+
expect(featuredItemsResponse).toStrictEqual([])
761+
})
762+
763+
test('should return featured items array given a valid collection alias when collection has featured items', async () => {
764+
const featuredItemsResponse = await sut.getCollectionFeaturedItems(testCollectionAlias)
765+
console.log({ featuredItemsResponse })
766+
767+
expect(featuredItemsResponse.length).toBe(1)
768+
expect(featuredItemsResponse[0].id).toBe(tetFeaturedItemId)
769+
expect(featuredItemsResponse[0].displayOrder).toBe(1)
770+
expect(featuredItemsResponse[0].content).toBe('<p class="rte-paragraph">Test content</p>')
771+
expect(featuredItemsResponse[0].imageFileUrl).toBe(
772+
'http://localhost:8080/api/access/dataverseFeatureItemImage/1'
773+
)
774+
expect(featuredItemsResponse[0].imageFileName).toBe('featured-item-test-image.png')
775+
})
776+
777+
test('should return error when collection does not exist', async () => {
778+
const invalidCollectionAlias = 'invalid-collection-alias'
779+
const expectedError = new ReadError(
780+
`[404] Can't find dataverse with identifier='${invalidCollectionAlias}'`
781+
)
782+
783+
await expect(sut.getCollectionFeaturedItems(invalidCollectionAlias)).rejects.toThrow(
784+
expectedError
785+
)
786+
})
787+
})
723788
})
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import axios from 'axios'
2+
import { File, Blob } from '@web-std/file'
3+
import { CollectionFeaturedItem } from '../../../src/collections/domain/models/CollectionFeaturedItem'
4+
import { ROOT_COLLECTION_ID } from '../../../src/collections/domain/models/Collection'
5+
import { TestConstants } from '../TestConstants'
6+
7+
interface CreateCollectionFeaturedItemData {
8+
content: string
9+
displayOrder?: number
10+
withFile?: boolean
11+
fileName?: string
12+
}
13+
14+
export async function createCollectionFeaturedItemViaApi(
15+
collectionAlias: string,
16+
{
17+
content,
18+
displayOrder = 1,
19+
withFile = false,
20+
fileName = 'test-image.png'
21+
}: CreateCollectionFeaturedItemData
22+
): Promise<CollectionFeaturedItem> {
23+
try {
24+
if (collectionAlias == undefined) {
25+
collectionAlias = ROOT_COLLECTION_ID
26+
}
27+
28+
const formData = new FormData()
29+
formData.append('content', content)
30+
formData.append('displayOrder', displayOrder.toString())
31+
32+
if (withFile) {
33+
const file = createImageFile(fileName)
34+
35+
formData.append('file', file)
36+
}
37+
38+
return await axios
39+
.post(`${TestConstants.TEST_API_URL}/dataverses/${collectionAlias}/featuredItem`, formData, {
40+
headers: {
41+
'Content-Type': 'multipart/form-data',
42+
'X-Dataverse-Key': process.env.TEST_API_KEY
43+
}
44+
})
45+
.then((response) => response.data.data)
46+
} catch (error) {
47+
console.log(error)
48+
throw new Error(`Error while creating collection featured item in ${collectionAlias}`)
49+
}
50+
}
51+
52+
export async function deleteCollectionFeaturedItemViaApi(featuredItemId: number): Promise<void> {
53+
try {
54+
return await axios.delete(
55+
`${TestConstants.TEST_API_URL}/dataverseFeaturedItems/${featuredItemId}`,
56+
{
57+
headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': process.env.TEST_API_KEY }
58+
}
59+
)
60+
} catch (error) {
61+
throw new Error(`Error while deleting collection featured item with id ${featuredItemId}`)
62+
}
63+
}
64+
65+
export function createImageFile(fileName = 'test-image.png'): File {
66+
// Binary data for a 1x1 black pixel PNG image
67+
const imageData = Uint8Array.from([
68+
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
69+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
70+
0x89, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x60, 0x00, 0x00, 0x00,
71+
0x02, 0x00, 0x01, 0xe2, 0x21, 0xbc, 0x33, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
72+
0x42, 0x60, 0x82
73+
])
74+
75+
const blob = new Blob([imageData], { type: 'image/png' })
76+
const imageFile = new File([blob], fileName, { type: 'image/png' })
77+
78+
return imageFile
79+
}

0 commit comments

Comments
 (0)