From 375cd0b4992cc30ba85d07f9316332287e264eca Mon Sep 17 00:00:00 2001 From: kansi Date: Tue, 27 Feb 2018 13:31:44 +0530 Subject: [PATCH] Problem: No tests for `GET /api/v1/validators` Solution: Add tests for the same --- tests/web/test_info.py | 1 + tests/web/test_validators.py | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/web/test_validators.py diff --git a/tests/web/test_info.py b/tests/web/test_info.py index f682b5e3..81c0972a 100644 --- a/tests/web/test_info.py +++ b/tests/web/test_info.py @@ -18,6 +18,7 @@ def test_api_root_endpoint(client, wsserver_base_url): 'streams': '{}/api/v1/streams/valid_transactions'.format( wsserver_base_url), 'metadata': '/api/v1/metadata/', + 'validators': '/api/v1/validators', } }, 'docs': 'https://docs.bigchaindb.com/projects/server/en/vtsttst/', diff --git a/tests/web/test_validators.py b/tests/web/test_validators.py new file mode 100644 index 00000000..d71e30a9 --- /dev/null +++ b/tests/web/test_validators.py @@ -0,0 +1,47 @@ +import pytest + +pytestmark = pytest.mark.tendermint + +VALIDATORS_ENDPOINT = '/api/v1/validators/' + + +def test_get_validators_endpoint(b, client, monkeypatch): + + def mock_get(uri): + return MockResponse() + monkeypatch.setattr('requests.get', mock_get) + + res = client.get(VALIDATORS_ENDPOINT) + + assert is_validator(res.json[0]) + assert res.status_code == 200 + + +def test_get_validators_500_endpoint(b, client, monkeypatch): + + def mock_get(uri): + return 'InvalidResponse' + monkeypatch.setattr('requests.get', mock_get) + + res = client.get(VALIDATORS_ENDPOINT) + assert res.status_code == 500 + + +# Helper +def is_validator(v): + return ('pub_key' in v) and ('voting_power' in v) + + +class MockResponse(): + + def json(self): + return {'id': '', + 'jsonrpc': '2.0', + 'result': + {'block_height': 5, + 'validators': [ + {'accum': 0, + 'address': 'F5426F0980E36E03044F74DD414248D29ABCBDB2', + 'pub_key': {'data': '4E2685D9016126864733225BE00F005515200727FBAB1312FC78C8B76831255A', + 'type': 'ed25519'}, + 'voting_power': 10}]}}