|
|
from base64 import b64encode
|
|
|
import logging
|
|
|
|
|
|
from django.test import TestCase
|
|
|
from boards.models import KeyPair, GlobalId, Post, Signature
|
|
|
from boards.models.post.sync import SyncManager
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
class KeyTest(TestCase):
|
|
|
def test_create_key(self):
|
|
|
key = KeyPair.objects.generate_key('ecdsa')
|
|
|
|
|
|
self.assertIsNotNone(key, 'The key was not created.')
|
|
|
|
|
|
def test_validation(self):
|
|
|
key = KeyPair.objects.generate_key(key_type='ecdsa')
|
|
|
message = 'msg'
|
|
|
signature_value = key.sign(message)
|
|
|
|
|
|
signature = Signature(key_type='ecdsa', key=key.public_key,
|
|
|
signature=signature_value)
|
|
|
valid = KeyPair.objects.verify(signature, message)
|
|
|
|
|
|
self.assertTrue(valid, 'Message verification failed.')
|
|
|
|
|
|
def test_primary_constraint(self):
|
|
|
KeyPair.objects.generate_key(key_type='ecdsa', primary=True)
|
|
|
|
|
|
with self.assertRaises(Exception):
|
|
|
KeyPair.objects.generate_key(key_type='ecdsa', primary=True)
|
|
|
|
|
|
def test_model_id_save(self):
|
|
|
model_id = GlobalId(key_type='test', key='test key', local_id='1')
|
|
|
model_id.save()
|
|
|
|
|
|
def test_request_get(self):
|
|
|
post = self._create_post_with_key()
|
|
|
|
|
|
request = GlobalId.objects.generate_request_get([post.global_id])
|
|
|
logger.debug(request)
|
|
|
|
|
|
key = KeyPair.objects.get(primary=True)
|
|
|
self.assertTrue('<request type="get" version="1.0">'
|
|
|
'<model name="post" version="1.0">'
|
|
|
'<id key="%s" local-id="1" type="%s" />'
|
|
|
'</model>'
|
|
|
'</request>' % (
|
|
|
key.public_key,
|
|
|
key.key_type,
|
|
|
) in request,
|
|
|
'Wrong XML generated for the GET request.')
|
|
|
|
|
|
def test_response_get(self):
|
|
|
post = self._create_post_with_key()
|
|
|
reply_post = Post.objects.create_post(title='test_title',
|
|
|
text='[post]%d[/post]' % post.id,
|
|
|
thread=post.get_thread())
|
|
|
|
|
|
response = SyncManager.generate_response_get([reply_post])
|
|
|
logger.debug(response)
|
|
|
|
|
|
key = KeyPair.objects.get(primary=True)
|
|
|
self.assertTrue('<status>success</status>'
|
|
|
'<models>'
|
|
|
'<model name="post">'
|
|
|
'<content>'
|
|
|
'<id key="%s" local-id="%d" type="%s" />'
|
|
|
'<title>test_title</title>'
|
|
|
'<text>[post]%s[/post]</text>'
|
|
|
'<thread><id key="%s" local-id="%d" type="%s" /></thread>'
|
|
|
'<pub-time>%s</pub-time>'
|
|
|
'</content>' % (
|
|
|
key.public_key,
|
|
|
reply_post.id,
|
|
|
key.key_type,
|
|
|
str(post.global_id),
|
|
|
key.public_key,
|
|
|
post.id,
|
|
|
key.key_type,
|
|
|
str(reply_post.get_pub_time_str()),
|
|
|
) in response,
|
|
|
'Wrong XML generated for the GET response.')
|
|
|
|
|
|
def _create_post_with_key(self):
|
|
|
KeyPair.objects.generate_key(primary=True)
|
|
|
|
|
|
return Post.objects.create_post(title='test_title', text='test_text')
|
|
|
|