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

Commit

Permalink
Adds tests for email endpoints and dispatch service (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
eastandwestwind authored Aug 19, 2022
1 parent 56087af commit 5ce4308
Show file tree
Hide file tree
Showing 7 changed files with 657 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The types of changes are:
""
* Adds infra for email config and dispatch [#1059](https://github.com/ethyca/fidesops/pull/1059)
* Add an endpoint that allows you to create a Saas connector and all supporting resources with a single request [#1076](https://github.com/ethyca/fidesops/pull/1076)
* Adds tests for email endpoints and service [#1112](https://github.com/ethyca/fidesops/pull/1112)

### Developer Experience

Expand Down
14 changes: 8 additions & 6 deletions src/fidesops/ops/api/v1/endpoints/email_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from typing import Optional

from fastapi import Depends, Security
from fastapi_pagination import Page, Params, paginate
from fastapi_pagination import Page, Params
from fastapi_pagination.bases import AbstractPage
from fastapi_pagination.ext.sqlalchemy import paginate
from sqlalchemy.orm import Session
from starlette.exceptions import HTTPException
from starlette.status import (
Expand All @@ -12,6 +13,7 @@
HTTP_400_BAD_REQUEST,
HTTP_404_NOT_FOUND,
HTTP_422_UNPROCESSABLE_ENTITY,
HTTP_500_INTERNAL_SERVER_ERROR,
)

from fidesops.ops.api import deps
Expand Down Expand Up @@ -75,7 +77,7 @@ def post_config(
except Exception as exc:
logger.warning(f"Create failed for email config {email_config.key}: {exc}")
raise HTTPException(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Config with key {email_config.key} failed to be added",
)

Expand All @@ -97,16 +99,16 @@ def patch_config_by_key(
try:
return update_email_config(db=db, key=config_key, config=email_config)
except EmailConfigNotFoundException:
logger.warning(f"No email config found with key {email_config.key}")
logger.warning(f"No email config found with key {config_key}")
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail=f"No email config found with key {email_config.key}",
status_code=HTTP_404_NOT_FOUND,
detail=f"No email config found with key {config_key}",
)

except Exception as exc:
logger.warning(f"Patch failed for email config {email_config.key}: {exc}")
raise HTTPException(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Config with key {email_config.key} failed to be added",
)

Expand Down
34 changes: 1 addition & 33 deletions src/fidesops/ops/schemas/email/email.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum
from typing import Any, Dict, Optional, Union

from pydantic import BaseModel, Extra, ValidationError, validator
from pydantic import BaseModel, Extra

from fidesops.ops.schemas import Msg
from fidesops.ops.schemas.shared_schemas import FidesOpsKey
Expand Down Expand Up @@ -94,38 +94,6 @@ class Config:
use_enum_values = False
orm_mode = True

@validator("details", pre=True, always=True)
def validate_details(
cls,
v: Dict[str, str],
values: Dict[str, Any],
) -> Dict[str, str]:
"""
Custom validation logic for the `details` field.
"""
service_type = values.get("service_type")
if not service_type:
raise ValueError("A `service_type` field must be specified.")

try:
schema = {
EmailServiceType.MAILGUN: EmailServiceDetailsMailgun,
}[service_type]
except KeyError:
raise ValueError(
f"`service type` {service_type} has no supported `details` validation."
)

try:
schema.parse_obj(v) # type: ignore
except ValidationError as exc:
# Pydantic requires validators raise either a ValueError, TypeError, or AssertionError
# so this exception is cast into a `ValueError`.
errors = [f"{err['msg']} {str(err['loc'])}" for err in exc.errors()]
raise ValueError(errors)

return v


class EmailConfigResponse(BaseModel):
"""Email Config Response Schema"""
Expand Down
4 changes: 2 additions & 2 deletions src/fidesops/ops/service/email/email_dispatch_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@ def _mailgun_dispatcher(
f"Email failed to send with status code {response.status_code}"
)
except Exception as e:
logger.error(e)
raise EmailDispatchException(format("Email failed to send: %s", str(e)))
logger.error("Email failed to send: %s", str(e))
raise EmailDispatchException(f"Email failed to send due to: {e}")
Loading

0 comments on commit 5ce4308

Please sign in to comment.