Commit 2f289bad authored by luxcem's avatar luxcem

adds testing for article list

parent 6615d819
......@@ -12,3 +12,4 @@ class Command(BaseCommand):
help = "Adds initial groups for the application (jedi and padawans)"
def handle(self, *args, **options):
pass
......@@ -14,3 +14,4 @@ class TestVote(TestCase):
def test_article(self):
assert type(self.article) == Article
assert str(self.article) == self.article.title
from django.urls import reverse
from django.test import TestCase
from django.urls import reverse
from userprofile.factories import ProfileFactory
from rp.factories import ArticleFactory
# from rp.models import Article
from rp.models import Article
class ArticleListTestCase(TestCase):
def setUp(self):
self.article = ArticleFactory()
self.profile = ProfileFactory()
self.user = self.profile.user
self.client.force_login(self.user)
self.article2 = ArticleFactory()
self.user = ProfileFactory().user
self.user2 = ProfileFactory().user
self.user3 = ProfileFactory().user
def test_votes(self):
url_list = reverse("rp:article-list")
response = self.client.get(url_list)
def test_empty_upvoted(self):
self.client.force_login(self.user)
response = self.client.get(reverse("rp:article-list", kwargs={
"filter_view": "flux"
}))
assert response.status_code == 200
# Get the context
context = response.context
assert context["und_votes"]["upvoted"].count() == 0
assert context["und_votes"]["downvoted"].count() == 0
def test_upvoted(self):
self.article.upvote(self.user.username)
self.client.force_login(self.user)
response = self.client.get(reverse("rp:article-list", kwargs={
"filter_view": "flux"
}))
assert list(
response.context["und_votes"]["upvoted"]) == [self.article.id]
assert list(
response.context["und_votes"]["downvoted"]) == []
def test_downvoted(self):
self.article.downvote(self.user.username)
self.client.force_login(self.user)
response = self.client.get(reverse("rp:article-list", kwargs={
"filter_view": "flux"
}))
assert list(
response.context["und_votes"]["upvoted"]) == []
assert list(
response.context["und_votes"]["downvoted"]) == [self.article.id]
def test_2users(self):
self.article.downvote(self.user.username)
self.article2.upvote(self.user.username)
self.article.upvote(self.user2.username)
self.client.force_login(self.user)
response = self.client.get(reverse("rp:article-list", kwargs={
"filter_view": "flux"
}))
assert list(
response.context["und_votes"]["upvoted"]) == [self.article2.id]
assert list(
response.context["und_votes"]["downvoted"]) == [self.article.id]
self.client.force_login(self.user2)
response = self.client.get(reverse("rp:article-list", kwargs={
"filter_view": "flux"
}))
assert list(
response.context["und_votes"]["upvoted"]) == [self.article.id]
assert list(
response.context["und_votes"]["downvoted"]) == []
def _test_filter_view(self, filter_view):
"""Return list of articles ids in filter_view"""
response = self.client.get(reverse("rp:article-list", kwargs={
"filter_view": filter_view
}))
return sorted(
response.context["object_list"].values_list('id', flat=True))
def test_filter_view(self):
self.article.upvote(self.user.username)
self.article.upvote(self.user2.username)
self.client.force_login(self.user)
assert self._test_filter_view("flux") == sorted(
[self.article.pk, self.article2.pk]
)
assert self._test_filter_view("published") == []
assert self._test_filter_view("draft") == []
assert self._test_filter_view("rejected") == []
self.article.upvote(self.user3.username)
assert self._test_filter_view("flux") == sorted(
[self.article.pk, self.article2.pk]
)
assert self._test_filter_view("published") == []
assert self._test_filter_view("draft") == [self.article.id]
assert self._test_filter_view("rejected") == []
self.article2.status = "REJECTED"
self.article2.save()
assert self._test_filter_view("published") == []
assert self._test_filter_view("flux") == [self.article.id]
assert self._test_filter_view("draft") == [self.article.id]
assert self._test_filter_view("rejected") == [self.article2.id]
self.article.status = "PUBLISHED"
self.article.save()
assert self._test_filter_view("published") == [self.article.id]
assert self._test_filter_view("flux") == []
assert self._test_filter_view("draft") == []
assert self._test_filter_view("rejected") == [self.article2.id]
......@@ -31,3 +31,22 @@ class VoteViewTestCase(TestCase):
assert response.status_code == 200
article_db = Article.objects.get(id=self.article.id)
assert article_db.und_score == -1
def test_publish(self):
url_publish = reverse("api:article-publish", kwargs={
"pk": self.article.id
})
url_reject = reverse("api:article-reject", kwargs={
"pk": self.article.id
})
response = self.client.post(url_publish)
assert response.status_code == 200
article_db = Article.objects.get(id=self.article.id)
assert article_db.status == "PUBLISHED"
response = self.client.post(url_reject)
assert response.status_code == 200
article_db = Article.objects.get(id=self.article.id)
assert article_db.status == "REJECTED"
......@@ -10,6 +10,7 @@ class UDList(ListView):
content_type = ContentType.objects.get_for_model(self.model)
queryset = self.get_queryset()
context = super().get_context_data(**kwargs)
context["und_votes"] = {
"upvoted": queryset.filter(
und_votes__username=self.request.user.username,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment