diff --git a/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.spec.ts b/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.spec.ts index f61d0eaa35..4187352e26 100644 --- a/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.spec.ts +++ b/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.spec.ts @@ -5,6 +5,7 @@ import { ServerStoredCommunityMetadata } from './storageServerProxy.types' import { jest } from '@jest/globals' import { prepareResponse } from './testUtils' import { createLibp2pAddress, getValidInvitationUrlTestData, validInvitationDatav1 } from '@quiet/common' +import { Response } from 'node-fetch' const mockFetch = async (responseData: Partial[]) => { /** Mock fetch responses and then initialize nest service */ @@ -16,11 +17,12 @@ const mockFetch = async (responseData: Partial[]) => { mockedFetch.mockResolvedValueOnce(prepareResponse(data)) } - global.fetch = mockedFetch const module = await Test.createTestingModule({ imports: [ServerProxyServiceModule], }).compile() - return module.get(ServerProxyService) + const service = module.get(ServerProxyService) + service.fetch = mockedFetch + return service } describe('Server Proxy Service', () => { @@ -49,7 +51,7 @@ describe('Server Proxy Service', () => { service.setServerAddress('http://whatever') const data = await service.downloadData('cid') expect(data).toEqual(clientMetadata) - expect(global.fetch).toHaveBeenCalledTimes(2) + expect(service.fetch).toHaveBeenCalledTimes(2) }) it('throws error if downloaded metadata does not have proper schema', async () => { @@ -74,6 +76,6 @@ describe('Server Proxy Service', () => { service.setServerAddress('http://whatever') const token = await service.auth() expect(token).toEqual(expectedToken) - expect(global.fetch).toHaveBeenCalledTimes(1) + expect(service.fetch).toHaveBeenCalledTimes(1) }) }) diff --git a/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.ts b/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.ts index 4a97ade66d..e3c8f8aa90 100644 --- a/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.ts +++ b/packages/backend/src/nest/storageServerProxy/storageServerProxy.service.ts @@ -4,7 +4,7 @@ import { ServerStoredCommunityMetadata } from './storageServerProxy.types' import fetchRetry, { RequestInitWithRetry } from 'fetch-retry' import Logger from '../common/logger' import { isServerStoredMetadata } from '../validation/validators' -import fetch from 'node-fetch' +import fetch, { Response } from 'node-fetch' class HTTPResponseError extends Error { response: Response @@ -20,7 +20,7 @@ export class ServerProxyService extends EventEmitter { private readonly logger = Logger(ServerProxyService.name) _serverAddress: string fetch: any - fetchConfig: any + fetchConfig: RequestInitWithRetry constructor() { super() @@ -85,7 +85,7 @@ export class ServerProxyService extends EventEmitter { if (!dataResponse.ok) { throw new HTTPResponseError('Failed to download data', dataResponse) } - const data: ServerStoredCommunityMetadata = await dataResponse.json() + const data = (await dataResponse.json()) as ServerStoredCommunityMetadata this.validateMetadata(data) this.logger('Downloaded data', data) return data diff --git a/packages/backend/src/nest/storageServerProxy/testUtils.ts b/packages/backend/src/nest/storageServerProxy/testUtils.ts index 710be9b126..ef7c69023a 100644 --- a/packages/backend/src/nest/storageServerProxy/testUtils.ts +++ b/packages/backend/src/nest/storageServerProxy/testUtils.ts @@ -1,3 +1,5 @@ +import { Response, Headers } from 'node-fetch' + export const prepareResponse = (responseData: Partial) => { const ok = responseData.status ? responseData.status >= 200 && responseData.status < 300 : false const response: Response = { @@ -8,6 +10,8 @@ export const prepareResponse = (responseData: Partial) => { statusText: '', type: 'basic', url: '', + size: 10, + buffer: () => Promise.resolve(Buffer.from('')), clone: function (): Response { throw new Error('Function not implemented.') },