Skip to content

Migrate "test/unit/test_providers_base_master_key.py" from unittest to pytest #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions test/unit/test_providers_base_master_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Test suite for aws_encryption_sdk.key_providers.base.MasterKey"""
import unittest

import attr
import pytest
import six
from mock import MagicMock, patch, sentinel

from aws_encryption_sdk.exceptions import ConfigMismatchError, IncorrectMasterKeyError, InvalidKeyIdError
Expand Down Expand Up @@ -63,12 +60,13 @@ class FakeMasterKey(MockMasterKey):
excinfo.match(r'MasterKey config classes must have a "provider_id" attribute defined.')


class TestMasterKey(unittest.TestCase):
def setUp(self):
class TestMasterKey(object):
@pytest.fixture(autouse=True)
def apply_fixture(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #127 : need to run teardown actions.

self.mock_data_key_len_check_patcher = patch("aws_encryption_sdk.internal.utils.source_data_key_length_check")
self.mock_data_key_len_check = self.mock_data_key_len_check_patcher.start()

def tearDown(self):
yield
# Run tearDown
self.mock_data_key_len_check_patcher.stop()

def test_parent(self):
Expand All @@ -85,8 +83,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
pass

with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
with pytest.raises(TypeError) as excinfo:
TestMasterKey()
excinfo.match("Can't instantiate abstract class TestMasterKey *")

def test_generate_data_key_enforcement(self):
class TestMasterKey(MasterKey):
Expand All @@ -98,8 +97,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
pass

with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
with pytest.raises(TypeError) as excinfo:
TestMasterKey()
excinfo.match("Can't instantiate abstract class TestMasterKey *")

def test_encrypt_data_key_enforcement(self):
class TestMasterKey(MasterKey):
Expand All @@ -111,8 +111,9 @@ def _generate_data_key(self, algorithm, encryption_context):
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
pass

with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
with pytest.raises(TypeError) as excinfo:
TestMasterKey()
excinfo.match("Can't instantiate abstract class TestMasterKey *")

def test_decrypt_data_key_enforcement(self):
class TestMasterKey(MasterKey):
Expand All @@ -124,8 +125,9 @@ def _generate_data_key(self, algorithm, encryption_context):
def _encrypt_data_key(self, data_key, algorithm, encryption_context):
pass

with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
with pytest.raises(TypeError) as excinfo:
TestMasterKey()
excinfo.match("Can't instantiate abstract class TestMasterKey *")

def test_new(self):
mock_master_key = MockMasterKey(
Expand All @@ -142,10 +144,9 @@ def test_new_conf_mismatch(self):
mock_config = MagicMock()
mock_config.__class__ = MockMasterKeyConfig
mock_config.provider_id = sentinel.mismatched_provider_id
with six.assertRaisesRegex(
self, ConfigMismatchError, "Config provider_id does not match MasterKey provider_id: *"
):
with pytest.raises(ConfigMismatchError) as excinfo:
MockMasterKey(config=mock_config)
excinfo.match("Config provider_id does not match MasterKey provider_id: *")

def test_owns_data_key_owned(self):
mock_master_key = MockMasterKey(
Expand Down Expand Up @@ -220,8 +221,9 @@ def test_new_master_key_invalid(self):
mock_encrypted_data_key=sentinel.encrypted_data_key,
mock_decrypted_data_key=sentinel.decrypted_data_key,
)
with six.assertRaisesRegex(self, InvalidKeyIdError, "MasterKeys can only provide themselves. *"):
with pytest.raises(InvalidKeyIdError) as excinfo:
mock_master_key._new_master_key(sentinel.another_key_id)
excinfo.match("MasterKeys can only provide themselves. *")

def test_key_check_valid(self):
mock_master_key = MockMasterKey(
Expand All @@ -243,8 +245,9 @@ def test_key_check_invalid(self):
)
mock_data_key = MagicMock()
mock_data_key.key_provider = sentinel.another_key_provider
with six.assertRaisesRegex(self, IncorrectMasterKeyError, "Provided data key provider *"):
with pytest.raises(IncorrectMasterKeyError) as excinfo:
mock_master_key._key_check(mock_data_key)
excinfo.match("Provided data key provider *")

def test_generate_data_key(self):
mock_master_key = MockMasterKey(
Expand Down