Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #132 from iotaledger/feature/111-tcp_is_too_a_nodeUri
Browse files Browse the repository at this point in the history
Added TCP to allowed schemes for node URIs
  • Loading branch information
todofixthis committed Jan 6, 2018
2 parents f31c6f7 + c6b2dcf commit 255abb9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
14 changes: 10 additions & 4 deletions iota/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class NodeUri(f.BaseFilter):
"""
Validates a string as a node URI.
"""
SCHEMES = {'tcp', 'udp'}
"""
Allowed schemes for node URIs.
"""

CODE_NOT_NODE_URI = 'not_node_uri'

templates = {
Expand All @@ -59,7 +64,7 @@ def _apply(self, value):

parsed = compat.urllib_parse.urlparse(value)

if parsed.scheme != 'udp':
if parsed.scheme not in self.SCHEMES:
return self._invalid_value(value, self.CODE_NOT_NODE_URI)

return value
Expand Down Expand Up @@ -148,14 +153,14 @@ class AddressNoChecksum(Trytes):
"""
Validates a sequence as an Address then chops off the checksum if it exists
"""
ADDRESS_BAD_CHECKSUM = 'address_bad_checksum'
ADDRESS_BAD_CHECKSUM = 'address_bad_checksum'

templates = {
ADDRESS_BAD_CHECKSUM: 'Checksum is {supplied_checksum}, should be {expected_checksum}?',
ADDRESS_BAD_CHECKSUM:
'Checksum is {supplied_checksum}, should be {expected_checksum}?',
}

def __init__(self):
# type: (type) -> None
super(AddressNoChecksum, self).__init__(result_type=Address)

def _apply(self, value):
Expand All @@ -180,4 +185,5 @@ def _apply(self, value):
'expected_checksum': value.with_valid_checksum().checksum,
},
)

return Address(value.address)
76 changes: 49 additions & 27 deletions test/filters_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import filters as f
from filters.test import BaseFilterTestCase

from iota import Address, TryteString, TransactionHash
from iota import Address, TransactionHash, TryteString
from iota.filters import AddressNoChecksum, GeneratedAddress, NodeUri, Trytes


Expand Down Expand Up @@ -70,19 +70,30 @@ def test_pass_none(self):
"""
self.assertFilterPasses(None)

def test_pass_uri(self):
"""The incoming value is a valid URI."""
def test_pass_udp(self):
"""
The incoming value is a valid UDP URI.
"""
self.assertFilterPasses('udp://localhost:14265/node')

def test_pass_tcp(self):
"""
The incoming value is a valid TCP URI.
https://github.com/iotaledger/iota.lib.py/issues/111
"""
self.assertFilterPasses('tcp://localhost:14265/node')

def test_fail_not_a_uri(self):
"""
The incoming value is not a URI.
Note: Internally, the filter uses `resolve_adapter`, which has its
own unit tests. We won't duplicate them here; a simple smoke
check should suffice.
Note: Internally, the filter uses ``resolve_adapter``, which has its
own unit tests. We won't duplicate them here; a simple smoke
check should suffice.
:py:class:`test.adapter_test.ResolveAdapterTestCase`
References:
- :py:class:`test.adapter_test.ResolveAdapterTestCase`
"""
self.assertFilterErrors(
'not a valid uri',
Expand All @@ -92,15 +103,17 @@ def test_fail_not_a_uri(self):
def test_fail_bytes(self):
"""
To ensure consistent behavior in Python 2 and 3, bytes are not
accepted.
accepted.
"""
self.assertFilterErrors(
b'udp://localhost:14265/node',
[f.Type.CODE_WRONG_TYPE],
)

def test_fail_wrong_type(self):
"""The incoming value is not a string."""
"""
The incoming value is not a string.
"""
self.assertFilterErrors(
# Use ``FilterRepeater(NodeUri)`` to validate a sequence of URIs.
['udp://localhost:14265/node'],
Expand All @@ -121,7 +134,9 @@ def test_pass_none(self):
self.assertFilterPasses(None)

def test_pass_ascii(self):
"""The incoming value is ASCII."""
"""
The incoming value is ASCII.
"""
trytes = b'RBTC9D9DCDQAEASBYBCCKBFA'

filter_ = self._filter(trytes)
Expand All @@ -130,7 +145,9 @@ def test_pass_ascii(self):
self.assertIsInstance(filter_.cleaned_data, TryteString)

def test_pass_bytearray(self):
"""The incoming value is a bytearray."""
"""
The incoming value is a bytearray.
"""
trytes = bytearray(b'RBTC9D9DCDQAEASBYBCCKBFA')

filter_ = self._filter(trytes)
Expand All @@ -139,7 +156,9 @@ def test_pass_bytearray(self):
self.assertIsInstance(filter_.cleaned_data, TryteString)

def test_pass_tryte_string(self):
"""The incoming value is a TryteString."""
"""
The incoming value is a TryteString.
"""
trytes = TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA')

filter_ = self._filter(trytes)
Expand All @@ -148,7 +167,9 @@ def test_pass_tryte_string(self):
self.assertIsInstance(filter_.cleaned_data, TryteString)

def test_pass_alternate_result_type(self):
"""Configuring the filter to return a specific type."""
"""
Configuring the filter to return a specific type.
"""
input_trytes = b'RBTC9D9DCDQAEASBYBCCKBFA'

result_trytes = (
Expand All @@ -165,11 +186,12 @@ def test_fail_not_trytes(self):
"""
The incoming value contains an invalid character.
Note: Internally, the filter uses `TryteString`, which has its own
unit tests. We won't duplicate them here; a simple smoke check
should suffice.
Note: Internally, the filter uses :py:class:`TryteString`, which has
its own unit tests. We won't duplicate them here; a simple smoke
check should suffice.
:ref:`test.types_test.TryteStringTestCase`
References:
- :py:class:`test.types_test.TryteStringTestCase`
"""
self.assertFilterErrors(
# Everyone knows there's no such thing as "8"!
Expand All @@ -180,7 +202,7 @@ def test_fail_not_trytes(self):
def test_fail_alternate_result_type(self):
"""
The incoming value is a valid tryte sequence, but the filter is
configured for a specific type with stricter validation.
configured for a specific type with stricter validation.
"""
trytes = (
# Ooh, just a little bit too long there.
Expand All @@ -194,10 +216,12 @@ def test_fail_alternate_result_type(self):
)

def test_fail_wrong_type(self):
"""The incoming value has an incompatible type."""
"""
The incoming value has an incompatible type.
"""
self.assertFilterErrors(
# Use ``FilterRepeater(Trytes)`` to validate a sequence of tryte
# representations.
# representations.
[TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA')],
[f.Type.CODE_WRONG_TYPE],
)
Expand All @@ -212,9 +236,6 @@ def setUp(self):
super(AddressNoChecksumTestCase, self).setUp()

# Define some addresses that we can reuse between tests
"""
Incoming value is not an :py:class:`Address` instance.
"""
self.tryte1 = (
b'TESTVALUE9DONTUSEINPRODUCTION99999FBFFTG'
b'QFWEHEL9KCAFXBJBXGE9HID9XCOHFIDABHDG9AHDR'
Expand All @@ -226,22 +247,23 @@ def setUp(self):

def test_pass_no_checksum_addy(self):
"""
Incoming value is tryte in address form or Address object
Incoming value is tryte in address form or Address object.
"""
self.assertFilterPasses(self.tryte1)
self.assertFilterPasses(self.address)

def test_pass_with_checksum_addy(self):
"""
After passing through the filter an address with a checksum should
return the address without
return the address without.
"""
self.assertFilterPasses(self.address_with_checksum, self.address)

def test_fail_with_bad_checksum_addy(self):
"""
If they've got a bad checksum in their address we should probably tell
them so they don't wonder why something works in one place and not another
If they've got a bad checksum in their address we should probably
tell them, so they don't wonder why something works in one place and
not another.
"""
self.assertFilterErrors(
self.address_with_bad_checksum,
Expand Down

0 comments on commit 255abb9

Please sign in to comment.