/
test_fileinfo.py
243 lines (209 loc) · 7.88 KB
/
test_fileinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#
# Copyright 2021 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
import logging
import pytest
from ocean_provider.constants import BaseURLs
from ocean_provider.utils.address import get_contract_address
from ocean_provider.utils.basics import get_config
from ocean_provider.utils.services import ServiceType
from tests.helpers.constants import ARWEAVE_TRANSACTION_ID
from tests.test_helpers import (
get_dataset_with_invalid_url_ddo,
get_first_service_by_type,
get_registered_asset,
)
logger = logging.getLogger(__name__)
fileinfo_url = BaseURLs.SERVICES_URL + "/fileinfo"
@pytest.mark.integration
def test_asset_info(client, publisher_wallet):
asset = get_registered_asset(publisher_wallet)
service = get_first_service_by_type(asset, ServiceType.ACCESS)
response = client.post(
fileinfo_url,
json={"did": asset.did, "serviceId": service.id, "checksum": "true"},
)
result = response.get_json()
assert response.status == "200 OK"
assert isinstance(result, list)
assert len(result) == 1
for file_info in result:
assert file_info["contentLength"]
assert file_info["contentType"] == "text/plain"
assert file_info["valid"] is True
assert (
file_info["checksum"]
== "1f7c17bed455f484f4d5ebc581cde6bc059977ef1e143b52a703f18b89c86a22"
)
assert file_info["checksumType"] == "sha256"
asset = get_dataset_with_invalid_url_ddo(client, publisher_wallet)
service = get_first_service_by_type(asset, ServiceType.ACCESS)
response = client.post(
fileinfo_url, json={"did": asset.did, "serviceId": service.id}
)
result = response.get_json()
assert response.status == "200 OK"
assert isinstance(result, list)
assert len(result) == 1
for file_info in result:
assert "contentLength" not in file_info or not file_info["contentLength"]
assert file_info["valid"] is False
@pytest.mark.unit
def test_check_url_good(client):
response = client.post(
fileinfo_url,
json={
"url": "https://s3.amazonaws.com/testfiles.oceanprotocol.com/info.0.json",
"type": "url",
"method": "GET",
},
)
result = response.get_json()
assert response.status == "200 OK"
for file_info in result:
assert file_info["contentLength"] == "1161"
assert file_info["contentType"] == "application/json"
assert file_info["valid"] is True
@pytest.mark.unit
def test_checksums(client):
data = {
"url": "https://raw.githubusercontent.com/tbertinmahieux/MSongsDB/master/Tasks_Demos/CoverSongs/shs_dataset_test.txt",
"type": "url",
"method": "GET",
"checksum": True,
}
response = client.post(fileinfo_url, json=data)
result = response.get_json()
assert response.status == "200 OK"
for file_info in result:
assert file_info["valid"] is True
assert file_info["type"] == "url"
assert (
file_info["checksum"]
== "1f7c17bed455f484f4d5ebc581cde6bc059977ef1e143b52a703f18b89c86a22"
)
assert file_info["checksumType"] == "sha256"
# big file, we should not have a checksum
data = {
"hash": "bafybeif4rpr4tuqfjmpxkavtsgzs6aliafwy7cywlge4756xpozqa5xezy",
"type": "ipfs",
"checksum": True,
}
response = client.post(fileinfo_url, json=data)
result = response.get_json()
assert response.status == "200 OK"
for file_info in result:
assert file_info["valid"] is True
assert file_info["type"] == "ipfs"
assert "checksum" not in file_info
assert "checksumType" not in file_info
@pytest.mark.unit
def test_check_url_bad(client):
data = {"url": "http://127.0.0.1/not_valid", "type": "url", "method": "GET"}
response = client.post(fileinfo_url, json=data)
result = response.get_json()
assert response.status == "200 OK"
for file_info in result:
assert file_info["valid"] is False
data = {"type": "invalid_type"}
response = client.post(fileinfo_url, json=data)
result = response.get_json()
assert response.status == "400 BAD REQUEST"
data = {"type": "ipfs"} # no hash
response = client.post(fileinfo_url, json=data)
result = response.get_json()
assert response.status == "400 BAD REQUEST"
data = {"type": "url"} # no url
response = client.post(fileinfo_url, json=data)
result = response.get_json()
assert response.status == "400 BAD REQUEST"
@pytest.mark.unit
def test_check_arweave_good(client):
payload = {
"type": "arweave",
"transactionId": ARWEAVE_TRANSACTION_ID,
}
response = client.post(fileinfo_url, json=payload)
result = response.get_json()
assert response.status == "200 OK", f"{result}"
assert isinstance(result, list)
assert len(result) == 1
for file_info in result:
assert file_info["contentLength"] == "5311"
assert file_info["contentType"] == "application/octet-stream"
assert file_info["valid"] is True
assert file_info["type"] == "arweave"
@pytest.mark.unit
def test_check_arweave_bad(client, monkeypatch):
payload = {"type": "arweave", "transactionId": "invalid"}
response = client.post(fileinfo_url, json=payload)
result = response.get_json()
assert response.status == "200 OK"
for file_info in result:
assert file_info["valid"] is False
payload = {"type": "arweave"} # No transactionId
response = client.post(fileinfo_url, json=payload)
result = response.get_json()
assert response.status == "400 BAD REQUEST", f"{result}"
monkeypatch.setenv("ARWEAVE_GATEWAY", "https://gateway_not_reachable")
payload = {
"type": "arweave",
"transactionId": ARWEAVE_TRANSACTION_ID,
}
response = client.post(fileinfo_url, json=payload)
result = response.get_json()
assert response.status == "200 OK"
assert result[0]["valid"] == False
@pytest.mark.integration
def test_check_smartcontract_simple(client, publisher_wallet, consumer_wallet, web3):
router_address = get_contract_address(
get_config().address_file, "Router", web3.chain_id
)
abi = {
"inputs": [],
"name": "getApprovedTokens",
"outputs": [{"internalType": "address[]", "name": "", "type": "address[]"}],
"stateMutability": "view",
"type": "function",
}
payload = {"type": "smartcontract", "address": router_address, "abi": abi}
response = client.post(fileinfo_url, json=payload)
result = response.get_json()
assert response.status == "200 OK", f"{result}"
assert isinstance(result, list)
assert len(result) == 1
@pytest.mark.integration
def test_check_smartcontract_with_userdata(
client, publisher_wallet, consumer_wallet, web3
):
dummy_asset = get_registered_asset(publisher_wallet)
dummy_service = get_first_service_by_type(dummy_asset, ServiceType.ACCESS)
# create abi for getId
abi = {
"inputs": [{"internalType": "address", "name": "user", "type": "address"}],
"name": "isERC20Deployer",
"outputs": [{"internalType": "bool", "name": "", "type": "bool"}],
"stateMutability": "view",
"type": "function",
}
userdata = {"user": publisher_wallet.address}
payload = {
"type": "smartcontract",
"address": dummy_service.datatoken_address,
"abi": abi,
}
# try first without userdata, should fail
response = client.post(fileinfo_url, json=payload)
result = response.get_json()
assert response.status == "400 BAD REQUEST", f"{result}"
# try with userdata, should be fine
payload_with_userdata = {
"type": "smartcontract",
"address": dummy_service.datatoken_address,
"abi": abi,
"userdata": userdata,
}
response = client.post(fileinfo_url, json=payload_with_userdata)
result = response.get_json()
assert response.status == "200 OK"