# Path: privagal/core/utils.py
# class PrivagalTestCase(TestCase):
#     client_class = PrivagalClient
#     login_template_name = None
# 
#     @classmethod
#     def setUpClass(cls):
#         super(PrivagalTestCase, cls).setUpClass()
#         if settings.PRIVAGAL_AUTH_TOKEN_REQUIRED:
#             cls.login_template_name = 'token_required.html'
#         else:
#             cls.login_template_name = 'password_required.html'
# 
#     @classmethod
#     def setUpTestData(cls):
#         cls.timeline = Page.objects.get(slug='timeline').specific
# 
#     def assertPageLogged(self, response):
#         self.assertTemplateNotUsed(
#             response, self.login_template_name, msg_prefix='Not page logged in')
# 
#     def assertNotPageLogged(self, response):
#         self.assertTemplateUsed(
#             response, self.login_template_name, msg_prefix='Page logged in')
# 
# Path: privagal/gallery/factories.py
# class GalleryFactory(PageFactory):
# 
#     class Meta:
#         model = Gallery
# 
#     date = fuzzy.FuzzyDate(datetime.date(2016, 1, 1))
#     body = factory.LazyAttribute(
#         lambda x: "<p>{}</p>".format(faker.paragraph()))
# 
#     @factory.post_generation
#     def images(self, created, extracted, **kwargs):
#         """
#         Usage example:
#         >>> GalleryFactory(images__images=[ImageFactory()])
#         """
#         images = kwargs.pop('images', [])
#         if not images:
#             images = [ImageFactory() for i in range(0, 3)]
# 
#         self.images = [ImageGalleryFactory.build(page=self, image=i) for i in images]


import os
import tempfile
import zipfile
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from privagal.core.utils import PrivagalTestCase
from ..factories import GalleryFactory

    def test_archive_dir_exists(self):
        media_root = tempfile.mkdtemp()

        self.client.page_login()
        with self.settings(MEDIA_ROOT=media_root):
            gallery = GalleryFactory()
            self.timeline.add_child(instance=gallery)
            url = reverse('galleries:download', kwargs={'pk': gallery.pk})

            self.client.get(url)
            response = self.client.get(url)

        self.assertTrue(response.status_code, 200)
        self.assertTrue(os.path.exists(os.path.join(media_root, 'archives')))

    def test_download_zip(self):
        self.client.page_login()
        response = self.client.get(self.url, follow=True)

        self.assertEqual(response['content-type'], 'application/zip')
        self.assertIn('attachment; filename=', response['content-disposition'])
        self.assertIn(
            '{gallery.date}-{gallery.pk}.zip'.format(gallery=self.gallery),
            response['content-disposition'],
        )
        self.assertTrue(response.streaming)

        images = [i.image.file for i in self.gallery.images.prefetch_related('image').all()]


# Path: privagal/core/utils.py
# class PrivagalTestCase(TestCase):
#     client_class = PrivagalClient
#     login_template_name = None
# 
#     @classmethod
#     def setUpClass(cls):
#         super(PrivagalTestCase, cls).setUpClass()
#         if settings.PRIVAGAL_AUTH_TOKEN_REQUIRED:
#             cls.login_template_name = 'token_required.html'
#         else:
#             cls.login_template_name = 'password_required.html'
# 
#     @classmethod
#     def setUpTestData(cls):
#         cls.timeline = Page.objects.get(slug='timeline').specific
# 
#     def assertPageLogged(self, response):
#         self.assertTemplateNotUsed(
#             response, self.login_template_name, msg_prefix='Not page logged in')
# 
#     def assertNotPageLogged(self, response):
#         self.assertTemplateUsed(
#             response, self.login_template_name, msg_prefix='Page logged in')
# 
# Path: privagal/gallery/factories.py
# class GalleryFactory(PageFactory):
# 
#     class Meta:
#         model = Gallery
# 
#     date = fuzzy.FuzzyDate(datetime.date(2016, 1, 1))
#     body = factory.LazyAttribute(
#         lambda x: "<p>{}</p>".format(faker.paragraph()))
# 
#     @factory.post_generation
#     def images(self, created, extracted, **kwargs):
#         """
#         Usage example:
#         >>> GalleryFactory(images__images=[ImageFactory()])
#         """
#         images = kwargs.pop('images', [])
#         if not images:
#             images = [ImageFactory() for i in range(0, 3)]
# 
#         self.images = [ImageGalleryFactory.build(page=self, image=i) for i in images]


# Path: privagal/gallery/tests/test_views.py
import os
import tempfile
import zipfile
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from privagal.core.utils import PrivagalTestCase
from ..factories import GalleryFactory


    def test_archive_dir_exists(self):
        media_root = tempfile.mkdtemp()

        self.client.page_login()
        with self.settings(MEDIA_ROOT=media_root):
            gallery = GalleryFactory()
            self.timeline.add_child(instance=gallery)
            url = reverse('galleries:download', kwargs={'pk': gallery.pk})

            self.client.get(url)
            response = self.client.get(url)

        self.assertTrue(response.status_code, 200)
        self.assertTrue(os.path.exists(os.path.join(media_root, 'archives')))

    def test_download_zip(self):
        self.client.page_login()
        response = self.client.get(self.url, follow=True)

        self.assertEqual(response['content-type'], 'application/zip')
        self.assertIn('attachment; filename=', response['content-disposition'])
        self.assertIn(
            '{gallery.date}-{gallery.pk}.zip'.format(gallery=self.gallery),
            response['content-disposition'],
        )
        self.assertTrue(response.streaming)

        images = [i.image.file for i in self.gallery.images.prefetch_related('image').all()]


        zf = zipfile.ZipFile(ContentFile(response.getvalue()), 'r')