Skip to content

Commit

Permalink
Change regular expression constants to custom from library, refactor …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
anastasiia-bilova committed May 17, 2019
1 parent fbaa974 commit 79988e9
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 123 deletions.
18 changes: 9 additions & 9 deletions cli/constants.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""
Provide constants for command line interface.
"""
from remme.models.general.patterns import RemmePatterns
from remme.models.utils.family_name import RemmeFamilyName

ADDRESS_REGEXP = r'^[0-9a-f]{70}$'
BLOCK_IDENTIFIER_REGEXP = r'^[0-9a-f]{128}$'
BATCH_ID_REGEXP = r'^[0-9a-f]{128}$'
PUBLIC_KEY_REGEXP = r'^[0-9a-f]{66}$'
PRIVATE_KEY_REGEXP = r'^[a-f0-9]{64}$'
PUBLIC_KEY_ADDRESS_REGEXP = r'^[0-9a-f]{70}$'
HEADER_SIGNATURE_REGEXP = r'^[0-9a-f]{128}$'
SWAP_IDENTIFIER_REGEXP = r'^[a-f0-9]{64}$'
TRANSACTION_HEADER_SIGNATURE_REGEXP = r'^[0-9a-f]{128}$'
ADDRESS_REGEXP = RemmePatterns.ADDRESS.value
BATCH_IDENTIFIER_REGEXP = RemmePatterns.HEADER_SIGNATURE.value
BLOCK_IDENTIFIER_REGEXP = RemmePatterns.HEADER_SIGNATURE.value
DOMAIN_NAME_REGEXP = r'(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]'
PRIVATE_KEY_REGEXP = RemmePatterns.PRIVATE_KEY.value
PUBLIC_KEY_ADDRESS_REGEXP = RemmePatterns.ADDRESS.value
PUBLIC_KEY_REGEXP = RemmePatterns.PUBLIC_KEY.value
SWAP_IDENTIFIER_REGEXP = RemmePatterns.SWAP_ID.value
TRANSACTION_IDENTIFIER_REGEXP = RemmePatterns.HEADER_SIGNATURE.value

PASSED_EXIT_FROM_COMMAND_CODE = 0
FAILED_EXIT_FROM_COMMAND_CODE = -1
Expand Down
12 changes: 6 additions & 6 deletions cli/generic/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

from cli.constants import (
ADDRESS_REGEXP,
BATCH_IDENTIFIER_REGEXP,
BLOCK_IDENTIFIER_REGEXP,
DOMAIN_NAME_REGEXP,
FAMILY_NAMES,
HEADER_SIGNATURE_REGEXP,
PRIVATE_KEY_REGEXP,
PUBLIC_KEY_ADDRESS_REGEXP,
SWAP_IDENTIFIER_REGEXP,
TRANSACTION_HEADER_SIGNATURE_REGEXP,
TRANSACTION_IDENTIFIER_REGEXP,
)


Expand Down Expand Up @@ -72,7 +72,7 @@ def _deserialize(self, value, attr, obj, **kwargs):
for identifier in value.split(','):
identifier = identifier.strip()

if re.match(pattern=TRANSACTION_HEADER_SIGNATURE_REGEXP, string=identifier) is None:
if re.match(pattern=TRANSACTION_IDENTIFIER_REGEXP, string=identifier) is None:
raise ValidationError(f'The following identifier `{identifier}` is invalid.')

validated_identifiers.append(identifier)
Expand All @@ -91,7 +91,7 @@ def _deserialize(self, value, attr, obj, **kwargs):
"""
transaction_identifier = value

if re.match(pattern=TRANSACTION_HEADER_SIGNATURE_REGEXP, string=transaction_identifier) is None:
if re.match(pattern=TRANSACTION_IDENTIFIER_REGEXP, string=transaction_identifier) is None:
raise ValidationError(f'The following identifier `{transaction_identifier}` is invalid.')

return transaction_identifier
Expand All @@ -111,7 +111,7 @@ def _deserialize(self, value, attr, obj, **kwargs):
for identifier in value.split(','):
identifier = identifier.strip()

if re.match(pattern=HEADER_SIGNATURE_REGEXP, string=identifier) is None:
if re.match(pattern=BATCH_IDENTIFIER_REGEXP, string=identifier) is None:
raise ValidationError(f'The following identifier `{identifier}` is invalid.')

validated_identifiers.append(identifier)
Expand All @@ -130,7 +130,7 @@ def _deserialize(self, value, attr, obj, **kwargs):
"""
batch_identifier = value

if re.match(pattern=HEADER_SIGNATURE_REGEXP, string=batch_identifier) is None:
if re.match(pattern=BATCH_IDENTIFIER_REGEXP, string=batch_identifier) is None:
raise ValidationError(f'The following identifier `{batch_identifier}` is invalid.')

return batch_identifier
Expand Down
24 changes: 13 additions & 11 deletions tests/account/test_transfer_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from click.testing import CliRunner

from cli.constants import (
BATCH_ID_REGEXP,
BATCH_IDENTIFIER_REGEXP,
DEV_BRANCH_NODE_IP_ADDRESS_FOR_TESTING,
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
FAILED_EXIT_FROM_COMMAND_CODE,
Expand All @@ -18,6 +18,8 @@
from cli.entrypoint import cli
from cli.utils import dict_to_pretty_json

ADDRESS_PRESENTED_ON_THE_TEST_NODE = '112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202'


def test_transfer_tokens():
"""
Expand All @@ -31,7 +33,7 @@ def test_transfer_tokens():
'--private-key',
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
'1000',
'--node-url',
Expand All @@ -41,7 +43,7 @@ def test_transfer_tokens():
batch_id = json.loads(result.output).get('result').get('batch_identifier')

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code
assert re.match(pattern=BATCH_ID_REGEXP, string=batch_id) is not None
assert re.match(pattern=BATCH_IDENTIFIER_REGEXP, string=batch_id) is not None


def test_transfer_tokens_invalid_private_key():
Expand All @@ -58,7 +60,7 @@ def test_transfer_tokens_invalid_private_key():
'--private-key',
invalid_private_key,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
'1000',
'--node-url',
Expand Down Expand Up @@ -124,7 +126,7 @@ def test_transfer_tokens_invalid_amount():
'--private-key',
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
invalid_amount,
'--node-url',
Expand All @@ -148,7 +150,7 @@ def test_transfer_tokens_with_insufficient_amount(insufficient_amount):
'--private-key',
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
insufficient_amount,
'--node-url',
Expand Down Expand Up @@ -182,15 +184,15 @@ def test_transfer_tokens_without_node_url(mocker, sent_transaction):
'--private-key',
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
'1000',
])

batch_id = json.loads(result.output).get('result').get('batch_identifier')

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code
assert re.match(pattern=BATCH_ID_REGEXP, string=batch_id) is not None
assert re.match(pattern=BATCH_IDENTIFIER_REGEXP, string=batch_id) is not None


def test_transfer_tokens_invalid_node_url():
Expand All @@ -207,7 +209,7 @@ def test_transfer_tokens_invalid_node_url():
'--private-key',
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
'1000',
'--node-url',
Expand Down Expand Up @@ -239,7 +241,7 @@ def test_transfer_tokens_node_url_with_protocol(node_url_with_protocol):
'--private-key',
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
'1000',
'--node-url',
Expand Down Expand Up @@ -272,7 +274,7 @@ def test_transfer_tokens_non_existing_node_url():
'--private-key',
DEV_BRANCH_NODE_PRIVATE_KEY_WITH_MONEY,
'--address-to',
'112007d71fa7e120c60fb392a64fd69de891a60c667d9ea9e5d9d9d617263be6c20202',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--amount',
'1000',
'--node-url',
Expand Down
20 changes: 7 additions & 13 deletions tests/batch/test_get_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
from cli.entrypoint import cli
from cli.utils import dict_to_pretty_json

BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE = 'ccb529856e538325b435c6a75261702d1bdb52d3873b29189a722330cda628a6' \
'62028a7b39d1f5475cb78f5fc12efb986a35553ce8f1b63580b97fc6ab9e9655'


def test_get_batch():
"""
Case: get a batch by identifier.
Expect: batch is returned.
"""
batch_id = 'ccb529856e538325b435c6a75261702d1bdb52d3873b29189a722330cda628a6' \
'62028a7b39d1f5475cb78f5fc12efb986a35553ce8f1b63580b97fc6ab9e9655'

runner = CliRunner()
result = runner.invoke(cli, [
'batch',
'get',
'--id',
batch_id,
BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE,
'--node-url',
DEV_BRANCH_NODE_IP_ADDRESS_FOR_TESTING,
])
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_get_batch_without_node_url(mocker):
Expect: batch is returned from a node on localhost.
"""
batch_id = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751'

expected_result = {
"data": {
Expand Down Expand Up @@ -131,15 +131,12 @@ def test_get_batch_with_invalid_node_url():
"""
invalid_node_url = 'my-node-url.com'

batch_id = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751'

runner = CliRunner()
result = runner.invoke(cli, [
'batch',
'get',
'--id',
batch_id,
BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE,
'--node-url',
invalid_node_url,
])
Expand All @@ -158,15 +155,12 @@ def test_get_batch_node_url_with_protocol(node_url_with_protocol):
Case: get a batch by its identifier by passing node URL with an explicit protocol.
Expect: the following node URL contains a protocol error message.
"""
batch_id = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751'

runner = CliRunner()
result = runner.invoke(cli, [
'batch',
'get',
'--id',
batch_id,
BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE,
'--node-url',
node_url_with_protocol,
])
Expand Down
21 changes: 7 additions & 14 deletions tests/batch/test_get_batch_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from cli.entrypoint import cli
from cli.utils import dict_to_pretty_json

BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751'


def test_get_committed_batch_status():
"""
Expand Down Expand Up @@ -97,9 +100,6 @@ def test_get_batch_status_without_node_url(mocker):
Case: get a batch status by its identifier without passing node URL.
Expect: batch status is returned from a node on localhost.
"""
batch_id = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751' \

committed_status = 'COMMITTED'

expected_result = {
Expand All @@ -114,7 +114,7 @@ def test_get_batch_status_without_node_url(mocker):
'batch',
'get-status',
'--id',
batch_id,
BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE,
])

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code
Expand All @@ -127,15 +127,13 @@ def test_get_batch_status_with_invalid_node_url():
Expect: the following node URL is invalid error message.
"""
invalid_node_url = 'domainwithoutextention'
batch_id = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751'

runner = CliRunner()
result = runner.invoke(cli, [
'batch',
'get-status',
'--id',
batch_id,
BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE,
'--node-url',
invalid_node_url,
])
Expand All @@ -158,15 +156,12 @@ def test_get_batch_status_node_url_with_protocol(node_url_with_protocol):
Case: get batch a status by its identifier by passing node URL with an explicit protocol.
Expect: the following node URL contains the protocol error message.
"""
batch_id = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751'

runner = CliRunner()
result = runner.invoke(cli, [
'batch',
'get-status',
'--id',
batch_id,
BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE,
'--node-url',
node_url_with_protocol,
])
Expand All @@ -189,15 +184,13 @@ def test_get_batch_status_with_non_existing_node_url():
Expect: check if node running at the URL error message.
"""
non_existing_node_url = 'non-existing-node.com'
batch_id = '6f200995e766da7218ec2a3d0aeabbe1151128063cdf4e954cd08390a879b28e' \
'085a06f8708d2e6bb34f6501e8ddc981f0353627c1d4f90c80a656a8090c8751'

runner = CliRunner()
result = runner.invoke(cli, [
'batch',
'get-status',
'--id',
batch_id,
BATCH_IDENTIFIER_PRESENTED_ON_THE_TEST_NODE,
'--node-url',
non_existing_node_url,
])
Expand Down
Loading

0 comments on commit 79988e9

Please sign in to comment.