Skip to content

Commit

Permalink
feat: Util file for mocking envs
Browse files Browse the repository at this point in the history
  • Loading branch information
fzavalia committed Aug 25, 2022
1 parent eefa414 commit 757074c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
34 changes: 26 additions & 8 deletions src/LAND/LAND.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@ import supertest from 'supertest'
import { buildURL } from '../../spec/utils'
import { app } from '../server'
import { MAX_COORDS } from './LAND.router'
import { getLandRouterEnvs } from './utils'

jest.mock('./utils')

const mockGetLandRouterEnvs = getLandRouterEnvs as jest.MockedFunction<
typeof getLandRouterEnvs
>

const server = supertest(app.getApp())

describe('LAND router', () => {
let url: string

beforeEach(() => {
jest.clearAllMocks()

mockGetLandRouterEnvs.mockImplementation(() => ({
ipfsUrl: 'https://ipfs.xyz',
ipfsProjectId: 'ipfsProjectId',
ipfsApiKey: 'ipfsApiKey',
explorerUrl: 'https://explorer.xyz',
}))
})

describe('when fetching the redirection file hashes', () => {
beforeEach(() => {
url = '/lands/redirectionHashes?'
Expand Down Expand Up @@ -57,8 +75,8 @@ describe('LAND router', () => {
expect(body).toEqual({
data: new Array(MAX_COORDS).fill({
contentHash:
'e30101701220166b6b4d93ece408525081ade3c0258c6e992180d9b4ceb469d08192589095d7',
ipfsHash: 'QmPrAgSua8WLPZ4CNL6tGMSdN45Kte3WjPJTPjfQSfv1kJ',
'e301017012205453e784584c205c23a771c67af071129721f0e21b0472e3061361005393a908',
ipfsHash: 'QmU1qAKrZKEUinZ7j7gbPcJ7dKSkJ6diHLk9YrB4PbLr7q',
x: 100,
y: 100,
}),
Expand Down Expand Up @@ -99,15 +117,15 @@ describe('LAND router', () => {
data: [
{
contentHash:
'e30101701220166b6b4d93ece408525081ade3c0258c6e992180d9b4ceb469d08192589095d7',
ipfsHash: 'QmPrAgSua8WLPZ4CNL6tGMSdN45Kte3WjPJTPjfQSfv1kJ',
'e301017012205453e784584c205c23a771c67af071129721f0e21b0472e3061361005393a908',
ipfsHash: 'QmU1qAKrZKEUinZ7j7gbPcJ7dKSkJ6diHLk9YrB4PbLr7q',
x: 100,
y: 100,
},
{
contentHash:
'e301017012207a05747aa476fe4f72b0316b99b6f5af96be152288d9a840aba5d2bc17bd88b7',
ipfsHash: 'QmWYyGnw1QRSZDZKaXppu9GW37JtFGdhgADLVSagguXgjg',
'e301017012204d02b4fe9cee2f8e4ab0a88adbf2338faa903355a0280d725f72f0f79929c079',
ipfsHash: 'QmTXGVk1DtCP6km25iNUmEmV2Wf7dCFAziwiTzZNeQKM9z',
x: 200,
y: 200,
},
Expand Down Expand Up @@ -149,8 +167,8 @@ describe('LAND router', () => {
data: [
{
contentHash:
'e30101701220166b6b4d93ece408525081ade3c0258c6e992180d9b4ceb469d08192589095d7',
ipfsHash: 'QmPrAgSua8WLPZ4CNL6tGMSdN45Kte3WjPJTPjfQSfv1kJ',
'e301017012205453e784584c205c23a771c67af071129721f0e21b0472e3061361005393a908',
ipfsHash: 'QmU1qAKrZKEUinZ7j7gbPcJ7dKSkJ6diHLk9YrB4PbLr7q',
x: 100,
y: 100,
},
Expand Down
59 changes: 26 additions & 33 deletions src/LAND/LAND.router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { env } from 'decentraland-commons'
import { server } from 'decentraland-server'
import FormData from 'form-data'
import { Request } from 'express'
Expand All @@ -12,19 +11,13 @@ import {
GetRedirectionHashesResponse,
UploadRedirectionResponse,
} from './LAND.types'
import { getLandRouterEnvs } from './utils'

export const MAX_COORDS = 150
const INDEX_FILE = 'index.html'

export class LANDRouter extends Router {
private ipfsUrl = env.get('IPFS_URL')
private ipfsProjectId = env.get('IPFS_PROJECT_ID')
private ipfsApiKey = env.get('IPFS_API_KEY')
private explorerUrl = env.get('EXPLORER_URL')

mount() {
this.verifyEnvs()

this.router.get(
'/lands/redirectionHashes',
server.handleRequest(this.getRedirectionHashes)
Expand All @@ -39,13 +32,24 @@ export class LANDRouter extends Router {
private uploadRedirection = async (
req: Request
): Promise<UploadRedirectionResponse> => {
const {
ipfsUrl,
ipfsProjectId,
ipfsApiKey,
explorerUrl,
} = getLandRouterEnvs()

const coords = server.extractFromReq(req, 'coords')

this.validateCoords(coords)

const locale = req.headers['accept-language']

const redirectionFile = this.generateRedirectionFile(coords, locale)
const redirectionFile = this.generateRedirectionFile(
coords,
explorerUrl,
locale
)

const formData = new FormData()

Expand All @@ -54,12 +58,12 @@ export class LANDRouter extends Router {
let result: FetchResponse

try {
result = await fetch(this.ipfsUrl + '/api/v0/add?pin=false', {
result = await fetch(ipfsUrl + '/api/v0/add?pin=false', {
method: 'post',
body: formData,
headers: {
Authorization: `Basic ${Buffer.from(
`${this.ipfsProjectId}:${this.ipfsApiKey}`
`${ipfsProjectId}:${ipfsApiKey}`
).toString('base64')}`,
},
})
Expand Down Expand Up @@ -90,11 +94,13 @@ export class LANDRouter extends Router {
private getRedirectionHashes = async (
req: Request
): Promise<GetRedirectionHashesResponse> => {
const { explorerUrl } = getLandRouterEnvs()

let coordsListQP: string | string[]

try {
coordsListQP = server.extractFromReq<string | string[]>(req, 'coords')
} catch (e) {
} catch (e: any) {
throw new HTTPError(e.message, {}, STATUS_CODES.badRequest)
}

Expand All @@ -116,7 +122,11 @@ export class LANDRouter extends Router {
for (const coords of coordsList) {
this.validateCoords(coords)

const redirectionFile = this.generateRedirectionFile(coords, locale)
const redirectionFile = this.generateRedirectionFile(
coords,
explorerUrl,
locale
)

const ipfsHash = await getCID({
path: INDEX_FILE,
Expand All @@ -137,24 +147,6 @@ export class LANDRouter extends Router {
return output
}

private verifyEnvs = () => {
if (!this.ipfsUrl) {
throw new Error('IPFS_URL not defined')
}

if (!this.ipfsProjectId) {
throw new Error('IPFS_PROJECT_ID not defined')
}

if (!this.ipfsApiKey) {
throw new Error('IPFS_API_KEY not defined')
}

if (!this.explorerUrl) {
throw new Error('EXPLORER_URL not defined')
}
}

private validateCoords = (coords: string): void => {
if (!/^-?[1-9]\d*,-?[1-9]\d*$/.test(coords)) {
throw new HTTPError(
Expand All @@ -167,6 +159,7 @@ export class LANDRouter extends Router {

private generateRedirectionFile = (
coords: string,
explorerUrl: string,
locale?: string
): Buffer => {
let messages: [string, string]
Expand All @@ -186,13 +179,13 @@ export class LANDRouter extends Router {
<head>
<meta
http-equiv="refresh"
content="0; URL=${this.explorerUrl}?position=${coords}"
content="0; URL=${explorerUrl}?position=${coords}"
/>
</head>
<body>
<p>
${messages[0]}
<a href="${this.explorerUrl}?position=${coords}">
<a href="${explorerUrl}?position=${coords}">
${messages[1]}
</a>.
</p>
Expand Down
31 changes: 31 additions & 0 deletions src/LAND/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { env } from 'decentraland-commons'

export const getLandRouterEnvs = () => {
const ipfsUrl: string | undefined = env.get('IPFS_URL')
const ipfsProjectId: string | undefined = env.get('IPFS_PROJECT_ID')
const ipfsApiKey: string | undefined = env.get('IPFS_API_KEY')
const explorerUrl: string | undefined = env.get('EXPLORER_URL')

if (!ipfsUrl) {
throw new Error('IPFS_URL not defined')
}

if (!ipfsProjectId) {
throw new Error('IPFS_PROJECT_ID not defined')
}

if (!ipfsApiKey) {
throw new Error('IPFS_API_KEY not defined')
}

if (!explorerUrl) {
throw new Error('EXPLORER_URL not defined')
}

return {
ipfsUrl,
ipfsProjectId,
ipfsApiKey,
explorerUrl,
}
}

0 comments on commit 757074c

Please sign in to comment.