Example #1
0
    def setup_class(self):
        # Setup a server
        self.server_gnupg_home = get_temporary_workdir()
        self.server_gpg = create_gpg(self.server_gnupg_home.name)
        self.server_passphrase = 'server-sicrit-passphrase'
        input_data = self.server_gpg.gen_key_input(
            key_length=1024,
            passphrase=self.server_passphrase,
            name_email='*****@*****.**')

        # Generate the key, making sure it worked
        self.server_key = self.server_gpg.gen_key(input_data)
        assert self.server_key.fingerprint

        # Export the key, making sure it worked
        self.server_keydata = self.server_gpg.export_keys(
            self.server_key.fingerprint,
            armor=True,
            minimal=True,
            passphrase=self.server_passphrase)
        assert self.server_keydata

        # Setup a user
        self.gnupg_home = get_temporary_workdir()
        self.gpg = create_gpg(self.gnupg_home.name)
        self.ga = GPGAuthSession(self.gpg, 'https://inexistant.example.com/',
                                 '/auth/')

        self.user_passphrase = 'user-sicrit-passphrase'
        input_data = self.gpg.gen_key_input(
            key_length=1024,
            passphrase=self.user_passphrase,
            name_email='*****@*****.**')

        # test_user_fingerprint_raises_without_key
        with pytest.raises(GPGAuthException):
            assert self.ga.user_fingerprint

        # Generate the key, making sure it worked
        self.user_key = self.gpg.gen_key(input_data)
        assert self.user_key.fingerprint

        # Use private API to set the passphrase
        self.ga._user_passphrase = self.user_passphrase

        # Export the user key
        self.user_keydata = self.gpg.export_keys(self.user_key.fingerprint,
                                                 armor=True,
                                                 minimal=True)
        assert self.user_keydata
        # … and import it in the server keyring
        import_result = self.server_gpg.import_keys(self.user_keydata)
        assert self.user_key.fingerprint in import_result.fingerprints
Example #2
0
def test_import_user_private_key_from_file_works(caplog):
    gpg_generator_home = get_temporary_workdir()
    gpg_generator = create_gpg(gpg_generator_home.name)

    # Generate a key
    passphrase = 'test-passphrase'
    input_data = gpg_generator.gen_key_input(key_length=1024,
                                             passphrase=passphrase)

    # Generate the key, making sure it worked
    key = gpg_generator.gen_key(input_data)
    assert key.fingerprint

    # Export the key, making sure it worked
    key_asc = gpg_generator.export_keys(key.fingerprint,
                                        armor=True,
                                        minimal=True,
                                        secret=True,
                                        passphrase=passphrase)
    assert key_asc

    # Create a temporary file, and use it
    with NamedTemporaryFile(mode='w') as private_key_file:
        private_key_file.write(key_asc)
        private_key_file.flush()

        # Setup a different gpg home
        gpg_home = get_temporary_workdir()
        gpg = create_gpg(gpg_home.name)

        caplog.set_level(logging.INFO)

        imported_fingerprint = import_user_private_key_from_file(
            gpg, private_key_file.name)
        # Check that it really worked
        assert imported_fingerprint == key.fingerprint
        # That we logged what we wanted
        assert caplog.record_tuples == [
            ('requests_gpgauthlib.utils', logging.INFO,
             'Importing the user private key; password prompt expected'),
            ('requests_gpgauthlib.utils', logging.INFO,
             'GPG key 0x%s successfully imported' % key.fingerprint),
            # FIXME: Check why that message is output twice
            ('requests_gpgauthlib.utils', logging.INFO,
             'GPG key 0x%s successfully imported' % key.fingerprint)
        ]
Example #3
0
def test_get_temporary_workdir_is_prefixed():
    assert 'requests_gpgauthlib-' in get_temporary_workdir().name
Example #4
0
def test_import_user_private_key_from_empty_file_raises():
    gpghome = get_temporary_workdir()
    gpg = create_gpg(gpghome.name)
    with NamedTemporaryFile(mode='w') as empty_key_file:
        with pytest.raises(GPGAuthKeyImportError):
            import_user_private_key_from_file(gpg, empty_key_file.name)
Example #5
0
def test_import_user_private_key_from_inexistant_file_raises():
    gpghome = get_temporary_workdir()
    gpg = create_gpg(gpghome.name)
    with pytest.raises(FileNotFoundError):
        import_user_private_key_from_file(gpg, '/inexistant')
Example #6
0
def test_create_gpg_has_its_home_where_we_say_we_want_it():
    workdir = get_temporary_workdir()
    gpg = create_gpg(workdir.name)
    assert workdir.name in gpg.gnupghome
Example #7
0
def test_create_gpg_gives_a_GPG_object():
    workdir = get_temporary_workdir()
    assert isinstance(create_gpg(workdir.name), GPG)
Example #8
0
def test_get_temporary_workdir_is_different():
    assert get_temporary_workdir().name != get_temporary_workdir().name