def setUp(self): # Create a dummy user, category, and article user = User.objects.create_user('user', '*****@*****.**', 'user') category = Category(name='Test') category.save() self.article = Article(author=user, category=category, title='Test', body='Test', status=Article.PUBLISHED, date=now()) self.article.save() # Create a dummy image o = BytesIO(smart_bytes('x\x9cb`\x01\x00\x00\x00\xff\xff\x03\x00\x00\x06\x00\x05')) self.image = Image(caption='Test', image=File(o, name='test.png')) self.image.save()
def setUp(self): # Create the request factory self.factory = RequestFactory() # Create three users - two normal users and an admin (staff) self.user1 = User.objects.create_user('user1', '*****@*****.**', 'user1') self.user2 = User.objects.create_user('user2', '*****@*****.**', 'user2') self.admin = User.objects.create_user('admin', '*****@*****.**', 'admin') self.admin.is_staff = True self.admin.save() # Create a dummy category for the articles self.category = Category(name='Test') self.category.save() # Create a dummy article for testing self.article = Article(author=self.user1, category=self.category, title='Test', body='Test', date=now())
def dummy_article(author, category, status=Article.PUBLISHED): """ 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_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 profile(request, profile): """Display a user's profile.""" return render(request, 'accounts/profile.html', { 'title': profile, 'profile': profile, 'articles': Article.apply_filter(request, Q(author=profile.user)), })
class TestCMarkdown(TestCase): """ Test that custom Markdown is correctly rendered. """ def setUp(self): # Create a dummy user, category, and article user = User.objects.create_user('user', '*****@*****.**', 'user') category = Category(name='Test') category.save() self.article = Article(author=user, category=category, title='Test', body='Test', status=Article.PUBLISHED, date=now()) self.article.save() # Create a dummy image o = BytesIO(smart_bytes('x\x9cb`\x01\x00\x00\x00\xff\xff\x03\x00\x00\x06\x00\x05')) self.image = Image(caption='Test', image=File(o, name='test.png')) self.image.save() def test_basic_markup(self): self.assertEqual(cm('**test**'), '<p><strong>test</strong></p>') self.assertEqual(cm('# Test'), '<h1 id="test">Test</h1>') self.assertEqual(cm('[test](http://example.org)'), '<p><a href="http://example.org">test</a></p>') self.assertEqual(cm('`test`'), '<p><code>test</code></p>') def test_linkify(self): self.assertEqual(cm('http://example.org'), '<p><a href="http://example.org">http://example.org</a></p>') def test_references(self): self.assertEqual(cm('[article:%d]' % self.article.id), '<p><a href="/articles/%d/test/">Test</a></p>' % self.article.id) self.assertEqual(cm('[image:%d]' % self.image.id), '<p><img alt="Test" src="%s" title="Test" /></p>' % self.image.image.url) def test_alerts(self): self.assertEqual( cm('[info]test[/info]'), '<div class="alert alert-info">\n<div class="media">' + '<span class="fa fa-3x fa-info-circle media-object pull-left">' + '</span><div class="media-body">\n<p>test</p>\n</div>\n</div>\n</div>' ) def test_strikethrough(self): self.assertEqual(cm('---test---'), '<p><s>test</s></p>') def test_escaping(self): self.assertEqual(cm('<>'), '<p><></p>') def test_unicode(self): self.assertEqual(cm(smart_text('\xce\xa9')), smart_text('<p>\xce\xa9</p>'))
def profile(request, profile): """ Display a user's profile. """ return render(request, 'accounts/profile.html', { 'title': profile, 'profile': profile, 'articles': Article.apply_filter(request, Q(author=profile.user)), })
def search(request): """Display search results.""" if not 'q' in request.GET: raise Http404 q = request.GET['q'] return render(request, 'articles/search.html', { 'title': 'Search Results for "%s"' % q, 'articles': Article.apply_filter(request, Q(title__icontains=q) | Q(body__icontains=q)).select_related('author', 'author__profile', 'category'), })
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 = User.objects.create_user('user1', '*****@*****.**', 'user1') self.user2 = User.objects.create_user('user2', '*****@*****.**', 'user2') self.admin = User.objects.create_user('admin', '*****@*****.**', 'admin') self.admin.is_staff = True self.admin.save() # Create a dummy category for the articles self.category = Category(name='Test') self.category.save() # Create a dummy article for testing self.article = Article(author=self.user1, category=self.category, title='Test', body='Test', date=now()) def request(self, article, user): request = self.factory.get(article.get_absolute_url()) request.user = user return view_article(request, article.id) def check_permission(self, user, access): if access: self.assertEqual(self.request(self.article, user).status_code, 200) else: with self.assertRaises(Http404): self.request(self.article, user) def test_access_to_draft_article(self): self.article.status = Article.DRAFT self.article.save() self.check_permission(AnonymousUser(), False) self.check_permission(self.user1, True) self.check_permission(self.user2, False) self.check_permission(self.admin, True) def test_access_to_unapproved_article(self): self.article.status = Article.UNAPPROVED self.article.save() self.check_permission(AnonymousUser(), False) self.check_permission(self.user1, True) self.check_permission(self.user2, False) self.check_permission(self.admin, True) def test_access_to_published_article(self): self.article.status = Article.PUBLISHED self.article.save() self.check_permission(AnonymousUser(), True) self.check_permission(self.user1, True) self.check_permission(self.user2, True) self.check_permission(self.admin, True)
def search(request): """ Display search results. """ if not 'q' in request.GET: raise Http404 q = request.GET['q'] return render( request, 'articles/search.html', { 'title': 'Search Results for "%s"' % q, 'articles': Article.apply_filter( request, Q(title__icontains=q) | Q(body__icontains=q)).select_related( 'author', 'author__profile', 'category'), })
def article(request, id): """Return information about a specific article.""" return [Article.objects(published=True).get(pk=id)]
def modified(request): """Return articles sorted by modification date.""" return Article.objects(published=True).order_by("-date")