test_article.py 7.42 KB
Newer Older
1
from django.test import TestCase, Client
2
from django.contrib.auth.models import User, Permission
Okhin's avatar
Okhin committed
3
from rest_framework.test import APIClient
luxcem's avatar
luxcem committed
4

luxcem's avatar
luxcem committed
5
6
7
from rp.models import Article
from rp.factories import ArticleFactory
from rp.apps import RpConfig
8
from rp.views.articles import ArticleList
luxcem's avatar
luxcem committed
9
10


11
class TestArticle(TestCase):
luxcem's avatar
luxcem committed
12
13
    def setUp(self):
        self.article = ArticleFactory()
luxcem's avatar
luxcem committed
14

luxcem's avatar
luxcem committed
15
16
    def test_init(self):
        assert RpConfig.name == "rp"
luxcem's avatar
luxcem committed
17

luxcem's avatar
luxcem committed
18
19
    def test_article(self):
        assert type(self.article) == Article
luxcem's avatar
luxcem committed
20
        assert str(self.article) == self.article.title
21

22
23
24
25
26
27
28
29
30
    def test_recover(self):
        article = ArticleFactory(status='NEW')
        article.recover()
        assert article.status == 'DRAFT'

        article = ArticleFactory(status='REJECTED')
        article.recover()
        assert article.status == 'DRAFT'

31
32
33
34
35
36
37
38
    def test_add_new_url(self):
        article = Article.add_new_url(url='https://www.example.org/article')
        assert article.status == 'NEW'
        assert article.score == 1

        article_again = Article.add_new_url(url='https://www.example.org/article')
        assert article_again.status == 'NEW'
        assert article_again.score == 2
39
40
41
42

class TestArticleViews(TestCase):
    def setUp(self):
        self.client = Client()
Okhin's avatar
Okhin committed
43
44
45
46
47
        self.articles = [ArticleFactory(tags=['Tag 1', 'Tag2'])
                         for i in range(0, 2 * ArticleList.paginate_by)]
        self.user = User.objects.create(username="test",
                                        email="test@example.org",
                                        password="test")
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
        for a in self.articles:
            a.save()

    def test_list_fr(self):
        # ArticleFactory use english by default, so we should have
        # no objects
        r = self.client.get('/rp/')
        assert len(r.context['object_list']) == 0

    def test_list_en(self):
        r = self.client.get('/rp/international')
        assert r.context['is_paginated']
        assert len(r.context['object_list']) == ArticleList.paginate_by

    def test_filter_tag(self):
        tag = self.articles[0].tags.all()[1]

        r = self.client.get('/rp/by-tag/{}'.format(tag.name))
        assert r.context['is_paginated']
        assert len(r.context['object_list']) == ArticleList.paginate_by
        r = self.client.get('/rp/by-tag/zogzog')
        assert len(r.context['object_list']) == 0

    def test_filter_view(self):
        new = [article for article in self.articles if article.status == 'NEW']

        r = self.client.get('/rp/by-tag/new')
        assert len(r.context['object_list']) == len(new)
        r = self.client.get('/rp/by-tag/nosuchtag')
        assert len(r.context['object_list']) == 0

    def test_search_view(self):
80
81
        article = ArticleFactory(title=u'Zog Zog chez les schtroumphs',
                                 lang='FR')
82
83
84
85
86
87
88
89
90
91
92
93
94
        article.save()

        r = self.client.get('/rp/', {'q': 'Zog Zog'})
        assert len(r.context['article_list']) == 1
        r = self.client.get('/rp/', {'q': 'Gargamel was here'})
        assert len(r.context['article_list']) == 0

    def test_detail_view(self):
        # Let's find a published article
        self.client.force_login(user=self.user)
        a = self.articles[0]
        r = self.client.get('/rp/article/view/{}'.format(a.pk))
        assert r.context['object'] == a
Okhin's avatar
Okhin committed
95
96
97
98
99
100
101
102
103
104


class TestArticleApi(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.articles = [ArticleFactory(tags=['Tag 1', 'Tag2'])
                         for i in range(0, 2 * ArticleList.paginate_by)]
        self.user = User.objects.create(username="test",
                                        email="test@example.org",
                                        password="test")
105
106
107
        self.jedi = User.objects.create(username="obiwan",
                                        email="o.kennoby@example.org",
                                        password="Thisaintthedroidyourelookin")
Okhin's avatar
Okhin committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        for a in self.articles:
            a.save()

    def test_api_get(self):
        r = self.client.get('/api/articles/1/', {})
        assert r.status_code == 200
        assert r.data['id'] == 1

    def test_api_list(self):
        r = self.client.get('/api/articles/', {})
        assert r.status_code == 200
        assert r.data['count'] == 2 * ArticleList.paginate_by

    def test_api_filter_tag(self):
        tag = self.articles[0].tags.all()[1]

        # All articles have Tag2 as a tag
        r = self.client.get('/api/articles-by-tag/{}/'.format(tag.name))
        assert r.status_code == 200
        assert r.data['count'] == 2 * ArticleList.paginate_by

        # Case sensitivity checking - tags are sensitive to case
        r = self.client.get('/api/articles-by-tag/{}/'.format(tag.name.lower()))
        assert r.status_code == 200
        assert r.data['count'] == 0

        # No article should have the ZogZog tag
        r = self.client.get('/api/articles-by-tag/zogzog/')
        assert r.status_code == 200
        assert r.data['count'] == 0

    def test_api_filter_search(self):
140
        # text = ' '.join(self.articles[0].extracts.split(' ')[:10])
141
        text = self.articles[0].title
Okhin's avatar
Okhin committed
142
143
144
145
        r = self.client.get('/api/articles-search/{}/'.format(text))
        assert r.status_code == 200
        assert r.data['count'] == 1
        assert r.data['results'][0]['id'] == self.articles[0].id
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

    def test_api_tag_push_unauth(self):
        a = ArticleFactory(tags=['ZogZog'],)
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
                              'tags': ','.join([t.name for t in a.tags.all()])
                              })
        assert r.status_code == 401

    def test_api_tag_push_auth(self):
        self.client.force_login(user=self.user)
        a = ArticleFactory(tags=['ZogZog'], status='NEW')
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
                              'tags': ','.join([t.name for t in a.tags.all()])
                              })
        assert r.status_code == 201

        # Need to test if we keep the tags
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
                              'tags': ''
                              })
169
        assert [t.name for t in a.tags.all()] == [t['name'] for t in r.data['tags']]
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

    def test_api_recover(self):
        # Can we recover if we're no Jedis
        self.client.force_login(user=self.user)
        a = ArticleFactory(status='NEW')
        r = self.client.post('/api/articles/{}/recover/'.format(a.id))
        assert r.status_code == 403

        # Can we recovr a NEW article and force it to DRAFT?
        self.user.user_permissions.add(Permission.objects.get(
            codename='can_change_status'))
        self.client.force_login(user=self.user)
        a = ArticleFactory(status='NEW')
        r = self.client.post('/api/articles/{}/recover/'.format(a.id))
        assert r.status_code == 200
        assert r.data['status'] == 'DRAFT'

        # Can we recovr a REJECTED article and force it to DRAFT?
        a = ArticleFactory(status='REJECTED')
        r = self.client.post('/api/articles/{}/recover/'.format(a.id))
        assert r.status_code == 200
        assert r.data['status'] == 'DRAFT'

        # We cannot recover a published article
        a = ArticleFactory(status='PUBLISHED')
        r = self.client.post('/api/articles/{}/recover/'.format(a.id))
        assert r.status_code == 403