This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
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,117 @@ | ||
import { MockAgent, setGlobalDispatcher, getGlobalDispatcher, Dispatcher } from 'undici' | ||
import env from '../../src/env' | ||
|
||
export const selfAlias = 'test-self' | ||
export const selfAddress = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY' | ||
export const notSelfAlias = 'test-not-self' | ||
export const notSelfAddress = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty' | ||
|
||
export function withIdentitySelfMock() { | ||
let originalDispatcher: Dispatcher | ||
let mockAgent: MockAgent | ||
beforeEach(function () { | ||
originalDispatcher = getGlobalDispatcher() | ||
mockAgent = new MockAgent() | ||
setGlobalDispatcher(mockAgent) | ||
const mockIdentity = mockAgent.get(`http://${env.IDENTITY_SERVICE_HOST}:${env.IDENTITY_SERVICE_PORT}`) | ||
mockIdentity | ||
.intercept({ | ||
path: '/v1/self', | ||
method: 'GET', | ||
}) | ||
.reply(200, { | ||
alias: selfAlias, | ||
address: selfAddress, | ||
}) | ||
.persist() | ||
|
||
mockIdentity | ||
.intercept({ | ||
path: `/v1/members/${selfAddress}`, | ||
method: 'GET', | ||
}) | ||
.reply(200, { | ||
alias: selfAlias, | ||
address: selfAddress, | ||
}) | ||
.persist() | ||
|
||
mockIdentity | ||
.intercept({ | ||
path: `/v1/members/${notSelfAddress}`, | ||
method: 'GET', | ||
}) | ||
.reply(200, { | ||
alias: notSelfAlias, | ||
address: notSelfAddress, | ||
}) | ||
.persist() | ||
}) | ||
|
||
afterEach(function () { | ||
setGlobalDispatcher(originalDispatcher) | ||
}) | ||
} | ||
|
||
export const withIpfsMock = (fileContent?: string | object | Buffer) => { | ||
let originalDispatcher: Dispatcher | ||
let mockAgent: MockAgent | ||
|
||
beforeEach(function () { | ||
originalDispatcher = getGlobalDispatcher() | ||
mockAgent = new MockAgent() | ||
setGlobalDispatcher(mockAgent) | ||
const mockIpfs = mockAgent.get(`http://${env.IPFS_HOST}:${env.IPFS_PORT}`) | ||
|
||
mockIpfs | ||
.intercept({ | ||
path: '/api/v0/add?cid-version=0&wrap-with-directory=true', | ||
method: 'POST', | ||
}) | ||
.reply(200, { Name: '', Hash: 'QmXVStDC6kTpVHY1shgBQmyA4SuSrYnNRnHSak5iB6Eehn', Size: '63052' }) | ||
|
||
mockIpfs | ||
.intercept({ | ||
path: '/api/v0/ls?arg=QmXVStDC6kTpVHY1shgBQmyA4SuSrYnNRnHSak5iB6Eehn', | ||
method: 'POST', | ||
}) | ||
.reply(200, { | ||
Objects: [{ Links: [{ Hash: 'file_hash', Name: 'test' }] }], | ||
}) | ||
if (fileContent) { | ||
mockIpfs | ||
.intercept({ | ||
path: '/api/v0/cat?arg=file_hash', | ||
method: 'POST', | ||
}) | ||
.reply(200, fileContent) | ||
} | ||
}) | ||
|
||
afterEach(function () { | ||
setGlobalDispatcher(originalDispatcher) | ||
}) | ||
} | ||
|
||
export const withIpfsMockError = () => { | ||
let originalDispatcher: Dispatcher | ||
let mockAgent: MockAgent | ||
|
||
beforeEach(function () { | ||
originalDispatcher = getGlobalDispatcher() | ||
mockAgent = new MockAgent() | ||
setGlobalDispatcher(mockAgent) | ||
const mockIpfs = mockAgent.get(`http://${env.IPFS_HOST}:${env.IPFS_PORT}`) | ||
|
||
mockIpfs | ||
.intercept({ | ||
path: '/api/v0/add?cid-version=0&wrap-with-directory=true', | ||
method: 'POST', | ||
}) | ||
.reply(500, 'error') | ||
}) | ||
|
||
afterEach(function () { | ||
setGlobalDispatcher(originalDispatcher) | ||
}) | ||
} |
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,24 @@ | ||
import request from 'supertest' | ||
import express from 'express' | ||
|
||
export const get = async (app: express.Express, endpoint: string, headers: object = {}): Promise<request.Test> => { | ||
return request(app).get(endpoint).set(headers) | ||
} | ||
|
||
export const post = async ( | ||
app: express.Express, | ||
endpoint: string, | ||
body: object, | ||
headers: object = {} | ||
): Promise<request.Test> => { | ||
return request(app).post(endpoint).send(body).set(headers) | ||
} | ||
|
||
export const postFile = async ( | ||
app: express.Express, | ||
endpoint: string, | ||
buf: Buffer, | ||
filename: string | ||
): Promise<request.Test> => { | ||
return request(app).post(endpoint).set({ accept: 'application/octect-stream' }).attach('file', buf, filename) | ||
} |
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,23 @@ | ||
import Database from '../../src/lib/db' | ||
|
||
const db = new Database().db() | ||
|
||
export const parametersAttachmentId = 'a789ad47-91c3-446e-90f9-a7c9b233eaf8' | ||
export const exampleDate = '2023-01-01T00:00:00.000Z' | ||
|
||
export const cleanup = async () => { | ||
await db.attachment().del() | ||
} | ||
|
||
export const attachmentSeed = async () => { | ||
await cleanup() | ||
await db.attachment().insert([ | ||
{ | ||
id: parametersAttachmentId, | ||
filename: 'test.txt', | ||
ipfs_hash: 'QmXVStDC6kTpVHY1shgBQmyA4SuSrYnNRnHSak5iB6Eehn', | ||
size: 42, | ||
created_at: exampleDate, | ||
}, | ||
]) | ||
} |