From 5b274cfc2ad234fb56d0a0b8eedc35e9ca595198 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Tue, 22 May 2018 15:16:34 -0400 Subject: [PATCH] include survey_spec in activity stream --- awx/api/serializers.py | 19 +++++++++++++ awx/main/signals.py | 4 +-- .../functional/models/test_activity_stream.py | 27 +++++++++++++------ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index c70eab3a25..531c6a869b 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -3117,6 +3117,15 @@ class JobTemplateSerializer(JobTemplateMixin, UnifiedJobTemplateSerializer, JobO return summary_fields +class JobTemplateWithSpecSerializer(JobTemplateSerializer): + ''' + Used for activity stream entries. + ''' + + class Meta: + model = JobTemplate + fields = ('*', 'survey_spec') + class JobSerializer(UnifiedJobSerializer, JobOptionsSerializer): @@ -3618,6 +3627,16 @@ class WorkflowJobTemplateSerializer(JobTemplateMixin, LabelsListMixin, UnifiedJo return vars_validate_or_raise(value) +class WorkflowJobTemplateWithSpecSerializer(WorkflowJobTemplateSerializer): + ''' + Used for activity stream entries. + ''' + + class Meta: + model = WorkflowJobTemplate + fields = ('*', 'survey_spec') + + class WorkflowJobSerializer(LabelsListMixin, UnifiedJobSerializer): class Meta: diff --git a/awx/main/signals.py b/awx/main/signals.py index 82eb41e18a..aa343fae42 100644 --- a/awx/main/signals.py +++ b/awx/main/signals.py @@ -396,7 +396,7 @@ model_serializer_mapping = { Credential: CredentialSerializer, Team: TeamSerializer, Project: ProjectSerializer, - JobTemplate: JobTemplateSerializer, + JobTemplate: JobTemplateWithSpecSerializer, Job: JobSerializer, AdHocCommand: AdHocCommandSerializer, NotificationTemplate: NotificationTemplateSerializer, @@ -404,7 +404,7 @@ model_serializer_mapping = { CredentialType: CredentialTypeSerializer, Schedule: ScheduleSerializer, Label: LabelSerializer, - WorkflowJobTemplate: WorkflowJobTemplateSerializer, + WorkflowJobTemplate: WorkflowJobTemplateWithSpecSerializer, WorkflowJobTemplateNode: WorkflowJobTemplateNodeSerializer, WorkflowJob: WorkflowJobSerializer, OAuth2AccessToken: OAuth2TokenSerializer, diff --git a/awx/main/tests/functional/models/test_activity_stream.py b/awx/main/tests/functional/models/test_activity_stream.py index eff901093e..1c59f1644d 100644 --- a/awx/main/tests/functional/models/test_activity_stream.py +++ b/awx/main/tests/functional/models/test_activity_stream.py @@ -15,9 +15,9 @@ from awx.main.models import ( ) # other AWX -from awx.main.utils import model_to_dict +from awx.main.utils import model_to_dict, model_instance_diff from awx.main.utils.common import get_allowed_fields -from awx.api.serializers import InventorySourceSerializer +from awx.main.signals import model_serializer_mapping # Django from django.contrib.auth.models import AnonymousUser @@ -26,11 +26,6 @@ from django.contrib.auth.models import AnonymousUser from crum import impersonate -model_serializer_mapping = { - InventorySource: InventorySourceSerializer -} - - class TestImplicitRolesOmitted: ''' Test that there is exactly 1 "create" entry in the activity stream for @@ -220,8 +215,24 @@ def test_modified_not_allowed_field(somecloud_type): activity_stream_registrar, but did not add its serializer to the model->serializer mapping. ''' - from awx.main.signals import model_serializer_mapping from awx.main.registrar import activity_stream_registrar for Model in activity_stream_registrar.models: assert 'modified' not in get_allowed_fields(Model(), model_serializer_mapping), Model + + +@pytest.mark.django_db +def test_survey_spec_create_entry(job_template, survey_spec_factory): + start_count = job_template.activitystream_set.count() + job_template.survey_spec = survey_spec_factory('foo') + job_template.save() + assert job_template.activitystream_set.count() == start_count + 1 + + +@pytest.mark.django_db +def test_survey_create_diff(job_template, survey_spec_factory): + old = JobTemplate.objects.get(pk=job_template.pk) + job_template.survey_spec = survey_spec_factory('foo') + before, after = model_instance_diff(old, job_template, model_serializer_mapping)['survey_spec'] + assert before == '{}' + assert json.loads(after) == survey_spec_factory('foo')