1
1
# -*- coding: utf-8 -*-
2
- import pytest
3
- import grequests
4
2
import httplib
5
3
import json
6
- from flask import url_for
7
4
5
+ import pytest
6
+ import grequests
7
+ from flask import url_for
8
8
from pyethapp .jsonrpc import address_encoder , address_decoder
9
9
10
- from raiden .api .v1 .encoding import HexAddressConverter
10
+ from raiden .api .v1 .encoding import (
11
+ AddressField ,
12
+ HexAddressConverter ,
13
+ )
11
14
from raiden .utils import channel_to_api_dict
12
15
from raiden .tests .utils .transfer import channel
13
16
from raiden .transfer .state import (
@@ -59,6 +62,28 @@ def test_hex_converter():
59
62
assert converter .to_python ('0x414d72a6f6e28f4950117696081450d63d56c354' ) == address
60
63
61
64
65
+ def test_address_field ():
66
+ # pylint: disable=protected-access
67
+ field = AddressField ()
68
+ attr = 'test'
69
+ data = object ()
70
+
71
+ # invalid hex data
72
+ with pytest .raises (Exception ):
73
+ field ._deserialize ('-' , attr , data )
74
+
75
+ # invalid address, too short
76
+ with pytest .raises (Exception ):
77
+ field ._deserialize ('1234' , attr , data )
78
+
79
+ # missing prefix 0x
80
+ with pytest .raises (Exception ):
81
+ field ._deserialize ('414d72a6f6e28f4950117696081450d63d56c354' , attr , data )
82
+
83
+ address = b'AMr\xa6 \xf6 \xe2 \x8f IP\x11 v\x96 \x08 \x14 P\xd6 =V\xc3 T'
84
+ assert field ._deserialize ('0x414d72a6f6e28f4950117696081450d63d56c354' , attr , data ) == address
85
+
86
+
62
87
@pytest .mark .parametrize ('blockchain_type' , ['geth' ])
63
88
@pytest .mark .parametrize ('number_of_nodes' , [2 ])
64
89
def test_channel_to_api_dict (raiden_network , token_addresses , settle_timeout ):
@@ -80,6 +105,71 @@ def test_channel_to_api_dict(raiden_network, token_addresses, settle_timeout):
80
105
assert result == expected_result
81
106
82
107
108
+ def test_url_with_invalid_address (rest_api_port_number , api_backend ):
109
+ """ Addresses required the leading 0x in the urls. """
110
+
111
+ url_without_prefix = (
112
+ 'http://localhost:{port}/api/1/'
113
+ 'channels/ea674fdde714fd979de3edf0f56aa9716b898ec8'
114
+ ).format (port = rest_api_port_number )
115
+
116
+ request = grequests .patch (
117
+ url_without_prefix ,
118
+ json = {'state' : CHANNEL_STATE_SETTLED }
119
+ )
120
+ response = request .send ().response
121
+
122
+ assert response .status_code == httplib .NOT_FOUND
123
+
124
+
125
+ def test_payload_with_address_without_prefix (api_backend ):
126
+ """ Addresses required leading 0x in the payload. """
127
+ invalid_address = '61c808d82a3ac53231750dadc13c777b59310bd9'
128
+ channel_data_obj = {
129
+ 'partner_address' : invalid_address ,
130
+ 'token_address' : '0xea674fdde714fd979de3edf0f56aa9716b898ec8' ,
131
+ 'settle_timeout' : 10 ,
132
+ }
133
+ request = grequests .put (
134
+ api_url_for (api_backend , 'channelsresource' ),
135
+ json = channel_data_obj
136
+ )
137
+ response = request .send ().response
138
+ assert response .status_code == httplib .BAD_REQUEST
139
+
140
+
141
+ def test_payload_with_address_invalid_chars (api_backend ):
142
+ """ Addresses cannot have invalid characters in it. """
143
+ invalid_address = '0x61c808d82a3ac53231750dadc13c777b59310bdg' # g at the end is invalid
144
+ channel_data_obj = {
145
+ 'partner_address' : invalid_address ,
146
+ 'token_address' : '0xea674fdde714fd979de3edf0f56aa9716b898ec8' ,
147
+ 'settle_timeout' : 10 ,
148
+ }
149
+ request = grequests .put (
150
+ api_url_for (api_backend , 'channelsresource' ),
151
+ json = channel_data_obj
152
+ )
153
+ response = request .send ().response
154
+ assert response .status_code == httplib .BAD_REQUEST
155
+
156
+
157
+ def test_payload_with_address_invalid_length (api_backend ):
158
+ """ Encoded addresses must have the right length. """
159
+ invalid_address = '0x61c808d82a3ac53231750dadc13c777b59310b' # g at the end is invalid
160
+ channel_data_obj = {
161
+ 'partner_address' : invalid_address ,
162
+ 'token_address' : '0xea674fdde714fd979de3edf0f56aa9716b898ec8' ,
163
+ 'settle_timeout' : 10 ,
164
+ }
165
+ request = grequests .put (
166
+ api_url_for (api_backend , 'channelsresource' ),
167
+ json = channel_data_obj
168
+ )
169
+ response = request .send ().response
170
+ assert response .status_code == httplib .BAD_REQUEST
171
+
172
+
83
173
def test_api_query_channels (
84
174
api_backend ,
85
175
api_test_context ,
0 commit comments