2016-05-06 15:42:43 +01:00
|
|
|
import json
|
2023-05-31 17:52:16 -07:00
|
|
|
from datetime import datetime
|
2021-03-10 13:55:06 +00:00
|
|
|
|
2016-05-06 15:42:43 +01:00
|
|
|
from flask import url_for
|
2021-03-10 13:55:06 +00:00
|
|
|
|
2016-05-06 15:42:43 +01:00
|
|
|
from app.dao.templates_dao import dao_update_template
|
2024-02-21 14:14:45 -05:00
|
|
|
from app.enums import TemplateProcessType
|
2024-05-23 13:59:51 -07:00
|
|
|
from app.utils import utc_now
|
2021-08-04 15:12:09 +01:00
|
|
|
from tests import create_admin_authorization_header
|
2016-05-06 15:42:43 +01:00
|
|
|
|
|
|
|
|
|
2016-05-10 14:55:59 +01:00
|
|
|
def test_template_history_version(notify_api, sample_user, sample_template):
|
2016-05-06 15:42:43 +01:00
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2021-08-04 15:12:09 +01:00
|
|
|
auth_header = create_admin_authorization_header()
|
2016-05-06 15:42:43 +01:00
|
|
|
endpoint = url_for(
|
2023-08-29 14:54:30 -07:00
|
|
|
"template.get_template_version",
|
2016-05-06 15:42:43 +01:00
|
|
|
service_id=sample_template.service.id,
|
|
|
|
|
template_id=sample_template.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
version=1,
|
|
|
|
|
)
|
2016-05-06 15:42:43 +01:00
|
|
|
resp = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
endpoint, headers=[("Content-Type", "application/json"), auth_header]
|
2016-05-06 15:42:43 +01:00
|
|
|
)
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
assert json_resp["data"]["id"] == str(sample_template.id)
|
|
|
|
|
assert json_resp["data"]["content"] == sample_template.content
|
|
|
|
|
assert json_resp["data"]["version"] == 1
|
2024-02-21 14:14:45 -05:00
|
|
|
assert json_resp["data"]["process_type"] == TemplateProcessType.NORMAL
|
2023-08-29 14:54:30 -07:00
|
|
|
assert json_resp["data"]["created_by"]["name"] == sample_user.name
|
|
|
|
|
assert (
|
|
|
|
|
datetime.strptime(
|
|
|
|
|
json_resp["data"]["created_at"], "%Y-%m-%d %H:%M:%S.%f"
|
|
|
|
|
).date()
|
2024-05-23 13:59:51 -07:00
|
|
|
== utc_now().date()
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2016-05-06 15:42:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_previous_template_history_version(notify_api, sample_template):
|
|
|
|
|
old_content = sample_template.content
|
|
|
|
|
sample_template.content = "New content"
|
2024-02-21 14:50:36 -05:00
|
|
|
sample_template.process_type = TemplateProcessType.PRIORITY
|
2016-05-06 15:42:43 +01:00
|
|
|
dao_update_template(sample_template)
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2021-08-04 15:12:09 +01:00
|
|
|
auth_header = create_admin_authorization_header()
|
2016-05-06 15:42:43 +01:00
|
|
|
endpoint = url_for(
|
2023-08-29 14:54:30 -07:00
|
|
|
"template.get_template_version",
|
2016-05-06 15:42:43 +01:00
|
|
|
service_id=sample_template.service.id,
|
|
|
|
|
template_id=sample_template.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
version=1,
|
|
|
|
|
)
|
2016-05-06 15:42:43 +01:00
|
|
|
resp = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
endpoint, headers=[("Content-Type", "application/json"), auth_header]
|
2016-05-06 15:42:43 +01:00
|
|
|
)
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
assert json_resp["data"]["id"] == str(sample_template.id)
|
|
|
|
|
assert json_resp["data"]["version"] == 1
|
|
|
|
|
assert json_resp["data"]["content"] == old_content
|
2024-02-21 14:14:45 -05:00
|
|
|
assert json_resp["data"]["process_type"] == TemplateProcessType.NORMAL
|
2016-05-06 15:42:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_404_missing_template_version(notify_api, sample_template):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2021-08-04 15:12:09 +01:00
|
|
|
auth_header = create_admin_authorization_header()
|
2016-05-06 15:42:43 +01:00
|
|
|
endpoint = url_for(
|
2023-08-29 14:54:30 -07:00
|
|
|
"template.get_template_version",
|
2016-05-06 15:42:43 +01:00
|
|
|
service_id=sample_template.service.id,
|
|
|
|
|
template_id=sample_template.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
version=2,
|
|
|
|
|
)
|
2016-05-06 15:42:43 +01:00
|
|
|
resp = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
endpoint, headers=[("Content-Type", "application/json"), auth_header]
|
2016-05-06 15:42:43 +01:00
|
|
|
)
|
|
|
|
|
assert resp.status_code == 404
|
2016-05-09 15:59:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_all_versions_of_template(notify_api, sample_template):
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
old_content = sample_template.content
|
|
|
|
|
newer_content = "Newer content"
|
|
|
|
|
newest_content = "Newest content"
|
|
|
|
|
sample_template.content = newer_content
|
|
|
|
|
dao_update_template(sample_template)
|
|
|
|
|
sample_template.content = newest_content
|
|
|
|
|
dao_update_template(sample_template)
|
2021-08-04 15:12:09 +01:00
|
|
|
auth_header = create_admin_authorization_header()
|
2016-05-09 15:59:34 +01:00
|
|
|
endpoint = url_for(
|
2023-08-29 14:54:30 -07:00
|
|
|
"template.get_template_versions",
|
2016-05-09 15:59:34 +01:00
|
|
|
service_id=sample_template.service.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
template_id=sample_template.id,
|
2016-05-09 15:59:34 +01:00
|
|
|
)
|
|
|
|
|
resp = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
endpoint, headers=[("Content-Type", "application/json"), auth_header]
|
2016-05-09 15:59:34 +01:00
|
|
|
)
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
assert len(json_resp["data"]) == 3
|
|
|
|
|
assert json_resp["data"][0]["content"] == newest_content
|
|
|
|
|
assert json_resp["data"][0]["updated_at"]
|
|
|
|
|
assert json_resp["data"][1]["content"] == newer_content
|
|
|
|
|
assert json_resp["data"][1]["updated_at"]
|
|
|
|
|
assert json_resp["data"][2]["content"] == old_content
|