Skip to content

Commit 92dd276

Browse files
committed
test: add unit and integration tests
1 parent df33a12 commit 92dd276

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

test/integration/info/DataverseInfoRepository.test.ts

Lines changed: 24 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,23 @@ 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+
test('should return terms when terms are set', async () => {
66+
const testTermsOfUse = 'Be excellent to each other.'
67+
await setApplicationTermsOfUseViaApi(testTermsOfUse)
68+
const actual = await sut.getApplicationTermsOfUse()
69+
70+
expect(actual).toBe(testTermsOfUse)
71+
72+
await deleteApplicationTermsOfUseViaApi()
73+
})
74+
})
5275
})

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)