mirror of
https://github.com/microsoft/onefuzz.git
synced 2025-06-19 13:03:44 +00:00
fix GithubIssueTemplate deserialization (#990)
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
# Licensed under the MIT License.
|
# Licensed under the MIT License.
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import pathlib
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from onefuzztypes.enums import OS, ContainerType
|
from onefuzztypes.enums import OS, ContainerType
|
||||||
@ -21,6 +22,7 @@ from onefuzztypes.models import (
|
|||||||
TeamsTemplate,
|
TeamsTemplate,
|
||||||
)
|
)
|
||||||
from onefuzztypes.primitives import Container
|
from onefuzztypes.primitives import Container
|
||||||
|
from onefuzztypes.requests import NotificationCreate
|
||||||
|
|
||||||
from __app__.onefuzzlib.orm import hide_secrets
|
from __app__.onefuzzlib.orm import hide_secrets
|
||||||
|
|
||||||
@ -91,3 +93,43 @@ class TestSecret(unittest.TestCase):
|
|||||||
self.assertIsInstance(notification.config.url.secret, SecretAddress)
|
self.assertIsInstance(notification.config.url.secret, SecretAddress)
|
||||||
else:
|
else:
|
||||||
self.fail(f"Invalid config type {type(notification.config)}")
|
self.fail(f"Invalid config type {type(notification.config)}")
|
||||||
|
|
||||||
|
def test_roundtrip_github_issue(self) -> None:
|
||||||
|
current_path = pathlib.Path(__file__).parent.absolute()
|
||||||
|
with open(
|
||||||
|
f"{current_path}"
|
||||||
|
+ "/../../../contrib/onefuzz-job-github-actions/github-issues.json"
|
||||||
|
) as json_file:
|
||||||
|
b = json.load(json_file)
|
||||||
|
b["container"] = "testing"
|
||||||
|
c = NotificationCreate.parse_obj(b)
|
||||||
|
d = c.json()
|
||||||
|
e = json.loads(d)
|
||||||
|
NotificationCreate.parse_obj(e)
|
||||||
|
|
||||||
|
def test_roundtrip_team_issue(self) -> None:
|
||||||
|
a = """
|
||||||
|
{
|
||||||
|
"config" : {"url": "http://test"},
|
||||||
|
"container": "testing"
|
||||||
|
}
|
||||||
|
|
||||||
|
""" # noqa
|
||||||
|
b = json.loads(a)
|
||||||
|
c = NotificationCreate.parse_obj(b)
|
||||||
|
d = c.json()
|
||||||
|
e = json.loads(d)
|
||||||
|
NotificationCreate.parse_obj(e)
|
||||||
|
|
||||||
|
def test_roundtrip_ado(self) -> None:
|
||||||
|
current_path = pathlib.Path(__file__).parent.absolute()
|
||||||
|
with open(
|
||||||
|
f"{current_path}"
|
||||||
|
+ "/../../../contrib/onefuzz-job-azure-devops-pipeline/ado-work-items.json" # noqa
|
||||||
|
) as json_file:
|
||||||
|
b = json.load(json_file)
|
||||||
|
b["container"] = "testing"
|
||||||
|
c = NotificationCreate.parse_obj(b)
|
||||||
|
d = c.json()
|
||||||
|
e = json.loads(d)
|
||||||
|
NotificationCreate.parse_obj(e)
|
||||||
|
@ -30,7 +30,6 @@ from uuid import UUID
|
|||||||
import msal
|
import msal
|
||||||
import requests
|
import requests
|
||||||
from azure.storage.blob import ContainerClient
|
from azure.storage.blob import ContainerClient
|
||||||
from onefuzztypes.models import SecretAddress, SecretData
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from tenacity import Future as tenacity_future
|
from tenacity import Future as tenacity_future
|
||||||
from tenacity import Retrying, retry
|
from tenacity import Retrying, retry
|
||||||
@ -378,8 +377,6 @@ def container_file_path(container_url: str, blob_name: str) -> str:
|
|||||||
def serialize(data: Any) -> Any:
|
def serialize(data: Any) -> Any:
|
||||||
if data is None:
|
if data is None:
|
||||||
return data
|
return data
|
||||||
if isinstance(data, SecretData) and not isinstance(data.secret, SecretAddress):
|
|
||||||
return serialize(data.secret)
|
|
||||||
if isinstance(data, BaseModel):
|
if isinstance(data, BaseModel):
|
||||||
return {serialize(a): serialize(b) for (a, b) in data.dict().items()}
|
return {serialize(a): serialize(b) for (a, b) in data.dict().items()}
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
#
|
|
||||||
# Copyright (c) Microsoft Corporation.
|
|
||||||
# Licensed under the MIT License.
|
|
||||||
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from onefuzztypes.models import TeamsTemplate
|
|
||||||
|
|
||||||
from onefuzz.backend import serialize
|
|
||||||
|
|
||||||
|
|
||||||
class TestSerialize(unittest.TestCase):
|
|
||||||
def test_cli_backend_secret_data_serialize(self) -> None:
|
|
||||||
base = TeamsTemplate(url="https://contoso.com")
|
|
||||||
converted = serialize(base)
|
|
||||||
self.assertEqual(converted, {"url": "https://contoso.com"})
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
@ -61,9 +61,9 @@ class SecretData(Generic[T]):
|
|||||||
secret: Union[T, SecretAddress]
|
secret: Union[T, SecretAddress]
|
||||||
|
|
||||||
def __init__(self, secret: Union[T, SecretAddress]):
|
def __init__(self, secret: Union[T, SecretAddress]):
|
||||||
if isinstance(secret, dict):
|
try:
|
||||||
self.secret = SecretAddress.parse_obj(secret)
|
self.secret = SecretAddress.parse_obj(secret)
|
||||||
else:
|
except Exception:
|
||||||
self.secret = secret
|
self.secret = secret
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@ -511,7 +511,7 @@ class GithubIssueTemplate(BaseModel):
|
|||||||
# validator needed for backward compatibility
|
# validator needed for backward compatibility
|
||||||
@validator("auth", pre=True, always=True)
|
@validator("auth", pre=True, always=True)
|
||||||
def validate_auth(cls, v: Any) -> SecretData:
|
def validate_auth(cls, v: Any) -> SecretData:
|
||||||
if isinstance(v, str):
|
if isinstance(v, GithubAuth):
|
||||||
return SecretData(secret=v)
|
return SecretData(secret=v)
|
||||||
elif isinstance(v, SecretData):
|
elif isinstance(v, SecretData):
|
||||||
return v
|
return v
|
||||||
@ -519,7 +519,7 @@ class GithubIssueTemplate(BaseModel):
|
|||||||
try:
|
try:
|
||||||
return SecretData(GithubAuth.parse_obj(v))
|
return SecretData(GithubAuth.parse_obj(v))
|
||||||
except Exception:
|
except Exception:
|
||||||
return SecretData(secret=v["secret"])
|
return SecretData(GithubAuth.parse_obj(v["secret"]))
|
||||||
else:
|
else:
|
||||||
raise TypeError(f"invalid datatype {type(v)}")
|
raise TypeError(f"invalid datatype {type(v)}")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user