test_article.py 12.5 KB
Newer Older
1
2
import json

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

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


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

luxcem's avatar
luxcem committed
18
19
    def test_init(self):
        assert RpConfig.name == "rp"
luxcem's avatar
luxcem committed
20

luxcem's avatar
luxcem committed
21
22
    def test_article(self):
        assert type(self.article) == Article
luxcem's avatar
luxcem committed
23
        assert str(self.article) == self.article.title
24

25
26
27
28
29
30
31
32
33
    def test_recover(self):
        article = ArticleFactory(status='NEW')
        article.recover()
        assert article.status == 'DRAFT'

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

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

39
40
41
        article2 = Article.add_new_url(url='https://www.example.org/article')
        assert article2.status == 'NEW'
        assert article2.score == 2
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
    def test_flags(self):
        assert not self.article.archive
        assert not self.article.quote
        assert not self.article.speak

    def test_toggle_flags(self):
        self.newarticle.toggle_archive()
        assert self.newarticle.archive
        self.newarticle.toggle_archive()
        assert not self.newarticle.archive

        self.newarticle.toggle_quote()
        assert self.newarticle.quote
        self.newarticle.toggle_quote()
        assert not self.newarticle.quote

        self.newarticle.toggle_speak()
        assert self.newarticle.speak
        self.newarticle.toggle_speak()
        assert not self.newarticle.speak

    def test_set_flags(self):
        # Method signature is set_flags(boolean: archive = False,
        #                               boolean: speak = False,
        #                               boolean: quote = False)
        # All falsg set to their default values
        self.newarticle.set_flags()
        assert not self.newarticle.archive
        assert not self.newarticle.speak
        assert not self.newarticle.quote

        self.newarticle.set_flags(speak=True)
        assert not self.newarticle.archive
        assert self.newarticle.speak
        assert not self.newarticle.quote

        self.newarticle.set_flags(quote=True)
        assert not self.newarticle.archive
        assert not self.newarticle.speak
        assert self.newarticle.quote

        self.newarticle.set_flags(quote=True, speak=True)
        assert not self.newarticle.archive
        assert self.newarticle.speak
        assert self.newarticle.quote

Okhin's avatar
Okhin committed
89
90
91
92
93
94
95
    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

96

97
98
99
class TestArticleViews(TestCase):
    def setUp(self):
        self.client = Client()
Okhin's avatar
Okhin committed
100
101
102
103
104
        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
108
109
110
111
112
113
114
115
        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')
116
117
118
119
        if r.context['is_paginated']:
            assert len(r.context['object_list']) == ArticleList.paginate_by
        else:
            assert len(r.context['object_list']) == 0
120
121
122
123
124

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

        r = self.client.get('/rp/by-tag/{}'.format(tag.name))
125
126
127
128
        if r.context['is_paginated']:
            assert len(r.context['object_list']) == ArticleList.paginate_by
        else:
            assert len(r.context['object_list']) == 0
129
130
131
132
        r = self.client.get('/rp/by-tag/zogzog')
        assert len(r.context['object_list']) == 0

    def test_search_view(self):
133
        article = ArticleFactory(title=u'Zog Zog chez les schtroumphs',
Okhin's avatar
Okhin committed
134
135
136
                                 lang='FR',
                                 archive=False,
                                 status='PUBLISHED')
137
138
139
140
141
142
143
        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

144
    def test_search_view_archived(self):
Okhin's avatar
Okhin committed
145
146
147
        archive = ArticleFactory(archive=True,
                                 lang='FR',
                                 status='PUBLISHED')
148
149
150
151
152
153
154
155

        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

156
    def test_search_view_speaks(self):
Okhin's avatar
Okhin committed
157
158
159
160
        speak = ArticleFactory(speak=True,
                               archive=False,
                               lang='FR',
                               status='PUBLISHED')
161
162
163
164
165
166
167
168
169

        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
170
171
172
173
        quote = ArticleFactory(quote=True,
                               archive=False,
                               lang='FR',
                               status='PUBLISHED')
174
175
176
177
178
179
180
181

        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

182
183
184
185
186
187
    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
188

Okhin's avatar
Okhin committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    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)
        a = self.articles[0]
        a.title = 'Zog Zog'
        a.screenshot = '/tmp/{}.png'.format(a.pk)
        r = self.client.post('/rp/article/edit/{}'.format(a.pk),
                             a.__dict__)
        a.refresh_from_db()
        assert r.status_code == 302
        assert a.title == 'Zog Zog'

Okhin's avatar
Okhin committed
207
208
209
210
211
212
213
214
215

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")
216
217
218
        self.jedi = User.objects.create(username="obiwan",
                                        email="o.kennoby@example.org",
                                        password="Thisaintthedroidyourelookin")
Okhin's avatar
Okhin committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
        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):
251
        # text = ' '.join(self.articles[0].extracts.split(' ')[:10])
252
        text = self.articles[0].title
253
        r = self.client.get('/api/articles/', {'q': text})
Okhin's avatar
Okhin committed
254
255
256
        assert r.status_code == 200
        assert r.data['count'] == 1
        assert r.data['results'][0]['id'] == self.articles[0].id
257

258
259
260
261
262
    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)

263
264
265
266
    def test_api_tag_push_unauth(self):
        a = ArticleFactory(tags=['ZogZog'],)
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
267
268
                              'tags': a.tags.all().values('name')},
                             format='json')
269
270
271
272
        assert r.status_code == 401

    def test_api_tag_push_auth(self):
        self.client.force_login(user=self.user)
273
        a = ArticleFactory(tags=['ZogZog', 'Blip Blop'], status='NEW')
274
275
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
276
277
                              'tags': a.tags.all().values('name')},
                             format='json')
278
        assert r.status_code == 201
279
        assert list(a.tags.all().values('name')) == [json.loads(t) for t in r.data['tags']]
280
281
282
283

        # Need to test if we keep the tags
        r = self.client.post('/api/articles/',
                             {'url': a.url, 'title': a.title,
Okhin's avatar
Okhin committed
284
285
                              },
                             format='json')
286
        assert list(a.tags.all().values('name')) == [json.loads(t) for t in r.data['tags']]
287
288
289
290
291
292
293
294

    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
295
        # Can we recover a NEW article and force it to DRAFT?
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
        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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

    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
        r = self.client.post('/api/articles/{}/set_priority/'.format(a.id))
        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
        r = self.client.post('/api/articles/{}/unset_priority/'.format(a.id))
        assert r.status_code == 200
        assert r.data['priority'] is False