Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

fix GithubIssueTemplate deserialization #990

Merged
merged 17 commits into from
Jun 17, 2021
Merged
42 changes: 42 additions & 0 deletions src/api-service/tests/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Licensed under the MIT License.

import json
import pathlib
import unittest

from onefuzztypes.enums import OS, ContainerType
Expand All @@ -21,6 +22,7 @@
TeamsTemplate,
)
from onefuzztypes.primitives import Container
from onefuzztypes.requests import NotificationCreate

from __app__.onefuzzlib.orm import hide_secrets

Expand Down Expand Up @@ -91,3 +93,43 @@ def test_read_secret(self) -> None:
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)
3 changes: 0 additions & 3 deletions src/cli/onefuzz/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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
Expand Down Expand Up @@ -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):
Expand Down
21 changes: 0 additions & 21 deletions src/cli/tests/test_serialize.py

This file was deleted.

8 changes: 4 additions & 4 deletions src/pytypes/onefuzztypes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -511,15 +511,15 @@ 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
elif isinstance(v, dict):
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)}")

Expand Down