diff --git a/oidc_provider/management/commands/creatersakey.py b/oidc_provider/management/commands/creatersakey.py index fb406f1..c6a5352 100644 --- a/oidc_provider/management/commands/creatersakey.py +++ b/oidc_provider/management/commands/creatersakey.py @@ -1,7 +1,7 @@ from Crypto.PublicKey import RSA from django.conf import settings -from django.core.management.base import BaseCommand, CommandError +from django.core.management.base import BaseCommand class Command(BaseCommand): @@ -11,8 +11,8 @@ class Command(BaseCommand): try: key = RSA.generate(1024) file_path = settings.BASE_DIR + '/OIDC_RSA_KEY.pem' - with open(file_path, 'w') as f: + with open(file_path, 'wb') as f: f.write(key.exportKey('PEM')) self.stdout.write('RSA key successfully created at: ' + file_path) except Exception as e: - self.stdout.write('Something goes wrong: ' + e.message) + self.stdout.write('Something goes wrong: {0}'.format(e)) diff --git a/oidc_provider/tests/test_creatersakey_command.py b/oidc_provider/tests/test_creatersakey_command.py new file mode 100644 index 0000000..81f676e --- /dev/null +++ b/oidc_provider/tests/test_creatersakey_command.py @@ -0,0 +1,11 @@ +from django.core.management import call_command +from django.test import TestCase, override_settings +from django.utils.six import StringIO + + +class CreateRSAKeyTest(TestCase): + @override_settings(BASE_DIR='/tmp') + def test_command_output(self): + out = StringIO() + call_command('creatersakey', stdout=out) + self.assertIn('RSA key successfully created', out.getvalue())