Updates server test for existing functions

This commit is contained in:
Oleg Silkin 2019-07-22 08:13:10 -04:00
parent add109e51c
commit 8af6a9eb07
2 changed files with 50 additions and 72 deletions

View file

@ -38,9 +38,7 @@ CREATE TABLE IF NOT EXISTS CHANNEL(
-- DROP INDEX IF EXISTS COMMENT_CLAIM_INDEX; -- DROP INDEX IF EXISTS COMMENT_CLAIM_INDEX;
CREATE INDEX IF NOT EXISTS CLAIM_COMMENT_INDEX ON COMMENT (LbryClaimId, CommentId); CREATE INDEX IF NOT EXISTS CLAIM_COMMENT_INDEX ON COMMENT (LbryClaimId, CommentId);
DROP INDEX CHANNEL_COMMENT_INDEX; CREATE INDEX IF NOT EXISTS CHANNEL_COMMENT_INDEX ON COMMENT(ChannelId, CommentId);
CREATE INDEX IF NOT EXISTS CHANNEL_COMMENT_INDEX ON COMMENT(ChannelId, CommentId)
;
-- VIEWS -- VIEWS
DROP VIEW IF EXISTS COMMENTS_ON_CLAIMS; DROP VIEW IF EXISTS COMMENTS_ON_CLAIMS;

View file

@ -1,10 +1,9 @@
import unittest import unittest
from random import randint
import requests import requests
import re import re
import faker
from itertools import * from itertools import *
import faker
from faker.providers import internet from faker.providers import internet
from faker.providers import lorem from faker.providers import lorem
from faker.providers import misc from faker.providers import misc
@ -16,6 +15,7 @@ fake.add_provider(internet)
fake.add_provider(lorem) fake.add_provider(lorem)
fake.add_provider(misc) fake.add_provider(misc)
def fake_lbryusername(): def fake_lbryusername():
return '@' + fake.user_name() return '@' + fake.user_name()
@ -31,7 +31,7 @@ def jsonrpc_post(url, method, **params):
def nothing(): def nothing():
return None pass
class ServerTest(unittest.TestCase): class ServerTest(unittest.TestCase):
@ -39,7 +39,6 @@ class ServerTest(unittest.TestCase):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.url = 'http://' + config['HOST'] + ':5921/api' self.url = 'http://' + config['HOST'] + ':5921/api'
def post_comment(self, **params): def post_comment(self, **params):
json_body = { json_body = {
'jsonrpc': '2.0', 'jsonrpc': '2.0',
@ -49,42 +48,40 @@ class ServerTest(unittest.TestCase):
} }
return requests.post(url=self.url, json=json_body) return requests.post(url=self.url, json=json_body)
def assertIsValidMessageTest(self, message, test): def is_valid_message(self, comment=None, claim_id=None, parent_id=None,
self.assertIsNotNone(message) channel_name=None, channel_id=None, signature=None, signing_ts=None):
try: try:
if not test['claim_id'] or \ assert comment is not None and claim_id is not None
(bool(test['channel_id']) ^ bool(test['channel_name'])): assert re.fullmatch('([a-f0-9]|[A-F0-9]){40}', claim_id)
self.assertIn('error', message) assert 0 < len(comment) <= 2000
self.assertNotIn('result', message) if parent_id is not None:
else: assert re.fullmatch('([a-f0-9]){64}', parent_id)
self.assertNotIn('error', message)
self.assertIn('result', message)
self.assertIn('comment_id', message['result'])
self.assertEquals(message['result']['claim_id'], test['claim_id'])
except AssertionError:
raise requests.HTTPError(message.text)
def isValidMessage(self, message: dict): if channel_name or channel_id or signature or signing_ts:
return message and type(message) is dict and ('error' in message or 'result' in message) assert channel_id is not None and channel_name is not None
assert re.fullmatch('([a-f0-9]|[A-F0-9]){40}', channel_id)
def isValidTest(self, test: dict): assert self.valid_channel_name(channel_name)
cond = test['claim_id'] and test['comment'] and not \ assert (signature is None and signing_ts is None) or \
(bool(test['channel_id']) ^ bool(test['channel_name'])) (signature is not None and signing_ts is not None)
if cond: if signature:
cond = (0 < len(test['comment']) <= 2000) and cond assert len(signature) == 128
if test['channel_id']: if parent_id:
cond = (1 < len(test['channel_name']) <= 256) and cond assert parent_id.isalnum()
channel_match = re.fullmatch( except Exception:
'^@(?:(?![\x00-\x08\x0b\x0c\x0e-\x1f\x23-\x26' return False
'\x2f\x3a\x3d\x3f-\x40\uFFFE-\U0000FFFF]).){1,255}$', return True
test['channel_name']
)
cond = cond and channel_match
return cond
def setUp(self) -> None: def setUp(self) -> None:
self.reply_id = 'ace7800f36e55c74c4aa6a698f97a7ee5f1ccb047b5a0730960df90e58c41dc2' self.reply_id = 'ace7800f36e55c74c4aa6a698f97a7ee5f1ccb047b5a0730960df90e58c41dc2'
@staticmethod
def valid_channel_name(channel_name):
return re.fullmatch(
'^@(?:(?![\x00-\x08\x0b\x0c\x0e-\x1f\x23-\x26'
'\x2f\x3a\x3d\x3f-\x40\uFFFE-\U0000FFFF]).){1,255}$',
channel_name
)
def test01CreateCommentNoReply(self): def test01CreateCommentNoReply(self):
anonymous_test = create_test_comments( anonymous_test = create_test_comments(
('claim_id', 'channel_id', 'channel_name', 'comment'), ('claim_id', 'channel_id', 'channel_name', 'comment'),
@ -97,12 +94,11 @@ class ServerTest(unittest.TestCase):
with self.subTest(test=test): with self.subTest(test=test):
message = self.post_comment(**test) message = self.post_comment(**test)
message = message.json() message = message.json()
if self.isValidTest(test): self.assertTrue('result' in message or 'error' in message)
self.assertIn('result', message) if 'error' in message:
self.assertIsNotNone(message['result']) self.assertFalse(self.is_valid_message(**test))
self.assertIn('comment_id', message['result'])
else: else:
self.assertIn('error', message) self.assertTrue(self.is_valid_message(**test))
def test02CreateNamedCommentsNoReply(self): def test02CreateNamedCommentsNoReply(self):
named_test = create_test_comments( named_test = create_test_comments(
@ -117,10 +113,11 @@ class ServerTest(unittest.TestCase):
with self.subTest(test=test): with self.subTest(test=test):
message = self.post_comment(**test) message = self.post_comment(**test)
message = message.json() message = message.json()
if self.isValidTest(test): self.assertTrue('result' in message or 'error' in message)
self.assertTrue(self.isValidMessage(message)) if 'error' in message:
self.assertFalse(self.is_valid_message(**test))
else: else:
self.assertFalse(self.isValidMessage(message)) self.assertTrue(self.is_valid_message(**test))
def test03CreateAllTestComments(self): def test03CreateAllTestComments(self):
test_all = create_test_comments(replace.keys(), **{ test_all = create_test_comments(replace.keys(), **{
@ -130,13 +127,11 @@ class ServerTest(unittest.TestCase):
with self.subTest(test=test): with self.subTest(test=test):
message = self.post_comment(**test) message = self.post_comment(**test)
message = message.json() message = message.json()
if self.isValidTest(test): self.assertTrue('result' in message or 'error' in message)
self.assertTrue(self.isValidMessage(message)) if 'error' in message:
self.assertNotIn('error', message) self.assertFalse(self.is_valid_message(**test))
self.assertIsNotNone(message['result'])
else: else:
self.assertIsNotNone(message) self.assertTrue(self.is_valid_message(**test))
self.assertIn('error', message)
def test04CreateAllReplies(self): def test04CreateAllReplies(self):
claim_id = '1d8a5cc39ca02e55782d619e67131c0a20843be8' claim_id = '1d8a5cc39ca02e55782d619e67131c0a20843be8'
@ -157,22 +152,17 @@ class ServerTest(unittest.TestCase):
claim_id=claim_id claim_id=claim_id
) )
for test in test_all: for test in test_all:
with self.subTest(test=test) as subtest: with self.subTest(test=test):
if test['parent_id'] != parent_id: if test['parent_id'] != parent_id:
continue continue
else: else:
message = self.post_comment(**test) message = self.post_comment(**test)
message = message.json() message = message.json()
if self.isValidTest(test): self.assertTrue('result' in message or 'error' in message)
self.assertTrue(self.isValidMessage(message)) if 'error' in message:
self.assertNotIn('error', message) self.assertFalse(self.is_valid_message(**test))
self.assertIsNotNone(message['result'])
message = message['result']
self.assertIn('parent_id', message)
self.assertEquals(message['parent_id'], parent_id)
else: else:
self.assertIn('error', message) self.assertTrue(self.is_valid_message(**test))
class ListCommentsTest(unittest.TestCase): class ListCommentsTest(unittest.TestCase):
@ -245,14 +235,4 @@ def create_test_comments(values: iter, **default):
for comb in vars_combo] for comb in vars_combo]
def create_comment(channel_name, channel_id, claim_id=None, maxchar=500, reply_id=None, signature=None, parent_id=None):
return {
'claim_id': claim_id if claim_id else fake.sha1(),
'comment': ''.join(fake.text(max_nb_chars=maxchar)),
'channel_name': channel_name,
'channel_id': channel_id,
'signature': signature if signature else fake.uuid4(),
'parent_id': reply_id
}