test_keys.py
87 lines
| 3.3 KiB
| text/x-python
|
PythonLexer
|
r837 | from base64 import b64encode | ||
|
r827 | import logging | ||
|
r822 | from django.test import TestCase | ||
|
r827 | from boards.models import KeyPair, GlobalId, Post | ||
|
r1227 | from boards.models.post.sync import SyncManager | ||
|
r827 | |||
logger = logging.getLogger(__name__) | ||||
|
r822 | |||
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 = key.sign(message) | ||||
valid = KeyPair.objects.verify(key.public_key, message, signature, | ||||
key_type='ecdsa') | ||||
self.assertTrue(valid, 'Message verification failed.') | ||||
def test_primary_constraint(self): | ||||
KeyPair.objects.generate_key(key_type='ecdsa', primary=True) | ||||
|
r835 | with self.assertRaises(Exception): | ||
|
r822 | KeyPair.objects.generate_key(key_type='ecdsa', primary=True) | ||
|
r827 | def test_model_id_save(self): | ||
|
r822 | model_id = GlobalId(key_type='test', key='test key', local_id='1') | ||
model_id.save() | ||||
|
r827 | def test_request_get(self): | ||
post = self._create_post_with_key() | ||||
|
r841 | request = GlobalId.objects.generate_request_get([post.global_id]) | ||
|
r827 | logger.debug(request) | ||
|
r837 | key = KeyPair.objects.get(primary=True) | ||
|
r828 | self.assertTrue('<request type="get" version="1.0">' | ||
'<model name="post" version="1.0">' | ||||
|
r837 | '<id key="%s" local-id="1" type="%s" />' | ||
|
r828 | '</model>' | ||
|
r837 | '</request>' % ( | ||
key.public_key, | ||||
key.key_type, | ||||
) in request, | ||||
|
r822 | 'Wrong XML generated for the GET request.') | ||
|
r827 | |||
def test_response_get(self): | ||||
post = self._create_post_with_key() | ||||
|
r829 | reply_post = Post.objects.create_post(title='test_title', | ||
|
r835 | text='[post]%d[/post]' % post.id, | ||
thread=post.get_thread()) | ||||
|
r827 | |||
|
r1227 | response = SyncManager().generate_response_get([reply_post]) | ||
|
r827 | logger.debug(response) | ||
|
r837 | key = KeyPair.objects.get(primary=True) | ||
self.assertTrue('<status>success</status>' | ||||
|
r828 | '<models>' | ||
|
r838 | '<model name="post">' | ||
'<content>' | ||||
|
r837 | '<id key="%s" local-id="%d" type="%s" />' | ||
|
r828 | '<title>test_title</title>' | ||
|
r1228 | '<text>[post]%s[/post]</text>' | ||
|
r936 | '<thread><id key="%s" local-id="%d" type="%s" /></thread>' | ||
|
r828 | '<pub-time>%s</pub-time>' | ||
|
r838 | '</content>' % ( | ||
|
r837 | key.public_key, | ||
|
r829 | reply_post.id, | ||
|
r837 | key.key_type, | ||
|
r1228 | str(post.global_id), | ||
|
r936 | key.public_key, | ||
|
r829 | post.id, | ||
|
r936 | key.key_type, | ||
|
r829 | str(reply_post.get_pub_time_epoch()), | ||
|
r828 | ) in response, | ||
|
r827 | 'Wrong XML generated for the GET response.') | ||
def _create_post_with_key(self): | ||||
|
r837 | KeyPair.objects.generate_key(primary=True) | ||
|
r827 | |||
return Post.objects.create_post(title='test_title', text='test_text') | ||||