test_votes_views.py 2.89 KB
Newer Older
luxcem's avatar
luxcem committed
1
2
from django.urls import reverse
from django.test import TestCase
3
from django.contrib.auth.models import Permission
cynddl's avatar
cynddl committed
4
5
from rest_framework.test import APIClient
from rest_framework.test import APIRequestFactory, force_authenticate
luxcem's avatar
luxcem committed
6
7
8
9

from userprofile.factories import ProfileFactory
from rp.factories import ArticleFactory
from rp.models import Article
cynddl's avatar
cynddl committed
10
from rp.api.views import ArticleViewSet
luxcem's avatar
luxcem committed
11
12
13
14


class VoteViewTestCase(TestCase):
    def setUp(self):
15
        self.article = ArticleFactory(status="NEW")
16
17
        self.article_draft = ArticleFactory(status="DRAFT")

luxcem's avatar
luxcem committed
18
19
        self.profile = ProfileFactory()
        self.user = self.profile.user
cynddl's avatar
cynddl committed
20
21
22
23
24
25
26
27
28

        p = Permission.objects.get(codename="can_vote")
        self.user.user_permissions.add(p)
        p = Permission.objects.get(codename="can_change_status")
        self.user.user_permissions.add(p)
        self.user.save()

        self.client = APIClient()
        self.client.force_authenticate(user=self.user)
luxcem's avatar
luxcem committed
29
30

    def test_votes(self):
luxcem's avatar
luxcem committed
31
32
        url_upvote = reverse("api:article-upvote", kwargs={
            "pk": self.article.id
luxcem's avatar
luxcem committed
33
34
        })

luxcem's avatar
luxcem committed
35
36
        url_downvote = reverse("api:article-downvote", kwargs={
            "pk": self.article.id
luxcem's avatar
luxcem committed
37
38
        })

luxcem's avatar
luxcem committed
39
        response = self.client.post(url_upvote)
cynddl's avatar
cynddl committed
40
        self.assertEqual(response.status_code, 200)
luxcem's avatar
luxcem committed
41
        article_db = Article.objects.get(id=self.article.id)
42
43
44
45
46
47
        assert article_db.score == 1

        response = self.client.post(url_upvote)
        self.assertEqual(response.status_code, 200)
        article_db.refresh_from_db()
        assert article_db.score == 2
luxcem's avatar
luxcem committed
48

luxcem's avatar
luxcem committed
49
        response = self.client.post(url_downvote)
cynddl's avatar
cynddl committed
50
        self.assertEqual(response.status_code, 200)
luxcem's avatar
luxcem committed
51
        article_db = Article.objects.get(id=self.article.id)
52
        assert article_db.score == 1
luxcem's avatar
luxcem committed
53
54
55

    def test_publish(self):
        url_publish = reverse("api:article-publish", kwargs={
56
            "pk": self.article_draft.id
luxcem's avatar
luxcem committed
57
58
59
        })

        response = self.client.post(url_publish)
cynddl's avatar
cynddl committed
60
        self.assertEqual(response.status_code, 200)
61
        article_db = Article.objects.get(id=self.article_draft.id)
luxcem's avatar
luxcem committed
62
63
        assert article_db.status == "PUBLISHED"

64
65
66
67
68
    def test_reject(self):
        url_reject = reverse("api:article-reject", kwargs={
            "pk": self.article.id
        })

cynddl's avatar
cynddl committed
69
70
71
72
73
74
75
        factory = APIRequestFactory()
        request = factory.post(url_reject)
        force_authenticate(request, user=self.user)
        view = ArticleViewSet.as_view({"post": "reject"})
        response = view(request, pk=self.article.id)
        self.assertEqual(response.status_code, 200)

luxcem's avatar
luxcem committed
76
77
        article_db = Article.objects.get(id=self.article.id)
        assert article_db.status == "REJECTED"
78
79
80
81
82
83
84
85
86

    def test_votes_error_if_publish(self):
        article = ArticleFactory(status="PUBLISHED")
        url_upvote = reverse("api:article-upvote", kwargs={
            "pk": article.id
        })

        response = self.client.post(url_upvote)
        assert response.status_code == 400