Skip to content

Commit

Permalink
Prevent downloads with partial data from being saved
Browse files Browse the repository at this point in the history
  • Loading branch information
edwoodward committed Oct 24, 2023
1 parent 80e330e commit df20c2d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
20 changes: 10 additions & 10 deletions salesforce/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ def create(self, validated_data):
resource_name = validated_data.get('resource_name', None)
contact_id = validated_data.get('contact_id', None)
rd = []
try:
rd = ResourceDownload.objects.filter(account_uuid=account_uuid, book=book)
if resource_name:
rd.filter(resource_name=resource_name)

rd = rd[0] # we only need the first result - but there might already be duplicates so this should handle that
rd.contact_id = contact_id
rd.save()
except (ResourceDownload.DoesNotExist, IndexError):
if book:
if book and contact_id:
try:
rd = ResourceDownload.objects.filter(account_uuid=account_uuid, book=book)
if resource_name:
rd.filter(resource_name=resource_name)

rd = rd[0] # we only need the first result - but there might already be duplicates so this should handle that
rd.contact_id = contact_id
rd.save()
except (ResourceDownload.DoesNotExist, IndexError):
rd = ResourceDownload.objects.create(**validated_data)

return rd
Expand Down
71 changes: 71 additions & 0 deletions salesforce/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

import vcr
import unittest
from unittest import skip
Expand All @@ -7,7 +9,10 @@
from django.test import LiveServerTestCase, TestCase, Client
from six import StringIO
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile

from books.models import BookIndex, Book
from pages.models import HomePage
from salesforce.models import Adopter, SalesforceSettings, MapBoxDataset, Partner, AdoptionOpportunityRecord, PartnerReview, SalesforceForms
from salesforce.views import Salesforce
from salesforce.salesforce import Salesforce as SF
Expand All @@ -18,6 +23,8 @@
from rest_framework.test import APIRequestFactory

from wagtail.test.utils import WagtailPageTests
from wagtail.models import Page
from wagtail.documents.models import Document

from shared.test_utilities import mock_user_login

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


class ResourceDownloadTest(TestCase):
def setUp(self):
self.client = Client()

@classmethod
def setUpTestData(cls):
# create root page
root_page = Page.objects.get(title="Root")
# create homepage
homepage = HomePage(title="Hello World",
slug="hello-world",
)
# add homepage to root page
root_page.add_child(instance=homepage)
# create book index page
book_index = BookIndex(title="Book Index",
page_description="Test",
dev_standard_1_description="Test",
dev_standard_2_description="Test",
dev_standard_3_description="Test",
dev_standard_4_description="Test",
)
# add book index to homepage
homepage.add_child(instance=book_index)
test_image = SimpleUploadedFile(name='openstax.png',
content=open("oxauth/static/images/openstax.png", 'rb').read())
cls.test_doc = Document.objects.create(title='Test Doc', file=test_image)

def test_resource_download_post(self):
root_page = Page.objects.get(title="Root")
book_index = BookIndex.objects.all()[0]
book = Book(title="University Physics",
slug="university-physics",
cnx_id='031da8d3-b525-429c-80cf-6c8ed997733a',
salesforce_book_id='',
description="Test Book",
cover=self.test_doc,
title_image=self.test_doc,
publish_date=datetime.date.today(),
locale=root_page.locale
)
book_index.add_child(instance=book)
data = {"book": book.pk,"book_format": "PDF","account_uuid": "310bb96b-0df8-4d10-a759-c7d366c1f524", "resource_name": "Book PDF", "contact_id": "0032f00003zYVdSAAZ"}
response = self.client.post('/apps/cms/api/salesforce/download-tracking/', data, format='json')
self.assertEqual("PDF", response.data['book_format'])

def test_resource_download_post_rejection(self):
root_page = Page.objects.get(title="Root")
book_index = BookIndex.objects.all()[0]
book = Book(title="Biology 2e",
slug="biology-2e",
cnx_id='031da8d3-b525-429c-80cf-6c8ed997744b',
salesforce_book_id='',
description="Test Book",
cover=self.test_doc,
title_image=self.test_doc,
publish_date=datetime.date.today(),
locale=root_page.locale
)
book_index.add_child(instance=book)
data = {"book": book.pk,"book_format": "PDF","account_uuid": "310bb96b-0df8-4d10-a759-c7d366c1f524", "resource_name": "Book PDF", "contact_id": ""}
response = self.client.post('/apps/cms/api/salesforce/download-tracking/', data, format='json')
self.assertEqual(None, response.data['book'])

0 comments on commit df20c2d

Please sign in to comment.