# 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/core/factories.py
# class TokenFactory(factory.DjangoModelFactory):
#     class Meta:
#         model = Token
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()
# 
# 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')


from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from privagal.gallery.factories import GalleryFactory
from ..factories import TokenFactory
from ..models import Token
from ..utils import PrivagalTestCase
        super(TokenLinkMoreButtonTestCase, cls).setUpTestData()
        cls.user = get_user_model().objects.create_user(
            'foo', is_staff=True, is_superuser=True)

    def tearDown(self):
        Token.objects.all().delete()

    def test_url_share(self):
        token = TokenFactory()
        gallery = GalleryFactory()
        self.timeline.add_child(instance=gallery)

        url = reverse('wagtailadmin_explore', args=(self.timeline.id,))
        self.client.force_login(self.user)
        response = self.client.get(url)
        self.assertContains(response, 'href="{url}?token={key}"'.format(
            url=gallery.full_url,
            key=token.key,
        ))

    def test_no_token(self):
        url = reverse('wagtailadmin_explore_root')
        self.client.force_login(self.user)
        response = self.client.get(url)
        self.assertNotContains(response, '?token=')

    def test_no_root_url_share(self):
        """
        We should only have the timeline token link, no one for the root page.
        """

# 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/core/factories.py
# class TokenFactory(factory.DjangoModelFactory):
#     class Meta:
#         model = Token
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()
# 
# 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/core/tests/test_wagtail_hooks.py
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from privagal.gallery.factories import GalleryFactory
from ..factories import TokenFactory
from ..models import Token
from ..utils import PrivagalTestCase

        super(TokenLinkMoreButtonTestCase, cls).setUpTestData()
        cls.user = get_user_model().objects.create_user(
            'foo', is_staff=True, is_superuser=True)

    def tearDown(self):
        Token.objects.all().delete()

    def test_url_share(self):
        token = TokenFactory()
        gallery = GalleryFactory()
        self.timeline.add_child(instance=gallery)

        url = reverse('wagtailadmin_explore', args=(self.timeline.id,))
        self.client.force_login(self.user)
        response = self.client.get(url)
        self.assertContains(response, 'href="{url}?token={key}"'.format(
            url=gallery.full_url,
            key=token.key,
        ))

    def test_no_token(self):
        url = reverse('wagtailadmin_explore_root')
        self.client.force_login(self.user)
        response = self.client.get(url)
        self.assertNotContains(response, '?token=')

    def test_no_root_url_share(self):
        """
        We should only have the timeline token link, no one for the root page.
        """

        TokenFactory()