From aeba8ff101e055c2d2eba559a9feeb7976a573c3 Mon Sep 17 00:00:00 2001 From: MellyGray Date: Wed, 29 Nov 2023 18:07:20 +0100 Subject: [PATCH] fix: add file categories in the create request --- .../integration/files/FilesRepository.test.ts | 23 ++++--------------- test/testHelpers/files/filesHelper.ts | 19 ++++++++------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/test/integration/files/FilesRepository.test.ts b/test/integration/files/FilesRepository.test.ts index 396eb057..a4705c17 100644 --- a/test/integration/files/FilesRepository.test.ts +++ b/test/integration/files/FilesRepository.test.ts @@ -4,7 +4,7 @@ import { assert } from 'sinon'; import { expect } from 'chai'; import { TestConstants } from '../../testHelpers/TestConstants'; import { createDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'; -import { uploadFileViaApi, setFileCategoriesViaApi } from '../../testHelpers/files/filesHelper'; +import { uploadFileViaApi } from '../../testHelpers/files/filesHelper'; import { DatasetsRepository } from '../../../src/datasets/infra/repositories/DatasetsRepository'; import { ReadError } from '../../../src/core/domain/repositories/ReadError'; import { FileSearchCriteria, FileAccessStatus, FileOrderCriteria } from '../../../src/files/domain/models/FileCriteria'; @@ -12,8 +12,7 @@ import { DatasetNotNumberedVersion } from '../../../src/datasets'; import { FileCounts } from '../../../src/files/domain/models/FileCounts'; import { FileDownloadSizeMode } from '../../../src'; -// TODO: Test skipped once we fix it on https://github.com/IQSS/dataverse-client-javascript/issues/101 -xdescribe('FilesRepository', () => { +describe('FilesRepository', () => { const sut: FilesRepository = new FilesRepository(); const testTextFile1Name = 'test-file-1.txt'; @@ -35,8 +34,8 @@ xdescribe('FilesRepository', () => { .catch(() => { fail('Test beforeAll(): Error while creating test Dataset'); }); - // Uploading test file 1 - await uploadFileViaApi(TestConstants.TEST_CREATED_DATASET_ID, testTextFile1Name) + // Uploading test file 1 with some categories + await uploadFileViaApi(TestConstants.TEST_CREATED_DATASET_ID, testTextFile1Name, {categories: [testCategoryName]}) .then() .catch((e) => { console.log(e); @@ -63,20 +62,6 @@ xdescribe('FilesRepository', () => { console.log(e); fail(`Tests beforeAll(): Error while uploading file ${testTabFile4Name}`); }); - // Categorize one of the uploaded test files - const currentTestFiles = await sut.getDatasetFiles( - TestConstants.TEST_CREATED_DATASET_ID, - latestDatasetVersionId, - false, - FileOrderCriteria.NAME_AZ, - ); - const testFile = currentTestFiles[0]; - setFileCategoriesViaApi(testFile.id, [testCategoryName]) - .then() - .catch((e) => { - console.log(e); - fail(`Tests beforeAll(): Error while setting file categories to ${testFile.name}`); - }); }); describe('getDatasetFiles', () => { diff --git a/test/testHelpers/files/filesHelper.ts b/test/testHelpers/files/filesHelper.ts index 054de0b5..893f06ab 100644 --- a/test/testHelpers/files/filesHelper.ts +++ b/test/testHelpers/files/filesHelper.ts @@ -3,6 +3,10 @@ import axios, { AxiosResponse } from 'axios'; import { TestConstants } from '../TestConstants'; import { readFile } from 'fs/promises'; +interface FileMetadata { + categories?: string[] +} + export const createFileModel = (): File => { return { id: 1, @@ -70,10 +74,16 @@ export const createFilePayload = (): any => { }; }; -export const uploadFileViaApi = async (datasetId: number, fileName: string): Promise => { +export const uploadFileViaApi = async (datasetId: number, fileName: string, fileMetadata?: FileMetadata): Promise => { const formData = new FormData(); const file = await readFile(`${__dirname}/${fileName}`); + formData.append('file', new Blob([file]), fileName); + + if(fileMetadata){ + formData.append('jsonData', JSON.stringify(fileMetadata)); + } + return await axios.post(`${TestConstants.TEST_API_URL}/datasets/${datasetId}/add`, formData, { headers: { 'Content-Type': 'multipart/form-data', @@ -81,10 +91,3 @@ export const uploadFileViaApi = async (datasetId: number, fileName: string): Pro }, }); }; - -export const setFileCategoriesViaApi = async (fileId: number, fileCategoryNames: string[]): Promise => { - const data = { categories: fileCategoryNames }; - return await axios.post(`${TestConstants.TEST_API_URL}/files/${fileId}/metadata/categories`, JSON.stringify(data), { - headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': process.env.TEST_API_KEY }, - }); -};