Repository: mozmeao/snippets-service

Issue:
Return `targeting` JEXL expression from ASRSnippet.render()



Discussion:

Resolving patch:
diff --git a/snippets/base/models.py b/snippets/base/models.py
--- a/snippets/base/models.py
+++ b/snippets/base/models.py
@@ -600,7 +600,7 @@ class Meta:
     def __str__(self):
         return self.name
 
-    def render(self):
+    def render(self, preview=False):
         data = json.loads(self.data)
 
         # Add snippet ID to template variables.
@@ -622,6 +622,9 @@ def render(self):
             'content': data,
         }
 
+        if not preview:
+            rendered_snippet['targeting'] = self.target.jexl_expr
+
         return rendered_snippet
 
     def get_preview_url(self):
diff --git a/snippets/base/views.py b/snippets/base/views.py
--- a/snippets/base/views.py
+++ b/snippets/base/views.py
@@ -84,7 +84,7 @@ def preview_asr_snippet(request, uuid):
         raise Http404()
 
     bundle_content = json.dumps({
-        'messages': [snippet.render()],
+        'messages': [snippet.render(preview=True)],
     })
     return HttpResponse(bundle_content, content_type='application/json')
 


Test patch:
diff --git a/snippets/base/tests/test_models.py b/snippets/base/tests/test_models.py
--- a/snippets/base/tests/test_models.py
+++ b/snippets/base/tests/test_models.py
@@ -327,13 +327,31 @@ def test_snippets(self):
 
 class ASRSnippetTests(TestCase):
     def test_render(self):
-        """
-
-        """
         snippet = ASRSnippetFactory.create(
             template__code='<p>{{ text }} {{ foo }}</p>',
-            data='{"text": "snippet id [[snippet_id]]", "foo": "bar"}')
+            data='{"text": "snippet id [[snippet_id]]", "foo": "bar"}',
+            target__jexl_expr='foo == bar')
         generated_result = snippet.render()
+        expected_result = {
+            'id': str(snippet.id),
+            'template': snippet.template.code_name,
+            'template_version': snippet.template.version,
+            'campaign': snippet.campaign.slug,
+            'content': {
+                'text': 'snippet id {}'.format(snippet.id),
+                'foo': 'bar',
+                'links': {},
+            },
+            'targeting': 'foo == bar'
+        }
+        self.assertEqual(generated_result, expected_result)
+
+    def test_render_preview_only(self):
+        snippet = ASRSnippetFactory.create(
+            template__code='<p>{{ text }} {{ foo }}</p>',
+            data='{"text": "snippet id [[snippet_id]]", "foo": "bar"}',
+            target__jexl_expr='foo == bar')
+        generated_result = snippet.render(preview=True)
         expected_result = {
             'id': str(snippet.id),
             'template': snippet.template.code_name,
diff --git a/snippets/base/tests/test_views.py b/snippets/base/tests/test_views.py
--- a/snippets/base/tests/test_views.py
+++ b/snippets/base/tests/test_views.py
@@ -260,7 +260,7 @@ def test_base(self):
         with patch('snippets.base.views.ASRSnippet.render') as render_mock:
             render_mock.return_value = 'foo'
             response = self.client.get(url)
-        self.assertTrue(render_mock.called)
+        render_mock.assert_called_with(preview=True)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response['Content-Type'], 'application/json')