From 909df688c499c4d3dd04113ff71513b0785adaad Mon Sep 17 00:00:00 2001 From: Michal Zielenkiewicz Date: Mon, 15 Apr 2024 08:53:32 +0200 Subject: [PATCH] Add Playwright tests to eth key validation --- playwright/tests/paraTimes.spec.ts | 38 ++++++++++++++++++++++++++- src/utils/__fixtures__/test-inputs.ts | 11 +++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/playwright/tests/paraTimes.spec.ts b/playwright/tests/paraTimes.spec.ts index aef21603d3..8a08290ff7 100644 --- a/playwright/tests/paraTimes.spec.ts +++ b/playwright/tests/paraTimes.spec.ts @@ -1,5 +1,5 @@ import { test, expect } from '@playwright/test' -import { privateKey, privateKeyAddress } from '../../src/utils/__fixtures__/test-inputs' +import { privateKey, privateKeyAddress, ethAccount } from '../../src/utils/__fixtures__/test-inputs' import { fillPrivateKeyWithoutPassword } from '../utils/fillPrivateKey' import { warnSlowApi } from '../utils/warnSlowApi' import { mockApi } from '../utils/mockApi' @@ -33,4 +33,40 @@ test.describe('ParaTimes', () => { await page.getByRole('button', { name: /Next/i }).click() await expect(page.getByPlaceholder('0x...')).toHaveValue('') }) + + test('should validate eth private key', async ({ page }) => { + const validKey = ethAccount.privateKey + const validKeyWithPrefix = `0x${validKey}` + const invalidKey = validKey.replace('c', 'g') + const invalidKeyWithPrefix = `0x${invalidKey}` + + async function testPrivateKeyValidation(key, expectedOutcome) { + await page.getByPlaceholder('Enter Ethereum-compatible private key').fill(key) + await page.getByRole('button', { name: 'Next' }).click() + await expect(page.getByText(expectedOutcome)).toBeVisible() + } + + await page.goto('/open-wallet/private-key') + await fillPrivateKeyWithoutPassword(page, { + privateKey: privateKey, + privateKeyAddress: privateKeyAddress, + persistenceCheckboxChecked: false, + persistenceCheckboxDisabled: false, + }) + await page.getByTestId('nav-paratime').click() + await page.getByRole('button', { name: /Withdraw/i }).click() + await page.getByRole('button', { name: 'Select a ParaTime' }).click() + await expect(page.getByRole('listbox')).toBeVisible() + await page.getByRole('listbox').locator('button', { hasText: 'Sapphire' }).click() + await page.getByRole('button', { name: 'Next' }).click() + await page.getByPlaceholder(privateKeyAddress).fill(privateKeyAddress) + // valid eth private keys + await testPrivateKeyValidation(validKey, /enter the amount/) + await page.getByRole('button', { name: 'Back' }).click() + await testPrivateKeyValidation(validKeyWithPrefix, /enter the amount/) + await page.getByRole('button', { name: 'Back' }).click() + // invalid eth private keys + await testPrivateKeyValidation(invalidKey, /private key is invalid/) + await testPrivateKeyValidation(invalidKeyWithPrefix, /private key is invalid/) + }) }) diff --git a/src/utils/__fixtures__/test-inputs.ts b/src/utils/__fixtures__/test-inputs.ts index 7e6a49f192..b7286b4010 100644 --- a/src/utils/__fixtures__/test-inputs.ts +++ b/src/utils/__fixtures__/test-inputs.ts @@ -213,6 +213,11 @@ export const walletExtensionV0PersistedState = { }, } satisfies WalletExtensionV0State +export const ethAccount = { + address: '0xbA1b346233E5bB5b44f5B4aC6bF224069f427b18', + privateKey: '6593a788d944bb3e25357df140fac5b0e6273f1500a3b37d6513bf9e9807afe2', +} + export const walletExtensionV0UnlockedState = { account: { address: 'oasis1qq30ejf9puuc6qnrazmy9dmn7f3gessveum5wnr6', @@ -235,9 +240,9 @@ export const walletExtensionV0UnlockedState = { }, }, evmAccounts: { - '0xbA1b346233E5bB5b44f5B4aC6bF224069f427b18': { - ethAddress: '0xbA1b346233E5bB5b44f5B4aC6bF224069f427b18', - ethPrivateKey: '6593a788d944bb3e25357df140fac5b0e6273f1500a3b37d6513bf9e9807afe2', + [ethAccount.address]: { + ethAddress: ethAccount.address, + ethPrivateKey: ethAccount.privateKey, }, }, createWallet: { checkbox: false, mnemonic: [] },