From 96e0a3ba12a80d59eca9dbd128c26042b646e0fd Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 29 Oct 2013 22:08:46 +0100 Subject: [PATCH] tests: factorize common code --- tests/api.py | 64 +++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/tests/api.py b/tests/api.py index 95f8c5b..45b3b6a 100644 --- a/tests/api.py +++ b/tests/api.py @@ -8,44 +8,42 @@ import rfc3161 class Rfc3161(unittest.TestCase): - def test_time_certum_pl(self): - PUBLIC_TSA_SERVER = 'http://time.certum.pl' - CERTIFICATE = os.path.join(os.path.dirname(__file__), - '../data/certum_certificate.crt') - data = 'xx' - certificate = file(CERTIFICATE).read() - value, substrate = rfc3161.RemoteTimestamper( - PUBLIC_TSA_SERVER, certificate=certificate)(data=data) + def default_test(self, tsa_server, certificate, username=None, password=None, data='xx', nonce=None, **kwargs): + kwargs.update({ + 'certificate': file(certificate).read() + }) + if username and password: + kwargs.update({ + 'username': username, + 'password': password, + }) + + timestamper = rfc3161.RemoteTimestamper(tsa_server, **kwargs) + kwargs = {} + if nonce: + kwargs['nonce'] = nonce + value, substrate = timestamper(data=data, **kwargs) self.assertIsInstance(rfc3161.get_timestamp(value), datetime.datetime) self.assertNotEqual(value, None) self.assertEqual(substrate, '') + def test_time_certum_pl(self): + self.default_test('http://time.certum.pl', + os.path.join(os.path.dirname(__file__), + '../data/certum_certificate.crt')) + def test_teszt_e_szigno_hu(self): - PUBLIC_TSA_SERVER = 'https://teszt.e-szigno.hu:440/tsa' - USERNAME = 'teszt' - PASSWORD = 'teszt' - CERTIFICATE = os.path.join(os.path.dirname(__file__), - '../data/e_szigno_test_tsa2.crt') data = '{"comment": "Envoi en Commission", "to": "Benjamin Dauvergne", "filetype": "Arr\u00eat CC", "from": "Benjamin Dauvergne", "files": [{"name": "affectations_ange1d.xlsx", "digest": "ce57e4ba353107dddaab91b9ad26c0569ffe0f94", "size": 16279}]}' - certificate = file(CERTIFICATE).read() - value, substrate = rfc3161.RemoteTimestamper( - PUBLIC_TSA_SERVER, certificate=certificate, username=USERNAME, - password=PASSWORD, hashname='sha256')(data=data) - self.assertIsInstance(rfc3161.get_timestamp(value), datetime.datetime) - self.assertNotEqual(value, None) - self.assertEqual(substrate, '') + self.default_test('https://teszt.e-szigno.hu:440/tsa', + username='teszt', password='teszt', + certificate=os.path.join(os.path.dirname(__file__), + '../data/e_szigno_test_tsa2.crt'), + data=data, hashname='sha256') def test_teszt_e_szigno_hu_with_nonce(self): - PUBLIC_TSA_SERVER = 'https://teszt.e-szigno.hu:440/tsa' - USERNAME = 'teszt' - PASSWORD = 'teszt' - CERTIFICATE = os.path.join(os.path.dirname(__file__), - '../data/e_szigno_test_tsa2.crt') - data = 'xx' - certificate = file(CERTIFICATE).read() - value, substrate = rfc3161.RemoteTimestamper( - PUBLIC_TSA_SERVER, certificate=certificate, username=USERNAME, - password=PASSWORD, hashname='sha256')(data=data, nonce=2) - self.assertIsInstance(rfc3161.get_timestamp(value), datetime.datetime) - self.assertNotEqual(value, None) - self.assertEqual(substrate, '') + data = '{"comment": "Envoi en Commission", "to": "Benjamin Dauvergne", "filetype": "Arr\u00eat CC", "from": "Benjamin Dauvergne", "files": [{"name": "affectations_ange1d.xlsx", "digest": "ce57e4ba353107dddaab91b9ad26c0569ffe0f94", "size": 16279}]}' + self.default_test('https://teszt.e-szigno.hu:440/tsa', + username='teszt', password='teszt', + certificate=os.path.join(os.path.dirname(__file__), + '../data/e_szigno_test_tsa2.crt'), + data=data, nonce=2, hashname='sha256')