Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
HYP-13: unstagged files.
Browse files Browse the repository at this point in the history
  • Loading branch information
n3op2 committed Nov 16, 2023
1 parent 3a07ccb commit dda713b
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
117 changes: 117 additions & 0 deletions test/helpers/mock.ts
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)
})
}
24 changes: 24 additions & 0 deletions test/helpers/routeHelper.ts
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)
}
23 changes: 23 additions & 0 deletions test/seeds/attachment.ts
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,
},
])
}

0 comments on commit dda713b

Please sign in to comment.