Skip to content

Commit

Permalink
Merge pull request #33 from dajiaji/fix-lint-error
Browse files Browse the repository at this point in the history
Fix lint error
  • Loading branch information
dajiaji committed Apr 25, 2021
2 parents 9a48d55 + 213eaf2 commit 7f25f12
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 95 deletions.
22 changes: 14 additions & 8 deletions cwt/recipient.py
Expand Up @@ -8,11 +8,14 @@ class Recipient(CBORProcessor):
"""
A COSE Recipient.
"""
def __init__(self,
protected: bytes = b"",
unprotected: Dict[int, Any] = {},
ciphertext: bytes = b"",
recipients: List[Any] = []):

def __init__(
self,
protected: bytes = b"",
unprotected: Dict[int, Any] = {},
ciphertext: bytes = b"",
recipients: List[Any] = [],
):

# Validate unprotected
if 1 in unprotected:
Expand Down Expand Up @@ -76,10 +79,11 @@ def to_list(self) -> List[Any]:
return res


class Recipients():
class Recipients:
"""
A Set of COSE Recipients.
"""

def __init__(self, recipients: List[Recipient]):
self._recipients = recipients
return
Expand All @@ -96,7 +100,7 @@ def derive_key(self, keys: List[COSEKey]) -> COSEKey:
raise ValueError("Failed to derive a key.")


class RecipientsBuilder():
class RecipientsBuilder:
"""
A Recipients Builder.
"""
Expand All @@ -115,7 +119,9 @@ def from_list(self, recipients: List[Any]) -> Recipients:
return Recipients(res)

def _create_recipient(self, recipient: List[Any]) -> Recipient:
if not isinstance(recipient, list) or (len(recipient) != 3 and len(recipient) != 4):
if not isinstance(recipient, list) or (
len(recipient) != 3 and len(recipient) != 4
):
raise ValueError("Invalid recipient format.")
if not isinstance(recipient[0], bytes):
raise ValueError("protected header should be bytes.")
Expand Down
113 changes: 26 additions & 87 deletions tests/test_recipient.py
Expand Up @@ -90,18 +90,20 @@ def test_recipient_constructor_with_alg_a128kw(self):
{1: -6, 4: b"our-secret"},
b"xxx",
[],
"ciphertext should be zero-length bytes."
"ciphertext should be zero-length bytes.",
),
(
b"",
{1: -6, 4: b"our-secret"},
b"",
[Recipient()],
"recipients should be absent."
"recipients should be absent.",
),
],
)
def test_recipient_constructor_with_invalid_args(self, protected, unprotected, ciphertext, recipients, msg):
def test_recipient_constructor_with_invalid_args(
self, protected, unprotected, ciphertext, recipients, msg
):
with pytest.raises(ValueError) as err:
Recipient(protected, unprotected, ciphertext, recipients)
pytest.fail("Recipient() should fail.")
Expand Down Expand Up @@ -195,90 +197,27 @@ def test_recipients_builder_from_list_with_recipients(self):
@pytest.mark.parametrize(
"invalid, msg",
[
(
[{}],
"Invalid recipient format."
),
(
[123],
"Invalid recipient format."
),
(
[[]],
"Invalid recipient format."
),
(
[[b"", {}]],
"Invalid recipient format."
),
(
[[b"", {}, b"", [], {}]],
"Invalid recipient format."
),
(
[["", {}, b""]],
"protected header should be bytes."
),
(
[[{}, {}, b""]],
"protected header should be bytes."
),
(
[[[], {}, b""]],
"protected header should be bytes."
),
(
[[123, {}, b""]],
"protected header should be bytes."
),
(
[[b"", [], b""]],
"unprotected header should be dict."
),
(
[[b"", "", b""]],
"unprotected header should be dict."
),
(
[[b"", b"", b""]],
"unprotected header should be dict."
),
(
[[b"", 123, b""]],
"unprotected header should be dict."
),
(
[[b"", {}, ""]],
"ciphertext should be bytes."
),
(
[[b"", {}, {}]],
"ciphertext should be bytes."
),
(
[[b"", {}, []]],
"ciphertext should be bytes."
),
(
[[b"", {}, 123]],
"ciphertext should be bytes."
),
(
[[b"", {}, b"", {}]],
"recipients should be list."
),
(
[[b"", {}, b"", ""]],
"recipients should be list."
),
(
[[b"", {}, b"", b""]],
"recipients should be list."
),
(
[[b"", {}, b"", 123]],
"recipients should be list."
),
([{}], "Invalid recipient format."),
([123], "Invalid recipient format."),
([[]], "Invalid recipient format."),
([[b"", {}]], "Invalid recipient format."),
([[b"", {}, b"", [], {}]], "Invalid recipient format."),
([["", {}, b""]], "protected header should be bytes."),
([[{}, {}, b""]], "protected header should be bytes."),
([[[], {}, b""]], "protected header should be bytes."),
([[123, {}, b""]], "protected header should be bytes."),
([[b"", [], b""]], "unprotected header should be dict."),
([[b"", "", b""]], "unprotected header should be dict."),
([[b"", b"", b""]], "unprotected header should be dict."),
([[b"", 123, b""]], "unprotected header should be dict."),
([[b"", {}, ""]], "ciphertext should be bytes."),
([[b"", {}, {}]], "ciphertext should be bytes."),
([[b"", {}, []]], "ciphertext should be bytes."),
([[b"", {}, 123]], "ciphertext should be bytes."),
([[b"", {}, b"", {}]], "recipients should be list."),
([[b"", {}, b"", ""]], "recipients should be list."),
([[b"", {}, b"", b""]], "recipients should be list."),
([[b"", {}, b"", 123]], "recipients should be list."),
],
)
def test_recipients_builder_from_list_with_invalid_args(self, invalid, msg):
Expand Down

0 comments on commit 7f25f12

Please sign in to comment.