Adds unittests & fixes comment_client functions

This commit is contained in:
Oleg Silkin 2019-07-24 21:18:21 -04:00 committed by Lex Berezhny
parent 14ec6ce43b
commit 2545ebbece
3 changed files with 110 additions and 25 deletions

View file

@ -3503,26 +3503,30 @@ class Daemon(metaclass=JSONRPCServerType):
Usage:
comment_delete (<comment_id> | --comment_id=<comment_id>)
Options:
--comment_id=<comment_id> : (str) The ID of the comment to be deleted.
--comment_id=<comment_id> : (str) The ID of the comment to be deleted.
Returns:
(dict) Object with the `comment_id` passed in as the key, and a flag indicating if it was deleted
{
<comment_id> (str): {
"deleted": (bool)
}
}
"""
abandon_comment_body = {'comment_id': comment_id}
channel = await comment_client.jsonrpc_post(
self.conf.comment_server, 'get_channel_from_comment_id', comment_id=comment_id
)
if not channel:
if 'error' in channel:
return {comment_id: {'deleted': False}}
channel = await self.get_channel_or_none(None, **channel)
abandon_comment_body.update({
'channel_id': channel.claim_id,
'channel_name': channel.claim_name,
})
comment_client.sign_comment(abandon_comment_body, channel, signing_field='comment_id')
resp = await comment_client.jsonrpc_post(self.conf.comment_server, 'delete_comment', abandon_comment_body)
return {comment_id: resp}
comment_client.sign_comment(abandon_comment_body, channel, abandon=True)
return await comment_client.jsonrpc_post(self.conf.comment_server, 'delete_comment', abandon_comment_body)
async def broadcast_or_release(self, account, tx, blocking=False):
try:

View file

@ -18,13 +18,18 @@ def get_encoded_signature(signature):
return ecdsa.util.sigencode_der(r, s, len(signature) * 4)
def is_comment_signed_by_channel(comment: dict, channel: Output):
def cid2hash(claim_id: str) -> bytes:
return binascii.unhexlify(claim_id.encode())[::-1]
def is_comment_signed_by_channel(comment: dict, channel: Output, abandon=False):
if type(channel) is Output:
try:
signing_field = comment['comment_id'] if abandon else comment['comment']
pieces = [
comment['signing_ts'].encode(),
channel.claim_hash,
comment['comment'].encode()
cid2hash(comment['channel_id']),
signing_field.encode()
]
return Output.is_signature_valid(
get_encoded_signature(comment['signature']),
@ -36,14 +41,15 @@ def is_comment_signed_by_channel(comment: dict, channel: Output):
return False
def sign_comment(comment: dict, channel: Output, signing_field='comment'):
timestamp = str(int(time.time())).encode()
pieces = [timestamp, channel.claim_hash, comment[signing_field].encode()]
def sign_comment(comment: dict, channel: Output, abandon=False):
timestamp = str(int(time.time()))
signing_field = comment['comment_id'] if abandon else comment['comment']
pieces = [timestamp.encode(), channel.claim_hash, signing_field.encode()]
digest = sha256(b''.join(pieces))
signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
comment.update({
'signature': binascii.hexlify(signature).decode(),
'signing_ts': timestamp.decode()
'signing_ts': timestamp
})

View file

@ -1,3 +1,4 @@
import time
from math import ceil
from aiohttp import web
@ -14,6 +15,19 @@ class MockedCommentServer:
'INVALID_METHOD': {'code': -32604, 'message': 'The Requested method does not exist'}
}
COMMENT_SCHEMA = {
'comment': None,
'comment_id': None,
'claim_id': None,
'parent_id': None,
'channel_name': None,
'channel_id': None,
'signature': None,
'signing_ts': None,
'timestamp': None,
'channel_url': None,
}
def __init__(self, port=2903):
self.port = port
self.app = web.Application(debug=True)
@ -23,13 +37,43 @@ class MockedCommentServer:
self.comments = []
self.comment_id = 0
def create_comment(self, **comment):
@classmethod
def _create_comment(cls, **kwargs):
schema = cls.COMMENT_SCHEMA.copy()
schema.update(**kwargs)
return schema
@staticmethod
def clean(d: dict):
return {k: v for k, v in d.items() if v}
def create_comment(self, channel_name=None, channel_id=None, **kwargs):
self.comment_id += 1
comment['comment_id'] = self.comment_id
if 'channel_id' in comment:
comment['channel_url'] = 'lbry://' + comment['channel_name'] + '#' + comment['channel_id']
comment_id = self.comment_id
channel_url = 'lbry://' + channel_name + '#' + channel_id if channel_id else None
comment = self._create_comment(
comment_id=str(comment_id),
channel_name=channel_name,
channel_id=channel_id,
channel_url=channel_url,
timestamp=str(int(time.time())),
**kwargs
)
self.comments.append(comment)
return comment
return self.clean(comment)
def delete_comment(self, comment_id: int, channel_id: str, **kwargs):
deleted = False
try:
if 0 <= comment_id <= len(self.comments) and self.comments[comment_id - 1]['channel_id'] == channel_id:
self.comments.pop(comment_id - 1)
deleted = True
finally:
return {
str(comment_id): {
'deleted': deleted
}
}
def get_claim_comments(self, page=1, page_size=50, **kwargs):
return {
@ -37,22 +81,36 @@ class MockedCommentServer:
'page_size': page_size,
'total_pages': ceil(len(self.comments)/page_size),
'total_items': len(self.comments),
'items': (self.comments[::-1])[(page - 1) * page_size: page * page_size]
'items': [self.clean(c) for c in (self.comments[::-1])[(page - 1) * page_size: page * page_size]]
}
def get_comment_channel_by_id(self, comment_id: int, **kwargs):
comment = self.comments[comment_id - 1]
return {
'channel_id': comment.get('channel_id'),
'channel_name': comment.get('channel_name')
}
methods = {
'get_claim_comments': get_claim_comments,
'create_comment': create_comment,
'delete_comment': delete_comment,
'get_channel_from_comment_id': get_comment_channel_by_id,
}
def process_json(self, body) -> dict:
response = {'jsonrpc': '2.0', 'id': body['id']}
if body['method'] in self.methods:
params = body.get('params', {})
result = self.methods[body['method']](self, **params)
response['result'] = result
else:
response['error'] = self.ERRORS['INVALID_METHOD']
try:
if body['method'] in self.methods:
params = body.get('params', {})
if 'comment_id' in params and type(params['comment_id']) is str:
params['comment_id'] = int(params['comment_id'])
result = self.methods[body['method']](self, **params)
response['result'] = result
else:
response['error'] = self.ERRORS['INVALID_METHOD']
except Exception:
response['error'] = self.ERRORS['UNKNOWN']
return response
async def start(self):
@ -173,3 +231,20 @@ class CommentCommands(CommandTestCase):
is_channel_signature_valid=True
)
self.assertIs(len(signed_comment_list['items']), 28)
async def test04_comment_abandons(self):
rswanson = (await self.channel_create('@RonSwanson'))['outputs'][0]
stream = (await self.stream_create('Pawnee Town Hall of Fame by Leslie Knope'))['outputs'][0]
comment = await self.daemon.jsonrpc_comment_create(
comment='KNOPE! WHAT DID I TELL YOU ABOUT PUTTING MY INFORMATION UP LIKE THAT',
claim_id=stream['claim_id'],
channel_id=rswanson['claim_id']
)
self.assertIn('signature', comment)
deleted = await self.daemon.jsonrpc_comment_abandon(comment['comment_id'])
self.assertIn(comment['comment_id'], deleted)
self.assertTrue(deleted[comment['comment_id']]['deleted'])
deleted = await self.daemon.jsonrpc_comment_abandon(comment['comment_id'])
self.assertIn(comment['comment_id'], deleted)
self.assertFalse(deleted[comment['comment_id']]['deleted'])