from io import StringIO import os import shutil import tempfile from zipfile import ZipFile from django.conf import settings from django.contrib.auth import get_user_model from django.core.exceptions import ValidationError from django.test import TestCase from django.test.utils import override_settings from django.urls import reverse from pycon.sponsorship.models import Benefit, Sponsor, SponsorBenefit,\ SponsorLevel from symposion.conference.models import current_conference User = get_user_model() class TestSponsorZipDownload(TestCase): def setUp(self): self.user = User.objects.create_user(username='joe', email='joe@example.com', password='joe') self.user.is_staff = True self.user.save() self.url = reverse("sponsor_zip_logos") self.assertTrue(self.client.login(username='joe@example.com', password='joe')) # we need a sponsor conference = current_conference() self.sponsor_level = SponsorLevel.objects.create( conference=conference, name="Lead", cost=1) self.sponsor = Sponsor.objects.create( name="Big Daddy", level=self.sponsor_level, active=True, ) # Create our benefits, of various types self.text_benefit = Benefit.objects.create(name="text", type="text") self.file_benefit = Benefit.objects.create(name="file", type="file") # These names must be spelled exactly this way: self.weblogo_benefit = Benefit.objects.create(name="Web logo", type="weblogo") self.printlogo_benefit = Benefit.objects.create(name="Print logo", type="file") self.advertisement_benefit = Benefit.objects.create(name="Advertisement", type="file") def validate_response(self, rsp, names_and_sizes): # Ensure a response from the view looks right, contains a valid # zip archive, has files with the right names and sizes. self.assertEqual("application/zip", rsp['Content-type']) prefix = settings.CONFERENCE_URL_PREFIXES[settings.CONFERENCE_ID] self.assertEqual( 'attachment; filename="pycon_%s_sponsorlogos.zip"' % prefix, rsp['Content-Disposition']) zipfile = ZipFile(StringIO(rsp.content), "r") # Check out the zip - testzip() returns None if no errors found self.assertIsNone(zipfile.testzip()) # Compare contents to what is expected infolist = zipfile.infolist() self.assertEqual(len(names_and_sizes), len(infolist)) for info, name_and_size in zip(infolist, names_and_sizes): name, size = name_and_size self.assertEqual(name, info.filename) self.assertEqual(size, info.file_size) def make_temp_file(self, name, size=0): # Create a temp file with the given name and size under self.temp_dir path = os.path.join(self.temp_dir, name) with open(path, "wb") as f: f.write(size * "x") def test_must_be_logged_in(self): # Must be logged in to use the view # If not logged in, doesn't redirect, just serves up a login view self.client.logout() rsp = self.client.get(self.url) self.assertEqual(200, rsp.status_code) self.assertIn("""
""", rsp.content) def test_must_be_staff(self): # Only staff can use the view # If not staff, doesn't show error, just serves up a login view # Also, the dashboard doesn't show the download button self.user.is_staff = False self.user.save() rsp = self.client.get(self.url) self.assertEqual(200, rsp.status_code) self.assertIn("""""", rsp.content) rsp = self.client.get(reverse('dashboard')) self.assertNotIn(self.url, rsp.content) def test_no_files(self): # If there are no sponsor files, we still work # And the dashboard shows our download button rsp = self.client.get(self.url) self.validate_response(rsp, []) rsp = self.client.get(reverse('dashboard')) self.assertIn(self.url, rsp.content) def test_different_benefit_types(self): # We only get files from the benefits named "Print logo" and "Web logo" # And we ignore any non-existent files try: # Create a temp dir for media files self.temp_dir = tempfile.mkdtemp() with override_settings(MEDIA_ROOT=self.temp_dir): # Give our sponsor some benefits SponsorBenefit.objects.create( sponsor=self.sponsor, benefit=self.text_benefit, text="Foo!" ) self.make_temp_file("file1", 10) SponsorBenefit.objects.create( sponsor=self.sponsor, benefit=self.file_benefit, upload="file1" ) self.make_temp_file("file2", 20) SponsorBenefit.objects.create( sponsor=self.sponsor, benefit=self.weblogo_benefit, upload="file2" ) # Benefit whose file is missing from the disk SponsorBenefit.objects.create( sponsor=self.sponsor, benefit=self.weblogo_benefit, upload="file3" ) # print logo benefit self.make_temp_file("file4", 40) SponsorBenefit.objects.create( sponsor=self.sponsor, benefit=self.printlogo_benefit, upload="file4" ) self.make_temp_file("file5", 50) SponsorBenefit.objects.create( sponsor=self.sponsor, benefit=self.advertisement_benefit, upload="file5" ) rsp = self.client.get(self.url) expected = [ ('web_logos/lead/big_daddy/file2', 20), ('print_logos/lead/big_daddy/file4', 40), ('advertisement/lead/big_daddy/file5', 50) ] self.validate_response(rsp, expected) finally: if hasattr(self, 'temp_dir'): # Clean up any temp media files shutil.rmtree(self.temp_dir) def test_file_org(self): # The zip file is organized into directories: # {print_logos,web_logos,advertisement}/