Skip to content
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

Make most of the fields in KeyinfoPubkey and KeyinfoSslcert models optional #1597

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions osc/obs_api/keyinfo_pubkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
class KeyinfoPubkey(XmlModel):
XML_TAG = "pubkey"

keyid: str = Field(
keyid: Optional[str] = Field(
xml_attribute=True,
)

userid: str = Field(
userid: Optional[str] = Field(
xml_attribute=True,
)

algo: str = Field(
algo: Optional[str] = Field(
xml_attribute=True,
)

keysize: str = Field(
keysize: Optional[str] = Field(
xml_attribute=True,
)

expires: int = Field(
expires: Optional[int] = Field(
xml_attribute=True,
)

fingerprint: str = Field(
fingerprint: Optional[str] = Field(
xml_attribute=True,
)

Expand All @@ -34,6 +34,10 @@ class KeyinfoPubkey(XmlModel):

def get_expires_str(self) -> str:
import datetime

if self.expires is None:
return ""

return datetime.datetime.fromtimestamp(self.expires).strftime("%Y-%m-%d %H:%M:%S")

def to_human_readable_string(self) -> str:
Expand Down
20 changes: 14 additions & 6 deletions osc/obs_api/keyinfo_sslcert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ class KeyinfoSslcert(XmlModel):
xml_attribute=True,
)

serial: str = Field(
serial: Optional[str] = Field(
xml_attribute=True,
)

issuer: Optional[str] = Field(
xml_attribute=True,
)

subject: str = Field(
subject: Optional[str] = Field(
xml_attribute=True,
)

algo: str = Field(
algo: Optional[str] = Field(
xml_attribute=True,
)

keysize: str = Field(
keysize: Optional[str] = Field(
xml_attribute=True,
)

begins: int = Field(
begins: Optional[int] = Field(
xml_attribute=True,
)

expires: int = Field(
expires: Optional[int] = Field(
xml_attribute=True,
)

Expand All @@ -46,10 +46,18 @@ class KeyinfoSslcert(XmlModel):

def get_begins_str(self) -> str:
import datetime

if self.begins is None:
return ""

return datetime.datetime.fromtimestamp(self.begins).strftime("%Y-%m-%d %H:%M:%S")

def get_expires_str(self) -> str:
import datetime

if self.expires is None:
return ""

return datetime.datetime.fromtimestamp(self.expires).strftime("%Y-%m-%d %H:%M:%S")

def to_human_readable_string(self) -> str:
Expand Down
3 changes: 3 additions & 0 deletions osc/output/tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def colorize(text, color):
if not color:
return text

if not text:
return text

result = ""
for i in color.split(","):
result += ESCAPE_CODES[i]
Expand Down
43 changes: 43 additions & 0 deletions tests/test_keyinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest

from osc import obs_api


class TestKeyinfo(unittest.TestCase):
def test_empty_pubkey(self):
ki = obs_api.Keyinfo()
ki.pubkey_list = [{"value": "<pubkey>"}]

expected = """
Type : GPG public key
User ID :
Algorithm :
Key size :
Expires :
Fingerprint :
<pubkey>""".strip()
actual = ki.pubkey_list[0].to_human_readable_string()
self.assertEqual(expected, actual)

def test_empty_sslcert(self):
ki = obs_api.Keyinfo()
ki.sslcert_list = [{"value": "<pubkey>"}]

expected = """
Type : SSL certificate
Subject :
Key ID :
Serial :
Issuer :
Algorithm :
Key size :
Begins :
Expires :
Fingerprint :
<pubkey>""".strip()
actual = ki.sslcert_list[0].to_human_readable_string()
self.assertEqual(expected, actual)


if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def test_wide_chars(self):
""".strip()
self.assertEqual(str(t), expected)

def test_empty_value_no_color(self):
t = KeyValueTable()
t.add("Key", "", color="bold")

expected = "Key : "
self.assertEqual(str(t), expected)


class TestPrintMsg(unittest.TestCase):
def setUp(self):
Expand Down
Loading