# Path: twobuntu/articles/models.py
# class Article(models.Model):
#     """
#     A post written in Markdown by an author.
#     """
# 
#     # Article status constants
#     DRAFT = 0
#     UNAPPROVED = 1
#     PUBLISHED = 2
# 
#     STATUS = (
#         (DRAFT, 'Draft'),
#         (UNAPPROVED, 'Unapproved'),
#         (PUBLISHED, 'Published'),
#     )
# 
#     author = models.ForeignKey(
#         User,
#         help_text="The user writing the article.",
#     )
#     category = models.ForeignKey(
#         Category,
#         help_text="The category of the article.",
#     )
#     title = models.CharField(
#         max_length=200,
#         help_text="The title of the article.",
#     )
#     body = models.TextField(help_text="The body of the article [in Markdown].")
#     image = models.ForeignKey(
#         Image,
#         blank=True,
#         null=True,
#         help_text="An image highlighting the contents of the article.",
#     )
#     status = models.PositiveSmallIntegerField(
#         choices=STATUS,
#         default=DRAFT,
#         help_text="The current status of the article.",
#     )
#     cc_license = models.BooleanField(
#         default=True,
#         help_text="Whether the article is released under the CC-BY-SA 4.0 license or not.",
#     )
#     date = models.DateTimeField()
# 
#     def __str__(self):
#         return '%s%s' % (
#             self.title,
#             ' [%s]' % self.get_status_display() if not self.status == self.PUBLISHED else '',
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         kwargs, slug = {'id': self.id}, slugify(self.title)
#         if slug:
#             kwargs['slug'] = slug
#         return ('articles:view', (), kwargs)
# 
#     def can_edit(self, request):
#         """
#         Determine if the article may be edited by the current user.
#         """
#         # The user must either be staff or have written the article and not published it yet
#         return request.user.is_staff or request.user == self.author and self.status == self.DRAFT
# 
#     def can_view(self, request):
#         """
#         Determine if the article may be viewed by the current user.
#         """
#         # The user must either be staff or have written the article or it must be published
#         return request.user.is_staff or request.user == self.author or self.status == self.PUBLISHED
# 
#     @property
#     def markdown_key(self):
#         """
#         Return the key used for caching markdown.
#         """
#         return 'article-%d-markdown' % self.id
# 
#     def related(self):
#         """
#         Returns a QuerySet of related articles.
#         """
#         return Article.objects.filter(~models.Q(id=self.id), status=self.PUBLISHED, category=self.category)
# 
#     def render(self):
#         """
#         Render the Markdown body.
#         """
#         md = cache.get(self.markdown_key)
#         if not md:
#             md = cmarkdown(self.body)
#             cache.set(self.markdown_key, md)
#         return md
# 
#     @classmethod
#     def apply_filter(cls, request, *args):
#         """
#         Return a QuerySet with the correct filter applied based on the request.
#         """
#         args = list(args)
#         if not request.user.is_staff:
#             args.append(models.Q(author=request.user.id) | models.Q(status=cls.PUBLISHED))
#         return cls.objects.filter(*args)
# 
#     class Meta:
#         ordering = ('status', '-date')
# 
# Path: twobuntu/articles/views.py
# @canonical(Article)
# def view(request, article):
#     """
#     Display the specified article.
#     """
#     if not article.can_view(request):
#         raise Http404
#     return render(request, 'articles/view.html', {
#         'title': article,
#         'parent': {
#             'title': article.category,
#             'url': article.category.get_absolute_url(),
#         },
#         'article': article,
#         'rendered': article.render(),
#         'can_edit': article.can_edit(request),
#         'social': (
#             ('https://www.facebook.com/sharer.php?u=', 'Facebook', 'fa-facebook-square'),
#             ('https://plus.google.com/share?url=', 'Google+', 'fa-google-plus-square'),
#             ('https://twitter.com/share?url=', 'Twitter', 'fa-twitter-square'),
#             ('http://tumblr.com/share?s=&v=3&u=', 'Tumblr', 'fa-tumblr-square'),
#             ('http://www.linkedin.com/shareArticle?mini=true&url=', 'LinkedIn', 'fa-linkedin-square'),
#         ),
#     })
# 
# Path: twobuntu/utils.py
# def dummy_article(author, category, status):
#     """
#     Generate a dummy article for testing.
#     """
#     article = Article(
#         author=author,
#         category=category,
#         title=uuid6(),
#         body=uuid6(),
#         status=status,
#         date=now(),
#     )
#     article.save()
#     return article
# 
# def dummy_category():
#     """
#     Generate a dummy category.
#     """
#     category = Category(name=uuid6())
#     category.save()
#     return category
# 
# def dummy_user(staff=False):
#     """
#     Generate a dummy user for testing.
#     """
#     if staff:
#         return User.objects.create_superuser(
#             uuid6(),
#             'user@example.com',
#             'password',
#         )
#     else:
#         return User.objects.create_user(uuid6())


from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.test import TestCase
from django.test.client import RequestFactory
from twobuntu.articles.models import Article
from twobuntu.articles.views import view as view_article
from twobuntu.utils import dummy_article, dummy_category, dummy_user



class TestArticlePermission(TestCase):
    """
    Test that the correct users have access to the correct articles.
    """

    def setUp(self):
        # Create the request factory
        self.factory = RequestFactory()
        # Create three users - two normal users and an admin (staff)
        self.user1 = dummy_user()

# Path: twobuntu/articles/models.py
# class Article(models.Model):
#     """
#     A post written in Markdown by an author.
#     """
# 
#     # Article status constants
#     DRAFT = 0
#     UNAPPROVED = 1
#     PUBLISHED = 2
# 
#     STATUS = (
#         (DRAFT, 'Draft'),
#         (UNAPPROVED, 'Unapproved'),
#         (PUBLISHED, 'Published'),
#     )
# 
#     author = models.ForeignKey(
#         User,
#         help_text="The user writing the article.",
#     )
#     category = models.ForeignKey(
#         Category,
#         help_text="The category of the article.",
#     )
#     title = models.CharField(
#         max_length=200,
#         help_text="The title of the article.",
#     )
#     body = models.TextField(help_text="The body of the article [in Markdown].")
#     image = models.ForeignKey(
#         Image,
#         blank=True,
#         null=True,
#         help_text="An image highlighting the contents of the article.",
#     )
#     status = models.PositiveSmallIntegerField(
#         choices=STATUS,
#         default=DRAFT,
#         help_text="The current status of the article.",
#     )
#     cc_license = models.BooleanField(
#         default=True,
#         help_text="Whether the article is released under the CC-BY-SA 4.0 license or not.",
#     )
#     date = models.DateTimeField()
# 
#     def __str__(self):
#         return '%s%s' % (
#             self.title,
#             ' [%s]' % self.get_status_display() if not self.status == self.PUBLISHED else '',
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         kwargs, slug = {'id': self.id}, slugify(self.title)
#         if slug:
#             kwargs['slug'] = slug
#         return ('articles:view', (), kwargs)
# 
#     def can_edit(self, request):
#         """
#         Determine if the article may be edited by the current user.
#         """
#         # The user must either be staff or have written the article and not published it yet
#         return request.user.is_staff or request.user == self.author and self.status == self.DRAFT
# 
#     def can_view(self, request):
#         """
#         Determine if the article may be viewed by the current user.
#         """
#         # The user must either be staff or have written the article or it must be published
#         return request.user.is_staff or request.user == self.author or self.status == self.PUBLISHED
# 
#     @property
#     def markdown_key(self):
#         """
#         Return the key used for caching markdown.
#         """
#         return 'article-%d-markdown' % self.id
# 
#     def related(self):
#         """
#         Returns a QuerySet of related articles.
#         """
#         return Article.objects.filter(~models.Q(id=self.id), status=self.PUBLISHED, category=self.category)
# 
#     def render(self):
#         """
#         Render the Markdown body.
#         """
#         md = cache.get(self.markdown_key)
#         if not md:
#             md = cmarkdown(self.body)
#             cache.set(self.markdown_key, md)
#         return md
# 
#     @classmethod
#     def apply_filter(cls, request, *args):
#         """
#         Return a QuerySet with the correct filter applied based on the request.
#         """
#         args = list(args)
#         if not request.user.is_staff:
#             args.append(models.Q(author=request.user.id) | models.Q(status=cls.PUBLISHED))
#         return cls.objects.filter(*args)
# 
#     class Meta:
#         ordering = ('status', '-date')
# 
# Path: twobuntu/articles/views.py
# @canonical(Article)
# def view(request, article):
#     """
#     Display the specified article.
#     """
#     if not article.can_view(request):
#         raise Http404
#     return render(request, 'articles/view.html', {
#         'title': article,
#         'parent': {
#             'title': article.category,
#             'url': article.category.get_absolute_url(),
#         },
#         'article': article,
#         'rendered': article.render(),
#         'can_edit': article.can_edit(request),
#         'social': (
#             ('https://www.facebook.com/sharer.php?u=', 'Facebook', 'fa-facebook-square'),
#             ('https://plus.google.com/share?url=', 'Google+', 'fa-google-plus-square'),
#             ('https://twitter.com/share?url=', 'Twitter', 'fa-twitter-square'),
#             ('http://tumblr.com/share?s=&v=3&u=', 'Tumblr', 'fa-tumblr-square'),
#             ('http://www.linkedin.com/shareArticle?mini=true&url=', 'LinkedIn', 'fa-linkedin-square'),
#         ),
#     })
# 
# Path: twobuntu/utils.py
# def dummy_article(author, category, status):
#     """
#     Generate a dummy article for testing.
#     """
#     article = Article(
#         author=author,
#         category=category,
#         title=uuid6(),
#         body=uuid6(),
#         status=status,
#         date=now(),
#     )
#     article.save()
#     return article
# 
# def dummy_category():
#     """
#     Generate a dummy category.
#     """
#     category = Category(name=uuid6())
#     category.save()
#     return category
# 
# def dummy_user(staff=False):
#     """
#     Generate a dummy user for testing.
#     """
#     if staff:
#         return User.objects.create_superuser(
#             uuid6(),
#             'user@example.com',
#             'password',
#         )
#     else:
#         return User.objects.create_user(uuid6())


# Path: twobuntu/articles/tests.py
from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.test import TestCase
from django.test.client import RequestFactory
from twobuntu.articles.models import Article
from twobuntu.articles.views import view as view_article
from twobuntu.utils import dummy_article, dummy_category, dummy_user




class TestArticlePermission(TestCase):
    """
    Test that the correct users have access to the correct articles.
    """

    def setUp(self):
        # Create the request factory
        self.factory = RequestFactory()
        # Create three users - two normal users and an admin (staff)
        self.user1 = dummy_user()

        self.user2 = dummy_user()