Beispiel #1
0
    def test_get_encryption_key__to_short__error(self):

        key = 'abc123'
        self.root_dir.join('.szczypiorek_encryption_key').write(
            base64.b64encode(
                json.dumps({
                    'key': key,
                    'hash': 'hash',
                }).encode('utf8')),
            mode='wb')

        with pytest.raises(EncryptionKeyTooShortError) as e:
            get_encryption_key()

        assert e.value.args[0] == normalize("""
            So it seems that the key used for encryption hidden in
            the '.szczypiorek_encryption_key' file is too short.

            Which means that because of some reason you've decided to mess
            around with the built-in generator of the secured key.

            Try to get access to the not broken version of the
            '.szczypiorek_encryption_key' file or if you have access to the not
            encrypted version you environment files simply remove the broken
            file and run 'decrypt' phase one more time.
        """)
Beispiel #2
0
    def test_get_encryption_key__file_does_not_exist__error(self):

        with pytest.raises(EncryptionKeyFileMissingError) as e:
            get_encryption_key()

        assert e.value.args[0] == normalize("""
            Couldn't find the '.szczypiorek_encryption_key' file. It is required
            for the correct functioning of the encryption and decryption
            phases.

            If you see this message while performing 'decrypt' then
            simply request the file from fellow code contributor.
            In the 'encrypt' scenario the file is created automatically.
        """)  # noqa
Beispiel #3
0
    def test_get_encryption_key__env_var__not_base64__error(self):

        os.environ['SZCZYPIOREK_ENCRYPTION_KEY'] = json.dumps({'key': 'key'})

        with pytest.raises(EncryptionKeyBrokenBase64Error) as e:
            get_encryption_key()

        assert e.value.args[0] == normalize("""
            The content of the 'SZCZYPIOREK_ENCRYPTION_KEY' environment variable was automatically
            encoded with base64 so that noone tries to mess around with it.
            So if you see this message that means that someone tried just that.

            Try to get access to the not broken version of the
            'SZCZYPIOREK_ENCRYPTION_KEY' environment variable or if you have access to the not
            encrypted version you environment files simply remove the broken
            file and run 'decrypt' phase one more time.
        """)  # noqa
Beispiel #4
0
    def test_get_encryption_key__not_json__error(self):

        self.root_dir.join('.szczypiorek_encryption_key').write(
            base64.b64encode(b'"key": "whatever"'), mode='wb')

        with pytest.raises(EncryptionKeyBrokenJsonError) as e:
            get_encryption_key()

        assert e.value.args[0] == normalize("""
            The content of the '.szczypiorek_encryption_key' file must be a valid
            json file encoded with base64. It takes the following shape:

            {
                "key": <automatically generated secret>,
                "hash": <automatically generated secret's hash>,
                "created_datetime": <iso datetime of the key creation>
            }
        """)  # noqa
Beispiel #5
0
    def test_get_encryption_key__file_not_gitignored__error(self):

        key = 'd8s9s8c9s8s9ds8d98sd9s89cs8c9s8d'
        self.root_dir.join('.szczypiorek_encryption_key').write(
            base64.b64encode(
                json.dumps({
                    'key': key,
                    'hash': 'hash',
                }).encode('utf8')),
            mode='wb')

        self.mocker.patch(
            'szczypiorek.crypto.assert_is_git_repository').return_value = True
        self.mocker.patch('szczypiorek.crypto.assert_is_git_ignored'
                          ).side_effect = FileNotIgnoredError('not ignored')

        with pytest.raises(FileNotIgnoredError) as e:
            get_encryption_key()

        assert e.value.args[0] == normalize('not ignored')
Beispiel #6
0
    def test_get_encryption_key__file__not_base64__error(self):

        self.root_dir.join('.szczypiorek_encryption_key').write(json.dumps({
            'key':
            'key'
        }).encode('utf8'),
                                                                mode='wb')

        with pytest.raises(EncryptionKeyBrokenBase64Error) as e:
            get_encryption_key()

        assert e.value.args[0] == normalize("""
            The content of the '.szczypiorek_encryption_key' file was automatically
            encoded with base64 so that noone tries to mess around with it.
            So if you see this message that means that someone tried just that.

            Try to get access to the not broken version of the
            '.szczypiorek_encryption_key' file or if you have access to the not
            encrypted version you environment files simply remove the broken
            file and run 'decrypt' phase one more time.
        """)  # noqa
Beispiel #7
0
    def test_get_encryption_key__all_good__success(self):

        key = 10 * 'd8s9s8c9s8s9ds8d98sd9s89cs8c9s8d'
        self.root_dir.join('.szczypiorek_encryption_key').write(
            base64.b64encode(
                json.dumps({
                    'key': key,
                    'hash': 'hash',
                }).encode('utf8')),
            mode='wb')

        assert get_encryption_key() == (key, 'hash')
Beispiel #8
0
    def test_get_encryption_key__custom_file__success(self):

        key = 10 * 'd8s9s8c9s8s9ds8d98sd9s89cs8c9s8d'
        self.root_dir.join('.development_encryption_key').write(
            base64.b64encode(
                json.dumps({
                    'key': key,
                    'hash': 'hash',
                }).encode('utf8')),
            mode='wb')

        assert get_encryption_key('.development_encryption_key') == (key,
                                                                     'hash')