Skip to content

Commit

Permalink
Tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
HughParry committed Jul 25, 2024
1 parent cd2ea36 commit baa3d03
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 72 deletions.
2 changes: 1 addition & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:cjs": "npx vite --config vite.cjs.config.ts build",
"build:debug": "tsc --build --verbose",
"build:config": "tsc --project",
"test": "echo \"No test specified\"",
"test": "npx vitest run --config ./vite.test.config.ts",
"mnemonic": "tsx ./scripts/generateMnemonic.ts",
"coverage": "npx c8 npm run test",
"eslint": "npx eslint . --cache --cache-location ../../node_modules/.cache/eslint/.eslintcache --no-error-on-unmatched-pattern --ignore-path ../../.eslintignore --quiet",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ vi.mock('@prosopo/env', () => ({
})),
}))

vi.mock('../../tasks/tasks.js', () => ({
vi.mock('../../../tasks/tasks.js', () => ({
Tasks: vi.fn().mockImplementation(() => ({
datasetManager: {
storeCommitmentsExternal: vi.fn().mockResolvedValue(undefined),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ vi.mock('@polkadot/util', () => ({
vi.mock('../../../../util.js', () => ({
shuffleArray: vi.fn(),
}))
vi.mock('./imgCaptchaTasksUtils', () => ({
vi.mock('../../../../tasks/imgCaptcha/imgCaptchaTasksUtils.js', () => ({
buildTreeAndGetCommitmentId: vi.fn(),
}))

Expand Down Expand Up @@ -148,75 +148,6 @@ describe('ImgCaptchaManager', () => {
})
})

it('should process dapp user solution successfully', async () => {
const userAccount = 'userAccount'
const dappAccount = 'dappAccount'
const requestHash = 'requestHash'
const captchas = [
{ captchaId: 'captcha1', solution: 'solution1', salt: 'salt1' },
] as unknown as CaptchaSolution[]
const signature = 'signature'
const timestamp = 'timestamp'
const signedTimestamp = 'signedTimestamp'

;(signatureVerify as any).mockReturnValueOnce({ isValid: true })
;(signatureVerify as any).mockReturnValueOnce({ isValid: true })
;(db.getCaptchaById as any).mockResolvedValue([])
;(parseAndSortCaptchaSolutions as any).mockReturnValue(captchas)
;(buildTreeAndGetCommitmentId as any).mockReturnValue({
tree: { proof: vi.fn().mockReturnValue([]) },
commitmentId: 'commitmentId',
})
;(db.getDappUserPending as any).mockResolvedValue({ deadlineTimestamp: Date.now() + 10000, salt: 'salt' })
;(computePendingRequestHash as any).mockReturnValue('requestHash')
;(db.storeDappUserSolution as any).mockResolvedValue({})
;(compareCaptchaSolutions as any).mockReturnValue(true)

const result = await imgCaptchaManager.dappUserSolution(
userAccount,
dappAccount,
requestHash,
captchas,
signature,
timestamp,
signedTimestamp
)

expect(result.verified).toBe(true)
expect(db.storeDappUserSolution).toHaveBeenCalled()
expect(db.approveDappUserCommitment).toHaveBeenCalledWith('commitmentId')
})

it('should throw an error if signature is invalid', async () => {
const userAccount = 'userAccount'
const dappAccount = 'dappAccount'
const requestHash = 'requestHash'
const captchas = [
{ captchaId: 'captcha1', solution: 'solution1', salt: 'salt1', datasetId: 'datasetId' },
] as unknown as CaptchaSolution[]
const signature = 'signature'
const timestamp = 'timestamp'
const signedTimestamp = 'signedTimestamp'

;(signatureVerify as any).mockReturnValue({ isValid: false })

await expect(
imgCaptchaManager.dappUserSolution(
userAccount,
dappAccount,
requestHash,
captchas,
signature,
timestamp,
signedTimestamp
)
).rejects.toThrow(
new ProsopoEnvError('GENERAL.INVALID_SIGNATURE', {
context: { failedFuncName: 'dappUserSolution', userAccount },
})
)
})

it('should validate received captchas against stored captchas', async () => {
const captchas = [
{ captchaId: 'captcha1', solution: 'solution1', salt: 'salt1' },
Expand Down

0 comments on commit baa3d03

Please sign in to comment.