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.
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
import unittest
|
||||
|
||||
from onefuzztypes.enums import OS, ContainerType
|
||||
@ -21,6 +22,7 @@ from onefuzztypes.models import (
|
||||
TeamsTemplate,
|
||||
)
|
||||
from onefuzztypes.primitives import Container
|
||||
from onefuzztypes.requests import NotificationCreate
|
||||
|
||||
from __app__.onefuzzlib.orm import hide_secrets
|
||||
|
||||
@ -91,3 +93,43 @@ class TestSecret(unittest.TestCase):
|
||||
self.assertIsInstance(notification.config.url.secret, SecretAddress)
|
||||
else:
|
||||
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 requests
|
||||
from azure.storage.blob import ContainerClient
|
||||
from onefuzztypes.models import SecretAddress, SecretData
|
||||
from pydantic import BaseModel, Field
|
||||
from tenacity import Future as tenacity_future
|
||||
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:
|
||||
if data is None:
|
||||
return data
|
||||
if isinstance(data, SecretData) and not isinstance(data.secret, SecretAddress):
|
||||
return serialize(data.secret)
|
||||
if isinstance(data, BaseModel):
|
||||
return {serialize(a): serialize(b) for (a, b) in data.dict().items()}
|
||||
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]
|
||||
|
||||
def __init__(self, secret: Union[T, SecretAddress]):
|
||||
if isinstance(secret, dict):
|
||||
try:
|
||||
self.secret = SecretAddress.parse_obj(secret)
|
||||
else:
|
||||
except Exception:
|
||||
self.secret = secret
|
||||
|
||||
def __str__(self) -> str:
|
||||
@ -511,7 +511,7 @@ class GithubIssueTemplate(BaseModel):
|
||||
# validator needed for backward compatibility
|
||||
@validator("auth", pre=True, always=True)
|
||||
def validate_auth(cls, v: Any) -> SecretData:
|
||||
if isinstance(v, str):
|
||||
if isinstance(v, GithubAuth):
|
||||
return SecretData(secret=v)
|
||||
elif isinstance(v, SecretData):
|
||||
return v
|
||||
@ -519,7 +519,7 @@ class GithubIssueTemplate(BaseModel):
|
||||
try:
|
||||
return SecretData(GithubAuth.parse_obj(v))
|
||||
except Exception:
|
||||
return SecretData(secret=v["secret"])
|
||||
return SecretData(GithubAuth.parse_obj(v["secret"]))
|
||||
else:
|
||||
raise TypeError(f"invalid datatype {type(v)}")
|
||||
|
||||
|
Reference in New Issue
Block a user