-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAccessRepository.test.ts
More file actions
69 lines (60 loc) · 1.97 KB
/
AccessRepository.test.ts
File metadata and controls
69 lines (60 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @jest-environment jsdom
*/
import { AccessRepository } from '../../../src/access/infra/repositories/AccessRepository'
import { GuestbookResponseDTO } from '../../../src/access/domain/dtos/GuestbookResponseDTO'
import {
ApiConfig,
DataverseApiAuthMechanism
} from '../../../src/core/infra/repositories/ApiConfig'
import { TestConstants } from '../../testHelpers/TestConstants'
describe('AccessRepository', () => {
const sut = new AccessRepository()
const guestbookResponse: GuestbookResponseDTO = {
guestbookResponse: {
answers: [{ id: 1, value: 'question 1' }]
}
}
beforeEach(() => {
window.localStorage.setItem(
TestConstants.TEST_BEARER_TOKEN_LOCAL_STORAGE_KEY,
JSON.stringify(TestConstants.TEST_DUMMY_BEARER_TOKEN)
)
})
afterEach(() => {
window.localStorage.clear()
})
test('uses fetch with credentials omit for bearer token auth', async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
TestConstants.TEST_BEARER_TOKEN_LOCAL_STORAGE_KEY
)
const fetchMock = jest.fn().mockResolvedValue({
ok: true,
status: 200,
headers: new Headers({ 'content-type': 'application/json' }),
json: jest.fn().mockResolvedValue({
data: {
signedUrl: 'https://signed.dataset'
}
})
} as unknown as Response)
global.fetch = fetchMock as typeof fetch
const actual = await sut.submitGuestbookForDatasetDownload(123, guestbookResponse, 'original')
expect(fetchMock).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/access/dataset/123?signed=true&format=original`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${TestConstants.TEST_DUMMY_BEARER_TOKEN}`
},
credentials: 'omit',
body: JSON.stringify(guestbookResponse)
}
)
expect(actual).toBe('https://signed.dataset')
})
})