2019-08-23 20:15:09 +02:00
|
|
|
from random import randint
|
2019-05-21 02:44:03 +02:00
|
|
|
import faker
|
2019-05-19 11:51:29 +02:00
|
|
|
from faker.providers import internet
|
|
|
|
from faker.providers import lorem
|
|
|
|
from faker.providers import misc
|
2019-05-19 08:23:20 +02:00
|
|
|
|
2020-03-31 03:59:26 +02:00
|
|
|
from src.database.models import create_comment
|
|
|
|
from src.database.models import delete_comment
|
|
|
|
from src.database.models import comment_list, get_comment, get_comments_by_id
|
|
|
|
from src.database.models import set_hidden_flag
|
2019-08-23 20:15:09 +02:00
|
|
|
from test.testcase import DatabaseTestCase
|
2019-05-21 02:44:03 +02:00
|
|
|
|
2019-05-19 11:51:29 +02:00
|
|
|
fake = faker.Faker()
|
|
|
|
fake.add_provider(internet)
|
|
|
|
fake.add_provider(lorem)
|
|
|
|
fake.add_provider(misc)
|
2019-05-19 08:23:20 +02:00
|
|
|
|
2019-05-19 11:51:29 +02:00
|
|
|
|
2019-08-04 23:48:32 +02:00
|
|
|
class TestDatabaseOperations(DatabaseTestCase):
|
2019-05-19 11:51:29 +02:00
|
|
|
def setUp(self) -> None:
|
|
|
|
super().setUp()
|
|
|
|
self.claimId = '529357c3422c6046d3fec76be2358004ba22e340'
|
|
|
|
|
2019-05-20 11:07:39 +02:00
|
|
|
def test01NamedComments(self):
|
2020-03-31 03:59:26 +02:00
|
|
|
comment = create_comment(
|
2019-05-19 08:23:20 +02:00
|
|
|
claim_id=self.claimId,
|
|
|
|
comment='This is a named comment',
|
2019-05-19 09:00:18 +02:00
|
|
|
channel_name='@username',
|
2019-05-19 08:23:20 +02:00
|
|
|
channel_id='529357c3422c6046d3fec76be2358004ba22abcd',
|
2020-03-31 03:59:26 +02:00
|
|
|
signature='22'*64,
|
2019-06-06 11:43:47 +02:00
|
|
|
signing_ts='aaa'
|
2019-05-19 08:23:20 +02:00
|
|
|
)
|
|
|
|
self.assertIsNotNone(comment)
|
2019-06-06 11:43:47 +02:00
|
|
|
self.assertNotIn('parent_in', comment)
|
2020-03-31 03:59:26 +02:00
|
|
|
|
2019-05-19 08:23:20 +02:00
|
|
|
previous_id = comment['comment_id']
|
2020-03-31 03:59:26 +02:00
|
|
|
reply = create_comment(
|
2019-05-19 08:23:20 +02:00
|
|
|
claim_id=self.claimId,
|
|
|
|
comment='This is a named response',
|
2019-05-19 09:00:18 +02:00
|
|
|
channel_name='@another_username',
|
2019-05-19 08:23:20 +02:00
|
|
|
channel_id='529357c3422c6046d3fec76be2358004ba224bcd',
|
2019-06-06 11:43:47 +02:00
|
|
|
parent_id=previous_id,
|
2020-03-31 03:59:26 +02:00
|
|
|
signature='11'*64,
|
2019-06-06 11:43:47 +02:00
|
|
|
signing_ts='aaa'
|
2019-05-19 08:23:20 +02:00
|
|
|
)
|
|
|
|
self.assertIsNotNone(reply)
|
|
|
|
self.assertEqual(reply['parent_id'], comment['comment_id'])
|
|
|
|
|
2019-05-20 11:07:39 +02:00
|
|
|
def test02AnonymousComments(self):
|
2020-02-18 21:28:46 +01:00
|
|
|
self.assertRaises(
|
2020-03-31 03:59:26 +02:00
|
|
|
ValueError,
|
|
|
|
create_comment,
|
2019-05-19 08:23:20 +02:00
|
|
|
claim_id=self.claimId,
|
2019-05-20 13:23:06 +02:00
|
|
|
comment='This is an ANONYMOUS comment'
|
2019-05-19 08:23:20 +02:00
|
|
|
)
|
|
|
|
|
2019-05-20 11:07:39 +02:00
|
|
|
def test03SignedComments(self):
|
2020-03-31 03:59:26 +02:00
|
|
|
comment = create_comment(
|
2019-05-19 08:23:20 +02:00
|
|
|
claim_id=self.claimId,
|
|
|
|
comment='I like big butts and i cannot lie',
|
2019-05-19 09:00:18 +02:00
|
|
|
channel_name='@sirmixalot',
|
2019-05-19 08:23:20 +02:00
|
|
|
channel_id='529357c3422c6046d3fec76be2358005ba22abcd',
|
2020-03-31 03:59:26 +02:00
|
|
|
signature='24'*64,
|
2019-06-06 11:43:47 +02:00
|
|
|
signing_ts='asdasd'
|
2019-05-19 08:23:20 +02:00
|
|
|
)
|
|
|
|
self.assertIsNotNone(comment)
|
2019-06-11 05:51:23 +02:00
|
|
|
self.assertIn('signing_ts', comment)
|
2020-03-31 03:59:26 +02:00
|
|
|
|
2019-05-19 08:23:20 +02:00
|
|
|
previous_id = comment['comment_id']
|
2020-03-31 03:59:26 +02:00
|
|
|
reply = create_comment(
|
2019-05-19 08:23:20 +02:00
|
|
|
claim_id=self.claimId,
|
|
|
|
comment='This is a LBRY verified response',
|
2019-05-19 09:00:18 +02:00
|
|
|
channel_name='@LBRY',
|
2019-05-19 08:23:20 +02:00
|
|
|
channel_id='529357c3422c6046d3fec76be2358001ba224bcd',
|
|
|
|
parent_id=previous_id,
|
2020-03-31 03:59:26 +02:00
|
|
|
signature='12'*64,
|
2019-06-06 11:43:47 +02:00
|
|
|
signing_ts='sfdfdfds'
|
2019-05-19 08:23:20 +02:00
|
|
|
)
|
|
|
|
self.assertIsNotNone(reply)
|
|
|
|
self.assertEqual(reply['parent_id'], comment['comment_id'])
|
2019-06-11 05:51:23 +02:00
|
|
|
self.assertIn('signing_ts', reply)
|
2019-05-19 08:23:20 +02:00
|
|
|
|
2019-05-20 11:07:39 +02:00
|
|
|
def test04UsernameVariations(self):
|
2019-06-08 00:42:17 +02:00
|
|
|
self.assertRaises(
|
2020-03-31 03:59:26 +02:00
|
|
|
ValueError,
|
|
|
|
create_comment,
|
2019-05-19 09:00:18 +02:00
|
|
|
claim_id=self.claimId,
|
|
|
|
channel_name='$#(@#$@#$',
|
|
|
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
2020-03-31 03:59:26 +02:00
|
|
|
comment='this is an invalid username',
|
|
|
|
signature='1' * 128,
|
|
|
|
signing_ts='123'
|
2019-05-19 09:00:18 +02:00
|
|
|
)
|
2020-03-31 03:59:26 +02:00
|
|
|
|
|
|
|
valid_username = create_comment(
|
2019-05-19 09:00:18 +02:00
|
|
|
claim_id=self.claimId,
|
2019-06-08 00:42:17 +02:00
|
|
|
channel_name='@' + 'a' * 255,
|
2019-05-19 09:00:18 +02:00
|
|
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
2020-03-31 03:59:26 +02:00
|
|
|
comment='this is a valid username',
|
|
|
|
signature='1'*128,
|
|
|
|
signing_ts='123'
|
2019-05-19 09:00:18 +02:00
|
|
|
)
|
2019-05-19 23:16:07 +02:00
|
|
|
self.assertIsNotNone(valid_username)
|
|
|
|
|
2019-06-08 00:42:17 +02:00
|
|
|
self.assertRaises(
|
2020-03-31 03:59:26 +02:00
|
|
|
ValueError,
|
|
|
|
create_comment,
|
|
|
|
claim_id=self.claimId,
|
|
|
|
channel_name='@' + 'a' * 256,
|
|
|
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
|
|
|
comment='this username is too long',
|
|
|
|
signature='2' * 128,
|
|
|
|
signing_ts='123'
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError,
|
|
|
|
create_comment,
|
2019-05-19 09:00:18 +02:00
|
|
|
claim_id=self.claimId,
|
|
|
|
channel_name='',
|
|
|
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
2020-03-31 03:59:26 +02:00
|
|
|
comment='this username should not default to ANONYMOUS',
|
|
|
|
signature='3' * 128,
|
|
|
|
signing_ts='123'
|
2019-05-19 09:00:18 +02:00
|
|
|
)
|
2020-03-31 03:59:26 +02:00
|
|
|
|
2019-06-08 00:42:17 +02:00
|
|
|
self.assertRaises(
|
2020-03-31 03:59:26 +02:00
|
|
|
ValueError,
|
|
|
|
create_comment,
|
2019-05-19 09:00:18 +02:00
|
|
|
claim_id=self.claimId,
|
|
|
|
channel_name='@',
|
|
|
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
2020-03-31 03:59:26 +02:00
|
|
|
comment='this username is too short',
|
|
|
|
signature='3' * 128,
|
|
|
|
signing_ts='123'
|
2019-05-19 09:00:18 +02:00
|
|
|
)
|
|
|
|
|
2020-02-18 21:28:59 +01:00
|
|
|
def test05HideComments(self):
|
2020-03-31 03:59:26 +02:00
|
|
|
comm = create_comment(
|
|
|
|
comment='Comment #1',
|
|
|
|
claim_id=self.claimId,
|
|
|
|
channel_id='1'*40,
|
|
|
|
channel_name='@Doge123',
|
|
|
|
signature='a'*128,
|
|
|
|
signing_ts='123'
|
|
|
|
)
|
|
|
|
comment = get_comment(comm['comment_id'])
|
2019-08-04 23:48:32 +02:00
|
|
|
self.assertFalse(comment['is_hidden'])
|
2020-03-31 03:59:26 +02:00
|
|
|
|
|
|
|
success = set_hidden_flag([comm['comment_id']])
|
2019-08-04 23:48:32 +02:00
|
|
|
self.assertTrue(success)
|
2020-03-31 03:59:26 +02:00
|
|
|
|
|
|
|
comment = get_comment(comm['comment_id'])
|
2019-08-04 23:48:32 +02:00
|
|
|
self.assertTrue(comment['is_hidden'])
|
2020-03-31 03:59:26 +02:00
|
|
|
|
|
|
|
success = set_hidden_flag([comm['comment_id']])
|
2019-08-04 23:48:32 +02:00
|
|
|
self.assertTrue(success)
|
2020-03-31 03:59:26 +02:00
|
|
|
|
|
|
|
comment = get_comment(comm['comment_id'])
|
2019-08-04 23:48:32 +02:00
|
|
|
self.assertTrue(comment['is_hidden'])
|
|
|
|
|
2020-02-18 21:28:59 +01:00
|
|
|
def test06DeleteComments(self):
|
2020-03-31 03:59:26 +02:00
|
|
|
# make sure that the comment was created
|
|
|
|
comm = create_comment(
|
|
|
|
comment='Comment #1',
|
|
|
|
claim_id=self.claimId,
|
|
|
|
channel_id='1'*40,
|
|
|
|
channel_name='@Doge123',
|
|
|
|
signature='a'*128,
|
|
|
|
signing_ts='123'
|
|
|
|
)
|
|
|
|
comments = comment_list(self.claimId)
|
|
|
|
match = [x for x in comments['items'] if x['comment_id'] == comm['comment_id']]
|
|
|
|
self.assertTrue(len(match) > 0)
|
|
|
|
|
|
|
|
deleted = delete_comment(comm['comment_id'])
|
2019-08-04 23:48:32 +02:00
|
|
|
self.assertTrue(deleted)
|
2020-03-31 03:59:26 +02:00
|
|
|
|
|
|
|
# make sure that we can't find the comment here
|
|
|
|
comments = comment_list(self.claimId)
|
|
|
|
match = [x for x in comments['items'] if x['comment_id'] == comm['comment_id']]
|
2019-08-09 07:20:00 +02:00
|
|
|
self.assertFalse(match)
|
2020-03-31 03:59:26 +02:00
|
|
|
self.assertRaises(
|
|
|
|
ValueError,
|
|
|
|
delete_comment,
|
|
|
|
comment_id=comm['comment_id'],
|
|
|
|
)
|
2019-08-04 23:48:32 +02:00
|
|
|
|
|
|
|
|
2019-05-20 07:18:56 +02:00
|
|
|
class ListDatabaseTest(DatabaseTestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
super().setUp()
|
|
|
|
top_coms, self.claim_ids = generate_top_comments(5, 75)
|
|
|
|
|
|
|
|
def testLists(self):
|
|
|
|
for claim_id in self.claim_ids:
|
|
|
|
with self.subTest(claim_id=claim_id):
|
2020-03-31 03:59:26 +02:00
|
|
|
comments = comment_list(claim_id)
|
2019-05-20 07:18:56 +02:00
|
|
|
self.assertIsNotNone(comments)
|
2019-05-29 23:12:41 +02:00
|
|
|
self.assertGreater(comments['page_size'], 0)
|
2019-08-04 23:48:32 +02:00
|
|
|
self.assertIn('has_hidden_comments', comments)
|
|
|
|
self.assertFalse(comments['has_hidden_comments'])
|
2020-03-31 03:59:26 +02:00
|
|
|
top_comments = comment_list(claim_id, top_level=True, page=1, page_size=50)
|
2019-05-20 07:18:56 +02:00
|
|
|
self.assertIsNotNone(top_comments)
|
2019-05-29 23:12:41 +02:00
|
|
|
self.assertEqual(top_comments['page_size'], 50)
|
|
|
|
self.assertEqual(top_comments['page'], 1)
|
|
|
|
self.assertGreaterEqual(top_comments['total_pages'], 0)
|
|
|
|
self.assertGreaterEqual(top_comments['total_items'], 0)
|
2020-03-31 03:59:26 +02:00
|
|
|
comment_ids = comment_list(claim_id, page_size=50, page=1)
|
2019-05-20 07:18:56 +02:00
|
|
|
with self.subTest(comment_ids=comment_ids):
|
|
|
|
self.assertIsNotNone(comment_ids)
|
|
|
|
self.assertLessEqual(len(comment_ids), 50)
|
2020-03-31 03:59:26 +02:00
|
|
|
matching_comments = (comment_ids)
|
2019-05-20 07:18:56 +02:00
|
|
|
self.assertIsNotNone(matching_comments)
|
|
|
|
self.assertEqual(len(matching_comments), len(comment_ids))
|
|
|
|
|
2019-08-04 23:48:32 +02:00
|
|
|
def testHiddenCommentLists(self):
|
|
|
|
claim_id = 'a'*40
|
2020-03-31 03:59:26 +02:00
|
|
|
comm1 = create_comment(
|
|
|
|
'Comment #1',
|
|
|
|
claim_id,
|
|
|
|
channel_id='1'*40,
|
|
|
|
channel_name='@Doge123',
|
|
|
|
signature='a'*128,
|
|
|
|
signing_ts='123'
|
|
|
|
)
|
|
|
|
comm2 = create_comment(
|
|
|
|
'Comment #2', claim_id,
|
|
|
|
channel_id='1'*40,
|
|
|
|
channel_name='@Doge123',
|
|
|
|
signature='b'*128,
|
|
|
|
signing_ts='123'
|
|
|
|
)
|
|
|
|
comm3 = create_comment(
|
|
|
|
'Comment #3', claim_id,
|
|
|
|
channel_id='1'*40,
|
|
|
|
channel_name='@Doge123',
|
|
|
|
signature='c'*128,
|
|
|
|
signing_ts='123'
|
|
|
|
)
|
2019-08-04 23:48:32 +02:00
|
|
|
comments = [comm1, comm2, comm3]
|
|
|
|
|
2020-03-31 03:59:26 +02:00
|
|
|
listed_comments = comment_list(claim_id)
|
|
|
|
self.assertEqual(len(comments), listed_comments['total_items'])
|
|
|
|
self.assertFalse(listed_comments['has_hidden_comments'])
|
2019-08-04 23:48:32 +02:00
|
|
|
|
2020-03-31 03:59:26 +02:00
|
|
|
set_hidden_flag([comm2['comment_id']])
|
|
|
|
hidden = comment_list(claim_id, exclude_mode='hidden')
|
2019-08-04 23:48:32 +02:00
|
|
|
|
2020-03-31 03:59:26 +02:00
|
|
|
self.assertTrue(hidden['has_hidden_comments'])
|
|
|
|
self.assertGreater(len(hidden['items']), 0)
|
2019-08-04 23:48:32 +02:00
|
|
|
|
2020-03-31 03:59:26 +02:00
|
|
|
visible = comment_list(claim_id, exclude_mode='visible')
|
|
|
|
self.assertFalse(visible['has_hidden_comments'])
|
|
|
|
self.assertNotEqual(listed_comments['items'], visible['items'])
|
2019-08-04 23:48:32 +02:00
|
|
|
|
2020-03-31 03:59:26 +02:00
|
|
|
# make sure the hidden comment is the one we marked as hidden
|
|
|
|
hidden_comment = hidden['items'][0]
|
|
|
|
self.assertEqual(hidden_comment['comment_id'], comm2['comment_id'])
|
2019-08-04 23:48:32 +02:00
|
|
|
|
2020-03-31 03:59:26 +02:00
|
|
|
hidden_ids = [c['comment_id'] for c in hidden['items']]
|
|
|
|
visible_ids = [c['comment_id'] for c in visible['items']]
|
2019-08-04 23:48:32 +02:00
|
|
|
composite_ids = hidden_ids + visible_ids
|
2020-03-31 03:59:26 +02:00
|
|
|
listed_comments = comment_list(claim_id)
|
|
|
|
all_ids = [c['comment_id'] for c in listed_comments['items']]
|
2019-08-04 23:48:32 +02:00
|
|
|
composite_ids.sort()
|
|
|
|
all_ids.sort()
|
|
|
|
self.assertEqual(composite_ids, all_ids)
|
|
|
|
|
2019-05-20 07:18:56 +02:00
|
|
|
|
2019-06-06 11:43:47 +02:00
|
|
|
def generate_top_comments(ncid=15, ncomm=100, minchar=50, maxchar=500):
|
|
|
|
claim_ids = [fake.sha1() for _ in range(ncid)]
|
|
|
|
top_comments = {
|
|
|
|
cid: [{
|
|
|
|
'claim_id': cid,
|
|
|
|
'comment': ''.join(fake.text(max_nb_chars=randint(minchar, maxchar))),
|
|
|
|
'channel_name': '@' + fake.user_name(),
|
|
|
|
'channel_id': fake.sha1(),
|
|
|
|
'signature': fake.uuid4(),
|
|
|
|
'signing_ts': fake.uuid4()
|
|
|
|
} for _ in range(ncomm)]
|
|
|
|
for cid in claim_ids
|
|
|
|
}
|
|
|
|
return top_comments, claim_ids
|
|
|
|
|
|
|
|
|
2019-05-19 11:51:29 +02:00
|
|
|
def generate_replies(top_comments):
|
2019-05-19 23:16:07 +02:00
|
|
|
return [{
|
|
|
|
'claim_id': comment['claim_id'],
|
|
|
|
'parent_id': comment['comment_id'],
|
|
|
|
'comment': ' '.join(fake.text(max_nb_chars=randint(50, 500))),
|
|
|
|
'channel_name': '@' + fake.user_name(),
|
|
|
|
'channel_id': fake.sha1(),
|
2019-06-06 11:43:47 +02:00
|
|
|
'signature': fake.uuid4(),
|
|
|
|
'signing_ts': fake.uuid4()
|
2019-05-19 23:16:07 +02:00
|
|
|
}
|
|
|
|
for claim, comments in top_comments.items()
|
|
|
|
for i, comment in enumerate(comments)
|
|
|
|
if comment # ensures comment is non-null
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def generate_replies_random(top_comments):
|
2019-05-19 11:51:29 +02:00
|
|
|
return [{
|
|
|
|
'claim_id': comment['claim_id'],
|
|
|
|
'parent_id': comment['comment_id'],
|
|
|
|
'comment': ' '.join(fake.text(max_nb_chars=randint(50, 2500))),
|
|
|
|
'channel_name': '@' + fake.user_name(),
|
2019-05-19 23:16:07 +02:00
|
|
|
'channel_id': fake.sha1() if hash(comment['comment_id']) % 5 == 0 else '',
|
2019-05-19 11:51:29 +02:00
|
|
|
'signature': fake.uuid4() if hash(comment['comment_id']) % 11 == 0 else None
|
|
|
|
}
|
|
|
|
for claim, comments in top_comments.items()
|
|
|
|
for i, comment in enumerate(comments)
|
|
|
|
if comment
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-05-19 23:16:07 +02:00
|
|
|
def generate_top_comments_random():
|
2019-05-19 11:51:29 +02:00
|
|
|
claim_ids = [fake.sha1() for _ in range(15)]
|
|
|
|
top_comments = {
|
|
|
|
cid: [{
|
|
|
|
'claim_id': cid,
|
|
|
|
'comment': ''.join(fake.text(max_nb_chars=randint(50, 2500))),
|
2019-05-19 23:16:07 +02:00
|
|
|
'channel_name': '@' + fake.user_name() if (hash(cid) * i) % 7 > 0 else '',
|
|
|
|
'channel_id': fake.sha1() if (hash(cid) * i) % 7 > 0 else '',
|
|
|
|
'signature': fake.uuid4() if (hash(cid) * i) % 7 > 0 > hash(cid) else None
|
|
|
|
} for i in range(randint(60, 200))]
|
2019-05-19 11:51:29 +02:00
|
|
|
for cid in claim_ids
|
|
|
|
}
|
2019-05-19 23:16:07 +02:00
|
|
|
return top_comments, claim_ids
|