diff --git a/src/info/domain/repositories/IDataverseInfoRepository.ts b/src/info/domain/repositories/IDataverseInfoRepository.ts index 5757ca0c..c9e1c2d1 100644 --- a/src/info/domain/repositories/IDataverseInfoRepository.ts +++ b/src/info/domain/repositories/IDataverseInfoRepository.ts @@ -2,4 +2,6 @@ import { DataverseVersion } from '../models/DataverseVersion'; export interface IDataverseInfoRepository { getDataverseVersion(): Promise; + getZipDownloadLimit(): Promise; + getMaxEmbargoDurationInMonths(): Promise; } diff --git a/src/info/domain/useCases/GetMaxEmbargoDurationInMonths.ts b/src/info/domain/useCases/GetMaxEmbargoDurationInMonths.ts new file mode 100644 index 00000000..b10c4d1e --- /dev/null +++ b/src/info/domain/useCases/GetMaxEmbargoDurationInMonths.ts @@ -0,0 +1,14 @@ +import { UseCase } from '../../../core/domain/useCases/UseCase'; +import { IDataverseInfoRepository } from '../repositories/IDataverseInfoRepository'; + +export class GetMaxEmbargoDurationInMonths implements UseCase { + private dataverseInfoRepository: IDataverseInfoRepository; + + constructor(dataverseInfoRepository: IDataverseInfoRepository) { + this.dataverseInfoRepository = dataverseInfoRepository; + } + + async execute(): Promise { + return await this.dataverseInfoRepository.getMaxEmbargoDurationInMonths(); + } +} diff --git a/src/info/domain/useCases/GetZipDownloadLimit.ts b/src/info/domain/useCases/GetZipDownloadLimit.ts new file mode 100644 index 00000000..84e8af4b --- /dev/null +++ b/src/info/domain/useCases/GetZipDownloadLimit.ts @@ -0,0 +1,14 @@ +import { UseCase } from '../../../core/domain/useCases/UseCase'; +import { IDataverseInfoRepository } from '../repositories/IDataverseInfoRepository'; + +export class GetZipDownloadLimit implements UseCase { + private dataverseInfoRepository: IDataverseInfoRepository; + + constructor(dataverseInfoRepository: IDataverseInfoRepository) { + this.dataverseInfoRepository = dataverseInfoRepository; + } + + async execute(): Promise { + return await this.dataverseInfoRepository.getZipDownloadLimit(); + } +} diff --git a/src/info/index.ts b/src/info/index.ts index e3a73f3d..af30081b 100644 --- a/src/info/index.ts +++ b/src/info/index.ts @@ -1,6 +1,12 @@ import { DataverseInfoRepository } from './infra/repositories/DataverseInfoRepository'; import { GetDataverseVersion } from './domain/useCases/GetDataverseVersion'; +import { GetZipDownloadLimit } from './domain/useCases/GetZipDownloadLimit'; +import { GetMaxEmbargoDurationInMonths } from './domain/useCases/GetMaxEmbargoDurationInMonths'; -const getDataverseVersion = new GetDataverseVersion(new DataverseInfoRepository()); +const dataverseInfoRepository = new DataverseInfoRepository(); -export { getDataverseVersion }; +const getDataverseVersion = new GetDataverseVersion(dataverseInfoRepository); +const getZipDownloadLimit = new GetZipDownloadLimit(dataverseInfoRepository); +const getMaxEmbargoDurationInMonths = new GetMaxEmbargoDurationInMonths(dataverseInfoRepository); + +export { getDataverseVersion, getZipDownloadLimit, getMaxEmbargoDurationInMonths }; diff --git a/src/info/infra/repositories/DataverseInfoRepository.ts b/src/info/infra/repositories/DataverseInfoRepository.ts index 5ba97862..ad3c83e3 100644 --- a/src/info/infra/repositories/DataverseInfoRepository.ts +++ b/src/info/infra/repositories/DataverseInfoRepository.ts @@ -4,14 +4,32 @@ import { DataverseVersion } from '../../domain/models/DataverseVersion'; import { AxiosResponse } from 'axios'; export class DataverseInfoRepository extends ApiRepository implements IDataverseInfoRepository { + private readonly infoResourceName: string = 'info'; + public async getDataverseVersion(): Promise { - return this.doGet('/info/version') + return this.doGet(this.buildApiEndpoint(this.infoResourceName, `version`)) .then((response) => this.getVersionFromResponse(response)) .catch((error) => { throw error; }); } + public async getZipDownloadLimit(): Promise { + return this.doGet(this.buildApiEndpoint(this.infoResourceName, `zipDownloadLimit`)) + .then((response) => response.data.data as number) + .catch((error) => { + throw error; + }); + } + + public async getMaxEmbargoDurationInMonths(): Promise { + return this.doGet(this.buildApiEndpoint(this.infoResourceName, `settings/:MaxEmbargoDurationInMonths`)) + .then((response) => response.data.data.message as number) + .catch((error) => { + throw error; + }); + } + private getVersionFromResponse(response: AxiosResponse): DataverseVersion { const responseData = response.data.data; return { diff --git a/test/integration/environment/.env b/test/integration/environment/.env index 0bcd5b25..80e9a14e 100644 --- a/test/integration/environment/.env +++ b/test/integration/environment/.env @@ -1,6 +1,6 @@ POSTGRES_VERSION=13 DATAVERSE_DB_USER=dataverse SOLR_VERSION=9.3.0 -DATAVERSE_IMAGE_REGISTRY=ghcr.io -DATAVERSE_IMAGE_TAG=9995-file-total-download-size-criteria +DATAVERSE_IMAGE_REGISTRY=docker.io +DATAVERSE_IMAGE_TAG=unstable DATAVERSE_BOOTSTRAP_TIMEOUT=5m diff --git a/test/integration/info/DataverseInfoRepository.test.ts b/test/integration/info/DataverseInfoRepository.test.ts index 0df2934b..a8342df7 100644 --- a/test/integration/info/DataverseInfoRepository.test.ts +++ b/test/integration/info/DataverseInfoRepository.test.ts @@ -1,14 +1,51 @@ import { DataverseInfoRepository } from '../../../src/info/infra/repositories/DataverseInfoRepository'; import { ApiConfig, DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'; import { TestConstants } from '../../testHelpers/TestConstants'; +import { setMaxEmbargoDurationInMonthsViaApi } from '../../testHelpers/info/infoHelper'; +import { ReadError } from '../../../src/core/domain/repositories/ReadError'; +import { assert } from 'sinon'; +import { fail } from 'assert'; -describe('getDataverseVersion', () => { +describe('DataverseInfoRepository', () => { const sut: DataverseInfoRepository = new DataverseInfoRepository(); - ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY); + beforeAll(async () => { + ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, process.env.TEST_API_KEY); + }); + + describe('getDataverseVersion', () => { + test('should return Dataverse version', async () => { + const actual = await sut.getDataverseVersion(); + expect(typeof actual.number).toBe('string'); + }); + }); + + describe('getZipDownloadLimit', () => { + test('should return zip download limit', async () => { + const actual = await sut.getZipDownloadLimit(); + expect(typeof actual).toBe('number'); + }); + }); + + describe('getMaxEmbargoDurationInMonths', () => { + test('should return error when the setting does not exist', async () => { + let error: ReadError = undefined; + await sut.getMaxEmbargoDurationInMonths().catch((e) => (error = e)); + assert.match( + error.message, + 'There was an error when reading the resource. Reason was: [404] Setting :MaxEmbargoDurationInMonths not found', + ); + }); - test('should return Dataverse version', async () => { - const actual = await sut.getDataverseVersion(); - expect(typeof actual.number).toBe('string'); + test('should return duration when the setting exists', async () => { + const testMaxEmbargoDurationInMonths = 12; + await setMaxEmbargoDurationInMonthsViaApi(testMaxEmbargoDurationInMonths) + .then() + .catch(() => { + fail('Test getMaxEmbargoDurationInMonths: Error while setting :MaxEmbargoDurationInMonths'); + }); + const actual = await sut.getMaxEmbargoDurationInMonths(); + assert.match(actual, testMaxEmbargoDurationInMonths); + }); }); }); diff --git a/test/testHelpers/info/infoHelper.ts b/test/testHelpers/info/infoHelper.ts new file mode 100644 index 00000000..8da92fb8 --- /dev/null +++ b/test/testHelpers/info/infoHelper.ts @@ -0,0 +1,14 @@ +import axios, { AxiosResponse } from 'axios'; +import { TestConstants } from '../TestConstants'; + +export const setMaxEmbargoDurationInMonthsViaApi = async ( + maxEmbargoDurationInMonths: number, +): Promise => { + return await axios.put( + `${TestConstants.TEST_API_URL}/admin/settings/:MaxEmbargoDurationInMonths`, + maxEmbargoDurationInMonths.toString(), + { + headers: { 'Content-Type': 'text/plain' }, + }, + ); +}; diff --git a/test/unit/info/DataverseInfoRepository.test.ts b/test/unit/info/DataverseInfoRepository.test.ts index 7a96c5a5..52c9ce73 100644 --- a/test/unit/info/DataverseInfoRepository.test.ts +++ b/test/unit/info/DataverseInfoRepository.test.ts @@ -6,52 +6,130 @@ import { ReadError } from '../../../src/core/domain/repositories/ReadError'; import { ApiConfig, DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'; import { TestConstants } from '../../testHelpers/TestConstants'; -describe('getDataverseVersion', () => { +describe('DataverseInfoRepository', () => { const sandbox: SinonSandbox = createSandbox(); const sut: DataverseInfoRepository = new DataverseInfoRepository(); - ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, TestConstants.TEST_DUMMY_API_KEY); + beforeEach(() => { + ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, TestConstants.TEST_DUMMY_API_KEY); + }); afterEach(() => { sandbox.restore(); }); - test('should return Dataverse version on successful response', async () => { - const testVersionNumber = '5.13'; - const testVersionBuild = 'testBuild'; - const testSuccessfulResponse = { - data: { - status: 'OK', + describe('getDataverseVersion', () => { + test('should return Dataverse version on successful response', async () => { + const testVersionNumber = '5.13'; + const testVersionBuild = 'testBuild'; + const testSuccessfulResponse = { + data: { + status: 'OK', + data: { + version: testVersionNumber, + build: testVersionBuild, + }, + }, + }; + const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse); + + const actual = await sut.getDataverseVersion(); + + assert.calledWithExactly( + axiosGetStub, + `${TestConstants.TEST_API_URL}/info/version`, + TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, + ); + assert.match(actual.number, testVersionNumber); + assert.match(actual.build, testVersionBuild); + }); + + test('should return error result on error response', async () => { + const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE); + + let error: ReadError = undefined; + await sut.getDataverseVersion().catch((e) => (error = e)); + + assert.calledWithExactly( + axiosGetStub, + `${TestConstants.TEST_API_URL}/info/version`, + TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, + ); + expect(error).to.be.instanceOf(Error); + }); + }); + + describe('getZipDownloadLimit', () => { + test('should return zip download limit on successful response', async () => { + const testZipDownloadLimit = 100; + const testSuccessfulResponse = { data: { - version: testVersionNumber, - build: testVersionBuild, + status: 'OK', + data: testZipDownloadLimit.toString(), }, - }, - }; - const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse); - - const actual = await sut.getDataverseVersion(); - - assert.calledWithExactly( - axiosGetStub, - `${TestConstants.TEST_API_URL}/info/version`, - TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, - ); - assert.match(actual.number, testVersionNumber); - assert.match(actual.build, testVersionBuild); + }; + const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse); + + const actual = await sut.getZipDownloadLimit(); + + assert.calledWithExactly( + axiosGetStub, + `${TestConstants.TEST_API_URL}/info/zipDownloadLimit`, + TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, + ); + assert.match(actual, testZipDownloadLimit); + }); + + test('should return error result on error response', async () => { + const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE); + + let error: ReadError = undefined; + await sut.getZipDownloadLimit().catch((e) => (error = e)); + + assert.calledWithExactly( + axiosGetStub, + `${TestConstants.TEST_API_URL}/info/zipDownloadLimit`, + TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, + ); + expect(error).to.be.instanceOf(Error); + }); }); - test('should return error result on error response', async () => { - const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE); + describe('getMaxEmbargoDurationInMonths', () => { + test('should return duration on successful response', async () => { + const testDuration = 12; + const testSuccessfulResponse = { + data: { + status: 'OK', + data: { + message: testDuration.toString(), + }, + }, + }; + const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse); + + const actual = await sut.getMaxEmbargoDurationInMonths(); + + assert.calledWithExactly( + axiosGetStub, + `${TestConstants.TEST_API_URL}/info/settings/:MaxEmbargoDurationInMonths`, + TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, + ); + assert.match(actual, testDuration); + }); + + test('should return error result on error response', async () => { + const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE); - let error: ReadError = undefined; - await sut.getDataverseVersion().catch((e) => (error = e)); + let error: ReadError = undefined; + await sut.getMaxEmbargoDurationInMonths().catch((e) => (error = e)); - assert.calledWithExactly( - axiosGetStub, - `${TestConstants.TEST_API_URL}/info/version`, - TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, - ); - expect(error).to.be.instanceOf(Error); + assert.calledWithExactly( + axiosGetStub, + `${TestConstants.TEST_API_URL}/info/settings/:MaxEmbargoDurationInMonths`, + TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG, + ); + expect(error).to.be.instanceOf(Error); + }); }); }); diff --git a/test/unit/info/GetMaxEmbargoDurationInMonths.test.ts b/test/unit/info/GetMaxEmbargoDurationInMonths.test.ts new file mode 100644 index 00000000..57953254 --- /dev/null +++ b/test/unit/info/GetMaxEmbargoDurationInMonths.test.ts @@ -0,0 +1,35 @@ +import { GetMaxEmbargoDurationInMonths } from '../../../src/info/domain/useCases/GetMaxEmbargoDurationInMonths'; +import { IDataverseInfoRepository } from '../../../src/info/domain/repositories/IDataverseInfoRepository'; +import { ReadError } from '../../../src/core/domain/repositories/ReadError'; +import { assert, createSandbox, SinonSandbox } from 'sinon'; + +describe('execute', () => { + const sandbox: SinonSandbox = createSandbox(); + + afterEach(() => { + sandbox.restore(); + }); + + test('should return duration on repository success', async () => { + const testDuration = 12; + const dataverseInfoRepositoryStub = {}; + dataverseInfoRepositoryStub.getMaxEmbargoDurationInMonths = sandbox.stub().returns(testDuration); + const sut = new GetMaxEmbargoDurationInMonths(dataverseInfoRepositoryStub); + + const actual = await sut.execute(); + + assert.match(actual, testDuration); + }); + + test('should return error result on repository error', async () => { + const dataverseInfoRepositoryStub = {}; + const testReadError = new ReadError(); + dataverseInfoRepositoryStub.getMaxEmbargoDurationInMonths = sandbox.stub().throwsException(testReadError); + const sut = new GetMaxEmbargoDurationInMonths(dataverseInfoRepositoryStub); + + let actualError: ReadError = undefined; + await sut.execute().catch((e) => (actualError = e)); + + assert.match(actualError, testReadError); + }); +}); diff --git a/test/unit/info/GetZipDownloadLimit.test.ts b/test/unit/info/GetZipDownloadLimit.test.ts new file mode 100644 index 00000000..94e1953a --- /dev/null +++ b/test/unit/info/GetZipDownloadLimit.test.ts @@ -0,0 +1,35 @@ +import { GetZipDownloadLimit } from '../../../src/info/domain/useCases/GetZipDownloadLimit'; +import { IDataverseInfoRepository } from '../../../src/info/domain/repositories/IDataverseInfoRepository'; +import { ReadError } from '../../../src/core/domain/repositories/ReadError'; +import { assert, createSandbox, SinonSandbox } from 'sinon'; + +describe('execute', () => { + const sandbox: SinonSandbox = createSandbox(); + + afterEach(() => { + sandbox.restore(); + }); + + test('should return successful result on repository success', async () => { + const testZipDownloadLimit = 100; + const dataverseInfoRepositoryStub = {}; + dataverseInfoRepositoryStub.getZipDownloadLimit = sandbox.stub().returns(testZipDownloadLimit); + const sut = new GetZipDownloadLimit(dataverseInfoRepositoryStub); + + const actual = await sut.execute(); + + assert.match(actual, testZipDownloadLimit); + }); + + test('should return error result on repository error', async () => { + const dataverseInfoRepositoryStub = {}; + const testReadError = new ReadError(); + dataverseInfoRepositoryStub.getZipDownloadLimit = sandbox.stub().throwsException(testReadError); + const sut = new GetZipDownloadLimit(dataverseInfoRepositoryStub); + + let actualError: ReadError = undefined; + await sut.execute().catch((e) => (actualError = e)); + + assert.match(actualError, testReadError); + }); +});