Skip to content

Commit e1695b2

Browse files
authored
Prevent downloads with partial data from being saved (#1516)
1 parent 540cd8d commit e1695b2

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

salesforce/serializers.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ def create(self, validated_data):
120120
resource_name = validated_data.get('resource_name', None)
121121
contact_id = validated_data.get('contact_id', None)
122122
rd = []
123-
try:
124-
rd = ResourceDownload.objects.filter(account_uuid=account_uuid, book=book)
125-
if resource_name:
126-
rd.filter(resource_name=resource_name)
127-
128-
rd = rd[0] # we only need the first result - but there might already be duplicates so this should handle that
129-
rd.contact_id = contact_id
130-
rd.save()
131-
except (ResourceDownload.DoesNotExist, IndexError):
132-
if book:
123+
if book and contact_id:
124+
try:
125+
rd = ResourceDownload.objects.filter(account_uuid=account_uuid, book=book)
126+
if resource_name:
127+
rd.filter(resource_name=resource_name)
128+
129+
rd = rd[0] # we only need the first result - but there might already be duplicates so this should handle that
130+
rd.contact_id = contact_id
131+
rd.save()
132+
except (ResourceDownload.DoesNotExist, IndexError):
133133
rd = ResourceDownload.objects.create(**validated_data)
134134

135135
return rd

salesforce/tests.py

+71
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
13
import vcr
24
import unittest
35
from unittest import skip
@@ -7,7 +9,10 @@
79
from django.test import LiveServerTestCase, TestCase, Client
810
from six import StringIO
911
from django.core.exceptions import ValidationError
12+
from django.core.files.uploadedfile import SimpleUploadedFile
1013

14+
from books.models import BookIndex, Book
15+
from pages.models import HomePage
1116
from salesforce.models import Adopter, SalesforceSettings, MapBoxDataset, Partner, AdoptionOpportunityRecord, PartnerReview, SalesforceForms
1217
from salesforce.views import Salesforce
1318
from salesforce.salesforce import Salesforce as SF
@@ -18,6 +23,8 @@
1823
from rest_framework.test import APIRequestFactory
1924

2025
from wagtail.test.utils import WagtailPageTests
26+
from wagtail.models import Page
27+
from wagtail.documents.models import Document
2128

2229
from shared.test_utilities import mock_user_login
2330

@@ -163,3 +170,67 @@ def test_query_opportunity_by_account_uuid(self):
163170
response = self.client.get('/apps/cms/api/salesforce/renewal?account_uuid=f826f1b1-ead5-4594-82b3-df9a2753cb43')
164171
self.assertIn(b'"students": "123"', response.content)
165172

173+
174+
class ResourceDownloadTest(TestCase):
175+
def setUp(self):
176+
self.client = Client()
177+
178+
@classmethod
179+
def setUpTestData(cls):
180+
# create root page
181+
root_page = Page.objects.get(title="Root")
182+
# create homepage
183+
homepage = HomePage(title="Hello World",
184+
slug="hello-world",
185+
)
186+
# add homepage to root page
187+
root_page.add_child(instance=homepage)
188+
# create book index page
189+
book_index = BookIndex(title="Book Index",
190+
page_description="Test",
191+
dev_standard_1_description="Test",
192+
dev_standard_2_description="Test",
193+
dev_standard_3_description="Test",
194+
dev_standard_4_description="Test",
195+
)
196+
# add book index to homepage
197+
homepage.add_child(instance=book_index)
198+
test_image = SimpleUploadedFile(name='openstax.png',
199+
content=open("oxauth/static/images/openstax.png", 'rb').read())
200+
cls.test_doc = Document.objects.create(title='Test Doc', file=test_image)
201+
202+
def test_resource_download_post(self):
203+
root_page = Page.objects.get(title="Root")
204+
book_index = BookIndex.objects.all()[0]
205+
book = Book(title="University Physics",
206+
slug="university-physics",
207+
cnx_id='031da8d3-b525-429c-80cf-6c8ed997733a',
208+
salesforce_book_id='',
209+
description="Test Book",
210+
cover=self.test_doc,
211+
title_image=self.test_doc,
212+
publish_date=datetime.date.today(),
213+
locale=root_page.locale
214+
)
215+
book_index.add_child(instance=book)
216+
data = {"book": book.pk,"book_format": "PDF","account_uuid": "310bb96b-0df8-4d10-a759-c7d366c1f524", "resource_name": "Book PDF", "contact_id": "0032f00003zYVdSAAZ"}
217+
response = self.client.post('/apps/cms/api/salesforce/download-tracking/', data, format='json')
218+
self.assertEqual("PDF", response.data['book_format'])
219+
220+
def test_resource_download_post_rejection(self):
221+
root_page = Page.objects.get(title="Root")
222+
book_index = BookIndex.objects.all()[0]
223+
book = Book(title="Biology 2e",
224+
slug="biology-2e",
225+
cnx_id='031da8d3-b525-429c-80cf-6c8ed997744b',
226+
salesforce_book_id='',
227+
description="Test Book",
228+
cover=self.test_doc,
229+
title_image=self.test_doc,
230+
publish_date=datetime.date.today(),
231+
locale=root_page.locale
232+
)
233+
book_index.add_child(instance=book)
234+
data = {"book": book.pk,"book_format": "PDF","account_uuid": "310bb96b-0df8-4d10-a759-c7d366c1f524", "resource_name": "Book PDF", "contact_id": ""}
235+
response = self.client.post('/apps/cms/api/salesforce/download-tracking/', data, format='json')
236+
self.assertEqual(None, response.data['book'])

0 commit comments

Comments
 (0)