Skip to content

Commit

Permalink
Apply review comments, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiia-bilova committed Apr 24, 2019
1 parent 73d5a43 commit 4c5d483
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 32 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ $ remme account transfer-tokens \

### Public key

Get list of the public keys by account address — ``remme public-key get-list``:
Get a list of the addresses of the public keys by account address — ``remme public-key get-list``:

| Arguments | Type | Required | Description |
| :-------: | :----: | :------: | ------------------------------------------ |
| address | String | Yes | Address to get list of the public keys by. |
| node-url | String | No | Node URL to apply a command to. |
| Arguments | Type | Required | Description |
| :-------: | :----: | :------: | --------------------------------------------------------------------- |
| address | String | Yes | Account address to get a list of the addresses of the public keys by. |
| node-url | String | No | Node URL to apply a command to. |

```bash
$ remme public-key get-list \
Expand Down Expand Up @@ -195,7 +195,7 @@ $ docker exec -it remme-core-cli bash

And now being in the container, you can develop the project. For instance, run tests and linters:

```bashpytest -vv tests/
```bash
$ pytest -vv tests/
$ flake8 cli && flake8 tests/
```
Expand Down
9 changes: 4 additions & 5 deletions cli/public_key/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Provide implementation of the command line interface's public key commands.
"""
import asyncio
import sys

import click
Expand All @@ -20,8 +19,6 @@
print_result,
)

loop = asyncio.get_event_loop()


@click.group('public-key', chain=True)
def public_key_commands():
Expand All @@ -36,7 +33,7 @@ def public_key_commands():
@public_key_commands.command('get-list')
def get_public_keys(address, node_url):
"""
Get list of the public keys by account address.
Get a list of the addresses of the public keys by account address.
"""
arguments, errors = GetPublicKeysForm().load({
'address': address,
Expand All @@ -50,7 +47,9 @@ def get_public_keys(address, node_url):
address = arguments.get('address')
node_url = arguments.get('node_url')

remme = Remme(network_config={'node_address': str(node_url) + ':8080'})
remme = Remme(network_config={
'node_address': str(node_url) + ':8080',
})

result, errors = PublicKey(service=remme).get_list(address=address)

Expand Down
4 changes: 2 additions & 2 deletions cli/public_key/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Provide forms for command line interface's account commands.
Provide forms for command line interface's public key commands.
"""
from marshmallow import Schema

Expand All @@ -11,7 +11,7 @@

class GetPublicKeysForm(Schema):
"""
Get list of the public keys of the public key form.
Get a list of the addresses of the public keys form.
"""

address = AccountAddressField(required=True)
Expand Down
2 changes: 1 addition & 1 deletion cli/public_key/help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Provide help messages for command line interface's public key commands.
"""
ADDRESS_ARGUMENT_HELP_MESSAGE = 'Account address to get list of the public keys by.'
ADDRESS_ARGUMENT_HELP_MESSAGE = 'Account address to get a list of the addresses of the public keys by.'
2 changes: 1 addition & 1 deletion cli/public_key/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class PublicKeyInterface:

def get_list(self, address):
"""
Get list of the public keys by account address.
Get a list of the addresses of the public keys by account address.
"""
pass
12 changes: 9 additions & 3 deletions cli/public_key/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ def __init__(self, service):

def get_list(self, address):
"""
Get list of the public keys by account address.
Get a list of the addresses of the public keys by account address.
"""
public_keys = loop.run_until_complete(self.service.public_key_storage.get_account_public_keys(address=address))
try:
public_key_addresses = loop.run_until_complete(
self.service.public_key_storage.get_account_public_keys(address=address),
)

except Exception as error:
return None, str(error)

return {
'public_keys': public_keys,
'public_key_addresses': public_key_addresses,
}, None
76 changes: 62 additions & 14 deletions tests/public_key/test_get_public_keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Provide tests for command line interface's public key get public keys commands.
Provide tests for command line interface's public key get public keys addresses commands.
"""
import json
import re
Expand All @@ -20,8 +20,8 @@

def test_get_public_keys():
"""
Case: get a list of the public keys by account address.
Expect: list of public keys, each public key matched regexp checking.
Case: get a list of the addresses of the public keys by account address.
Expect: list of the addresses of the public keys, each public key address matched regexp checking.
"""
runner = CliRunner()
result = runner.invoke(cli, [
Expand All @@ -33,17 +33,17 @@ def test_get_public_keys():
NODE_IP_ADDRESS_FOR_TESTING,
])

public_keys = json.loads(result.output).get('result').get('public_keys')
public_key_addresses = json.loads(result.output).get('result').get('public_key_addresses')

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code

for public_key in public_keys:
for public_key in public_key_addresses:
assert re.match(pattern=ADDRESS_REGEXP, string=public_key) is not None


def test_get_public_keys_invalid_address():
"""
Case: get a list of the public keys by invalid address.
Case: get a list of the addresses of the public keys by invalid address.
Expect: the following address is not valid error message.
"""
invalid_address = '1120076ecf036e857f42129b58303bcf1e03723764a1702cbe98529802aad8514ee3zz'
Expand All @@ -67,23 +67,23 @@ def test_get_public_keys_invalid_address():

def test_get_public_keys_without_node_url(mocker):
"""
Case: get a list of the public keys by address without passing node URL.
Expect: list of public keys is returned from node on localhost.
Case: get a list of the addresses of the public keys without passing node URL.
Expect: list of the addresses of the public keys is returned from node on localhost.
"""
list_of_public_keys = [
public_key_addresses = [
'a23be14785e7b073b50e24f72e086675289795b969a895a7f02202404086946e8ddc5b',
'a23be17265e8393dd9ae7a46f1be662f86130c434fd54576a7d92b678e5c30de4f677f',
]

mock_public_key_get_public_keys = mocker.patch('cli.public_key.service.loop.run_until_complete')
mock_public_key_get_public_keys.return_value = list_of_public_keys
mock_public_key_get_public_keys.return_value = public_key_addresses

runner = CliRunner()
result = runner.invoke(cli, ['public-key', 'get-list', '--address', ADDRESS_PRESENTED_ON_THE_TEST_NODE])

expected_result = {
'result': {
'public_keys': list_of_public_keys,
'public_key_addresses': public_key_addresses,
},
}

Expand All @@ -93,7 +93,7 @@ def test_get_public_keys_without_node_url(mocker):

def test_get_public_keys_invalid_node_url():
"""
Case: get a list of the public keys by passing invalid node URL.
Case: get a list of the addresses of the public keys by passing invalid node URL.
Expect: the following node URL is invalid error message.
"""
invalid_node_url = 'domainwithoutextention'
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_get_public_keys_invalid_node_url():

def test_get_public_keys_node_url_with_http():
"""
Case: get a list of the public keys by passing node URL with explicit HTTP protocol.
Case: get a list of the addresses of the public keys by passing node URL with explicit HTTP protocol.
Expect: the following node URL contains protocol error message.
"""
node_url_with_http_protocol = 'http://masternode.com'
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_get_public_keys_node_url_with_http():

def test_get_public_keys_node_url_with_https():
"""
Case: get a list of the public keys by passing node URL with explicit HTTPS protocol.
Case: get a list of the addresses of the public keys by passing node URL with explicit HTTPS protocol.
Expect: the following node URL contains protocol error message.
"""
node_url_with_https_protocol = 'https://masternode.com'
Expand All @@ -176,3 +176,51 @@ def test_get_public_keys_node_url_with_https():

assert FAILED_EXIT_FROM_COMMAND_CODE == result.exit_code
assert dict_to_pretty_json(expected_error) in result.output


def test_get_public_keys_non_existing_address():
"""
Case: get a list of the addresses of the public keys by passing non existing address.
Expect: empty list of the addresses of the public keys is returned.
"""
non_existing_address = '1120076ecf036e857f42129b58303bcf1e03723764a1702cbe98529802aad8514ee3c1'

runner = CliRunner()
result = runner.invoke(cli, [
'public-key',
'get-list',
'--address',
non_existing_address,
'--node-url',
NODE_IP_ADDRESS_FOR_TESTING,
])

public_key_addresses = json.loads(result.output).get('result').get('public_key_addresses')

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code
assert isinstance(public_key_addresses, list)


def test_get_public_keys_non_existing_node_url():
"""
Case: get a list of the addresses of the public keys by passing non existing node URL.
Expect: check if node running at URL error message.
"""
non_existing_node_url = 'non-existing-node.com'

runner = CliRunner()
result = runner.invoke(cli, [
'public-key',
'get-list',
'--address',
ADDRESS_PRESENTED_ON_THE_TEST_NODE,
'--node-url',
non_existing_node_url,
])

expected_error = {
'errors': f'Please check if your node running at http://{non_existing_node_url}:8080.',
}

assert FAILED_EXIT_FROM_COMMAND_CODE == result.exit_code
assert dict_to_pretty_json(expected_error) in result.output

0 comments on commit 4c5d483

Please sign in to comment.