Skip to content

Commit dc1a19f

Browse files
committed
copilot suggestions implemented, removed process_type, added schema
1 parent b935674 commit dc1a19f

3 files changed

Lines changed: 66 additions & 13 deletions

File tree

app/v3/template/get_template.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from flask import jsonify
22

3-
from app import api_user, authenticated_service
3+
from app import DATETIME_FORMAT, api_user, authenticated_service
44
from app.dao import templates_dao
55
from app.errors import InvalidRequest
66
from app.models import ApiKeyPermission
@@ -24,14 +24,13 @@ def _serialize_template(template) -> dict:
2424
"id": str(template.id),
2525
"name": template.name,
2626
"type": template.template_type,
27-
"created_at": template.created_at.isoformat(),
28-
"updated_at": template.updated_at.isoformat() if template.updated_at else None,
27+
"created_at": template.created_at.strftime(DATETIME_FORMAT),
28+
"updated_at": template.updated_at.strftime(DATETIME_FORMAT) if template.updated_at else None,
2929
"created_by": template.created_by.email_address,
3030
"version": template.version,
3131
"body": template.content,
3232
"subject": template.subject if template.template_type != "sms" else None,
3333
"postage": template.postage,
34-
"process_type": template.process_type,
3534
"template_category_id": str(template.template_category_id) if template.template_category_id else None,
3635
"folder_id": folder_id,
3736
"archived": template.archived,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from app.models import TEMPLATE_TYPES
2+
from app.schema_validation.definitions import uuid
3+
4+
get_template_by_id_request = {
5+
"$schema": "http://json-schema.org/draft-04/schema#",
6+
"description": "schema for parameters allowed when getting template by id",
7+
"type": "object",
8+
"properties": {"id": uuid},
9+
"required": ["id"],
10+
"additionalProperties": False,
11+
}
12+
13+
get_template_by_id_response = {
14+
"$schema": "http://json-schema.org/draft-04/schema#",
15+
"description": "GET template by id schema response",
16+
"type": "object",
17+
"title": "response v3/template",
18+
"properties": {
19+
"id": uuid,
20+
"type": {"enum": TEMPLATE_TYPES},
21+
"created_at": {
22+
"format": "date-time",
23+
"type": "string",
24+
"description": "Date+time created",
25+
},
26+
"updated_at": {
27+
"format": "date-time",
28+
"type": ["string", "null"],
29+
"description": "Date+time updated",
30+
},
31+
"created_by": {"type": "string"},
32+
"version": {"type": "integer"},
33+
"body": {"type": "string"},
34+
"subject": {"type": ["string", "null"]},
35+
"name": {"type": "string"},
36+
"postage": {"type": ["string", "null"]},
37+
"template_category_id": {"type": ["string", "null"]},
38+
"folder_id": {"type": ["string", "null"]},
39+
"archived": {"type": "boolean"},
40+
},
41+
"required": [
42+
"id",
43+
"type",
44+
"created_at",
45+
"updated_at",
46+
"version",
47+
"created_by",
48+
"body",
49+
"name",
50+
"archived",
51+
],
52+
}

tests/app/v3/template/test_get_template.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from notifications_python_client.authentication import create_jwt_token
55

66
from app.models import EMAIL_TYPE, SMS_TYPE
7-
from tests.app.db import create_template
7+
from tests.app.db import create_service, create_template
88

99

1010
def _auth_header(api_key):
@@ -48,7 +48,9 @@ def test_get_template_returns_richer_fields_than_v2(self, client, sample_service
4848
assert "archived" in data
4949
assert "template_category_id" in data
5050
assert "folder_id" in data
51-
assert "process_type" in data
51+
52+
# process_type is intentionally NOT exposed in v3
53+
assert "process_type" not in data
5254

5355
# Core fields still present
5456
assert "id" in data
@@ -73,7 +75,8 @@ def test_get_template_returns_403_without_manage_templates_permission(self, clie
7375

7476
assert response.status_code == 403
7577
data = json.loads(response.get_data(as_text=True))
76-
assert "manage templates" in data["message"].lower() or "manage_templates" in data["message"].lower()
78+
message = data["errors"][0]["message"].lower()
79+
assert "manage templates" in message or "manage_templates" in message
7780

7881
def test_get_template_returns_404_for_nonexistent_template(self, client, sample_service, create_api_key_with_manage_api_perm):
7982
nonexistent_id = uuid.uuid4()
@@ -87,20 +90,19 @@ def test_get_template_returns_404_for_nonexistent_template(self, client, sample_
8790
assert response.status_code == 404
8891

8992
def test_get_template_returns_404_for_template_belonging_to_other_service(
90-
self, client, sample_service, sample_template, create_api_key_with_manage_api_perm
93+
self, client, sample_service, create_api_key_with_manage_api_perm
9194
):
9295
"""Templates from other services must not be accessible."""
96+
other_service = create_service(service_name=f"other service {uuid.uuid4()}")
97+
other_template = create_template(other_service, template_type=SMS_TYPE)
9398
auth_header = _auth_header(create_api_key_with_manage_api_perm)
9499

95100
response = client.get(
96-
f"/v3/template/{sample_template.id}",
101+
f"/v3/template/{other_template.id}",
97102
headers=[("Content-Type", "application/json"), auth_header],
98103
)
99104

100-
# sample_template belongs to sample_service so this should work;
101-
# if services differ the DAO raises NoResultFound → 404
102-
# This test just verifies service scoping is enforced.
103-
assert response.status_code in (200, 404)
105+
assert response.status_code == 404
104106

105107
def test_get_sms_template_has_null_subject(self, client, sample_service, create_api_key_with_manage_api_perm):
106108
template = create_template(sample_service, template_type=SMS_TYPE)

0 commit comments

Comments
 (0)