Skip to content

Commit

Permalink
fix: tests and types for server proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiM committed Apr 18, 2024
1 parent b471731 commit cf369a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>[]) => {
/** Mock fetch responses and then initialize nest service */
Expand All @@ -16,11 +17,12 @@ const mockFetch = async (responseData: Partial<Response>[]) => {
mockedFetch.mockResolvedValueOnce(prepareResponse(data))
}

global.fetch = mockedFetch
const module = await Test.createTestingModule({
imports: [ServerProxyServiceModule],
}).compile()
return module.get<ServerProxyService>(ServerProxyService)
const service = module.get<ServerProxyService>(ServerProxyService)
service.fetch = mockedFetch
return service
}

describe('Server Proxy Service', () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +20,7 @@ export class ServerProxyService extends EventEmitter {
private readonly logger = Logger(ServerProxyService.name)
_serverAddress: string
fetch: any
fetchConfig: any
fetchConfig: RequestInitWithRetry<typeof fetch>

constructor() {
super()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/nest/storageServerProxy/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Response, Headers } from 'node-fetch'

export const prepareResponse = (responseData: Partial<Response>) => {
const ok = responseData.status ? responseData.status >= 200 && responseData.status < 300 : false
const response: Response = {
Expand All @@ -8,6 +10,8 @@ export const prepareResponse = (responseData: Partial<Response>) => {
statusText: '',
type: 'basic',
url: '',
size: 10,
buffer: () => Promise.resolve(Buffer.from('')),
clone: function (): Response {
throw new Error('Function not implemented.')
},
Expand Down

0 comments on commit cf369a4

Please sign in to comment.