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

feat: add validation not repeated slots in emotes #109

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions src/validations/access-checker/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseUrn } from '@dcl/urn-resolver'
import { OK, Validation, validationFailed } from '../../types'
import { isOldEmote } from '../profile'
import { ADR_74_TIMESTAMP, ADR_75_TIMESTAMP } from '../timestamps'
import { conditionalValidation, validationAfterADR75, validationGroup } from '../validations'
import { conditionalValidation, validationAfterADR74, validationAfterADR75, validationGroup } from '../validations'

export const pointerIsValid: Validation = {
validate: async ({ externalCalls }, deployment) => {
Expand Down Expand Up @@ -126,4 +126,19 @@ export const ownsItems: Validation = conditionalValidation(
}
)

export const profiles: Validation = validationGroup(pointerIsValid, ownsNames, ownsItems)
export const profileSlotsAreNotRepeated: Validation = validationAfterADR74({
validate: async (components, deployment) => {
const allAvatars: Avatar[] = deployment.entity.metadata?.avatars ?? []
const allEmotes: { slot: number }[] = allAvatars.flatMap((avatar) => avatar.avatar.emotes ?? [])
const usedSlots = new Set()
for (const { slot } of allEmotes) {
if (usedSlots.has(slot)) {
return validationFailed('Emote slots should not be repeated.')
}
usedSlots.add(slot)
}
return OK
}
})

export const profiles: Validation = validationGroup(pointerIsValid, ownsNames, ownsItems, profileSlotsAreNotRepeated)
19 changes: 19 additions & 0 deletions test/unit/validations/profiles.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EntityType } from '@dcl/schemas'
import sharp from 'sharp'
import { ValidationResponse } from '../../../src'
import { profileSlotsAreNotRepeated } from '../../../src/validations/access-checker/profiles'
import {
emoteUrns,
faceThumbnail,
Expand Down Expand Up @@ -377,5 +378,23 @@ describe('Profiles', () => {
'The slot 10 of the emote urn:decentraland:matic:collections-v2:0xa7f6eba61566fd4b3012569ef30f0200ec138aa5:0 must be a number between 0 and 9 (inclusive).'
)
})

it('After ADR 74, when emote slots are repaated, should return the correct error', async () => {
const entity = buildEntity({
type: EntityType.PROFILE,
metadata: validProfileMetadataWithEmotes([
{ slot: 0, urn: 'urn:decentraland:matic:collections-v2:0xa7f6eba61566fd4b3012569ef30f0200ec138aa5:0' },
{ slot: 1, urn: 'urn:decentraland:matic:collections-v2:0xa7f6eba61566fd4b3012569ef30f0200ec138aa5:0' },
{ slot: 1, urn: 'urn:decentraland:matic:collections-v2:0xa7f6eba61566fd4b3012569ef30f0200ec138aa5:0' }
]),
timestamp: ADR_74_TIMESTAMP + 1
})
const deployment = buildDeployment({ entity })

const result = await profileSlotsAreNotRepeated.validate(components, deployment)

expect(result.ok).toBeFalsy()
expect(result.errors).toContain('Emote slots should not be repeated.')
})
})
})