Skip to content
Snippets Groups Projects
Commit 6ef7aab0 authored by Jonas Braathen's avatar Jonas Braathen
Browse files

Changes the notification format a little

- The field 'source' is now formatted like 'greg:instance_name:environment'
- The field 'type' is now formatted like 'object_type.operation'
- The field 'data' now contains the identifiers of all objects related to the change
parent a0ff58ea
No related branches found
No related tags found
1 merge request!115Greg 86 notification format changes
...@@ -26,7 +26,7 @@ def generate_event_type(n: Notification) -> str: ...@@ -26,7 +26,7 @@ def generate_event_type(n: Notification) -> str:
routing key is removed results in minor changes to the code. routing key is removed results in minor changes to the code.
""" """
object_type = camel_to_snake(n.object_type) object_type = camel_to_snake(n.object_type)
return f"{settings.INTERNAL_RK_PREFIX}.{object_type}.{n.operation}" return f"{object_type}.{n.operation}"
def create_cloud_event_payload(n: Notification) -> str: def create_cloud_event_payload(n: Notification) -> str:
...@@ -52,14 +52,12 @@ def create_cloud_event_payload(n: Notification) -> str: ...@@ -52,14 +52,12 @@ def create_cloud_event_payload(n: Notification) -> str:
object_type = camel_to_snake(n.object_type) object_type = camel_to_snake(n.object_type)
content: dict[str, Union[str, dict[str, str]]] = { content: dict[str, Union[str, dict[str, str]]] = {
"id": str(n.id), "id": str(n.id),
"source": f"urn:greg:{settings.INSTANCE_NAME}:{object_type}:{n.identifier}", "source": f"greg:{settings.INSTANCE_NAME}:{settings.ENVIRONMENT}",
"specversion": "1.0", "specversion": "1.0",
"type": generate_event_type(n), "type": generate_event_type(n),
} }
if n.object_type in ("Role", "Consent", "Identity"): if n.meta:
content["data"] = { content["data"] = n.meta
"person_id": n.meta.get("person_id"),
}
return json.dumps(content) return json.dumps(content)
......
...@@ -167,15 +167,22 @@ def m2m_changed_notification_callback( ...@@ -167,15 +167,22 @@ def m2m_changed_notification_callback(
def _create_metadata(instance) -> Dict: def _create_metadata(instance) -> Dict:
meta = {} meta = {}
if isinstance(instance, Person):
if isinstance(instance, Role): meta["person_id"] = instance.id
elif isinstance(instance, Role):
meta["person_id"] = instance.person.id meta["person_id"] = instance.person.id
meta["type_id"] = instance.type.id meta["role_id"] = instance.id
if isinstance(instance, Identity): meta["role_type"] = instance.type.identifier
meta["role_type_id"] = instance.type.id
elif isinstance(instance, RoleType):
meta["role_type_id"] = instance.id
elif isinstance(instance, Identity):
meta["person_id"] = instance.person.id meta["person_id"] = instance.person.id
meta["identity_id"] = instance.id meta["identity_id"] = instance.id
if isinstance(instance, Consent): meta["identity_type"] = instance.type
elif isinstance(instance, Consent):
meta["person_id"] = instance.person.id meta["person_id"] = instance.person.id
meta["consent_id"] = instance.type.id meta["consent_id"] = instance.id
meta["consent_type"] = instance.type.identifier
meta["consent_type_id"] = instance.type.id
return meta return meta
import json
import pytest import pytest
from greg.management.commands.notification_publisher import ( from greg.management.commands.notification_publisher import (
handle_one_notification, handle_one_notification,
create_cloud_event_payload,
) )
from greg.models import Notification from greg.models import Notification
@pytest.fixture
def role_type_notification():
Notification.objects.create(
identifier=5,
object_type="RoleType",
operation="update",
issued_at=1234,
meta={
"integer": 123,
"string": "foo",
},
)
return Notification.objects.get(id=1)
@pytest.mark.django_db @pytest.mark.django_db
def test_handle_one_notification(notification, pcm_mock): def test_handle_one_notification(role_type_notification, pcm_mock):
"""Check that Notifications are deleted when published to message queue""" """Check that Notifications are deleted when published to message queue"""
assert Notification.objects.count() == 1 assert Notification.objects.count() == 1
handle_one_notification(notification, pcm_mock, exchange="bar") handle_one_notification(role_type_notification, pcm_mock, exchange="bar")
assert Notification.objects.count() == 0 assert Notification.objects.count() == 0
@pytest.mark.django_db
def test_create_cloud_event_payload(role_type_notification):
payload = create_cloud_event_payload(role_type_notification)
assert json.loads(payload) == {
"id": "1",
"source": "greg:local:unittest",
"specversion": "1.0",
"type": "role_type.update",
"data": {"integer": 123, "string": "foo"},
}
...@@ -68,7 +68,7 @@ def test_role_add_notification( ...@@ -68,7 +68,7 @@ def test_role_add_notification(
org_unit_bar: OrganizationalUnit, org_unit_bar: OrganizationalUnit,
sponsor: Sponsor, sponsor: Sponsor,
): ):
Role.objects.create( role = Role.objects.create(
person=person, person=person,
type=role_type_foo, type=role_type_foo,
start_date="2021-05-06", start_date="2021-05-06",
...@@ -79,9 +79,10 @@ def test_role_add_notification( ...@@ -79,9 +79,10 @@ def test_role_add_notification(
notifications = Notification.objects.filter(object_type="Role") notifications = Notification.objects.filter(object_type="Role")
assert len(notifications) == 1 assert len(notifications) == 1
assert notifications[0].operation == "add" assert notifications[0].operation == "add"
meta_data = notifications[0].meta meta = notifications[0].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["type_id"] == role_type_foo.id assert meta["role_id"] == role.id
assert meta["role_type"] == role_type_foo.identifier
@pytest.mark.django_db @pytest.mark.django_db
...@@ -91,7 +92,7 @@ def test_role_update_notification( ...@@ -91,7 +92,7 @@ def test_role_update_notification(
org_unit_bar: OrganizationalUnit, org_unit_bar: OrganizationalUnit,
sponsor: Sponsor, sponsor: Sponsor,
): ):
Role.objects.create( role = Role.objects.create(
person=person, person=person,
type=role_type_foo, type=role_type_foo,
start_date="2021-05-06", start_date="2021-05-06",
...@@ -106,9 +107,10 @@ def test_role_update_notification( ...@@ -106,9 +107,10 @@ def test_role_update_notification(
notifications = Notification.objects.filter(object_type="Role") notifications = Notification.objects.filter(object_type="Role")
assert len(notifications) == 2 assert len(notifications) == 2
assert notifications[1].operation == "update" assert notifications[1].operation == "update"
meta_data = notifications[1].meta meta = notifications[1].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["type_id"] == role_type_foo.id assert meta["role_id"] == role.id
assert meta["role_type"] == role_type_foo.identifier
@pytest.mark.django_db @pytest.mark.django_db
...@@ -118,7 +120,7 @@ def test_role_delete_notification( ...@@ -118,7 +120,7 @@ def test_role_delete_notification(
org_unit_bar: OrganizationalUnit, org_unit_bar: OrganizationalUnit,
sponsor: Sponsor, sponsor: Sponsor,
): ):
Role.objects.create( role = Role.objects.create(
person=person, person=person,
type=role_type_foo, type=role_type_foo,
start_date="2021-05-06", start_date="2021-05-06",
...@@ -132,27 +134,31 @@ def test_role_delete_notification( ...@@ -132,27 +134,31 @@ def test_role_delete_notification(
notifications = Notification.objects.filter(object_type="Role") notifications = Notification.objects.filter(object_type="Role")
assert len(notifications) == 2 assert len(notifications) == 2
assert notifications[1].operation == "delete" assert notifications[1].operation == "delete"
meta_data = notifications[1].meta meta = notifications[1].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["type_id"] == role_type_foo.id assert meta["role_id"] == role.id
assert meta["role_type"] == role_type_foo.identifier
assert meta["role_type_id"] == role_type_foo.id
@pytest.mark.django_db @pytest.mark.django_db
def test_consent_add_notification(person: Person, consent_type: ConsentType): def test_consent_add_notification(person: Person, consent_type: ConsentType):
Consent.objects.create( consent = Consent.objects.create(
person=person, type=consent_type, consent_given_at="2021-06-20" person=person, type=consent_type, consent_given_at="2021-06-20"
) )
notifications = Notification.objects.filter(object_type="Consent") notifications = Notification.objects.filter(object_type="Consent")
assert len(notifications) == 1 assert len(notifications) == 1
assert notifications[0].identifier == person.id assert notifications[0].identifier == person.id
meta_data = notifications[0].meta meta = notifications[0].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["consent_id"] == consent_type.id assert meta["consent_id"] == consent.id
assert meta["consent_type"] == consent.type.identifier
assert meta["consent_type_id"] == consent.type.id
@pytest.mark.django_db @pytest.mark.django_db
def test_consent_update_notification(person: Person, consent_type: ConsentType): def test_consent_update_notification(person: Person, consent_type: ConsentType):
Consent.objects.create( consent = Consent.objects.create(
person=person, type=consent_type, consent_given_at="2021-06-20" person=person, type=consent_type, consent_given_at="2021-06-20"
) )
consents = Consent.objects.filter(person=person, type=consent_type) consents = Consent.objects.filter(person=person, type=consent_type)
...@@ -162,14 +168,16 @@ def test_consent_update_notification(person: Person, consent_type: ConsentType): ...@@ -162,14 +168,16 @@ def test_consent_update_notification(person: Person, consent_type: ConsentType):
notifications = Notification.objects.filter(object_type="Consent") notifications = Notification.objects.filter(object_type="Consent")
assert len(notifications) == 2 assert len(notifications) == 2
assert notifications[0].identifier == person.id assert notifications[0].identifier == person.id
meta_data = notifications[0].meta meta = notifications[0].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["consent_id"] == consent_type.id assert meta["consent_id"] == consent.id
assert meta["consent_type"] == consent.type.identifier
assert meta["consent_type_id"] == consent.type.id
@pytest.mark.django_db @pytest.mark.django_db
def test_consent_delete_notification(person: Person, consent_type: ConsentType): def test_consent_delete_notification(person: Person, consent_type: ConsentType):
Consent.objects.create( consent = Consent.objects.create(
person=person, type=consent_type, consent_given_at="2021-06-20" person=person, type=consent_type, consent_given_at="2021-06-20"
) )
consents = Consent.objects.filter(person=person, type=consent_type) consents = Consent.objects.filter(person=person, type=consent_type)
...@@ -179,9 +187,11 @@ def test_consent_delete_notification(person: Person, consent_type: ConsentType): ...@@ -179,9 +187,11 @@ def test_consent_delete_notification(person: Person, consent_type: ConsentType):
assert len(notifications) == 2 assert len(notifications) == 2
assert notifications[1].identifier == person.id assert notifications[1].identifier == person.id
assert notifications[1].operation == "delete" assert notifications[1].operation == "delete"
meta_data = notifications[0].meta meta = notifications[0].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["consent_id"] == consent_type.id assert meta["consent_id"] == consent.id
assert meta["consent_type"] == consent_type.identifier
assert meta["consent_type_id"] == consent_type.id
@pytest.mark.django_db @pytest.mark.django_db
...@@ -193,9 +203,10 @@ def test_identity_add_notification( ...@@ -193,9 +203,10 @@ def test_identity_add_notification(
assert notifications[0].identifier == person.id assert notifications[0].identifier == person.id
assert notifications[0].operation == "add" assert notifications[0].operation == "add"
meta_data = notifications[0].meta meta = notifications[0].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["identity_id"] == identity.id assert meta["identity_id"] == identity.id
assert meta["identity_type"] == identity.type
@pytest.mark.django_db @pytest.mark.django_db
...@@ -212,6 +223,7 @@ def test_identity_update_notification( ...@@ -212,6 +223,7 @@ def test_identity_update_notification(
assert len(notifications) == 2 assert len(notifications) == 2
assert notifications[1].operation == "update" assert notifications[1].operation == "update"
meta_data = notifications[1].meta meta = notifications[1].meta
assert meta_data["person_id"] == person.id assert meta["person_id"] == person.id
assert meta_data["identity_id"] == identity.id assert meta["identity_id"] == identity.id
assert meta["identity_type"] == identity.type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment