test_article.py 12.7 KB
Newer Older
1
from random import randint
2
3
import json

4
from django.test import TestCase, Client
5
from django.core import serializers
6
from django.contrib.auth.models import User, Permission
Okhin's avatar
Okhin committed
7
from rest_framework.test import APIClient
luxcem's avatar
luxcem committed
8

luxcem's avatar
luxcem committed
9
10
11
from rp.models import Article
from rp.factories import ArticleFactory
from rp.apps import RpConfig
12
from rp.views.articles import ArticleList
luxcem's avatar
luxcem committed
13
14


15
class TestArticle(TestCase):
luxcem's avatar
luxcem committed
16
    def setUp(self):
17
        self.article = ArticleFactory(archive=False, quote=False, speak=False)
18
19
20
21
        self.newarticle = ArticleFactory(status='NEW',
                                         archive=False,
                                         quote=False,
                                         speak=False)
luxcem's avatar
luxcem committed
22

luxcem's avatar
luxcem committed
23
24
    def test_init(self):
        assert RpConfig.name == "rp"
luxcem's avatar
luxcem committed
25

luxcem's avatar
luxcem committed
26
27
    def test_article(self):
        assert type(self.article) == Article
luxcem's avatar
luxcem committed
28
        assert str(self.article) == self.article.title
29

30
31
32
33
34
35
36
37
38
    def test_recover(self):
        article = ArticleFactory(status='NEW')
        article.recover()
        assert article.status == 'DRAFT'

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

39
40
41
42
43
    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

44
45
        article2 = Article.add_new_url(url='https://www.example.org/article',
                                       tags=["Tag 1", "Tag 2"])
46
47
        assert article2.status == 'NEW'
        assert article2.score == 2
48
49
50
51
        assert set(article2.tags.names()) == {"Tag 1", "Tag 2"}

        article3 = Article.add_new_url(url='https://www.example.org/article', 
                                       tags=["Merged Tag"])
52

53
54
55
56
57
    def test_flags(self):
        assert not self.article.archive
        assert not self.article.quote
        assert not self.article.speak

Okhin's avatar
Okhin committed
58
59
60
61
62
63
64
    def test_update(self):
        old_title = self.article.title
        self.article.title = old_title[::-1]
        self.article.save()
        self.article.refresh_from_db()
        assert self.article.title != old_title

65

66
67
68
class TestArticleViews(TestCase):
    def setUp(self):
        self.client = Client()
69
        self.articles = [ArticleFactory()
Okhin's avatar
Okhin committed
70
71
72
73
                         for i in range(0, 2 * ArticleList.paginate_by)]
        self.user = User.objects.create(username="test",
                                        email="test@example.org",
                                        password="test")
74
75
76
77
78
79
80
81
82
83
84
        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')
85
86
87
88
        if r.context['is_paginated']:
            assert len(r.context['object_list']) == ArticleList.paginate_by
        else:
            assert len(r.context['object_list']) == 0
89
90

    def test_filter_tag(self):
91
        tag = 'Tag 1'
92

93
        r = self.client.get('/rp/by-tag/{}'.format(tag))
94
95
96
97
        if r.context['is_paginated']:
            assert len(r.context['object_list']) == ArticleList.paginate_by
        else:
            assert len(r.context['object_list']) == 0
98
99
100
101
        r = self.client.get('/rp/by-tag/zogzog')
        assert len(r.context['object_list']) == 0

    def test_search_view(self):
102
        article = ArticleFactory(title=u'Zog Zog chez les schtroumphs',
Okhin's avatar
Okhin committed
103
104
105
                                 lang='FR',
                                 archive=False,
                                 status='PUBLISHED')
106
107
108
109
110
111
112
        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

113
    def test_search_view_archived(self):
Okhin's avatar
Okhin committed
114
115
116
        archive = ArticleFactory(archive=True,
                                 lang='FR',
                                 status='PUBLISHED')
117
118
119
120
121
122
123
124

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

        r = self.client.get('/rp/', {'archive': 'false'})
        assert len(r.context['article_list']) == 0

125
    def test_search_view_speaks(self):
Okhin's avatar
Okhin committed
126
127
128
129
        speak = ArticleFactory(speak=True,
                               archive=False,
                               lang='FR',
                               status='PUBLISHED')
130
131
132
133
134
135
136
137
138

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

        r = self.client.get('/rp/', {'speak': 'false'})
        assert len(r.context['article_list']) == 0

    def test_search_view_quoted(self):
Okhin's avatar
Okhin committed
139
140
141
142
        quote = ArticleFactory(quote=True,
                               archive=False,
                               lang='FR',
                               status='PUBLISHED')
143
144
145
146
147
148
149
150

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

        r = self.client.get('/rp/', {'quote': 'false'})
        assert len(r.context['article_list']) == 0

151
152
153
154
155
156
    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
157

Okhin's avatar
Okhin committed
158
159
160
161
162
163
164
165
166
    def test_article_edit_unauth(self):
        a = self.articles[0]
        r = self.client.post('/rp/article/edit/{}'.format(a.pk))
        assert r.status_code == 403

    def test_article_edit(self):
        self.user.user_permissions.add(Permission.objects.get(
            codename='can_edit'))
        self.client.force_login(user=self.user)
167

Okhin's avatar
Okhin committed
168
        pk = self.articles[0].pk
169
170
171
        a = {'title': 'Zog Zog',
             'status': 'NEW',
             'lang': 'FR',
172
173
             'url': 'https://www.example.org/',
             'tags': ["Merged Tag"]}
174
        r = self.client.post('/rp/article/edit/{}/'.format(pk), a)
Okhin's avatar
Okhin committed
175
        a = Article.objects.get(pk=pk)
176
        assert r.status_code == 302  # We're redirecting to the view or preview of the article
Okhin's avatar
Okhin committed
177
        assert a.title == 'Zog Zog'
178
        assert set(a.tags.names()) == set(self.articles[0].tags.names()) | {"merged tag"}
Okhin's avatar
Okhin committed
179

Okhin's avatar
Okhin committed
180
181
182
183

class TestArticleApi(TestCase):
    def setUp(self):
        self.client = APIClient()
184
        self.articles = [ArticleFactory(tags=["Tag {}".format(n) for n in range(randint(1, 10))])
Okhin's avatar
Okhin committed
185
186
187
188
                         for i in range(0, 2 * ArticleList.paginate_by)]
        self.user = User.objects.create(username="test",
                                        email="test@example.org",
                                        password="test")
189
190
191
        self.jedi = User.objects.create(username="obiwan",
                                        email="o.kennoby@example.org",
                                        password="Thisaintthedroidyourelookin")
192

Okhin's avatar
Okhin committed
193
194
195
196
197
198
199
200
201
202
203
204
205
        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

206
207
208
209
210
    def test_api_edit(self):
        self.user.user_permissions.add(Permission.objects.get(
            codename='can_edit'))
        self.client.force_login(user=self.user)

Okhin's avatar
Okhin committed
211
212
213
214
        pk = self.articles[0].pk
        article = {'title': 'Zog Zog',
                   'url': 'https://article.org/Zog+Zog'}
        r = self.client.put('/api/articles/{}/'.format(pk),
215
216
                            article,
                            format='json')
217

218
        assert r.status_code == 200
Okhin's avatar
Okhin committed
219
        assert Article.objects.get(pk=pk).title == 'Zog Zog'
220

221
222
223
224
225
226
    def test_api_edit_tags(self):
        # Checking that we indeed change the tags
        self.user.user_permissions.add(Permission.objects.get(
            codename='can_edit'))
        self.client.force_login(user=self.user)

Okhin's avatar
Okhin committed
227
228
229
230
231
232
        pk = self.articles[0].pk
        article = {'title': 'Zog Zog',
                   'url': 'https://article.org/Zog+Zog',
                   'tags': '["New Tag 1", "New Tag 2"]'}
        r = self.client.put('/api/articles/{}/'.format(pk),
                            article,
233
234
                            format='json')
        assert r.status_code == 200
Okhin's avatar
Okhin committed
235
236
        a = Article.objects.get(pk=pk)
        assert list(a.tags.values('name',).order_by('name')) == [{'name': 'New Tag 1'},
237
                                                                 {'name': 'New Tag 2'}]
238

Okhin's avatar
Okhin committed
239
    def test_api_filter_tag(self):
240
241
        tag = 'Tag 1'
        tagged = Article.objects.filter(tags__name__in=[tag]).count()
Okhin's avatar
Okhin committed
242

243
        r = self.client.get('/api/articles-by-tag/{}/'.format(tag))
Okhin's avatar
Okhin committed
244
        assert r.status_code == 200
245
        assert r.data['count'] == tagged
Okhin's avatar
Okhin committed
246
247

        # Case sensitivity checking - tags are sensitive to case
248
        r = self.client.get('/api/articles-by-tag/{}/'.format(tag.upper()))
Okhin's avatar
Okhin committed
249
250
251
252
253
254
255
256
257
        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):
258
        text = self.articles[0].title
259
        r = self.client.get('/api/articles/', {'q': text})
Okhin's avatar
Okhin committed
260
261
262
        assert r.status_code == 200
        assert r.data['count'] == 1
        assert r.data['results'][0]['id'] == self.articles[0].id
263

264
265
266
267
268
    def test_api_filter_flag_search(self):
        r = self.client.get('/api/articles/', {'quote': 'both'})
        assert r.status_code == 200
        assert r.data['count'] == len(self.articles)

269
    def test_api_tag_push_unauth(self):
Okhin's avatar
Okhin committed
270
        a = ArticleFactory()
271
272
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
Okhin's avatar
Okhin committed
273
                              'tags': '["Tag 1", "Tag 2"]'},
274
                             format='json')
275
276
277
278
        assert r.status_code == 401

    def test_api_tag_push_auth(self):
        self.client.force_login(user=self.user)
279
        a = ArticleFactory(status='NEW')
280
281
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
282
                              'tags': a.tags.all()},
283
                             format='json')
284
        assert r.status_code == 201
285
        assert list(a.tags.all()) == r.data['tags']
286
287
288

        # Need to test if we keep the tags
        r = self.client.post('/api/articles/',
289
                             {'url': a.url, 'title': a.title},
Okhin's avatar
Okhin committed
290
                             format='json')
291
        assert list(a.tags.all()) == r.data['tags']
292
293
294
295
296
297
298
299

    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

Okhin's avatar
Okhin committed
300
        # Can we recover a NEW article and force it to DRAFT?
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
        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))
318
        assert r.status_code == 400
319
320
321
322
323
324
325

    def test_api_set_priority(self):
        self.user.user_permissions.add(Permission.objects.get(
            codename='can_change_priority'))
        self.client.force_login(user=self.user)
        a = ArticleFactory(status='DRAFT')
        assert a.priority is False
326
        r = self.client.post('/api/articles/{}/set-priority/'.format(a.id))
327
328
329
330
331
332
333
334
335
        assert r.status_code == 200
        assert r.data['priority']

    def test_api_unset_priority(self):
        self.user.user_permissions.add(Permission.objects.get(
            codename='can_change_priority'))
        self.client.force_login(user=self.user)
        a = ArticleFactory(status='DRAFT', priority=True)
        assert a.priority
336
        r = self.client.post('/api/articles/{}/unset-priority/'.format(a.id))
337
338
        assert r.status_code == 200
        assert r.data['priority'] is False