# Path: privagal/timeline/factories.py
# class TimelineFactory(PageFactory):
# 
#     class Meta:
#         model = Timeline
# 
#     @factory.post_generation
#     def create(self, created, extracted, **kwargs):
#         """
#         Timeline doesn't really need a known parent but only the root page so
#         BUILD_STRATEGY isn't really required. Anyway, this is more simple to
#         achieve it in that way (instead of setting CREATE_STRATEGY and playing
#         with a lazy path attribute and MP tree...)
#         """
#         if extracted is False:
#             return
#         root = self.get_first_root_node()
#         root.add_child(instance=self)
# 
#     @factory.post_generation
#     def galleries(self, created, extracted, **kwargs):
#         galleries = extracted or []
#         for gallery in galleries:
#             self.add_child(instance=gallery)
# 
# 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')
# 
# def check_view_restrictions(request, page):
#     """
#     Mimic default Wagtail core behaviour but throw a 403 exception instead of
#     a redirect.
# 
#     See wagtail.wagtailcore.wagtail_hooks.check_view_restrictions
#     """
#     restrictions = page.get_view_restrictions()
#     if restrictions:
#         passed_restrictions = request.session.get('passed_page_view_restrictions', [])
# 
#         for restriction in restrictions:
#             if restriction.id not in passed_restrictions:
#                 raise PermissionDenied


from django.core.exceptions import PermissionDenied
from django.test import RequestFactory
from wagtail.wagtailcore.models import PageViewRestriction
from privagal.timeline.factories import TimelineFactory
from ..utils import PrivagalTestCase, check_view_restrictions
# -*- coding: utf-8 -*-
from __future__ import unicode_literals






class PageAuthTestCase(PrivagalTestCase):

    def test_password_required(self):
        response = self.client.get(self.timeline.url)
        self.assertNotPageLogged(response)

    def test_page_login(self):
        self.client.page_login()
        response = self.client.get(self.timeline.url)
        self.assertPageLogged(response)

    def test_page_logout(self):
        self.client.page_login()
        response = self.client.get(self.timeline.url)
        self.assertPageLogged(response)

        self.client.page_logout()
        response = self.client.get(self.timeline.url)
        self.assertNotPageLogged(response)



# Path: privagal/timeline/factories.py
# class TimelineFactory(PageFactory):
# 
#     class Meta:
#         model = Timeline
# 
#     @factory.post_generation
#     def create(self, created, extracted, **kwargs):
#         """
#         Timeline doesn't really need a known parent but only the root page so
#         BUILD_STRATEGY isn't really required. Anyway, this is more simple to
#         achieve it in that way (instead of setting CREATE_STRATEGY and playing
#         with a lazy path attribute and MP tree...)
#         """
#         if extracted is False:
#             return
#         root = self.get_first_root_node()
#         root.add_child(instance=self)
# 
#     @factory.post_generation
#     def galleries(self, created, extracted, **kwargs):
#         galleries = extracted or []
#         for gallery in galleries:
#             self.add_child(instance=gallery)
# 
# 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')
# 
# def check_view_restrictions(request, page):
#     """
#     Mimic default Wagtail core behaviour but throw a 403 exception instead of
#     a redirect.
# 
#     See wagtail.wagtailcore.wagtail_hooks.check_view_restrictions
#     """
#     restrictions = page.get_view_restrictions()
#     if restrictions:
#         passed_restrictions = request.session.get('passed_page_view_restrictions', [])
# 
#         for restriction in restrictions:
#             if restriction.id not in passed_restrictions:
#                 raise PermissionDenied


# Path: privagal/core/tests/test_utils.py
from django.core.exceptions import PermissionDenied
from django.test import RequestFactory
from wagtail.wagtailcore.models import PageViewRestriction
from privagal.timeline.factories import TimelineFactory
from ..utils import PrivagalTestCase, check_view_restrictions

# -*- coding: utf-8 -*-
from __future__ import unicode_literals






class PageAuthTestCase(PrivagalTestCase):

    def test_password_required(self):
        response = self.client.get(self.timeline.url)
        self.assertNotPageLogged(response)

    def test_page_login(self):
        self.client.page_login()
        response = self.client.get(self.timeline.url)
        self.assertPageLogged(response)

    def test_page_logout(self):
        self.client.page_login()
        response = self.client.get(self.timeline.url)
        self.assertPageLogged(response)

        self.client.page_logout()
        response = self.client.get(self.timeline.url)
        self.assertNotPageLogged(response)



class PageCheckRestrictionTestCase(PrivagalTestCase):