2019-07-30 06:22:24 +02:00
|
|
|
import os
|
2019-12-30 23:26:37 +01:00
|
|
|
import random
|
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
import aiohttp
|
2019-05-23 12:36:03 +02:00
|
|
|
from itertools import *
|
2019-07-22 14:13:10 +02:00
|
|
|
|
|
|
|
import faker
|
2019-05-23 12:36:03 +02:00
|
|
|
from faker.providers import internet
|
|
|
|
from faker.providers import lorem
|
|
|
|
from faker.providers import misc
|
|
|
|
|
2020-03-27 06:26:13 +01:00
|
|
|
from src.main import get_config, CONFIG_FILE
|
2019-07-30 06:22:24 +02:00
|
|
|
from src.server import app
|
2019-12-30 23:26:37 +01:00
|
|
|
from src.server.validation import is_valid_base_comment
|
|
|
|
|
2019-08-23 20:15:09 +02:00
|
|
|
from test.testcase import AsyncioTestCase
|
2019-07-30 06:22:24 +02:00
|
|
|
|
2019-05-23 12:36:03 +02:00
|
|
|
|
2020-03-27 06:26:13 +01:00
|
|
|
config = get_config(CONFIG_FILE)
|
|
|
|
|
|
|
|
|
2019-09-07 02:27:14 +02:00
|
|
|
if 'slack_webhook' in config:
|
|
|
|
config.pop('slack_webhook')
|
|
|
|
|
|
|
|
|
2019-05-23 12:36:03 +02:00
|
|
|
fake = faker.Faker()
|
|
|
|
fake.add_provider(internet)
|
|
|
|
fake.add_provider(lorem)
|
|
|
|
fake.add_provider(misc)
|
|
|
|
|
2019-07-22 14:13:10 +02:00
|
|
|
|
2019-12-30 23:28:40 +01:00
|
|
|
def fake_lbryusername() -> str:
|
2019-07-22 02:09:22 +02:00
|
|
|
return '@' + fake.user_name()
|
|
|
|
|
|
|
|
|
2019-12-30 23:28:40 +01:00
|
|
|
def nothing():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def fake_signature() -> str:
|
|
|
|
return fake.sha256() + fake.sha256()
|
|
|
|
|
|
|
|
|
|
|
|
def fake_signing_ts() -> str:
|
|
|
|
return str(random.randint(1, 2**32 - 1))
|
|
|
|
|
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
async def jsonrpc_post(url, method, **params):
|
2019-07-22 02:09:22 +02:00
|
|
|
json_body = {
|
|
|
|
'jsonrpc': '2.0',
|
|
|
|
'id': None,
|
|
|
|
'method': method,
|
|
|
|
'params': params
|
|
|
|
}
|
2019-07-30 06:22:24 +02:00
|
|
|
async with aiohttp.request('POST', url, json=json_body) as request:
|
|
|
|
return await request.json()
|
2019-07-22 02:09:22 +02:00
|
|
|
|
|
|
|
|
2019-07-24 08:11:30 +02:00
|
|
|
replace = {
|
|
|
|
'claim_id': fake.sha1,
|
|
|
|
'comment': fake.text,
|
|
|
|
'channel_id': fake.sha1,
|
|
|
|
'channel_name': fake_lbryusername,
|
2019-12-30 23:28:40 +01:00
|
|
|
'signature': fake_signature,
|
|
|
|
'signing_ts': fake_signing_ts,
|
|
|
|
'parent_id': fake.sha256,
|
2019-07-24 08:11:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def create_test_comments(values: iter, **default):
|
|
|
|
vars_combo = chain.from_iterable(combinations(values, r) for r in range(1, len(values) + 1))
|
|
|
|
return [{k: replace[k]() if k in comb else v for k, v in default.items()}
|
|
|
|
for comb in vars_combo]
|
|
|
|
|
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
class ServerTest(AsyncioTestCase):
|
|
|
|
db_file = 'test.db'
|
|
|
|
|
2019-05-23 12:36:03 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-08-24 05:51:39 +02:00
|
|
|
self.host = 'localhost'
|
|
|
|
self.port = 5931
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return f'http://{self.host}:{self.port}/api'
|
2019-05-23 12:36:03 +02:00
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls) -> None:
|
|
|
|
print('exit reached')
|
|
|
|
os.remove(cls.db_file)
|
|
|
|
|
|
|
|
async def asyncSetUp(self):
|
|
|
|
await super().asyncSetUp()
|
|
|
|
self.server = app.CommentDaemon(config, db_file=self.db_file)
|
2019-08-24 05:51:39 +02:00
|
|
|
await self.server.start(host=self.host, port=self.port)
|
2019-07-30 06:22:24 +02:00
|
|
|
self.addCleanup(self.server.stop)
|
|
|
|
|
|
|
|
async def post_comment(self, **params):
|
|
|
|
return await jsonrpc_post(self.url, 'create_comment', **params)
|
2019-05-23 12:36:03 +02:00
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
async def test01CreateCommentNoReply(self):
|
2019-05-23 12:36:03 +02:00
|
|
|
anonymous_test = create_test_comments(
|
|
|
|
('claim_id', 'channel_id', 'channel_name', 'comment'),
|
|
|
|
comment=None,
|
|
|
|
channel_name=None,
|
|
|
|
channel_id=None,
|
|
|
|
claim_id=None
|
|
|
|
)
|
|
|
|
for test in anonymous_test:
|
2020-02-18 21:27:29 +01:00
|
|
|
with self.subTest(test='null fields: ' + ', '.join(k for k, v in test.items() if not v)):
|
2019-07-30 06:22:24 +02:00
|
|
|
message = await self.post_comment(**test)
|
2019-07-22 14:13:10 +02:00
|
|
|
self.assertTrue('result' in message or 'error' in message)
|
|
|
|
if 'error' in message:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertFalse(is_valid_base_comment(**test))
|
2019-05-23 12:36:03 +02:00
|
|
|
else:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertTrue(is_valid_base_comment(**test))
|
2019-05-23 12:36:03 +02:00
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
async def test02CreateNamedCommentsNoReply(self):
|
2019-05-23 12:36:03 +02:00
|
|
|
named_test = create_test_comments(
|
|
|
|
('channel_name', 'channel_id', 'signature'),
|
|
|
|
claim_id='1234567890123456789012345678901234567890',
|
|
|
|
channel_name='@JamieFoxx',
|
|
|
|
channel_id='1234567890'*4,
|
|
|
|
comment='blah blah blahbity blah',
|
|
|
|
signature=None
|
|
|
|
)
|
|
|
|
for test in named_test:
|
|
|
|
with self.subTest(test=test):
|
2019-07-30 06:22:24 +02:00
|
|
|
message = await self.post_comment(**test)
|
2019-07-22 14:13:10 +02:00
|
|
|
self.assertTrue('result' in message or 'error' in message)
|
|
|
|
if 'error' in message:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertFalse(is_valid_base_comment(**test))
|
2019-05-23 12:36:03 +02:00
|
|
|
else:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertTrue(is_valid_base_comment(**test))
|
2019-05-23 12:36:03 +02:00
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
async def test03CreateAllTestComments(self):
|
2019-05-23 12:36:03 +02:00
|
|
|
test_all = create_test_comments(replace.keys(), **{
|
|
|
|
k: None for k in replace.keys()
|
|
|
|
})
|
|
|
|
for test in test_all:
|
|
|
|
with self.subTest(test=test):
|
2019-07-30 06:22:24 +02:00
|
|
|
message = await self.post_comment(**test)
|
2019-07-22 14:13:10 +02:00
|
|
|
self.assertTrue('result' in message or 'error' in message)
|
|
|
|
if 'error' in message:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertFalse(is_valid_base_comment(**test))
|
2019-05-23 12:36:03 +02:00
|
|
|
else:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertTrue(is_valid_base_comment(**test))
|
2019-05-23 12:36:03 +02:00
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
async def test04CreateAllReplies(self):
|
2019-05-23 12:36:03 +02:00
|
|
|
claim_id = '1d8a5cc39ca02e55782d619e67131c0a20843be8'
|
2019-07-30 06:22:24 +02:00
|
|
|
parent_comment = await self.post_comment(
|
2019-05-23 12:36:03 +02:00
|
|
|
channel_name='@KevinWalterRabie',
|
|
|
|
channel_id=fake.sha1(),
|
|
|
|
comment='Hello everybody and welcome back to my chan nel',
|
|
|
|
claim_id=claim_id,
|
2019-12-30 23:28:40 +01:00
|
|
|
signing_ts='1234',
|
|
|
|
signature='_'*128
|
2019-05-23 12:36:03 +02:00
|
|
|
)
|
2019-07-30 06:22:24 +02:00
|
|
|
parent_id = parent_comment['result']['comment_id']
|
2019-05-23 12:36:03 +02:00
|
|
|
test_all = create_test_comments(
|
|
|
|
('comment', 'channel_name', 'channel_id', 'signature', 'parent_id'),
|
|
|
|
parent_id=parent_id,
|
|
|
|
comment=None,
|
|
|
|
channel_name=None,
|
|
|
|
channel_id=None,
|
|
|
|
signature=None,
|
|
|
|
claim_id=claim_id
|
|
|
|
)
|
|
|
|
for test in test_all:
|
2019-07-22 14:13:10 +02:00
|
|
|
with self.subTest(test=test):
|
2019-05-23 12:36:03 +02:00
|
|
|
if test['parent_id'] != parent_id:
|
|
|
|
continue
|
|
|
|
else:
|
2019-07-30 06:22:24 +02:00
|
|
|
message = await self.post_comment(**test)
|
2019-07-22 14:13:10 +02:00
|
|
|
self.assertTrue('result' in message or 'error' in message)
|
|
|
|
if 'error' in message:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertFalse(is_valid_base_comment(**test))
|
2019-05-23 12:36:03 +02:00
|
|
|
else:
|
2020-02-18 21:27:29 +01:00
|
|
|
self.assertTrue(is_valid_base_comment(**test))
|
|
|
|
|
|
|
|
async def testSlackWebhook(self):
|
|
|
|
claim_id = '1d8a5cc39ca02e55782d619e67131c0a20843be8'
|
|
|
|
channel_name = '@name'
|
|
|
|
channel_id = fake.sha1()
|
|
|
|
signature = '{}'*64
|
|
|
|
signing_ts = '1234'
|
|
|
|
|
|
|
|
base = await self.post_comment(
|
|
|
|
channel_name=channel_name,
|
|
|
|
channel_id=channel_id,
|
|
|
|
comment='duplicate',
|
|
|
|
claim_id=claim_id,
|
|
|
|
signing_ts=signing_ts,
|
|
|
|
signature=signature
|
|
|
|
)
|
|
|
|
|
|
|
|
comment_id = base['result']['comment_id']
|
|
|
|
|
|
|
|
with self.subTest(test=comment_id):
|
|
|
|
await self.post_comment(
|
|
|
|
channel_name=channel_name,
|
|
|
|
channel_id=channel_id,
|
|
|
|
comment='duplicate',
|
|
|
|
claim_id=claim_id,
|
|
|
|
signing_ts=signing_ts,
|
|
|
|
signature=signature
|
|
|
|
)
|
2019-05-23 12:36:03 +02:00
|
|
|
|
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
class ListCommentsTest(AsyncioTestCase):
|
2019-05-23 12:36:03 +02:00
|
|
|
replace = {
|
|
|
|
'claim_id': fake.sha1,
|
|
|
|
'comment': fake.text,
|
|
|
|
'channel_id': fake.sha1,
|
|
|
|
'channel_name': fake_lbryusername,
|
2019-12-30 23:28:40 +01:00
|
|
|
'signature': fake_signature,
|
|
|
|
'signing_ts': fake_signing_ts,
|
2019-05-23 12:36:03 +02:00
|
|
|
'parent_id': nothing
|
|
|
|
}
|
|
|
|
|
2019-08-24 05:51:39 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.host = 'localhost'
|
|
|
|
self.port = 5931
|
|
|
|
self.db_file = 'list_test.db'
|
|
|
|
self.claim_id = '1d8a5cc39ca02e55782d619e67131c0a20843be8'
|
|
|
|
self.comment_ids = None
|
2019-05-23 12:36:03 +02:00
|
|
|
|
2019-08-24 05:51:39 +02:00
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return f'http://{self.host}:{self.port}/api'
|
|
|
|
|
|
|
|
async def post_comment(self, **params):
|
|
|
|
return await jsonrpc_post(self.url, 'create_comment', **params)
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
2019-07-30 06:22:24 +02:00
|
|
|
print('exit reached')
|
2019-08-24 05:51:39 +02:00
|
|
|
os.remove(self.db_file)
|
2019-07-30 06:22:24 +02:00
|
|
|
|
2019-10-16 23:00:21 +02:00
|
|
|
async def create_lots_of_comments(self, n=23):
|
|
|
|
self.comment_list = [{key: self.replace[key]() for key in self.replace.keys()} for _ in range(23)]
|
|
|
|
for comment in self.comment_list:
|
|
|
|
comment['claim_id'] = self.claim_id
|
|
|
|
self.comment_ids = [(await self.post_comment(**comm))['result']['comment_id']
|
|
|
|
for comm in self.comment_list]
|
|
|
|
|
2019-07-30 06:22:24 +02:00
|
|
|
async def asyncSetUp(self):
|
|
|
|
await super().asyncSetUp()
|
|
|
|
self.server = app.CommentDaemon(config, db_file=self.db_file)
|
2019-08-24 05:51:39 +02:00
|
|
|
await self.server.start(self.host, self.port)
|
2019-07-30 06:22:24 +02:00
|
|
|
self.addCleanup(self.server.stop)
|
|
|
|
|
|
|
|
async def testListComments(self):
|
2019-10-16 23:00:21 +02:00
|
|
|
await self.create_lots_of_comments()
|
2019-08-24 05:51:39 +02:00
|
|
|
response_one = await jsonrpc_post(
|
|
|
|
self.url, 'get_claim_comments', page_size=20, page=1, top_level=1, claim_id=self.claim_id
|
|
|
|
)
|
2019-05-23 12:36:03 +02:00
|
|
|
self.assertIsNotNone(response_one)
|
|
|
|
self.assertIn('result', response_one)
|
2019-05-30 00:01:48 +02:00
|
|
|
response_one: dict = response_one['result']
|
2019-10-08 22:11:54 +02:00
|
|
|
self.assertEqual(response_one['page_size'], len(response_one['items']))
|
2019-05-30 00:01:48 +02:00
|
|
|
self.assertIn('items', response_one)
|
2019-10-16 23:00:21 +02:00
|
|
|
|
|
|
|
comments = response_one['items']
|
|
|
|
hidden = list(filter(lambda c: c['is_hidden'], comments))
|
|
|
|
self.assertEqual(hidden, [])
|
|
|
|
|
2019-07-22 02:09:22 +02:00
|
|
|
self.assertGreaterEqual(response_one['total_pages'], response_one['page'])
|
|
|
|
last_page = response_one['total_pages']
|
2019-07-30 06:22:24 +02:00
|
|
|
response = await jsonrpc_post(self.url, 'get_claim_comments', page_size=20,
|
|
|
|
page=last_page, top_level=1, claim_id=self.claim_id)
|
2019-05-23 12:36:03 +02:00
|
|
|
self.assertIsNotNone(response)
|
|
|
|
self.assertIn('result', response)
|
2019-05-30 00:01:48 +02:00
|
|
|
response: dict = response['result']
|
2019-07-22 02:09:22 +02:00
|
|
|
self.assertIs(type(response['items']), list)
|
2019-05-30 00:01:48 +02:00
|
|
|
self.assertEqual(response['total_items'], response_one['total_items'])
|
2019-07-22 02:09:22 +02:00
|
|
|
self.assertEqual(response['total_pages'], response_one['total_pages'])
|