Skip to content

Commit c748a8f

Browse files
authored
Merge pull request #296 from IQSS/feat/293-get-app-terms-of-use
Get Application Terms of Use - Use Case
2 parents 24d92b3 + b6ed17d commit c748a8f

File tree

9 files changed

+201
-2
lines changed

9 files changed

+201
-2
lines changed

docs/useCases.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The different use cases currently available in the package are classified below,
7676
- [Get Dataverse Backend Version](#get-dataverse-backend-version)
7777
- [Get Maximum Embargo Duration In Months](#get-maximum-embargo-duration-in-months)
7878
- [Get ZIP Download Limit](#get-zip-download-limit)
79+
- [Get Application Terms of Use](#get-application-terms-of-use)
7980
- [Contact](#Contact)
8081
- [Send Feedback to Object Contacts](#send-feedback-to-object-contacts)
8182

@@ -1838,6 +1839,26 @@ getZipDownloadLimit.execute().then((downloadLimit: number) => {
18381839

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

1842+
#### Get Application Terms of Use
1843+
1844+
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.
1845+
1846+
##### Example call:
1847+
1848+
```typescript
1849+
import { getApplicationTermsOfUse } from '@iqss/dataverse-client-javascript'
1850+
1851+
/* ... */
1852+
1853+
getApplicationTermsOfUse.execute().then((termsOfUse: string) => {
1854+
/* ... */
1855+
})
1856+
1857+
/* ... */
1858+
```
1859+
1860+
_See [use case](../src/info/domain/useCases/GetApplicationTermsOfUse.ts) implementation_.
1861+
18411862
## Contact
18421863

18431864
#### Send Feedback to Object Contacts

src/info/domain/repositories/IDataverseInfoRepository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface IDataverseInfoRepository {
44
getDataverseVersion(): Promise<DataverseVersion>
55
getZipDownloadLimit(): Promise<number>
66
getMaxEmbargoDurationInMonths(): Promise<number>
7+
getApplicationTermsOfUse(lang?: string): Promise<string>
78
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { UseCase } from '../../../core/domain/useCases/UseCase'
2+
import { IDataverseInfoRepository } from '../repositories/IDataverseInfoRepository'
3+
4+
export class GetApplicationTermsOfUse implements UseCase<string> {
5+
private dataverseInfoRepository: IDataverseInfoRepository
6+
7+
constructor(dataverseInfoRepository: IDataverseInfoRepository) {
8+
this.dataverseInfoRepository = dataverseInfoRepository
9+
}
10+
11+
/**
12+
* Returns a string containing the application terms of use.
13+
*
14+
* @param {string} [lang] - Optional language parameter to specify the language of the terms of use.
15+
* @returns {Promise<string>}
16+
*/
17+
async execute(lang?: string): Promise<string> {
18+
return await this.dataverseInfoRepository.getApplicationTermsOfUse(lang)
19+
}
20+
}

src/info/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ import { DataverseInfoRepository } from './infra/repositories/DataverseInfoRepos
22
import { GetDataverseVersion } from './domain/useCases/GetDataverseVersion'
33
import { GetZipDownloadLimit } from './domain/useCases/GetZipDownloadLimit'
44
import { GetMaxEmbargoDurationInMonths } from './domain/useCases/GetMaxEmbargoDurationInMonths'
5+
import { GetApplicationTermsOfUse } from './domain/useCases/GetApplicationTermsOfUse'
56

67
const dataverseInfoRepository = new DataverseInfoRepository()
78

89
const getDataverseVersion = new GetDataverseVersion(dataverseInfoRepository)
910
const getZipDownloadLimit = new GetZipDownloadLimit(dataverseInfoRepository)
1011
const getMaxEmbargoDurationInMonths = new GetMaxEmbargoDurationInMonths(dataverseInfoRepository)
12+
const getApplicationTermsOfUse = new GetApplicationTermsOfUse(dataverseInfoRepository)
1113

12-
export { getDataverseVersion, getZipDownloadLimit, getMaxEmbargoDurationInMonths }
14+
export {
15+
getDataverseVersion,
16+
getZipDownloadLimit,
17+
getMaxEmbargoDurationInMonths,
18+
getApplicationTermsOfUse
19+
}

src/info/infra/repositories/DataverseInfoRepository.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,20 @@ export class DataverseInfoRepository extends ApiRepository implements IDataverse
3939
build: responseData.build
4040
}
4141
}
42+
43+
public async getApplicationTermsOfUse(lang?: string): Promise<string> {
44+
return this.doGet(
45+
this.buildApiEndpoint(this.infoResourceName, `applicationTermsOfUse`),
46+
false,
47+
{
48+
...(lang ? { lang } : {})
49+
}
50+
)
51+
.then((response: AxiosResponse<{ data: { message: string } }>) => {
52+
return response.data.data.message
53+
})
54+
.catch((error) => {
55+
throw error
56+
})
57+
}
4258
}

test/integration/info/DataverseInfoRepository.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
DataverseApiAuthMechanism
55
} from '../../../src/core/infra/repositories/ApiConfig'
66
import { TestConstants } from '../../testHelpers/TestConstants'
7-
import { setMaxEmbargoDurationInMonthsViaApi } from '../../testHelpers/info/infoHelper'
7+
import {
8+
deleteApplicationTermsOfUseViaApi,
9+
setApplicationTermsOfUseViaApi,
10+
setMaxEmbargoDurationInMonthsViaApi
11+
} from '../../testHelpers/info/infoHelper'
812
import { ReadError } from '../../../src/core/domain/repositories/ReadError'
913

1014
describe('DataverseInfoRepository', () => {
@@ -49,4 +53,24 @@ describe('DataverseInfoRepository', () => {
4953
expect(actual).toBe(testMaxEmbargoDurationInMonths)
5054
})
5155
})
56+
57+
describe('getApplicationTermsOfUse', () => {
58+
test('should return no terms message when terms are not set', async () => {
59+
const defaultNoTermsOfUseMessage =
60+
'There are no Terms of Use for this Dataverse installation.'
61+
const actual = await sut.getApplicationTermsOfUse()
62+
63+
expect(actual).toBe(defaultNoTermsOfUseMessage)
64+
})
65+
66+
test('should return terms when terms are set', async () => {
67+
const testTermsOfUse = 'Be excellent to each other.'
68+
await setApplicationTermsOfUseViaApi(testTermsOfUse)
69+
const actual = await sut.getApplicationTermsOfUse()
70+
71+
expect(actual).toBe(testTermsOfUse)
72+
73+
await deleteApplicationTermsOfUseViaApi()
74+
})
75+
})
5276
})

test/testHelpers/info/infoHelper.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,19 @@ export const setMaxEmbargoDurationInMonthsViaApi = async (
1212
}
1313
)
1414
}
15+
16+
export const setApplicationTermsOfUseViaApi = async (
17+
applicationTermsOfUse: string
18+
): Promise<AxiosResponse> => {
19+
return await axios.put(
20+
`${TestConstants.TEST_API_URL}/admin/settings/:ApplicationTermsOfUse`,
21+
applicationTermsOfUse,
22+
{
23+
headers: { 'Content-Type': 'text/plain' }
24+
}
25+
)
26+
}
27+
28+
export const deleteApplicationTermsOfUseViaApi = async (): Promise<AxiosResponse> => {
29+
return await axios.delete(`${TestConstants.TEST_API_URL}/admin/settings/:ApplicationTermsOfUse`)
30+
}

test/unit/info/DataverseInfoRepository.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,69 @@ describe('DataverseInfoRepository', () => {
123123
expect(error).toBeInstanceOf(Error)
124124
})
125125
})
126+
127+
describe('getApplicationTermsOfUse', () => {
128+
test('should return terms of use on successful response', async () => {
129+
const testTermsOfUse = 'Be excellent to each other.'
130+
const testSuccessfulResponse = {
131+
data: {
132+
status: 'OK',
133+
data: {
134+
message: testTermsOfUse
135+
}
136+
}
137+
}
138+
jest.spyOn(axios, 'get').mockResolvedValue(testSuccessfulResponse)
139+
140+
const actual = await sut.getApplicationTermsOfUse()
141+
142+
expect(axios.get).toHaveBeenCalledWith(
143+
`${TestConstants.TEST_API_URL}/info/applicationTermsOfUse`,
144+
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
145+
)
146+
expect(actual).toMatch(testTermsOfUse)
147+
})
148+
149+
test('should return terms of use on successful response with lang', async () => {
150+
const testLang = 'en'
151+
const testTermsOfUse = 'Be excellent to each other.'
152+
const testSuccessfulResponse = {
153+
data: {
154+
status: 'OK',
155+
data: {
156+
message: testTermsOfUse
157+
}
158+
}
159+
}
160+
jest.spyOn(axios, 'get').mockResolvedValue(testSuccessfulResponse)
161+
162+
const actual = await sut.getApplicationTermsOfUse(testLang)
163+
164+
expect(axios.get).toHaveBeenCalledWith(
165+
`${TestConstants.TEST_API_URL}/info/applicationTermsOfUse`,
166+
{
167+
params: {
168+
lang: testLang
169+
},
170+
headers: {
171+
'Content-Type': 'application/json'
172+
}
173+
}
174+
)
175+
expect(actual).toMatch(testTermsOfUse)
176+
})
177+
178+
test('should return error result on error response', async () => {
179+
jest.spyOn(axios, 'get').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)
180+
181+
let error: ReadError | undefined
182+
await sut.getApplicationTermsOfUse().catch((e) => (error = e))
183+
184+
expect(axios.get).toHaveBeenCalledWith(
185+
`${TestConstants.TEST_API_URL}/info/applicationTermsOfUse`,
186+
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
187+
)
188+
expect(error).toBeInstanceOf(Error)
189+
})
190+
})
126191
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ReadError } from '../../../src/core/domain/repositories/ReadError'
2+
import { IDataverseInfoRepository } from '../../../src/info/domain/repositories/IDataverseInfoRepository'
3+
import { GetApplicationTermsOfUse } from '../../../src/info/domain/useCases/GetApplicationTermsOfUse'
4+
5+
describe('execute', () => {
6+
test('should return successful result with terms of use on repository success', async () => {
7+
const testTermsOfUse = 'Be excellent to each other.'
8+
const dataverseInfoRepositoryStub: IDataverseInfoRepository = {} as IDataverseInfoRepository
9+
dataverseInfoRepositoryStub.getApplicationTermsOfUse = jest
10+
.fn()
11+
.mockResolvedValue(testTermsOfUse)
12+
const sut = new GetApplicationTermsOfUse(dataverseInfoRepositoryStub)
13+
14+
const actual = await sut.execute()
15+
16+
expect(actual).toBe(testTermsOfUse)
17+
})
18+
19+
test('should return error result on repository error', async () => {
20+
const dataverseInfoRepositoryStub: IDataverseInfoRepository = {} as IDataverseInfoRepository
21+
const testReadError = new ReadError()
22+
dataverseInfoRepositoryStub.getApplicationTermsOfUse = jest
23+
.fn()
24+
.mockRejectedValue(testReadError)
25+
const sut = new GetApplicationTermsOfUse(dataverseInfoRepositoryStub)
26+
27+
await expect(sut.execute()).rejects.toThrow(ReadError)
28+
})
29+
})

0 commit comments

Comments
 (0)