mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-02 17:48:36 -04:00
Fixed test_get_notification.py tests
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
from datetime import timezone
|
||||
from datetime import timezone, datetime
|
||||
from uuid import UUID
|
||||
|
||||
from marshmallow import Schema, fields, pre_dump
|
||||
from marshmallow import EXCLUDE, Schema, fields, post_dump, pre_dump
|
||||
|
||||
from app.schemas import FlexibleDateTime, JobSchema, TemplateSchema
|
||||
from app import ma
|
||||
|
||||
class PublicTemplateSchema(Schema):
|
||||
id = fields.UUID(required=True)
|
||||
@@ -35,36 +38,63 @@ class PublicNotificationSchema(Schema):
|
||||
body = fields.String(required=True)
|
||||
content_char_count = fields.Integer(required=True)
|
||||
|
||||
@pre_dump
|
||||
def transform(self, notification, **kwargs):
|
||||
|
||||
class PublicNotificationResponseSchema(PublicNotificationSchema):
|
||||
class Meta:
|
||||
unknown = EXCLUDE
|
||||
|
||||
@post_dump
|
||||
def transform(self, data, **kwargs):
|
||||
def to_rfc3339(dt):
|
||||
if dt is None:
|
||||
return None
|
||||
if isinstance(dt, str):
|
||||
try:
|
||||
dt = datetime.fromisoformat(dt)
|
||||
except ValueError:
|
||||
return dt # fallback, might already be valid
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
return dt.isoformat().replace("+00:00", "Z")
|
||||
|
||||
return {
|
||||
**notification.__dict__,
|
||||
"created_at": to_rfc3339(getattr(notification, "created_at", None)),
|
||||
"sent_at": to_rfc3339(getattr(notification, "sent_at", None)),
|
||||
"updated_at": to_rfc3339(getattr(notification, "updated_at", None)),
|
||||
"sent_by": getattr(notification, "sent_by", None),
|
||||
"reference": getattr(notification, "reference", None),
|
||||
"service": str(notification.service.id) if notification.service else None,
|
||||
"api_key": str(notification.api_key.id) if notification.api_key else None,
|
||||
"body": getattr(notification, "body", None)
|
||||
or (notification.template.content if notification.template else ""),
|
||||
"content_char_count": len(
|
||||
getattr(notification, "body", "")
|
||||
or (notification.template.content if notification.template else "")
|
||||
),
|
||||
"job": (
|
||||
{
|
||||
"id": str(notification.job.id),
|
||||
"original_file_name": notification.job.original_file_name,
|
||||
}
|
||||
if hasattr(notification, "job") and notification.job
|
||||
else None
|
||||
),
|
||||
}
|
||||
data["created_at"] = to_rfc3339(data.get("created_at"))
|
||||
data["sent_at"] = to_rfc3339(data.get("sent_at"))
|
||||
data["updated_at"] = to_rfc3339(data.get("updated_at"))
|
||||
|
||||
# Fallback content
|
||||
template = data.get("template", {})
|
||||
body = data.get("body") or (template.get("content") if isinstance(template, dict) else "")
|
||||
data["body"] = body or ""
|
||||
data["content_char_count"] = len(data["body"])
|
||||
|
||||
# Extract UUID string for service
|
||||
service = data.get("service")
|
||||
if hasattr(service, "id"):
|
||||
data["service"] = str(service.id)
|
||||
elif isinstance(service, UUID):
|
||||
data["service"] = str(service)
|
||||
elif isinstance(service, str) and service.startswith("<Service "):
|
||||
# fallback if __str__ was called on the SQLAlchemy object
|
||||
data["service"] = service.split("<Service ")[1].rstrip(">")
|
||||
else:
|
||||
data["service"] = str(service) # best effort fallback
|
||||
|
||||
# Extract UUID string for api_key
|
||||
api_key = data.get("api_key")
|
||||
if hasattr(api_key, "id"):
|
||||
data["api_key"] = str(api_key.id)
|
||||
elif isinstance(api_key, UUID):
|
||||
data["api_key"] = str(api_key)
|
||||
elif isinstance(api_key, str) and api_key.startswith("<ApiKey "):
|
||||
data["api_key"] = api_key.split("<ApiKey ")[1].rstrip(">")
|
||||
else:
|
||||
data["api_key"] = str(api_key) if api_key else None
|
||||
|
||||
# Fix job dict
|
||||
job = data.get("job")
|
||||
if isinstance(job, dict) and "id" in job:
|
||||
job_id = job.get("id")
|
||||
job["id"] = str(job_id) if job_id else None
|
||||
data["job"] = job
|
||||
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user