-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add basic storage service proxy tests
- Loading branch information
Showing
4 changed files
with
134 additions
and
18 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
packages/backend/src/nest/storageServerProxy/storageServerProxy.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { TestModule } from '../common/test.module' | ||
import { ServerProxyServiceModule } from './storageServerProxy.module' | ||
import { ServerProxyService } from './storageServerProxy.service' | ||
import { ServerStoredCommunityMetadata } from './storageServerProxy.types' | ||
import { jest } from '@jest/globals' | ||
import { prepareResponse } from './testUtils' | ||
import { createLibp2pAddress, getValidInvitationUrlTestData, validInvitationDatav1 } from '@quiet/common' | ||
|
||
const mockFetch = async (responseData: Partial<Response>[]) => { | ||
/** Mock fetch responses and then initialize nest service */ | ||
const mockedFetch = jest.fn(() => { | ||
return Promise.resolve(prepareResponse(responseData[0])) | ||
}) | ||
|
||
for (const data of responseData) { | ||
mockedFetch.mockResolvedValueOnce(prepareResponse(data)) | ||
} | ||
|
||
global.fetch = mockedFetch | ||
const module = await Test.createTestingModule({ | ||
imports: [ServerProxyServiceModule], | ||
}).compile() | ||
return { service: module.get<ServerProxyService>(ServerProxyService), module } | ||
} | ||
|
||
describe('Server Proxy Service', () => { | ||
let testingModule: TestingModule | ||
let clientMetadata: ServerStoredCommunityMetadata | ||
beforeEach(() => { | ||
const data = getValidInvitationUrlTestData(validInvitationDatav1[0]).data | ||
clientMetadata = { | ||
id: '12345678', | ||
ownerCertificate: 'MIIDeTCCAyCgAwIBAgIGAYv8J0ToMAoGCCqGSM49BAMCMBIxEDAOBgNVBAMTB21hYzIzMT', | ||
rootCa: 'MIIBUjCB+KADAgECAgEBMAoGCCqGSM49BAMCMBIxEDAOBgNVBAM', | ||
ownerOrbitDbIdentity: data.ownerOrbitDbIdentity, | ||
peerList: [createLibp2pAddress(data.pairs[0].onionAddress, data.pairs[0].peerId)], | ||
psk: data.psk, | ||
} | ||
}) | ||
|
||
afterEach(async () => { | ||
jest.clearAllMocks() | ||
await testingModule.close() | ||
}) | ||
|
||
it('downloads data for existing cid and proper server address', async () => { | ||
const { module, service } = await mockFetch([ | ||
{ status: 200, json: () => Promise.resolve({ access_token: 'secretToken' }) }, | ||
{ status: 200, json: () => Promise.resolve(clientMetadata) }, | ||
]) | ||
testingModule = module | ||
service.setServerAddress('http://whatever') | ||
const data = await service.downloadData('cid') | ||
expect(data).toEqual(clientMetadata) | ||
expect(global.fetch).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('obtains token', async () => { | ||
const expectedToken = 'verySecretToken' | ||
const { module, service } = await mockFetch([ | ||
{ status: 200, json: () => Promise.resolve({ access_token: expectedToken }) }, | ||
]) | ||
testingModule = module | ||
service.setServerAddress('http://whatever') | ||
const token = await service.auth() | ||
expect(token).toEqual(expectedToken) | ||
expect(global.fetch).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export const prepareResponse = (responseData: Partial<Response>) => { | ||
const ok = responseData.status ? responseData.status >= 200 && responseData.status < 300 : false | ||
const response: Response = { | ||
headers: new Headers(), | ||
ok, | ||
redirected: false, | ||
status: 200, | ||
statusText: '', | ||
type: 'basic', | ||
url: '', | ||
clone: function (): Response { | ||
throw new Error('Function not implemented.') | ||
}, | ||
body: null, | ||
bodyUsed: false, | ||
arrayBuffer: function (): Promise<ArrayBuffer> { | ||
throw new Error('Function not implemented.') | ||
}, | ||
blob: function (): Promise<Blob> { | ||
throw new Error('Function not implemented.') | ||
}, | ||
formData: function (): Promise<FormData> { | ||
throw new Error('Function not implemented.') | ||
}, | ||
json: function (): Promise<any> { | ||
throw new Error('Function not implemented.') | ||
}, | ||
text: function (): Promise<string> { | ||
throw new Error('Function not implemented.') | ||
}, | ||
...responseData, | ||
} | ||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters