-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAccessRepository.test.ts
More file actions
141 lines (125 loc) · 4.84 KB
/
AccessRepository.test.ts
File metadata and controls
141 lines (125 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { AccessRepository } from '../../../src/access/infra/repositories/AccessRepository'
import {
ApiConfig,
DataverseApiAuthMechanism
} from '../../../src/core/infra/repositories/ApiConfig'
import { TestConstants } from '../../testHelpers/TestConstants'
import { GuestbookResponseDTO } from '../../../src/access/domain/dtos/GuestbookResponseDTO'
import {
CreatedDatasetIdentifiers,
createDataset,
DatasetNotNumberedVersion,
WriteError
} from '../../../src'
import { uploadFileViaApi, testTextFile1Name } from '../../testHelpers/files/filesHelper'
import { FilesRepository } from '../../../src/files/infra/repositories/FilesRepository'
import { FileOrderCriteria } from '../../../src/files/domain/models/FileCriteria'
import { deletePublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
describe('AccessRepository', () => {
const sut: AccessRepository = new AccessRepository()
const filesRepository: FilesRepository = new FilesRepository()
let testDatasetIds: CreatedDatasetIdentifiers
let testFileId: number
const guestbookResponse: GuestbookResponseDTO = {
guestbookResponse: {
name: 'Terminal Test',
email: 'terminal@example.edu',
institution: 'Dataverse Client JavaScript',
position: 'CLI',
answers: []
}
}
beforeAll(async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.API_KEY,
process.env.TEST_API_KEY
)
try {
testDatasetIds = await createDataset.execute(TestConstants.TEST_NEW_DATASET_DTO)
await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name)
const filesSubset = await filesRepository.getDatasetFiles(
testDatasetIds.numericId,
DatasetNotNumberedVersion.LATEST,
false,
FileOrderCriteria.NAME_AZ
)
testFileId = filesSubset.files[0].id
} catch (error) {
throw new Error('Tests beforeAll(): Error while setting up access integration test data.')
}
})
afterAll(async () => {
try {
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
} catch (error) {
throw new Error('Tests afterAll(): Error while cleaning up access integration test data.')
}
})
describe('submitGuestbookForDatafileDownload', () => {
test('should return signed url for datafile download', async () => {
const actual = await sut.submitGuestbookForDatafileDownload(testFileId, guestbookResponse)
expect(actual).toEqual(expect.any(String))
expect(() => new URL(actual)).not.toThrow()
})
test('should return error when datafile does not exist', async () => {
const nonExistentId = 999999999
await expect(
sut.submitGuestbookForDatafileDownload(nonExistentId, guestbookResponse)
).rejects.toThrow(WriteError)
})
})
describe('submitGuestbookForDatafilesDownload', () => {
test('should return signed url for datafiles download', async () => {
const actual = await sut.submitGuestbookForDatafilesDownload([testFileId], guestbookResponse)
expect(actual).toEqual(expect.any(String))
expect(actual.length).toBeGreaterThan(0)
})
})
describe('submitGuestbookForDatasetDownload', () => {
test('should return signed url for dataset download', async () => {
const actual = await sut.submitGuestbookForDatasetDownload(
testDatasetIds.numericId,
guestbookResponse
)
expect(actual).toEqual(expect.any(String))
expect(() => new URL(actual)).not.toThrow()
})
test('should preserve format=original in signed url for dataset download', async () => {
const actual = await sut.submitGuestbookForDatasetDownload(
testDatasetIds.numericId,
guestbookResponse,
'original'
)
const signedUrl = new URL(actual)
expect(signedUrl.searchParams.get('format')).toEqual('original')
})
test('should return error when dataset does not exist', async () => {
const nonExistentId = 999999999
await expect(
sut.submitGuestbookForDatasetDownload(nonExistentId, guestbookResponse)
).rejects.toThrow(WriteError)
})
})
describe('submitGuestbookForDatasetVersionDownload', () => {
test('should return signed url for dataset version download', async () => {
const actual = await sut.submitGuestbookForDatasetVersionDownload(
testDatasetIds.numericId,
DatasetNotNumberedVersion.LATEST,
guestbookResponse
)
expect(actual).toEqual(expect.any(String))
expect(() => new URL(actual)).not.toThrow()
})
test('should return error when dataset version does not exist', async () => {
const nonExistentId = 999999999
await expect(
sut.submitGuestbookForDatasetVersionDownload(
nonExistentId,
DatasetNotNumberedVersion.LATEST,
guestbookResponse
)
).rejects.toThrow(WriteError)
})
})
})