Skip to content

Commit

Permalink
test: about handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aleortega committed Oct 29, 2024
1 parent 42c5c57 commit 7e52e31
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/controllers/handlers/about-main-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ import { HandlerContextWithPath, ServiceUnavailableError } from '../../types'
import { About } from '@dcl/catalyst-api-specs/lib/client'
import { randomInt } from 'crypto'

const BLACKLISTED_CATALYSTS: [] = []

export async function aboutMainHandler(
context: Pick<HandlerContextWithPath<'catalystsProvider' | 'mainRealmProvider', '/main/about'>, 'components' | 'url'>
context: Pick<
HandlerContextWithPath<'catalystsProvider' | 'mainRealmProvider' | 'config', '/main/about'>,
'components' | 'url'
>
): Promise<{ status: 200; body: About }> {
const {
components: { catalystsProvider, mainRealmProvider }
components: { catalystsProvider, mainRealmProvider, config }
} = context
const blacklistedCatalyst = ((await config.getString('BLACKLISTED_CATALYST')) || '').split(';').filter(Boolean)
const catalysts = await catalystsProvider.getHealhtyCatalysts()

if (catalysts.length === 0) {
throw new ServiceUnavailableError('No content catalysts available')
}

const filteredCatalysts = BLACKLISTED_CATALYSTS.length
const filteredCatalysts = blacklistedCatalyst.length
? catalysts.filter(
(catalyst: any) =>
!BLACKLISTED_CATALYSTS.some((blackListedCatalyst) => catalyst.url.toLowerCase().includes(blackListedCatalyst))
!blacklistedCatalyst.some((blackListedCatalyst) => catalyst.url.toLowerCase().includes(blackListedCatalyst))
)
: catalysts

Expand Down
99 changes: 98 additions & 1 deletion test/unit/about-main-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { MainRealmProviderComponent } from '../../src/adapters/main-realm-provid
import { aboutMainHandler } from '../../src/controllers/handlers/about-main-handler'

describe('about main handler unit test', () => {
let mockedConfig = {
getString: jest.fn().mockResolvedValue('')
}

function executeHandler(
catalystsProvider: CatalystsProvider,
mainRealmProvider: MainRealmProviderComponent,
Expand All @@ -12,7 +16,7 @@ describe('about main handler unit test', () => {
if (catalyst) {
url = new URL(`http://localhost/main/about?catalyst=${catalyst}`)
}
return aboutMainHandler({ components: { catalystsProvider, mainRealmProvider }, url })
return aboutMainHandler({ components: { catalystsProvider, mainRealmProvider, config: mockedConfig as any }, url })
}

it('should return main realm about', async () => {
Expand Down Expand Up @@ -163,4 +167,97 @@ describe('about main handler unit test', () => {
const { body } = await executeHandler(catalysts, mainRealmProvider, 'https://my-catalyst.decentraland.org')
expect(body.content.publicUrl).not.toEqual('https://my-catalyst.decentraland.org')
})

it('should filter out blacklisted catalysts', async () => {
const BLACKLISTED_CATALYSTS = 'https://peer-ec1.decentraland.org'
mockedConfig.getString.mockResolvedValue(BLACKLISTED_CATALYSTS)

const catalysts = {
getHealhtyCatalysts: jest.fn().mockResolvedValue([
{
healthy: true,
acceptingUsers: true,
about: {
content: {
publicurl: 'https://peer-ec2.decentraland.org/content'
},
configurations: {
realmName: 'ec2'
}
},
url: 'https://peer-ec2.decentraland.org'
},
{
healthy: true,
acceptingUsers: true,
about: {
content: {
publicurl: 'https://peer-ec1.decentraland.org/content'
},
configurations: {
realmName: 'ec1'
}
},
url: 'https://peer-ec1.decentraland.org'
}
])
}

const mainRealmProvider = {
getStatus: jest.fn().mockResolvedValue({
healthy: true,
userCount: 100,
adapter: 'adapter',
realmName: 'main'
})
}

const { body } = await executeHandler(catalysts, mainRealmProvider, 'https://peer-ec1.decentraland.org')

expect(body.configurations.realmName).not.toEqual('https://peer-ec1.decentraland.org')
expect(body.content.publicUrl).not.toEqual('https://peer-ec1.decentraland.org/content')
})

it('should not filter out blacklisted catalysts if blacklists is empty', async () => {
const BLACKLISTED_CATALYSTS = ''
mockedConfig.getString.mockResolvedValue(BLACKLISTED_CATALYSTS)

const catalysts = {
getHealhtyCatalysts: jest.fn().mockResolvedValue([
{
healthy: true,
acceptingUsers: true,
about: {
configurations: {
realmName: 'ec2'
}
},
url: 'https://peer-ec2.decentraland.org'
},
{
healthy: true,
acceptingUsers: true,
about: {
configurations: {
realmName: 'ec1'
}
},
url: 'https://peer-ec1.decentraland.org'
}
])
}

const mainRealmProvider = {
getStatus: jest.fn().mockResolvedValue({
healthy: true,
userCount: 100,
adapter: 'adapter',
realmName: 'main'
})
}

const { body } = await executeHandler(catalysts, mainRealmProvider, 'https://peer-ec1.decentraland.org')

expect(body.configurations.realmName).toEqual('main')
})
})

0 comments on commit 7e52e31

Please sign in to comment.