test_udlistview.py 4.29 KB
Newer Older
luxcem's avatar
luxcem committed
1
from django.test import TestCase
luxcem's avatar
luxcem committed
2
from django.urls import reverse
luxcem's avatar
luxcem committed
3 4 5

from userprofile.factories import ProfileFactory
from rp.factories import ArticleFactory
luxcem's avatar
luxcem committed
6
from rp.models import Article
luxcem's avatar
luxcem committed
7 8 9 10


class ArticleListTestCase(TestCase):
    def setUp(self):
11 12
        self.article = ArticleFactory(status='NEW')
        self.article2 = ArticleFactory(status='NEW')
luxcem's avatar
luxcem committed
13 14 15 16

        self.user = ProfileFactory().user
        self.user2 = ProfileFactory().user
        self.user3 = ProfileFactory().user
luxcem's avatar
luxcem committed
17

luxcem's avatar
luxcem committed
18 19 20 21 22
    def test_empty_upvoted(self):
        self.client.force_login(self.user)
        response = self.client.get(reverse("rp:article-list", kwargs={
            "filter_view": "flux"
        }))
luxcem's avatar
luxcem committed
23
        assert response.status_code == 200
luxcem's avatar
luxcem committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        # 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]
        )
108 109 110

        self.article.save()

luxcem's avatar
luxcem committed
111 112 113 114
        assert self._test_filter_view("published") == []
        assert self._test_filter_view("draft") == [self.article.id]
        assert self._test_filter_view("rejected") == []

115
        self.article2.reject()
luxcem's avatar
luxcem committed
116 117 118
        self.article2.save()

        assert self._test_filter_view("published") == []
119
        assert self._test_filter_view("flux") == []
luxcem's avatar
luxcem committed
120 121 122
        assert self._test_filter_view("draft") == [self.article.id]
        assert self._test_filter_view("rejected") == [self.article2.id]

123
        self.article.publish()
luxcem's avatar
luxcem committed
124 125 126 127 128 129
        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]