Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/useCases.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The different use cases currently available in the package are classified below,
- [Get Dataverse Backend Version](#get-dataverse-backend-version)
- [Get Maximum Embargo Duration In Months](#get-maximum-embargo-duration-in-months)
- [Get ZIP Download Limit](#get-zip-download-limit)
- [Get Application Terms of Use](#get-application-terms-of-use)
- [Contact](#Contact)
- [Send Feedback to Object Contacts](#send-feedback-to-object-contacts)

Expand Down Expand Up @@ -1781,6 +1782,26 @@ getZipDownloadLimit.execute().then((downloadLimit: number) => {

_See [use case](../src/info/domain/useCases/GetZipDownloadLimit.ts) implementation_.

#### Get Application Terms of Use

Returns the Application Terms of Use. If you have enabled Internationalization you can pass a two-character language code (e.g. “en”) as the lang parameter.

##### Example call:

```typescript
import { getApplicationTermsOfUse } from '@iqss/dataverse-client-javascript'

/* ... */

getApplicationTermsOfUse.execute().then((termsOfUse: string) => {
/* ... */
})

/* ... */
```

_See [use case](../src/info/domain/useCases/GetApplicationTermsOfUse.ts) implementation_.

## Contact

#### Send Feedback to Object Contacts
Expand Down
1 change: 1 addition & 0 deletions src/info/domain/repositories/IDataverseInfoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface IDataverseInfoRepository {
getDataverseVersion(): Promise<DataverseVersion>
getZipDownloadLimit(): Promise<number>
getMaxEmbargoDurationInMonths(): Promise<number>
getApplicationTermsOfUse(lang?: string): Promise<string>
}
20 changes: 20 additions & 0 deletions src/info/domain/useCases/GetApplicationTermsOfUse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { UseCase } from '../../../core/domain/useCases/UseCase'
import { IDataverseInfoRepository } from '../repositories/IDataverseInfoRepository'

export class GetApplicationTermsOfUse implements UseCase<string> {
private dataverseInfoRepository: IDataverseInfoRepository

constructor(dataverseInfoRepository: IDataverseInfoRepository) {
this.dataverseInfoRepository = dataverseInfoRepository
}

/**
* Returns a string containing the application terms of use.
*
* @param {string} [lang] - Optional language parameter to specify the language of the terms of use.
* @returns {Promise<string>}
*/
async execute(lang?: string): Promise<string> {
return await this.dataverseInfoRepository.getApplicationTermsOfUse(lang)
}
}
9 changes: 8 additions & 1 deletion src/info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { DataverseInfoRepository } from './infra/repositories/DataverseInfoRepos
import { GetDataverseVersion } from './domain/useCases/GetDataverseVersion'
import { GetZipDownloadLimit } from './domain/useCases/GetZipDownloadLimit'
import { GetMaxEmbargoDurationInMonths } from './domain/useCases/GetMaxEmbargoDurationInMonths'
import { GetApplicationTermsOfUse } from './domain/useCases/GetApplicationTermsOfUse'

const dataverseInfoRepository = new DataverseInfoRepository()

const getDataverseVersion = new GetDataverseVersion(dataverseInfoRepository)
const getZipDownloadLimit = new GetZipDownloadLimit(dataverseInfoRepository)
const getMaxEmbargoDurationInMonths = new GetMaxEmbargoDurationInMonths(dataverseInfoRepository)
const getApplicationTermsOfUse = new GetApplicationTermsOfUse(dataverseInfoRepository)

export { getDataverseVersion, getZipDownloadLimit, getMaxEmbargoDurationInMonths }
export {
getDataverseVersion,
getZipDownloadLimit,
getMaxEmbargoDurationInMonths,
getApplicationTermsOfUse
}
16 changes: 16 additions & 0 deletions src/info/infra/repositories/DataverseInfoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ export class DataverseInfoRepository extends ApiRepository implements IDataverse
build: responseData.build
}
}

public async getApplicationTermsOfUse(lang?: string): Promise<string> {
return this.doGet(
this.buildApiEndpoint(this.infoResourceName, `applicationTermsOfUse`),
false,
{
...(lang ? { lang } : {})
}
)
.then((response: AxiosResponse<{ data: { message: string } }>) => {
return response.data.data.message
})
.catch((error) => {
throw error
})
}
}
26 changes: 25 additions & 1 deletion test/integration/info/DataverseInfoRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
DataverseApiAuthMechanism
} from '../../../src/core/infra/repositories/ApiConfig'
import { TestConstants } from '../../testHelpers/TestConstants'
import { setMaxEmbargoDurationInMonthsViaApi } from '../../testHelpers/info/infoHelper'
import {
deleteApplicationTermsOfUseViaApi,
setApplicationTermsOfUseViaApi,
setMaxEmbargoDurationInMonthsViaApi
} from '../../testHelpers/info/infoHelper'
import { ReadError } from '../../../src/core/domain/repositories/ReadError'

describe('DataverseInfoRepository', () => {
Expand Down Expand Up @@ -49,4 +53,24 @@ describe('DataverseInfoRepository', () => {
expect(actual).toBe(testMaxEmbargoDurationInMonths)
})
})

describe('getApplicationTermsOfUse', () => {
test('should return no terms message when terms are not set', async () => {
const defaultNoTermsOfUseMessage =
'There are no Terms of Use for this Dataverse installation.'
const actual = await sut.getApplicationTermsOfUse()

expect(actual).toBe(defaultNoTermsOfUseMessage)
})

test('should return terms when terms are set', async () => {
const testTermsOfUse = 'Be excellent to each other.'
await setApplicationTermsOfUseViaApi(testTermsOfUse)
const actual = await sut.getApplicationTermsOfUse()

expect(actual).toBe(testTermsOfUse)

await deleteApplicationTermsOfUseViaApi()
})
})
})
16 changes: 16 additions & 0 deletions test/testHelpers/info/infoHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ export const setMaxEmbargoDurationInMonthsViaApi = async (
}
)
}

export const setApplicationTermsOfUseViaApi = async (
applicationTermsOfUse: string
): Promise<AxiosResponse> => {
return await axios.put(
`${TestConstants.TEST_API_URL}/admin/settings/:ApplicationTermsOfUse`,
applicationTermsOfUse,
{
headers: { 'Content-Type': 'text/plain' }
}
)
}

export const deleteApplicationTermsOfUseViaApi = async (): Promise<AxiosResponse> => {
return await axios.delete(`${TestConstants.TEST_API_URL}/admin/settings/:ApplicationTermsOfUse`)
}
65 changes: 65 additions & 0 deletions test/unit/info/DataverseInfoRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,69 @@ describe('DataverseInfoRepository', () => {
expect(error).toBeInstanceOf(Error)
})
})

describe('getApplicationTermsOfUse', () => {
test('should return terms of use on successful response', async () => {
const testTermsOfUse = 'Be excellent to each other.'
const testSuccessfulResponse = {
data: {
status: 'OK',
data: {
message: testTermsOfUse
}
}
}
jest.spyOn(axios, 'get').mockResolvedValue(testSuccessfulResponse)

const actual = await sut.getApplicationTermsOfUse()

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/info/applicationTermsOfUse`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
)
expect(actual).toMatch(testTermsOfUse)
})

test('should return terms of use on successful response with lang', async () => {
const testLang = 'en'
const testTermsOfUse = 'Be excellent to each other.'
const testSuccessfulResponse = {
data: {
status: 'OK',
data: {
message: testTermsOfUse
}
}
}
jest.spyOn(axios, 'get').mockResolvedValue(testSuccessfulResponse)

const actual = await sut.getApplicationTermsOfUse(testLang)

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/info/applicationTermsOfUse`,
{
params: {
lang: testLang
},
headers: {
'Content-Type': 'application/json'
}
}
)
expect(actual).toMatch(testTermsOfUse)
})

test('should return error result on error response', async () => {
jest.spyOn(axios, 'get').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)

let error: ReadError | undefined
await sut.getApplicationTermsOfUse().catch((e) => (error = e))

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/info/applicationTermsOfUse`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
)
expect(error).toBeInstanceOf(Error)
})
})
})
29 changes: 29 additions & 0 deletions test/unit/info/GetApplicationTermsOfUse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ReadError } from '../../../src/core/domain/repositories/ReadError'
import { IDataverseInfoRepository } from '../../../src/info/domain/repositories/IDataverseInfoRepository'
import { GetApplicationTermsOfUse } from '../../../src/info/domain/useCases/GetApplicationTermsOfUse'

describe('execute', () => {
test('should return successful result with terms of use on repository success', async () => {
const testTermsOfUse = 'Be excellent to each other.'
const dataverseInfoRepositoryStub: IDataverseInfoRepository = {} as IDataverseInfoRepository
dataverseInfoRepositoryStub.getApplicationTermsOfUse = jest
.fn()
.mockResolvedValue(testTermsOfUse)
const sut = new GetApplicationTermsOfUse(dataverseInfoRepositoryStub)

const actual = await sut.execute()

expect(actual).toBe(testTermsOfUse)
})

test('should return error result on repository error', async () => {
const dataverseInfoRepositoryStub: IDataverseInfoRepository = {} as IDataverseInfoRepository
const testReadError = new ReadError()
dataverseInfoRepositoryStub.getApplicationTermsOfUse = jest
.fn()
.mockRejectedValue(testReadError)
const sut = new GetApplicationTermsOfUse(dataverseInfoRepositoryStub)

await expect(sut.execute()).rejects.toThrow(ReadError)
})
})