44
55import { AccessRepository } from '../../../src/access/infra/repositories/AccessRepository'
66import { GuestbookResponseDTO } from '../../../src/access/domain/dtos/GuestbookResponseDTO'
7+ import { WriteError } from '../../../src/core/domain/repositories/WriteError'
78import {
89 ApiConfig ,
910 DataverseApiAuthMechanism
@@ -12,6 +13,7 @@ import { TestConstants } from '../../testHelpers/TestConstants'
1213
1314describe ( 'AccessRepository' , ( ) => {
1415 const sut = new AccessRepository ( )
16+ const originalFetch = global . fetch
1517 const guestbookResponse : GuestbookResponseDTO = {
1618 guestbookResponse : {
1719 answers : [ { id : 1 , value : 'question 1' } ]
@@ -26,6 +28,7 @@ describe('AccessRepository', () => {
2628 } )
2729
2830 afterEach ( ( ) => {
31+ global . fetch = originalFetch
2932 window . localStorage . clear ( )
3033 } )
3134
@@ -66,4 +69,52 @@ describe('AccessRepository', () => {
6669 )
6770 expect ( actual ) . toBe ( 'https://signed.dataset' )
6871 } )
72+
73+ test ( 'parses signed url from a JSON body when content-type is incorrect' , async ( ) => {
74+ ApiConfig . init (
75+ TestConstants . TEST_API_URL ,
76+ DataverseApiAuthMechanism . BEARER_TOKEN ,
77+ undefined ,
78+ TestConstants . TEST_BEARER_TOKEN_LOCAL_STORAGE_KEY
79+ )
80+
81+ const fetchMock = jest . fn ( ) . mockResolvedValue ( {
82+ ok : true ,
83+ status : 200 ,
84+ headers : new Headers ( { 'content-type' : 'text/plain' } ) ,
85+ text : jest
86+ . fn ( )
87+ . mockResolvedValue ( JSON . stringify ( { data : { signedUrl : 'https://signed.text' } } ) )
88+ } as unknown as Response )
89+
90+ global . fetch = fetchMock as typeof fetch
91+
92+ await expect ( sut . submitGuestbookForDatasetDownload ( 123 , guestbookResponse ) ) . resolves . toBe (
93+ 'https://signed.text'
94+ )
95+ } )
96+
97+ test ( 'throws WriteError when signedUrl is missing from a successful response' , async ( ) => {
98+ ApiConfig . init (
99+ TestConstants . TEST_API_URL ,
100+ DataverseApiAuthMechanism . BEARER_TOKEN ,
101+ undefined ,
102+ TestConstants . TEST_BEARER_TOKEN_LOCAL_STORAGE_KEY
103+ )
104+
105+ const fetchMock = jest . fn ( ) . mockResolvedValue ( {
106+ ok : true ,
107+ status : 200 ,
108+ headers : new Headers ( { 'content-type' : 'application/json' } ) ,
109+ json : jest . fn ( ) . mockResolvedValue ( {
110+ data : { }
111+ } )
112+ } as unknown as Response )
113+
114+ global . fetch = fetchMock as typeof fetch
115+
116+ await expect ( sut . submitGuestbookForDatasetDownload ( 123 , guestbookResponse ) ) . rejects . toThrow (
117+ new WriteError ( 'Missing signedUrl in access download response.' )
118+ )
119+ } )
69120} )
0 commit comments