import logging from django.test import TestCase from boards.models import KeyPair, GlobalId, Post 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 = 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) 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 = Post.objects.generate_request_get([post]) logger.debug(request) self.assertTrue('' '' '' '' '' 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()) reply_reply_post = Post.objects.create_post(title='', text='[post]%d[/post]' % reply_post.id, thread=post.get_thread()) response = Post.objects.generate_response_get([reply_post]) logger.debug(response) self.assertTrue('' 'success' '' '' '' 'test_title' '[post]%d[/post]' '%d' '%s' '%s' '' '' '' '' '' '' '' '' '' % ( reply_post.id, post.id, post.id, str(reply_post.get_edit_time_epoch()), str(reply_post.get_pub_time_epoch()), post.id, reply_reply_post.id, ) in response, 'Wrong XML generated for the GET response.') def _create_post_with_key(self): key = KeyPair(public_key='pubkey', private_key='privkey', key_type='test_key_type', primary=True) key.save() return Post.objects.create_post(title='test_title', text='test_text')