from django.test import TestCase, Client from django.contrib.auth.models import User, Permission from rest_framework.test import APIClient from rp.models import Article from rp.factories import ArticleFactory from rp.apps import RpConfig from rp.views.articles import ArticleList class TestArticle(TestCase): def setUp(self): self.article = ArticleFactory() def test_init(self): assert RpConfig.name == "rp" def test_article(self): assert type(self.article) == Article assert str(self.article) == self.article.title def test_recover(self): article = ArticleFactory(status='NEW') article.recover() assert article.status == 'DRAFT' article = ArticleFactory(status='REJECTED') article.recover() assert article.status == 'DRAFT' 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 class TestArticleViews(TestCase): def setUp(self): self.client = Client() 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") 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): article = ArticleFactory(title=u'Zog Zog chez les schtroumphs', lang='FR') 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 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") self.jedi = User.objects.create(username="obiwan", email="o.kennoby@example.org", password="Thisaintthedroidyourelookin") 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): # text = ' '.join(self.articles[0].extracts.split(' ')[:10]) text = self.articles[0].title 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 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': '' }) assert [t.name for t in a.tags.all()] == [t['name'] for t in r.data['tags']] 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