|
| 1 | +import datetime |
| 2 | + |
1 | 3 | import vcr
|
2 | 4 | import unittest
|
3 | 5 | from unittest import skip
|
|
7 | 9 | from django.test import LiveServerTestCase, TestCase, Client
|
8 | 10 | from six import StringIO
|
9 | 11 | from django.core.exceptions import ValidationError
|
| 12 | +from django.core.files.uploadedfile import SimpleUploadedFile |
10 | 13 |
|
| 14 | +from books.models import BookIndex, Book |
| 15 | +from pages.models import HomePage |
11 | 16 | from salesforce.models import Adopter, SalesforceSettings, MapBoxDataset, Partner, AdoptionOpportunityRecord, PartnerReview, SalesforceForms
|
12 | 17 | from salesforce.views import Salesforce
|
13 | 18 | from salesforce.salesforce import Salesforce as SF
|
|
18 | 23 | from rest_framework.test import APIRequestFactory
|
19 | 24 |
|
20 | 25 | from wagtail.test.utils import WagtailPageTests
|
| 26 | +from wagtail.models import Page |
| 27 | +from wagtail.documents.models import Document |
21 | 28 |
|
22 | 29 | from shared.test_utilities import mock_user_login
|
23 | 30 |
|
@@ -163,3 +170,67 @@ def test_query_opportunity_by_account_uuid(self):
|
163 | 170 | response = self.client.get('/apps/cms/api/salesforce/renewal?account_uuid=f826f1b1-ead5-4594-82b3-df9a2753cb43')
|
164 | 171 | self.assertIn(b'"students": "123"', response.content)
|
165 | 172 |
|
| 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