-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAccessRepository.test.ts
More file actions
120 lines (103 loc) · 3.59 KB
/
AccessRepository.test.ts
File metadata and controls
120 lines (103 loc) · 3.59 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @jest-environment jsdom
*/
import { AccessRepository } from '../../../src/access/infra/repositories/AccessRepository'
import { GuestbookResponseDTO } from '../../../src/access/domain/dtos/GuestbookResponseDTO'
import { WriteError } from '../../../src/core/domain/repositories/WriteError'
import {
ApiConfig,
DataverseApiAuthMechanism
} from '../../../src/core/infra/repositories/ApiConfig'
import { TestConstants } from '../../testHelpers/TestConstants'
describe('AccessRepository', () => {
const sut = new AccessRepository()
const originalFetch = global.fetch
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(() => {
global.fetch = originalFetch
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')
})
test('parses signed url from a JSON body when content-type is incorrect', 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': 'text/plain' }),
text: jest
.fn()
.mockResolvedValue(JSON.stringify({ data: { signedUrl: 'https://signed.text' } }))
} as unknown as Response)
global.fetch = fetchMock as typeof fetch
await expect(sut.submitGuestbookForDatasetDownload(123, guestbookResponse)).resolves.toBe(
'https://signed.text'
)
})
test('throws WriteError when signedUrl is missing from a successful response', 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: {}
})
} as unknown as Response)
global.fetch = fetchMock as typeof fetch
await expect(sut.submitGuestbookForDatasetDownload(123, guestbookResponse)).rejects.toThrow(
new WriteError('Missing signedUrl in access download response.')
)
})
})