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

783 run the cypress tests against the procaptcha bundle #785

Merged
merged 64 commits into from
Nov 21, 2023
Merged
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
42ecca4
Move cypress tests into shared package
forgetso Nov 10, 2023
71f33ee
sort out deps and references
forgetso Nov 11, 2023
973d76d
Run cypress tests over both demos
forgetso Nov 14, 2023
59a8134
package-lock.json and remove unused commands
forgetso Nov 14, 2023
2aab067
remove build command from demos and just serve them using vite instead
forgetso Nov 14, 2023
fc3902c
Import contracts
forgetso Nov 14, 2023
c938ee7
Fix vite config path
forgetso Nov 14, 2023
a1ef053
Import contracts and fix bundle command in workflow
forgetso Nov 14, 2023
924d60e
fix bundle command
forgetso Nov 14, 2023
d951165
Change cypress selector to work for both demos
forgetso Nov 14, 2023
ac7b0a3
Import contracts
forgetso Nov 14, 2023
73cef23
Try making the array into a selection
forgetso Nov 14, 2023
26eb579
make sure checkbox is checked
forgetso Nov 14, 2023
b49b9e9
Add a replacer for the demo bundle HTML files
forgetso Nov 14, 2023
cdd4237
add some debug
forgetso Nov 14, 2023
7b4c454
- Use vite for cypress tests
forgetso Nov 15, 2023
207578e
Import contracts and lint
forgetso Nov 15, 2023
b4bb8b8
fix up tests
forgetso Nov 15, 2023
8d3d162
deps
forgetso Nov 15, 2023
12da39c
reduce logging
forgetso Nov 15, 2023
6eb27f4
Fix cypress tests for react client-example
forgetso Nov 16, 2023
50183f6
Remove the need for dapp to be a contract in setup scripts
forgetso Nov 16, 2023
03bf38f
Merge branch 'main' into 783-run-the-cypress-tests-against-the-procap…
forgetso Nov 16, 2023
d74f867
Import contracts
forgetso Nov 16, 2023
1900d62
package-lock.json
forgetso Nov 16, 2023
986acd6
Fix dapp setup script
forgetso Nov 16, 2023
2669b37
Get rid of polkadotJS warnings
forgetso Nov 16, 2023
058fc3a
Try changing the order in beforeAll
forgetso Nov 16, 2023
5de035b
Try changing order in tests
forgetso Nov 16, 2023
2a47d5b
Fix client-example vite dev server
forgetso Nov 16, 2023
ff987a8
Turn watch off for cypress and client-example. Reduce log output
forgetso Nov 16, 2023
75d43c9
Import contracts
forgetso Nov 16, 2023
29d33ea
Use tsc build of server instead of bundle
forgetso Nov 16, 2023
8fc87db
remove timeout
forgetso Nov 16, 2023
4bda10d
Try to get client-example running alongside bundle
forgetso Nov 17, 2023
f2e2fc1
try production mode
forgetso Nov 17, 2023
facb899
Use concurrently to serve multiple vite instances at once
forgetso Nov 17, 2023
6735a08
Reduce logging
forgetso Nov 17, 2023
57b8399
fix some ts errors in client-example vite config
forgetso Nov 17, 2023
8dca052
linting and fix concurrently command
forgetso Nov 17, 2023
546e160
Use cypress run instead of open
forgetso Nov 17, 2023
b35b056
import contracts
forgetso Nov 17, 2023
51d58cc
remove noisy logs
forgetso Nov 17, 2023
d0792a8
trigger workflow
forgetso Nov 17, 2023
08dda76
Try adding a sleep before the tests
forgetso Nov 17, 2023
d9f4bc4
Set cypress to listen on all addresses instead of localhost
forgetso Nov 17, 2023
c0e71e9
Increase timeout
forgetso Nov 17, 2023
c7943f7
debug cypress
forgetso Nov 17, 2023
60f0c46
Check that the checkbox is visible on-page
forgetso Nov 17, 2023
6f59765
try hacky solution
forgetso Nov 18, 2023
b60e6ba
Try return
forgetso Nov 20, 2023
3180471
trim vite config
forgetso Nov 20, 2023
2eac100
simplify cypress chain
forgetso Nov 20, 2023
7b35cda
try xvfb-run
forgetso Nov 20, 2023
3a8e793
run headed
forgetso Nov 20, 2023
e0f17e2
Use the same dapp site key in all template env files
forgetso Nov 20, 2023
7f7612f
Update env.development in scripts
forgetso Nov 20, 2023
9d438e9
Use run instead of open
forgetso Nov 20, 2023
a268cb8
Use correct success flag
forgetso Nov 20, 2023
96e0dbf
Test using dev bundle of procaptcha
forgetso Nov 20, 2023
63a8900
Import contracts
forgetso Nov 20, 2023
bc0e029
Import contracts
forgetso Nov 20, 2023
9e730ec
Import contracts
forgetso Nov 20, 2023
0e807dd
lint
forgetso Nov 20, 2023
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
Prev Previous commit
Next Next commit
fix up tests
  • Loading branch information
forgetso committed Nov 15, 2023
commit b4bb8b8db6e24dc3b51a4d076e32c30dc3018510
4 changes: 2 additions & 2 deletions demos/client-bundle-example/src/index.html
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
<div
class="procaptcha"
data-theme="light"
data-sitekey="5FvUYVfyKR679ezT55X8vLxhGgyrpGEQef8vAcUdYKrkEVkg"
data-sitekey="5HFb4WEMuhxDxU8hWwtHdgC7SEFgTtpdX4YByib929ykKg3n"
></div>
<input type="submit" class="mui-btn mui-btn--raised"/>
</form>
@@ -37,7 +37,7 @@

// Render the CAPTCHA explicitly on a container with id "procaptcha-container"
window.procaptcha.render('procaptcha-container', {
siteKey: '5FvUYVfyKR679ezT55X8vLxhGgyrpGEQef8vAcUdYKrkEVkg',
siteKey: '5HFb4WEMuhxDxU8hWwtHdgC7SEFgTtpdX4YByib929ykKg3n',
theme: 'dark',
callback: 'onCaptchaVerified',
})
4 changes: 2 additions & 2 deletions demos/cypress-shared/cypress/e2e/captcha.cy.ts
Original file line number Diff line number Diff line change
@@ -14,11 +14,11 @@
/// <reference types="cypress" />
import '@cypress/xpath'
import { Captcha } from '@prosopo/types'
import { captchaData } from '@prosopo/datasets'
import { datasetWithSolutionHashes } from '@prosopo/datasets'

describe('Captchas', () => {
beforeEach(() => {
const solutions = captchaData.captchas.map((captcha) => ({
const solutions = datasetWithSolutionHashes.captchas.map((captcha) => ({
captchaContentId: captcha.captchaContentId,
solution: captcha.solution,
}))
14 changes: 12 additions & 2 deletions dev/config/src/vite/vite.test.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineConfig } from 'vitest/config'
import VitePluginCloseAndCopy from './vite-plugin-close-and-copy.js'
import VitePluginSourcemapExclude from './vite-plugin-sourcemap-exclude.js'

export default function () {
@@ -15,15 +16,24 @@ export default function () {
},
},
test: {
watch: false,
//reporters: ['basic', 'hanging-process'], https://github.com/vitest-dev/vitest/issues/4415
include: ['../../packages/*/src/**/*.test.ts', '../../contracts/*/src/**/*.test.ts'],
exclude: ['../../demos/**/*'], // '../!packages/**/*'],
singleThread: true,
watchExclude: ['**/node_modules/**', '**/dist/**'],
watchExclude: [
'**/node_modules/**',
'**/dist/**',
'**/demos/**',
'../../packages/*/dist/**',
'../../packages/datasets-fs/src/tests/data/**',
],
logHeapUsage: true,
coverage: {
enabled: true,
},
useAtomics: true,
},
plugins: [VitePluginSourcemapExclude({ excludeNodeModules: true })],
plugins: [VitePluginSourcemapExclude({ excludeNodeModules: true }), VitePluginCloseAndCopy()],
})
}
4 changes: 2 additions & 2 deletions dev/scripts/src/setup/provider.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ import { ProsopoEnvError } from '@prosopo/common'
import { ProviderEnvironment } from '@prosopo/types-env'
import { ReturnNumber } from '@prosopo/typechain-types'
import { Tasks } from '@prosopo/provider'
import { captchaData } from '@prosopo/datasets'
import { datasetWithSolutionHashes } from '@prosopo/datasets'
import { getSendAmount, getStakeAmount, sendFunds } from './funds.js'
import { stringToU8a } from '@polkadot/util'
import { wrapQuery } from '@prosopo/contract'
@@ -105,5 +105,5 @@ export async function setupProvider(env: ProviderEnvironment, provider: IProvide
await tasks.contract.tx.providerUpdate(...providerUpdateArgs)

logger.info(' - providerSetDataset')
await tasks.providerSetDataset(captchaData)
await tasks.providerSetDataset(datasetWithSolutionHashes)
}
5 changes: 4 additions & 1 deletion packages/common/src/error.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { TranslationKey, i18n as i18next, translationKeys } from './index.js'
import { ZodError } from 'zod'
import { at } from '@prosopo/util'

export type TOptions = Record<string, string>
@@ -75,7 +76,9 @@ export class ProsopoEnvError extends ProsopoBaseError {
}

console.error('\n********************* ERROR *********************\n')
if (this.cause?.message && this.cause.message.length > MAX_ERROR_LENGTH) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (typeof this.cause != ZodError && this.cause?.message && this.cause.message.length > MAX_ERROR_LENGTH) {
this.cause.message = `${at(this.cause.message, -MAX_ERROR_LENGTH)}...`
}
console.error(this.cause, this.stack, ...params)
1 change: 1 addition & 0 deletions packages/datasets/src/captcha/captcha.ts
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ export const NO_SOLUTION_VALUE = 'NO_SOLUTION'
export function parseCaptchaDataset(datasetJSON: JSON): DatasetRaw {
try {
const result = DatasetWithNumericSolutionSchema.parse(datasetJSON)

const result2: DatasetRaw = {
format: result.format,
captchas: result.captchas.map((captcha) => {
2 changes: 1 addition & 1 deletion packages/datasets/src/index.ts
Original file line number Diff line number Diff line change
@@ -12,4 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.
export * from './captcha/index.js'
export { captchaData } from './tests/mocks/data/captchas.js'
export { datasetWithSolutionHashes, datasetWithIndexSolutions } from './tests/mocks/data/captchas.js'
12 changes: 11 additions & 1 deletion packages/datasets/src/tests/mocks/data/captchas.ts
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
// limitations under the License.
import { DatasetWithIdsAndTree } from '@prosopo/types'

export const captchaData = {
export const datasetWithSolutionHashes = {
datasetId: '0x9f460e81ac9c71b486f796a21bb36e2263694756a6621134d110da217fd3ef25',
datasetContentId: '0x3a2dbbf5610f04f54581843db3adf7e0fadc02cdb8e42e30b028c850e0603165',
format: 'SelectAll',
@@ -1041,3 +1041,13 @@ export const captchaData = {
},
],
} as DatasetWithIdsAndTree

// Replace the hashes in the solution items above with their index in the items array
export const datasetWithIndexSolutions = {
...datasetWithSolutionHashes,
captchas: datasetWithSolutionHashes.captchas.map((captcha, index) => ({
...captcha,
//for solution in captcha.solution, find the index of captcha.item with item.hash == solution
solution: captcha.solution?.map((solution) => captcha.items.findIndex((item) => item.hash === solution)),
})),
}
12 changes: 6 additions & 6 deletions packages/provider/src/tests/batch/commitments.test.ts
Original file line number Diff line number Diff line change
@@ -124,15 +124,15 @@ describe('BATCH TESTS', function () {
.unwrap()
.unwrap()
const dappAccount = await getUser(env, AccountKey.dappsWithStake)
const randomCaptchasResult = await providerTasks.db.getRandomCaptcha(false, providerDetails.datasetId)
const randomCaptchasResult = await providerTasks.db.getRandomCaptcha(true, providerDetails.datasetId)

if (randomCaptchasResult) {
const solutions = await providerTasks.db.getSolutions(providerDetails.datasetId.toString())
const solutionIndex = solutions.findIndex(
(s) => s.captchaContentId === at(randomCaptchasResult, 0).captchaContentId
)
const solution = at(solutions, solutionIndex).solution
const unsolvedCaptcha = at(randomCaptchasResult, 0)
const solution = [
at(unsolvedCaptcha.items, 0).hash || '',
at(unsolvedCaptcha.items, 2).hash || '',
at(unsolvedCaptcha.items, 3).hash || '',
]
const captchaSolution: CaptchaSolution = { ...unsolvedCaptcha, solution, salt: randomAsHex() }
const commitmentIds: string[] = []

10 changes: 5 additions & 5 deletions packages/provider/src/tests/dataUtils/DatabasePopulator.ts
Original file line number Diff line number Diff line change
@@ -17,15 +17,16 @@ import { AnyNumber } from '@polkadot/types-codec/types'
import { BN } from '@polkadot/util/bn'
import { ContractDeployer, ProsopoContractError, getPairAsync, wrapQuery } from '@prosopo/contract'
import { DappPayee, Payee } from '@prosopo/captcha-contract/types-returns'
import { DatasetWithIdsAndTree } from '@prosopo/types'
import { EventRecord } from '@polkadot/types/interfaces'
import { IDatabaseAccounts } from './DatabaseAccounts.js'
import { ProsopoEnvError, TranslationKey } from '@prosopo/common'
import { ProviderEnvironment } from '@prosopo/env'
import { ReturnNumber } from '@prosopo/typechain-types'
import { Tasks } from '../../tasks/index.js'
import { sendFunds as _sendFunds, getSendAmount, getStakeAmount } from './funds.js'
import { captchaData } from '@prosopo/datasets'
import { createType } from '@polkadot/types/create'
import { datasetWithSolutionHashes } from '@prosopo/datasets'
import { get } from '@prosopo/util'
import { mnemonicGenerate } from '@polkadot/util-crypto/mnemonic'
import { randomAsHex } from '@polkadot/util-crypto/random'
@@ -277,13 +278,13 @@ class DatabasePopulator implements IDatabaseAccounts, IDatabasePopulatorMethods
}
}

private async addDataset(account: Account, datasetJSON: JSON) {
private async addDataset(account: Account, dataset: DatasetWithIdsAndTree) {
try {
await this.changeSigner(account)

const tasks = new Tasks(this.mockEnv)

await tasks.providerSetDatasetFromFile(datasetJSON)
await tasks.providerSetDataset(dataset)
} catch (e) {
throw this.createError(e as Error, this.addDataset.name)
}
@@ -295,8 +296,7 @@ class DatabasePopulator implements IDatabaseAccounts, IDatabasePopulatorMethods

const account = await this.registerProvider(fund, url, true)
await this.updateProvider(account, url)
const datasetJSON = JSON.parse(JSON.stringify(captchaData))
await this.addDataset(account, datasetJSON)
await this.addDataset(account, datasetWithSolutionHashes)

this._registeredProvidersWithStakeAndDataset.push(account)

31 changes: 14 additions & 17 deletions packages/provider/src/tests/tasks/tasks.test.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,12 @@
// You should have received a copy of the GNU General Public License
import { AccountKey } from '../dataUtils/DatabaseAccounts.js'
import { BN } from '@polkadot/util/bn'
import { CaptchaMerkleTree, computeCaptchaSolutionHash, computePendingRequestHash } from '@prosopo/datasets'
import {
CaptchaMerkleTree,
computeCaptchaSolutionHash,
computePendingRequestHash,
datasetWithSolutionHashes,
} from '@prosopo/datasets'
import { CaptchaSolution, DappUserSolutionResult } from '@prosopo/types'
import { CaptchaStatus, Commit, DappPayee, Payee } from '@prosopo/captcha-contract/types-returns'
import {
@@ -34,8 +39,8 @@ import { ReturnNumber } from '@prosopo/typechain-types'
import { ViteTestContext } from '@prosopo/env'
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
import { at, get } from '@prosopo/util'
import { captchaData } from '@prosopo/datasets'
import { createType } from '@polkadot/types/create'
import { datasetWithIndexSolutions } from '@prosopo/datasets'
import { getSendAmount, getStakeAmount, sendFunds } from '../dataUtils/funds.js'
import { getTestConfig } from '@prosopo/config'
import { getUser } from '../getUser.js'
@@ -200,7 +205,7 @@ describe.sequential('CONTRACT TASKS', async function (): Promise<void> {

const tasks = await getSignedTasks(env, providerAccount)

await tasks.providerSetDatasetFromFile(JSON.parse(JSON.stringify(captchaData)))
await tasks.providerSetDatasetFromFile(JSON.parse(JSON.stringify(datasetWithIndexSolutions)))
}, 8000)

test('Provider add dataset with too few captchas will fail', async ({ env }): Promise<void> => {
@@ -209,7 +214,7 @@ describe.sequential('CONTRACT TASKS', async function (): Promise<void> {
const tasks = await getSignedTasks(env, providerAccount)

// copy captchaData and remove all but one captcha
const dataset = { ...captchaData }
const dataset = { ...datasetWithIndexSolutions }
dataset.captchas = dataset.captchas.slice(0, 1)
try {
await tasks.providerSetDatasetFromFile(JSON.parse(JSON.stringify(dataset)))
@@ -223,7 +228,7 @@ describe.sequential('CONTRACT TASKS', async function (): Promise<void> {

const tasks = await getSignedTasks(env, providerAccount)

const dataset = { ...captchaData }
const dataset = { ...datasetWithIndexSolutions }
// remove solution field from each captcha
dataset.captchas = dataset.captchas.map((captcha) => {
const { solution, ...rest } = captcha
@@ -242,7 +247,7 @@ describe.sequential('CONTRACT TASKS', async function (): Promise<void> {
const tasks = await getSignedTasks(env, providerAccount)

try {
await tasks.providerSetDatasetFromFile(JSON.parse(JSON.stringify(captchaData)))
await tasks.providerSetDatasetFromFile(JSON.parse(JSON.stringify(datasetWithIndexSolutions)))
} catch (e) {
expect(e).to.match(/ProviderInactive/)
}
@@ -785,23 +790,15 @@ describe.sequential('CONTRACT TASKS', async function (): Promise<void> {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const dappUserAccount = await getUser(env, AccountKey.dappUsers)
const dappAccount = await getUser(env, AccountKey.dappsWithStake)
// there must be at least one provider in the contract and db
await getUser(env, AccountKey.providersWithStakeAndDataset)

const dappUserTasks = await getSignedTasks(env, dappUserAccount)
const solvedCaptchaCount = env.config.captchas.solved.count
const unsolvedCaptchaCount = env.config.captchas.unsolved.count
const { provider } = (
await dappUserTasks.contract.query.getRandomActiveProvider(
accountAddress(dappUserAccount),
accountContract(dappAccount)
)
).value
.unwrap()
.unwrap()

const { captchas, requestHash } = await dappUserTasks.getRandomCaptchasAndRequestHash(
provider.datasetId.toString(),
datasetWithSolutionHashes.datasetId.toString(), // This is the dataset that all test providers have loaded
hexHash(accountAddress(dappUserAccount))
)

@@ -869,7 +866,7 @@ describe.sequential('CONTRACT TASKS', async function (): Promise<void> {
).result
expect(resultproviderUpdate2?.isError).to.be.false

await tasks.providerSetDatasetFromFile(JSON.parse(JSON.stringify(captchaData)))
await tasks.providerSetDatasetFromFile(JSON.parse(JSON.stringify(datasetWithIndexSolutions)))

const dappAccount = await getUser(env, AccountKey.dappsWithStake)
const dappUser = await getUser(env, AccountKey.dappUsers)
Loading