From 62b1158c69a217f0a38708a40ca6740455aecb79 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 11 Jul 2018 14:50:01 +0200 Subject: [PATCH] fix: Fixes #130 latest rust reports chains differently (#133) * fix: Detect chain specs that enable test environment and dev accounts Issue #130 * test: refactor moving chain checker into meaningful function name * fix: Detect check specs requiring backward compatibility encoding for Issue #130 * remove comment * remove comment and restore source maps * fix: Fix the license information in test files * refactor: Move cases into array with meaningful array. Cater for numbers * refactor: Reduce how condensed the code is * refactor: Use Polkadot isUndefined helper * refactor: Move RegEx instantiation outside function so not called every time * refactor: Move new utility functions and associated tests into ui-react-rx/src/util * fix: Update to cater for renaming of poc-2 to staging See commit updates since `poc-2` has been renamed to `staging` https://github.com/paritytech/polkadot/commit/3426552c763a157fa872ab11c1029ed4834f7a42 --- packages/apps/src/NodeInfo.tsx | 3 ++- packages/ui-react-rx/src/Api/index.tsx | 6 ++--- .../ui-react-rx/src/util/isTestChain.spec.js | 24 ++++++++++++++++++ packages/ui-react-rx/src/util/isTestChain.ts | 17 +++++++++++++ .../src/util/shouldUseLatestChain.spec.js | 25 +++++++++++++++++++ .../src/util/shouldUseLatestChain.ts | 17 +++++++++++++ 6 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 packages/ui-react-rx/src/util/isTestChain.spec.js create mode 100644 packages/ui-react-rx/src/util/isTestChain.ts create mode 100644 packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js create mode 100644 packages/ui-react-rx/src/util/shouldUseLatestChain.ts diff --git a/packages/apps/src/NodeInfo.tsx b/packages/apps/src/NodeInfo.tsx index eb2b32bd43..69503a430f 100644 --- a/packages/apps/src/NodeInfo.tsx +++ b/packages/apps/src/NodeInfo.tsx @@ -6,6 +6,7 @@ import { I18nProps } from '@polkadot/ui-app/types'; import React from 'react'; +import isTestChain from '@polkadot/ui-react-rx/util/isTestChain'; import classes from '@polkadot/ui-app/util/classes'; import keyring from '@polkadot/ui-keyring/index'; import Chain from '@polkadot/ui-react-rx/Chain'; @@ -17,7 +18,7 @@ import translate from './translate'; type Props = I18nProps & {}; function updateTestInfo (chain?: string) { - keyring.setTestMode(chain === 'dev'); + keyring.setTestMode(isTestChain(chain)); } function NodeInfo ({ className, style, t }: Props) { diff --git a/packages/ui-react-rx/src/Api/index.tsx b/packages/ui-react-rx/src/Api/index.tsx index 3d64ce7ec4..89ac439d10 100644 --- a/packages/ui-react-rx/src/Api/index.tsx +++ b/packages/ui-react-rx/src/Api/index.tsx @@ -8,6 +8,8 @@ import { EncodingVersions } from '@polkadot/params/types'; import { ApiProps } from '../types'; import React from 'react'; + +import shouldUseLatestChain from '@polkadot/ui-react-rx/util/shouldUseLatestChain'; import createWsProvider from '@polkadot/api-provider/ws'; import createApi from '@polkadot/api-rx'; import defaults from '@polkadot/api-rx/defaults'; @@ -26,9 +28,7 @@ type State = ApiProps & { }; function apiSupport (chain?: string): EncodingVersions { - return chain === undefined || chain === 'poc-1' - ? 'poc-1' - : 'latest'; + return shouldUseLatestChain(chain) ? 'latest' : 'poc-1'; } export default class Api extends React.PureComponent { diff --git a/packages/ui-react-rx/src/util/isTestChain.spec.js b/packages/ui-react-rx/src/util/isTestChain.spec.js new file mode 100644 index 0000000000..4f19e43154 --- /dev/null +++ b/packages/ui-react-rx/src/util/isTestChain.spec.js @@ -0,0 +1,24 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isTestChain from './isTestChain'; + +describe('check chain spec to configure keyring test mode and dev accounts availability', () => { + it('enables test environment when chain specification matches text of dev or loc(al)', () => { + const validTestModeChainSpecsWithDev = ['Development disorder', 'my development', 'a devotion to']; + const validTestModeChainSpecsWithLoc = ['Local beer', 'one local beer', 'one good locust']; + + for (let s of validTestModeChainSpecsWithDev.concat(validTestModeChainSpecsWithLoc)) { + expect(isTestChain(s)).toEqual(true); + } + }); + + it('disables keyring test mode when chain specification is not a test mode or undefined or number type', () => { + const invalidTestModeChainSpecs = ['PoC-1 Testnet', 'Staging Testnet', 'future PoC-2 Testnet', 'a pocadot?', undefined, 0]; + + for (let s of invalidTestModeChainSpecs) { + expect(isTestChain(s)).toEqual(false); + } + }); +}); diff --git a/packages/ui-react-rx/src/util/isTestChain.ts b/packages/ui-react-rx/src/util/isTestChain.ts new file mode 100644 index 0000000000..2dd0f879ce --- /dev/null +++ b/packages/ui-react-rx/src/util/isTestChain.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isUndefined from '@polkadot/util/is/undefined'; + +const re = new RegExp('(dev|loc)', 'i'); + +export default function isTestChain (chain?: string): boolean { + if (isUndefined(chain)) { + return false; + } + + const match = re.test(chain.toString().toLowerCase()); + + return match ? true : false; +} diff --git a/packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js b/packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js new file mode 100644 index 0000000000..26f50103ff --- /dev/null +++ b/packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js @@ -0,0 +1,25 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import shouldUseLatestChain from './shouldUseLatestChain'; + +describe('check chain spec to configure encoding', () => { + it('enables PoC-1 encoding when chain specification is PoC-1 or undefined', () => { + let validChainSpecWithPoC1 = ['try poc-1 now', 'testing PoC-1 Testnet']; + const validChainSpecWithUndefinedOrNumber = [undefined]; + + for (let s of validChainSpecWithPoC1.concat(validChainSpecWithUndefinedOrNumber)) { + expect(shouldUseLatestChain(s)).toEqual(false); + } + }); + + it('enables Latest encoding when chain specification is NOT PoC-1 or undefined', () => { + const invalidChainSpecWithoutPoC1OrUndefined = ['poc-2', 'PoC-2 Testnet', 'staging', + 'Staging Testnet', 'Local', 'Development', ' development', 'dev', 'Local', ' local ', 'loc', 0]; + + for (let s of invalidChainSpecWithoutPoC1OrUndefined) { + expect(shouldUseLatestChain(s)).toEqual(true); + } + }); +}); diff --git a/packages/ui-react-rx/src/util/shouldUseLatestChain.ts b/packages/ui-react-rx/src/util/shouldUseLatestChain.ts new file mode 100644 index 0000000000..376fa5fd01 --- /dev/null +++ b/packages/ui-react-rx/src/util/shouldUseLatestChain.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isUndefined from '@polkadot/util/is/undefined'; + +const re = new RegExp('(poc-1)', 'i'); + +export default function shouldUseLatestChain (chain?: string): boolean { + if (isUndefined(chain)) { + return false; + } + + const match = re.test(chain.toString().toLowerCase()); + + return match ? false : true; +}