added --uri to purchase_list and file_list now includes purchase_receipt field

This commit is contained in:
Lex Berezhny 2019-10-29 22:56:28 -04:00
parent c4c50699cc
commit 3624a3b450
8 changed files with 132 additions and 92 deletions

View file

@ -1772,7 +1772,9 @@ class Daemon(metaclass=JSONRPCServerType):
"""
@requires(STREAM_MANAGER_COMPONENT)
def jsonrpc_file_list(self, sort=None, reverse=False, comparison=None, page=None, page_size=None, **kwargs):
async def jsonrpc_file_list(
self, sort=None, reverse=False, comparison=None,
wallet_id=None, page=None, page_size=None, **kwargs):
"""
List files limited by optional filters
@ -1784,7 +1786,7 @@ class Daemon(metaclass=JSONRPCServerType):
[--claim_name=<claim_name>] [--blobs_in_stream=<blobs_in_stream>]
[--blobs_remaining=<blobs_remaining>] [--sort=<sort_by>]
[--comparison=<comparison>] [--full_status=<full_status>] [--reverse]
[--page=<page>] [--page_size=<page_size>]
[--page=<page>] [--page_size=<page_size>] [--wallet_id=<wallet_id>]
Options:
--sd_hash=<sd_hash> : (str) get file with matching sd hash
@ -1806,14 +1808,27 @@ class Daemon(metaclass=JSONRPCServerType):
--comparison=<comparison> : (str) logical comparison, (eq | ne | g | ge | l | le)
--page=<page> : (int) page to return during paginating
--page_size=<page_size> : (int) number of items on page during pagination
--wallet_id=<wallet_id> : (str) add purchase receipts from this wallet
Returns: {Paginated[File]}
"""
wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
sort = sort or 'rowid'
comparison = comparison or 'eq'
return paginate_list(
paginated = paginate_list(
self.stream_manager.get_filtered_streams(sort, reverse, comparison, **kwargs), page, page_size
)
if paginated['items']:
receipts = {
txo.purchased_claim_id: txo for txo in
await self.ledger.db.get_purchases(
accounts=wallet.accounts,
purchased_claim_id__in=[s.claim_id for s in paginated['items']]
)
}
for stream in paginated['items']:
stream.purchase_receipt = receipts.get(stream.claim_id)
return paginated
@requires(STREAM_MANAGER_COMPONENT)
async def jsonrpc_file_set_status(self, status, **kwargs):
@ -1990,18 +2005,19 @@ class Daemon(metaclass=JSONRPCServerType):
@requires(WALLET_COMPONENT)
async def jsonrpc_purchase_create(
self, claim_id, wallet_id=None, funding_account_ids=None,
self, claim_id=None, url=None, wallet_id=None, funding_account_ids=None,
allow_duplicate_purchase=False, override_max_key_fee=False, preview=False, blocking=False):
"""
Purchase a claim.
Usage:
purchase_create (<claim_id> | --claim_id=<claim_id>) [--wallet_id=<wallet_id>]
purchase_create (--claim_id=<claim_id> | --url=<url>) [--wallet_id=<wallet_id>]
[--funding_account_ids=<funding_account_ids>...]
[--allow_duplicate_purchase] [--override_max_key_fee] [--preview] [--blocking]
Options:
--claim_id=<claim_id> : (str) id of claim to purchase
--url=<url> : (str) lookup claim to purchase by url
--wallet_id=<wallet_id> : (str) restrict operation to specific wallet
--funding_account_ids=<funding_account_ids>: (list) ids of accounts to fund this transaction
--allow_duplicate_purchase : (bool) allow purchasing claim_id you already own
@ -2014,9 +2030,17 @@ class Daemon(metaclass=JSONRPCServerType):
wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
assert not wallet.is_locked, "Cannot spend funds with locked wallet, unlock first."
accounts = wallet.get_accounts_or_all(funding_account_ids)
txo = await self.ledger.get_claim_by_claim_id(accounts, claim_id)
if not txo or not txo.is_claim:
raise Exception(f"Could not find claim with claim_id '{claim_id}'. ")
txo = None
if claim_id:
txo = await self.ledger.get_claim_by_claim_id(accounts, claim_id)
if not isinstance(txo, Output) or not txo.is_claim:
raise Exception(f"Could not find claim with claim_id '{claim_id}'. ")
elif url:
txo = (await self.ledger.resolve(accounts, [url]))[url]
if not isinstance(txo, Output) or not txo.is_claim:
raise Exception(f"Could not find claim with url '{url}'. ")
else:
raise Exception(f"Missing argument claim_id or url. ")
if not allow_duplicate_purchase and txo.purchase_receipt:
raise Exception(
f"You already have a purchase for claim_id '{claim_id}'. "

View file

@ -153,6 +153,8 @@ class JSONResponseEncoder(JSONEncoder):
}
def encode_output(self, txo, check_signature=True):
if not txo:
return
tx_height = txo.tx_ref.height
best_height = self.ledger.headers.height
output = {
@ -283,6 +285,7 @@ class JSONResponseEncoder(JSONEncoder):
'channel_name': managed_stream.channel_name,
'claim_name': managed_stream.claim_name,
'content_fee': managed_stream.content_fee,
'purchase_receipt': self.encode_output(managed_stream.purchase_receipt),
'added_on': managed_stream.added_on,
'height': tx_height,
'confirmations': (best_height + 1) - tx_height if tx_height > 0 else tx_height,

View file

@ -61,6 +61,7 @@ class ManagedStream:
'download_id',
'rowid',
'content_fee',
'purchase_receipt',
'downloader',
'analytics_manager',
'fully_reflected',
@ -94,6 +95,7 @@ class ManagedStream:
self.download_id = download_id or binascii.hexlify(generate_id()).decode()
self.rowid = rowid
self.content_fee = content_fee
self.purchase_receipt = None
self._added_on = added_on
self.downloader = StreamDownloader(self.loop, self.config, self.blob_manager, sd_hash, descriptor)
self.analytics_manager = analytics_manager

View file

@ -279,8 +279,8 @@ class CommandTestCase(IntegrationTestCase):
async def claim_search(self, **kwargs):
return (await self.out(self.daemon.jsonrpc_claim_search(**kwargs)))['items']
def file_list(self, *args, **kwargs):
return self.sout(self.daemon.jsonrpc_file_list(*args, **kwargs))['items']
async def file_list(self, *args, **kwargs):
return (await self.out(self.daemon.jsonrpc_file_list(*args, **kwargs)))['items']
@staticmethod
def get_claim_id(tx):

View file

@ -672,14 +672,14 @@ class StreamCommands(ClaimTestCase):
tx = await self.out(self.stream_create(title='created'))
txo = tx['outputs'][0]
claim_id, expected = txo['claim_id'], txo['value']
files = self.file_list()
files = await self.file_list()
self.assertEqual(1, len(files))
self.assertEqual(tx['txid'], files[0]['txid'])
self.assertEqual(expected, files[0]['metadata'])
# update with metadata-only changes
tx = await self.out(self.stream_update(claim_id, title='update 1'))
files = self.file_list()
files = await self.file_list()
expected['title'] = 'update 1'
self.assertEqual(1, len(files))
self.assertEqual(tx['txid'], files[0]['txid'])
@ -688,7 +688,7 @@ class StreamCommands(ClaimTestCase):
# update with new data
tx = await self.out(self.stream_update(claim_id, title='update 2', data=b'updated data'))
expected = tx['outputs'][0]['value']
files = self.file_list()
files = await self.file_list()
self.assertEqual(1, len(files))
self.assertEqual(tx['txid'], files[0]['txid'])
self.assertEqual(expected, files[0]['metadata'])
@ -1055,10 +1055,10 @@ class StreamCommands(ClaimTestCase):
self.assertEqual(txs[0]['value'], '0.0')
self.assertEqual(txs[0]['fee'], '-0.020107')
await self.assertBalance(self.account, '7.479893')
self.assertEqual(1, len(self.file_list()))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
await self.daemon.jsonrpc_file_delete(delete_all=True)
self.assertEqual(0, len(self.file_list()))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
await self.stream_update(claim_id, bid='1.0') # updates previous claim
txs = (await self.out(self.daemon.jsonrpc_transaction_list()))['items']
@ -1100,12 +1100,12 @@ class StreamCommands(ClaimTestCase):
file.flush()
tx1 = await self.publish('foo', bid='1.0', file_path=file.name)
self.assertEqual(1, len(self.file_list()))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
# doesn't error on missing arguments when doing an update stream
tx2 = await self.publish('foo', tags='updated')
self.assertEqual(1, len(self.file_list()))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual(self.get_claim_id(tx1), self.get_claim_id(tx2))
# update conflict with two claims of the same name
@ -1113,14 +1113,14 @@ class StreamCommands(ClaimTestCase):
with self.assertRaisesRegex(Exception, "There are 2 claims for 'foo'"):
await self.daemon.jsonrpc_publish('foo')
self.assertEqual(2, len(self.file_list()))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 2)
# abandon duplicate stream
await self.stream_abandon(self.get_claim_id(tx3))
# publish to a channel
await self.channel_create('@abc')
tx3 = await self.publish('foo', channel_name='@abc')
self.assertEqual(2, len(self.file_list()))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 2)
r = await self.resolve('lbry://@abc/foo')
self.assertEqual(
r['lbry://@abc/foo']['claim_id'],
@ -1129,7 +1129,7 @@ class StreamCommands(ClaimTestCase):
# publishing again clears channel
tx4 = await self.publish('foo', languages='uk-UA', tags=['Anime', 'anime '])
self.assertEqual(2, len(self.file_list()))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 2)
r = await self.resolve('lbry://foo')
claim = r['lbry://foo']
self.assertEqual(claim['txid'], tx4['outputs'][0]['txid'])

View file

@ -1,5 +1,4 @@
import asyncio
import logging
import os
from binascii import hexlify
@ -19,23 +18,23 @@ class FileCommands(CommandTestCase):
await self.stream_create('foo', '0.01')
await self.stream_create('foo2', '0.01')
file1, file2 = self.file_list('claim_name')
file1, file2 = await self.file_list('claim_name')
self.assertEqual(file1['claim_name'], 'foo')
self.assertEqual(file2['claim_name'], 'foo2')
await self.daemon.jsonrpc_file_delete(claim_name='foo')
self.assertEqual(len(self.file_list()), 1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
await self.daemon.jsonrpc_file_delete(claim_name='foo2')
self.assertEqual(len(self.file_list()), 0)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
await self.daemon.jsonrpc_get('lbry://foo')
self.assertEqual(len(self.file_list()), 1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
async def test_announces(self):
# announces on publish
self.assertEqual(await self.daemon.storage.get_blobs_to_announce(), [])
await self.stream_create('foo', '0.01')
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())["items"][0]
self.assertSetEqual(
set(await self.daemon.storage.get_blobs_to_announce()),
{stream.sd_hash, stream.descriptor.blobs[0].blob_hash}
@ -53,7 +52,7 @@ class FileCommands(CommandTestCase):
self.assertTrue(
await self.daemon.jsonrpc_file_delete(claim_name=claim_name, delete_from_download_dir=True)
)
self.assertItemCount(self.daemon.jsonrpc_file_list(), 0)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
self.assertFalse(os.path.isfile(full_path))
async def test_publish_with_illegal_chars(self):
@ -66,7 +65,7 @@ class FileCommands(CommandTestCase):
prefix, suffix = 'derp?', '.ext.'
san_prefix, san_suffix = 'derp', '.ext'
tx = await self.stream_create(claim_name, '0.01', prefix=prefix, suffix=suffix)
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())["items"][0]
claim_id = self.get_claim_id(tx)
# Assert that file list and source contains the local unsanitized name, but suggested name is sanitized
@ -86,7 +85,7 @@ class FileCommands(CommandTestCase):
# Re-download deleted file and assert that the file name is sanitized
full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name, save_file=True)).full_path
stream_file_name = os.path.basename(full_path)
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())["items"][0]
file_list_name = stream.file_name
suggested_file_name = stream.descriptor.suggested_file_name
@ -109,7 +108,7 @@ class FileCommands(CommandTestCase):
san_prefix, san_suffix = 'derpyderp', '.ext'
tx = await self.stream_update(claim_id, data=b'amazing content', prefix=prefix, suffix=suffix)
full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name, save_file=True)).full_path
updated_stream = self.daemon.jsonrpc_file_list()['items'][0]
updated_stream = (await self.daemon.jsonrpc_file_list())["items"][0]
stream_file_name = os.path.basename(full_path)
source_file_name = tx['outputs'][0]['value']['source']['name']
@ -124,14 +123,14 @@ class FileCommands(CommandTestCase):
async def test_file_list_fields(self):
await self.stream_create('foo', '0.01')
file_list = self.file_list()
file_list = await self.file_list()
self.assertEqual(
file_list[0]['timestamp'],
None
)
self.assertEqual(file_list[0]['confirmations'], -1)
await self.daemon.jsonrpc_resolve('foo')
file_list = self.file_list()
file_list = await self.file_list()
self.assertEqual(
file_list[0]['timestamp'],
self.ledger.headers[file_list[0]['height']]['timestamp']
@ -161,39 +160,39 @@ class FileCommands(CommandTestCase):
claim.stream.description = "fix typos, fix the world"
await self.blockchain_update_name(txid, hexlify(claim.to_bytes()).decode(), '0.01')
await self.daemon.jsonrpc_resolve('lbry://bar')
file_list = self.daemon.jsonrpc_file_list()['items']
file_list = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(file_list[0].stream_claim_info.claim.stream.description, claim.stream.description)
async def test_file_list_paginated_output(self):
await self.create_streams_in_range(0, 20)
page = self.file_list(page_size=20)
page = await self.file_list(page_size=20)
page_claim_ids = [item['claim_id'] for item in page]
self.assertListEqual(page_claim_ids, self.stream_claim_ids)
page = self.file_list(page_size=6)
page = await self.file_list(page_size=6)
page_claim_ids = [item['claim_id'] for item in page]
self.assertListEqual(page_claim_ids, self.stream_claim_ids[:6])
page = self.file_list(page_size=6, page=2)
page = await self.file_list(page_size=6, page=2)
page_claim_ids = [item['claim_id'] for item in page]
self.assertListEqual(page_claim_ids, self.stream_claim_ids[6:12])
out_of_bounds = self.file_list(page=5, page_size=6)
out_of_bounds = await self.file_list(page=5, page_size=6)
self.assertEqual(out_of_bounds, [])
complete = self.daemon.jsonrpc_file_list()
complete = await self.daemon.jsonrpc_file_list()
self.assertEqual(complete['total_pages'], 1)
self.assertEqual(complete['total_items'], 20)
page = self.daemon.jsonrpc_file_list(page_size=10, page=1)
page = await self.daemon.jsonrpc_file_list(page_size=10, page=1)
self.assertEqual(page['total_pages'], 2)
self.assertEqual(page['total_items'], 20)
self.assertEqual(page['page'], 1)
full = self.sout(self.daemon.jsonrpc_file_list(page_size=20, page=1))
page1 = self.file_list(page=1, page_size=10)
page2 = self.file_list(page=2, page_size=10)
full = await self.out(self.daemon.jsonrpc_file_list(page_size=20, page=1))
page1 = await self.file_list(page=1, page_size=10)
page2 = await self.file_list(page=2, page_size=10)
self.assertEqual(page1 + page2, full['items'])
async def test_download_different_timeouts(self):
@ -214,12 +213,12 @@ class FileCommands(CommandTestCase):
self.assertEqual('Failed to download sd blob %s within timeout' % sd_hash, resp['error'])
async def wait_files_to_complete(self):
while self.file_list(status='running'):
while await self.file_list(status='running'):
await asyncio.sleep(0.01)
async def test_filename_conflicts_management_on_resume_download(self):
await self.stream_create('foo', '0.01', data=bytes([0] * (1 << 23)))
file_info = self.file_list()[0]
file_info = (await self.file_list())[0]
original_path = os.path.join(self.daemon.conf.download_dir, file_info['file_name'])
await self.daemon.jsonrpc_file_delete(claim_name='foo')
await self.daemon.jsonrpc_get('lbry://foo')
@ -230,7 +229,7 @@ class FileCommands(CommandTestCase):
await asyncio.wait_for(self.wait_files_to_complete(), timeout=5) # if this hangs, file didn't get set completed
# check that internal state got through up to the file list API
stream = self.daemon.stream_manager.get_stream_by_stream_hash(file_info['stream_hash'])
file_info = self.file_list()[0]
file_info = (await self.file_list())[0]
self.assertEqual(stream.file_name, file_info['file_name'])
# checks if what the API shows is what he have at the very internal level.
self.assertEqual(stream.full_path, file_info['download_path'])
@ -238,7 +237,7 @@ class FileCommands(CommandTestCase):
async def test_incomplete_downloads_erases_output_file_on_stop(self):
tx = await self.stream_create('foo', '0.01', data=b'deadbeef' * 1000000)
sd_hash = tx['outputs'][0]['value']['source']['sd_hash']
file_info = self.file_list()[0]
file_info = (await self.file_list())[0]
await self.daemon.jsonrpc_file_delete(claim_name='foo')
blobs = await self.server_storage.get_blobs_for_stream(
await self.server_storage.get_stream_hash_for_sd_hash(sd_hash)
@ -278,8 +277,8 @@ class FileCommands(CommandTestCase):
# start the download
resp = await self.out(self.daemon.jsonrpc_get('lbry://foo', timeout=2))
self.assertNotIn('error', resp)
self.assertEqual(len(self.file_list()), 1)
self.assertEqual('running', self.file_list()[0]['status'])
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual('running', (await self.file_list())[0]['status'])
await self.daemon.jsonrpc_file_set_status('stop', claim_name='foo')
# recover blobs
@ -291,7 +290,7 @@ class FileCommands(CommandTestCase):
await self.daemon.jsonrpc_file_set_status('start', claim_name='foo')
await asyncio.wait_for(self.wait_files_to_complete(), timeout=5)
file_info = self.file_list()[0]
file_info = (await self.file_list())[0]
self.assertEqual(file_info['blobs_completed'], file_info['blobs_in_stream'])
self.assertEqual('finished', file_info['status'])
@ -326,7 +325,7 @@ class FileCommands(CommandTestCase):
await self.daemon.jsonrpc_file_delete(claim_name='expensive')
response = await self.out(self.daemon.jsonrpc_get('lbry://expensive'))
self.assertEqual(response['error'], 'Not enough funds to cover this transaction.')
self.assertEqual(len(self.file_list()), 0)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
# FAIL: beyond maximum key fee
await self.stream_create(
@ -335,7 +334,7 @@ class FileCommands(CommandTestCase):
)
await self.daemon.jsonrpc_file_delete(claim_name='maxkey')
response = await self.out(self.daemon.jsonrpc_get('lbry://maxkey'))
self.assertEqual(len(self.file_list()), 0)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
self.assertEqual(
response['error'], 'Purchase price of 111.0 LBC exceeds maximum configured price of 100.0 LBC (50.0 USD).'
)
@ -351,7 +350,7 @@ class FileCommands(CommandTestCase):
raw_content_fee = response.content_fee.raw
await self.ledger.wait(response.content_fee)
await self.assertBalance(self.account, '8.925538')
self.assertEqual(len(self.file_list()), 1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
await asyncio.wait_for(self.wait_files_to_complete(), timeout=1)
@ -367,8 +366,8 @@ class FileCommands(CommandTestCase):
self.daemon.stream_manager.stop()
await self.daemon.stream_manager.start()
self.assertEqual(len(self.file_list()), 1)
self.assertEqual(self.daemon.jsonrpc_file_list()['items'][0].content_fee.raw, raw_content_fee)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].content_fee.raw, raw_content_fee)
await self.daemon.jsonrpc_file_delete(claim_name='icanpay')
# PASS: no fee address --> use the claim address to pay
@ -379,12 +378,12 @@ class FileCommands(CommandTestCase):
tx, fee_amount='2.0', fee_currency='LBC', claim_address=target_address
)
await self.daemon.jsonrpc_file_delete(claim_name='nofeeaddress')
self.assertEqual(len(self.file_list()), 0)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
response = await self.out(self.daemon.jsonrpc_get('lbry://nofeeaddress'))
self.assertIsNone(self.daemon.jsonrpc_file_list()['items'][0].stream_claim_info.claim.stream.fee.address)
self.assertIsNone((await self.daemon.jsonrpc_file_list())['items'][0].stream_claim_info.claim.stream.fee.address)
self.assertIsNotNone(response['content_fee'])
self.assertEqual(len(self.file_list()), 1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual(response['content_fee']['outputs'][0]['amount'], '2.0')
self.assertEqual(response['content_fee']['outputs'][0]['address'], target_address)
@ -405,7 +404,7 @@ class FileCommands(CommandTestCase):
# Assert the file downloads
await asyncio.wait_for(self.wait_files_to_complete(), timeout=1)
self.assertEqual(len(self.file_list()), 1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
# Assert the transaction is recorded to the blockchain
starting_balance = await self.blockchain.get_balance()
@ -424,7 +423,7 @@ class FileCommands(CommandTestCase):
await self.__raw_value_update_no_fee_amount(tx, target_address)
await self.daemon.jsonrpc_file_delete(claim_name='nullfee')
response = await self.daemon.jsonrpc_get('lbry://nullfee')
self.assertEqual(len(self.file_list()), 1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertIsNone(response.content_fee)
self.assertTrue(response.stream_claim_info.claim.stream.has_fee)
self.assertDictEqual(

View file

@ -108,6 +108,11 @@ class PurchaseCommandTests(CommandTestCase):
tx = await self.daemon.jsonrpc_purchase_create(claim_id, allow_duplicate_purchase=True)
await self.assertStreamPurchased(stream, tx)
# purchase by uri
abc_stream = await self.priced_stream('abc')
tx = await self.daemon.jsonrpc_purchase_create(url='lbry://abc')
await self.assertStreamPurchased(abc_stream, tx)
async def test_purchase_and_transaction_list(self):
self.assertItemCount(await self.daemon.jsonrpc_purchase_list(), 0)
self.assertItemCount(await self.daemon.jsonrpc_transaction_list(), 1)
@ -140,3 +145,10 @@ class PurchaseCommandTests(CommandTestCase):
url = result[0]['canonical_url']
resolve = await self.resolve(url)
self.assertEqual(result[0]['claim_id'], resolve[url]['purchase_receipt']['claim_id'])
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
await self.daemon.jsonrpc_get('lbry://a')
await self.daemon.jsonrpc_get('lbry://b')
files = await self.file_list()
self.assertEqual(files[0]['claim_id'], files[0]['purchase_receipt']['claim_id'])
self.assertEqual(files[1]['claim_id'], files[1]['purchase_receipt']['claim_id'])

View file

@ -32,7 +32,7 @@ class RangeRequests(CommandTestCase):
await self.stream_create('foo', '0.01', data=self.data, file_size=file_size)
if save_blobs:
self.assertGreater(len(os.listdir(self.daemon.blob_manager.blob_dir)), 1)
await self.daemon.jsonrpc_file_list()['items'][0].fully_reflected.wait()
await (await self.daemon.jsonrpc_file_list())['items'][0].fully_reflected.wait()
await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo')
self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir)))
# await self._restart_stream_manager()
@ -40,7 +40,7 @@ class RangeRequests(CommandTestCase):
site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host,
self.daemon.conf.streaming_port)
await site.start()
self.assertListEqual(self.daemon.jsonrpc_file_list()['items'], [])
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
async def _test_range_requests(self):
name = 'foo'
@ -127,7 +127,7 @@ class RangeRequests(CommandTestCase):
await self._setup_stream(self.data)
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
@ -136,7 +136,7 @@ class RangeRequests(CommandTestCase):
# test that repeated range requests do not create duplicate files
for _ in range(3):
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
@ -152,13 +152,13 @@ class RangeRequests(CommandTestCase):
self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir)
)
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
@ -171,7 +171,7 @@ class RangeRequests(CommandTestCase):
self.data = get_random_bytes((MAX_BLOB_SIZE - 1) * 4)
await self._setup_stream(self.data, save_blobs=False)
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir)))
@ -179,7 +179,7 @@ class RangeRequests(CommandTestCase):
# test that repeated range requests do not create duplicate files
for _ in range(3):
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
current_files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir)))
@ -194,12 +194,12 @@ class RangeRequests(CommandTestCase):
self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir)
)
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNone(stream.download_directory)
self.assertIsNone(stream.full_path)
current_files_in_download_dir = list(os.scandir(os.path.dirname(self.daemon.conf.data_dir)))
@ -212,7 +212,7 @@ class RangeRequests(CommandTestCase):
await self._setup_stream(self.data, save_files=True)
await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list()['items']
streams = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(1, len(streams))
stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -223,7 +223,7 @@ class RangeRequests(CommandTestCase):
for _ in range(3):
await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list()['items']
streams = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(1, len(streams))
stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -240,7 +240,7 @@ class RangeRequests(CommandTestCase):
self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir)
)
streams = self.daemon.jsonrpc_file_list()['items']
streams = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(1, len(streams))
stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -248,7 +248,7 @@ class RangeRequests(CommandTestCase):
self.assertTrue(os.path.isfile(stream.full_path))
await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list()['items']
streams = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(1, len(streams))
stream = streams[0]
self.assertTrue(os.path.isfile(self.daemon.blob_manager.get_blob(stream.sd_hash).file_path))
@ -267,7 +267,7 @@ class RangeRequests(CommandTestCase):
self.daemon.conf.save_blobs = False
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertTrue(os.path.isdir(stream.download_directory))
self.assertTrue(os.path.isfile(stream.full_path))
full_path = stream.full_path
@ -275,7 +275,7 @@ class RangeRequests(CommandTestCase):
for _ in range(3):
await self._test_range_requests()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertTrue(os.path.isdir(stream.download_directory))
self.assertTrue(os.path.isfile(stream.full_path))
current_files_in_download_dir = list(os.scandir(os.path.dirname(full_path)))
@ -288,14 +288,14 @@ class RangeRequests(CommandTestCase):
self.assertEqual(
len(files_in_download_dir), len(current_files_in_download_dir)
)
streams = self.daemon.jsonrpc_file_list()['items']
streams = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(1, len(streams))
stream = streams[0]
self.assertTrue(os.path.isdir(stream.download_directory))
self.assertTrue(os.path.isfile(stream.full_path))
await self._test_range_requests()
streams = self.daemon.jsonrpc_file_list()['items']
streams = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(1, len(streams))
stream = streams[0]
self.assertTrue(os.path.isdir(stream.download_directory))
@ -311,31 +311,31 @@ class RangeRequests(CommandTestCase):
async def test_switch_save_blobs_while_running(self):
await self.test_streaming_only_without_blobs()
self.daemon.conf.save_blobs = True
blobs_in_stream = self.daemon.jsonrpc_file_list()['items'][0].blobs_in_stream
sd_hash = self.daemon.jsonrpc_file_list()['items'][0].sd_hash
blobs_in_stream = (await self.daemon.jsonrpc_file_list())['items'][0].blobs_in_stream
sd_hash = (await self.daemon.jsonrpc_file_list())['items'][0].sd_hash
start_file_count = len(os.listdir(self.daemon.blob_manager.blob_dir))
await self._test_range_requests()
self.assertEqual(start_file_count + blobs_in_stream, len(os.listdir(self.daemon.blob_manager.blob_dir)))
self.assertEqual(0, self.daemon.jsonrpc_file_list()['items'][0].blobs_remaining)
self.assertEqual(0, (await self.daemon.jsonrpc_file_list())['items'][0].blobs_remaining)
# switch back
self.daemon.conf.save_blobs = False
await self._test_range_requests()
self.assertEqual(start_file_count + blobs_in_stream, len(os.listdir(self.daemon.blob_manager.blob_dir)))
self.assertEqual(0, self.daemon.jsonrpc_file_list()['items'][0].blobs_remaining)
self.assertEqual(0, (await self.daemon.jsonrpc_file_list())['items'][0].blobs_remaining)
await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, sd_hash=sd_hash)
self.assertEqual(start_file_count, len(os.listdir(self.daemon.blob_manager.blob_dir)))
await self._test_range_requests()
self.assertEqual(start_file_count, len(os.listdir(self.daemon.blob_manager.blob_dir)))
self.assertEqual(blobs_in_stream, self.daemon.jsonrpc_file_list()['items'][0].blobs_remaining)
self.assertEqual(blobs_in_stream, (await self.daemon.jsonrpc_file_list())['items'][0].blobs_remaining)
async def test_file_save_streaming_only_save_blobs(self):
await self.test_streaming_only_with_blobs()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNone(stream.full_path)
self.server.stop_server()
await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir)
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNotNone(stream.full_path)
await stream.finished_writing.wait()
with open(stream.full_path, 'rb') as f:
@ -344,18 +344,18 @@ class RangeRequests(CommandTestCase):
async def test_file_save_stop_before_finished_streaming_only(self, wait_for_start_writing=False):
await self.test_streaming_only_with_blobs()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNone(stream.full_path)
self.server.stop_server()
await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir)
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
path = stream.full_path
self.assertIsNotNone(path)
if wait_for_start_writing:
await stream.started_writing.wait()
self.assertTrue(os.path.isfile(path))
await self._restart_stream_manager()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNotNone(stream.full_path)
self.assertFalse(os.path.isfile(path))
if wait_for_start_writing:
@ -367,10 +367,10 @@ class RangeRequests(CommandTestCase):
async def test_file_save_streaming_only_dont_save_blobs(self):
await self.test_streaming_only_without_blobs()
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
self.assertIsNone(stream.full_path)
await self.daemon.jsonrpc_file_save('test', self.daemon.conf.data_dir)
stream = self.daemon.jsonrpc_file_list()['items'][0]
stream = (await self.daemon.jsonrpc_file_list())['items'][0]
await stream.finished_writing.wait()
with open(stream.full_path, 'rb') as f:
self.assertEqual(self.data, f.read())
@ -398,7 +398,7 @@ class RangeRequestsLRUCache(CommandTestCase):
self.daemon.conf.save_blobs = False
self.daemon.conf.save_files = False
await self.stream_create('foo', '0.01', data=self.data, file_size=0)
await self.daemon.jsonrpc_file_list()['items'][0].fully_reflected.wait()
await (await self.daemon.jsonrpc_file_list())['items'][0].fully_reflected.wait()
await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo')
self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir)))
@ -406,10 +406,10 @@ class RangeRequestsLRUCache(CommandTestCase):
site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host,
self.daemon.conf.streaming_port)
await site.start()
self.assertListEqual(self.daemon.jsonrpc_file_list()['items'], [])
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
await self._request_stream()
self.assertEqual(1, self.daemon.jsonrpc_file_list()['total_items'])
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.server.stop_server()
# running with cache size 0 gets through without errors without