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