lbry-sdk/tests/integration/test_comment_commands.py

106 lines
3.8 KiB
Python
Raw Normal View History

2019-04-07 20:38:34 +02:00
import asyncio
from aiohttp import web
from lbrynet.testcase import CommandTestCase
class FakedCommentServer:
2019-06-03 05:09:56 +02:00
2019-04-07 20:38:34 +02:00
ERRORS = {
'INVALID_PARAMS': {'code': -32602, 'message': 'Invalid parameters'},
'INTERNAL': {'code': -32603, 'message': 'An internal error'},
'UNKNOWN': {'code': -1, 'message': 'An unknown or very miscellaneous error'},
'INVALID_METHOD': {'code': -32604, 'message': 'The Requested method does not exist'}
2019-04-07 20:38:34 +02:00
}
def __init__(self, port=2903):
self.port = port
self.app = web.Application(debug=True)
self.app.add_routes([web.post('/api', self.api)])
self.runner = None
self.server = None
2019-06-03 05:09:56 +02:00
self.comments = []
self.comment_id = 0
2019-04-07 20:38:34 +02:00
2019-06-03 05:09:56 +02:00
def create_comment(self, **comment):
self.comment_id += 1
comment['comment_id'] = self.comment_id
self.comments.append(comment)
return comment
2019-04-07 20:38:34 +02:00
def get_claim_comments(self, page=1, page_size=50, **kwargs):
2019-06-03 05:09:56 +02:00
return self.comments[:page_size]
2019-04-07 20:38:34 +02:00
methods = {
'get_claim_comments': get_claim_comments,
'create_comment': create_comment,
2019-04-07 20:38:34 +02:00
}
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
2019-04-07 20:38:34 +02:00
else:
response['error'] = self.ERRORS['INVALID_METHOD']
2019-04-07 20:38:34 +02:00
return response
2019-06-03 05:09:56 +02:00
async def start(self):
2019-04-07 20:38:34 +02:00
self.runner = web.AppRunner(self.app)
await self.runner.setup()
self.server = web.TCPSite(self.runner, 'localhost', self.port)
await self.server.start()
2019-06-03 05:09:56 +02:00
async def stop(self):
await self.runner.shutdown()
2019-04-07 20:38:34 +02:00
await self.runner.cleanup()
async def api(self, request):
body = await request.json()
if type(body) is list or type(body) is dict:
if type(body) is list:
response = [self.process_json(part) for part in body]
else:
response = self.process_json(body)
return web.json_response(response)
else:
raise TypeError('invalid type passed')
2019-04-07 20:38:34 +02:00
class CommentCommands(CommandTestCase):
async def asyncSetUp(self):
await super().asyncSetUp()
self.daemon.conf.comment_server = 'http://localhost:2903/api'
2019-06-03 05:09:56 +02:00
self.comment_server = FakedCommentServer(2903)
await self.comment_server.start()
self.addCleanup(self.comment_server.stop)
2019-04-07 20:38:34 +02:00
async def test_comment_create(self):
2019-06-03 05:09:56 +02:00
channel = (await self.channel_create('@JimmyBuffett'))['outputs'][0]
stream = (await self.stream_create())['outputs'][0]
self.assertEqual(0, len(await self.daemon.jsonrpc_comment_list(stream['claim_id'])))
2019-04-07 20:38:34 +02:00
comment = await self.daemon.jsonrpc_comment_create(
2019-06-03 05:09:56 +02:00
claim_id=stream['claim_id'],
channel_id=channel['claim_id'],
comment="It's 5 O'Clock Somewhere"
2019-04-07 20:38:34 +02:00
)
2019-06-03 05:09:56 +02:00
comments = await self.daemon.jsonrpc_comment_list(stream['claim_id'])
self.assertEqual(1, len(comments))
self.assertEqual(comment['comment_id'], comments[0]['comment_id'])
self.assertEqual(stream['claim_id'], comments[0]['claim_id'])
channel2 = (await self.channel_create('@BuffettJimmy'))['outputs'][0]
await self.daemon.jsonrpc_comment_create(
claim_id=stream['claim_id'],
channel_name=channel2['name'],
comment='Let\'s all go to Margaritaville',
2019-06-03 05:09:56 +02:00
parent_id=comments[0]['comment_id']
2019-04-07 20:38:34 +02:00
)
2019-06-03 05:09:56 +02:00
comments = await self.daemon.jsonrpc_comment_list(stream['claim_id'])
self.assertEqual(2, len(comments))
self.assertEqual(comments[1]['channel_id'], channel2['claim_id'])
self.assertEqual(comments[0]['comment_id'], comments[1]['parent_id'])