|
| 1 | +import { |
| 2 | + ApiConfig, |
| 3 | + DataverseApiAuthMechanism |
| 4 | +} from '../../../src/core/infra/repositories/ApiConfig' |
| 5 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 6 | +import { ExternalToolsRepository } from '../../../src/externalTools/infra/ExternalToolsRepository' |
| 7 | +import { |
| 8 | + deleteExternalToolViaApi, |
| 9 | + createExternalToolViaApi, |
| 10 | + CREATE_FILE_EXTERNAL_TOOL_PAYLOAD, |
| 11 | + CREATE_DATASET_EXTERNAL_TOOL_PAYLOAD |
| 12 | +} from '../../testHelpers/externalTools/externalToolsHelper' |
| 13 | +import { createDataset, CreatedDatasetIdentifiers, getDatasetFiles, WriteError } from '../../../src' |
| 14 | +import { |
| 15 | + createCollectionViaApi, |
| 16 | + deleteCollectionViaApi |
| 17 | +} from '../../testHelpers/collections/collectionHelper' |
| 18 | +import { uploadFileViaApi } from '../../testHelpers/files/filesHelper' |
| 19 | +import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' |
| 20 | + |
| 21 | +describe('ExternalToolsRepository', () => { |
| 22 | + const sut: ExternalToolsRepository = new ExternalToolsRepository() |
| 23 | + |
| 24 | + beforeAll(async () => { |
| 25 | + ApiConfig.init( |
| 26 | + TestConstants.TEST_API_URL, |
| 27 | + DataverseApiAuthMechanism.API_KEY, |
| 28 | + process.env.TEST_API_KEY |
| 29 | + ) |
| 30 | + }) |
| 31 | + |
| 32 | + describe('getExternalTools', () => { |
| 33 | + test('should return all external tools availables in the installation', async () => { |
| 34 | + const createdToolResponse = await createExternalToolViaApi('file') |
| 35 | + const actual = await sut.getExternalTools() |
| 36 | + |
| 37 | + expect(actual.length).toBe(1) |
| 38 | + expect(actual[0].id).toBe(createdToolResponse.data.data.id) |
| 39 | + |
| 40 | + await deleteExternalToolViaApi(createdToolResponse.data.data.id) |
| 41 | + }) |
| 42 | + |
| 43 | + test('should return empty array if no external tools are available', async () => { |
| 44 | + const actual = await sut.getExternalTools() |
| 45 | + |
| 46 | + expect(actual.length).toBe(0) |
| 47 | + expect(actual).toStrictEqual([]) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe('getFileExternalToolUrl', () => { |
| 52 | + const testCollectionAlias = 'getFileExternalToolUrlFunctionalTestCollection' |
| 53 | + let testDatasetIds: CreatedDatasetIdentifiers |
| 54 | + const testTextFile1Name = 'test-file-1.txt' |
| 55 | + let testFileId: number |
| 56 | + let testDatasetExternalToolId: number |
| 57 | + let testFileExternalToolId: number |
| 58 | + |
| 59 | + beforeAll(async () => { |
| 60 | + try { |
| 61 | + // Create a Collection |
| 62 | + await createCollectionViaApi(testCollectionAlias) |
| 63 | + // Create a Dataset |
| 64 | + testDatasetIds = await createDataset.execute( |
| 65 | + TestConstants.TEST_NEW_DATASET_DTO, |
| 66 | + testCollectionAlias |
| 67 | + ) |
| 68 | + // Upload a file to the Dataset |
| 69 | + await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name) |
| 70 | + // Save File Id |
| 71 | + const datasetFiles = await getDatasetFiles.execute(testDatasetIds.numericId) |
| 72 | + testFileId = datasetFiles.files[0].id |
| 73 | + // Create a dataset-level External Tool |
| 74 | + const createdExtToolResponse1 = await createExternalToolViaApi('dataset') |
| 75 | + testDatasetExternalToolId = createdExtToolResponse1.data.data.id |
| 76 | + // Create a file-level External Tool |
| 77 | + const createdExtToolResponse2 = await createExternalToolViaApi('file') |
| 78 | + testFileExternalToolId = createdExtToolResponse2.data.data.id |
| 79 | + } catch (error) { |
| 80 | + throw new Error('Tests beforeAll(): Error setting up test data.') |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + afterAll(async () => { |
| 85 | + try { |
| 86 | + await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) |
| 87 | + await deleteCollectionViaApi(testCollectionAlias) |
| 88 | + await deleteExternalToolViaApi(testDatasetExternalToolId) |
| 89 | + await deleteExternalToolViaApi(testFileExternalToolId) |
| 90 | + } catch (error) { |
| 91 | + throw new Error('Tests afterAll(): Error cleaning up test data.') |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + test('should return file external tool url', async () => { |
| 96 | + const fileExternalToolUrl = await sut.getFileExternalToolUrl( |
| 97 | + testFileId, |
| 98 | + testFileExternalToolId, |
| 99 | + { |
| 100 | + preview: true, |
| 101 | + locale: 'en' |
| 102 | + } |
| 103 | + ) |
| 104 | + expect(fileExternalToolUrl.fileId).toBe(testFileId) |
| 105 | + expect(fileExternalToolUrl.displayName).toBe(CREATE_FILE_EXTERNAL_TOOL_PAYLOAD.displayName) |
| 106 | + expect(fileExternalToolUrl.toolUrlResolved).toContain( |
| 107 | + CREATE_FILE_EXTERNAL_TOOL_PAYLOAD.toolUrl |
| 108 | + ) |
| 109 | + expect(fileExternalToolUrl.toolUrlResolved).toContain(`preview=true`) |
| 110 | + expect(fileExternalToolUrl.preview).toBe(true) |
| 111 | + }) |
| 112 | + |
| 113 | + test('should return error if file external tool id does not exist', async () => { |
| 114 | + await expect( |
| 115 | + sut.getFileExternalToolUrl(testFileId, 999999, { |
| 116 | + preview: true, |
| 117 | + locale: 'en' |
| 118 | + }) |
| 119 | + ).rejects.toThrow(WriteError) // e.g. [400] External tool not found with id: 999999 |
| 120 | + }) |
| 121 | + |
| 122 | + test('should return error if toolId is not for a file-level external tool', async () => { |
| 123 | + await expect( |
| 124 | + sut.getFileExternalToolUrl(testFileId, testDatasetExternalToolId, { |
| 125 | + preview: true, |
| 126 | + locale: 'en' |
| 127 | + }) |
| 128 | + ).rejects.toThrow(WriteError) // e.g. [400] External tool does not have file scope. |
| 129 | + }) |
| 130 | + |
| 131 | + test('should return error if file id does not exist', async () => { |
| 132 | + await expect( |
| 133 | + sut.getFileExternalToolUrl(56565656, testFileExternalToolId, { |
| 134 | + preview: true, |
| 135 | + locale: 'en' |
| 136 | + }) |
| 137 | + ).rejects.toThrow(WriteError) // e.g. [404] File not found for given id: 56565656 |
| 138 | + }) |
| 139 | + }) |
| 140 | + |
| 141 | + describe('getDatasetExternalToolUrl', () => { |
| 142 | + const testCollectionAlias = 'getDatasetExternalToolUrlFunctionalTestCollection' |
| 143 | + let testDatasetIds: CreatedDatasetIdentifiers |
| 144 | + const testTextFile1Name = 'test-file-1.txt' |
| 145 | + let testDatasetExternalToolId: number |
| 146 | + let testFileExternalToolId: number |
| 147 | + |
| 148 | + beforeAll(async () => { |
| 149 | + try { |
| 150 | + // Create a Collection |
| 151 | + await createCollectionViaApi(testCollectionAlias) |
| 152 | + // Create a Dataset |
| 153 | + testDatasetIds = await createDataset.execute( |
| 154 | + TestConstants.TEST_NEW_DATASET_DTO, |
| 155 | + testCollectionAlias |
| 156 | + ) |
| 157 | + // Upload a file to the Dataset |
| 158 | + await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name) |
| 159 | + // Create a dataset-level External Tool |
| 160 | + const createdExtToolResponse1 = await createExternalToolViaApi('dataset') |
| 161 | + testDatasetExternalToolId = createdExtToolResponse1.data.data.id |
| 162 | + // Create a file-level External Tool |
| 163 | + const createdExtToolResponse2 = await createExternalToolViaApi('file') |
| 164 | + testFileExternalToolId = createdExtToolResponse2.data.data.id |
| 165 | + } catch (error) { |
| 166 | + throw new Error('Tests beforeAll(): Error setting up test data.') |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + afterAll(async () => { |
| 171 | + try { |
| 172 | + await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) |
| 173 | + await deleteCollectionViaApi(testCollectionAlias) |
| 174 | + await deleteExternalToolViaApi(testDatasetExternalToolId) |
| 175 | + await deleteExternalToolViaApi(testFileExternalToolId) |
| 176 | + } catch (error) { |
| 177 | + throw new Error('Tests afterAll(): Error cleaning up test data.') |
| 178 | + } |
| 179 | + }) |
| 180 | + |
| 181 | + test('should return dataset external tool url', async () => { |
| 182 | + const datasetfileExternalToolUrl = await sut.getDatasetExternalToolUrl( |
| 183 | + testDatasetIds.numericId, |
| 184 | + testDatasetExternalToolId, |
| 185 | + { |
| 186 | + preview: true, |
| 187 | + locale: 'en' |
| 188 | + } |
| 189 | + ) |
| 190 | + expect(datasetfileExternalToolUrl.datasetId).toBe(testDatasetIds.numericId) |
| 191 | + expect(datasetfileExternalToolUrl.displayName).toBe( |
| 192 | + CREATE_DATASET_EXTERNAL_TOOL_PAYLOAD.displayName |
| 193 | + ) |
| 194 | + expect(datasetfileExternalToolUrl.toolUrlResolved).toContain( |
| 195 | + CREATE_DATASET_EXTERNAL_TOOL_PAYLOAD.toolUrl |
| 196 | + ) |
| 197 | + expect(datasetfileExternalToolUrl.toolUrlResolved).toContain(`preview=true`) |
| 198 | + expect(datasetfileExternalToolUrl.preview).toBe(true) |
| 199 | + }) |
| 200 | + |
| 201 | + test('should return error if dataset external tool id does not exist', async () => { |
| 202 | + await expect( |
| 203 | + sut.getDatasetExternalToolUrl(testDatasetIds.numericId, 999999, { |
| 204 | + preview: true, |
| 205 | + locale: 'en' |
| 206 | + }) |
| 207 | + ).rejects.toThrow(WriteError) // e.g. [400] External tool not found with id: 999999 |
| 208 | + }) |
| 209 | + |
| 210 | + test('should return error if toolId is not for a dataset-level external tool', async () => { |
| 211 | + await expect( |
| 212 | + sut.getDatasetExternalToolUrl(testDatasetIds.numericId, testFileExternalToolId, { |
| 213 | + preview: true, |
| 214 | + locale: 'en' |
| 215 | + }) |
| 216 | + ).rejects.toThrow(WriteError) // e.g. [400] External tool does not have dataset scope. |
| 217 | + }) |
| 218 | + |
| 219 | + test('should return error if dataset id does not exist', async () => { |
| 220 | + await expect( |
| 221 | + sut.getDatasetExternalToolUrl(56565656, testDatasetExternalToolId, { |
| 222 | + preview: true, |
| 223 | + locale: 'en' |
| 224 | + }) |
| 225 | + ).rejects.toThrow(WriteError) // e.g. [404] Dataset not found for given id: 56565656 |
| 226 | + }) |
| 227 | + }) |
| 228 | +}) |
0 commit comments