Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: validator configurable via IConfigComponent #113

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/sharp": "^0.30.2",
"@typescript-eslint/eslint-plugin": "5.21.0",
"@typescript-eslint/parser": "5.21.0",
"@well-known-components/env-config-provider": "^1.1.1",
"eslint": "8.14.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export * from './validations'
* @public
*/
export const createValidator = (
components: Pick<ContentValidatorComponents, 'externalCalls' | 'logs' | 'theGraphClient' | 'subGraphs'>
components: Pick<ContentValidatorComponents, 'config' | 'externalCalls' | 'logs' | 'theGraphClient' | 'subGraphs'>
): Validator => {
const logs = components.logs.getLogger('ContentValidator')

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthChain, Entity, EthAddress } from '@dcl/schemas'
import { ILoggerComponent } from '@well-known-components/interfaces'
import { IConfigComponent, ILoggerComponent } from '@well-known-components/interfaces'
import { ISubgraphComponent, Variables } from '@well-known-components/thegraph-component'
import { PermissionResult } from './the-graph-client/the-graph-client'

Expand Down Expand Up @@ -163,6 +163,7 @@ export type BlockInformation = {
* @public
*/
export type ContentValidatorComponents = {
config: IConfigComponent
logs: ILoggerComponent
theGraphClient: TheGraphClient
externalCalls: ExternalCalls
Expand Down
9 changes: 3 additions & 6 deletions src/validations/access-checker/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import { scenes } from './scenes'
import { stores } from './stores'
import { wearables } from './wearables'

/**
* Whether to ignore checks for access permission using blockchain data. Useful during content development.
*/
const IGNORE_BLOCKCHAIN_ACCESS_CHECKS = process.env.IGNORE_BLOCKCHAIN_ACCESS_CHECKS === 'true'

const accessCheckers: Record<EntityType, Validation> = {
[EntityType.PROFILE]: profiles,
[EntityType.SCENE]: scenes,
Expand All @@ -26,7 +21,9 @@ const accessCheckers: Record<EntityType, Validation> = {
*/
export const access: Validation = {
validate: async (components, deployment: DeploymentToValidate) => {
if (IGNORE_BLOCKCHAIN_ACCESS_CHECKS) return OK
if ((await components.config.getString('IGNORE_BLOCKCHAIN_ACCESS_CHECKS')) === 'true') {
return OK
}

const { externalCalls } = components
const deployedBeforeDCLLaunch = deployment.entity.timestamp <= LEGACY_CONTENT_MIGRATION_TIMESTAMP
Expand Down
9 changes: 8 additions & 1 deletion test/setup/mock.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ILoggerComponent } from '@well-known-components/interfaces'
import { IConfigComponent, ILoggerComponent } from '@well-known-components/interfaces'
import { ISubgraphComponent } from '@well-known-components/thegraph-component'
import { createTheGraphClient } from '../../src'
import { ContentValidatorComponents, ExternalCalls, QueryGraph, SubGraphs } from '../../src/types'
import { ItemCollection } from '../../src/validations/access-checker/items/collection-asset'
import { createConfigComponent } from '@well-known-components/env-config-provider'

export const buildLogger = (): ILoggerComponent => ({
getLogger: () => ({
Expand All @@ -15,6 +16,8 @@ export const buildLogger = (): ILoggerComponent => ({
})

export const buildComponents = (components?: Partial<ContentValidatorComponents>): ContentValidatorComponents => {
const config = components?.config ?? buildConfig({})

const externalCalls = components?.externalCalls ?? buildExternalCalls()

const logs = components?.logs ?? buildLogger()
Expand All @@ -23,13 +26,17 @@ export const buildComponents = (components?: Partial<ContentValidatorComponents>
const theGraphClient = components?.theGraphClient ?? createTheGraphClient({ logs, subGraphs, ...components })

return {
config,
logs,
theGraphClient,
externalCalls,
subGraphs
}
}

export const buildConfig = (optionMap: Partial<Record<string, string>>): IConfigComponent =>
createConfigComponent({ ...optionMap })

export const buildExternalCalls = (externalCalls?: Partial<ExternalCalls>): ExternalCalls => ({
isContentStoredAlready: () => Promise.resolve(new Map()),
fetchContentFileSize: () => Promise.resolve(undefined),
Expand Down
40 changes: 39 additions & 1 deletion test/unit/access/scenes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { scenes } from '../../../src/validations/access-checker/scenes'
import { access } from '../../../src/validations/access-checker/access'
import { buildSceneDeployment } from '../../setup/deployments'
import { buildComponents, buildExternalCalls } from '../../setup/mock'
import { buildComponents, buildConfig, buildExternalCalls } from '../../setup/mock'
import { throws } from 'assert'

describe('Access: scenes', () => {
it('When a non-decentraland address tries to deploy a default scene, then an error is returned', async () => {
Expand Down Expand Up @@ -44,4 +46,40 @@ describe('Access: scenes', () => {
'Scene pointers should only contain two integers separated by a comma, for example (10,10) or (120,-45). Invalid pointer: invalid-pointer'
)
})

describe('blockchain access check validations', () => {
it('When IGNORE_BLOCKCHAIN_ACCESS_CHECKS=false, then ownerAddress function is invoked', async () => {
const pointers = ['Default10']
const deployment = buildSceneDeployment(pointers)
const config = buildConfig({
IGNORE_BLOCKCHAIN_ACCESS_CHECKS: 'false'
})
const ownerAddress = jest.fn()
ownerAddress.mockResolvedValue('0xAddress')
const externalCalls = buildExternalCalls({
isAddressOwnedByDecentraland: () => true,
ownerAddress
})

const response = await access.validate(buildComponents({ config, externalCalls }), deployment)
expect(response.ok).toBeFalsy()
expect(ownerAddress).toHaveBeenCalled()
})

it('When IGNORE_BLOCKCHAIN_ACCESS_CHECKS=false, then ownerAddress function is not invoked', async () => {
const pointers = ['Default10']
const deployment = buildSceneDeployment(pointers)
const config = buildConfig({
IGNORE_BLOCKCHAIN_ACCESS_CHECKS: 'true'
})
const ownerAddress = jest.fn()
const externalCalls = buildExternalCalls({
ownerAddress
})

const response = await access.validate(buildComponents({ config, externalCalls }), deployment)
expect(response.ok).toBeTruthy()
expect(ownerAddress).not.toHaveBeenCalled()
})
})
})
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,13 @@
"@typescript-eslint/types" "5.29.0"
eslint-visitor-keys "^3.3.0"

"@well-known-components/env-config-provider@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@well-known-components/env-config-provider/-/env-config-provider-1.1.1.tgz#d04215847fcab83a1ab05e375df8e2141dae23cd"
integrity sha512-toj2HcxYZRf9UuBqEC9mrej9yyQlGU/15oGtShrDtNC9/OVLBNY3q8cNgLLKbPOMLaRJkjdQvIvpjb+Q+E+PFQ==
dependencies:
dotenv "^8.2.0"

"@well-known-components/http-server@^1.1.0":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@well-known-components/http-server/-/http-server-1.1.2.tgz#39d038da07efd3c1a3bc9a7edab5418c8162910f"
Expand Down Expand Up @@ -2535,6 +2542,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dotenv@^8.2.0:
version "8.6.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b"
integrity sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==

ee-first@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down