diff --git a/apps/rp/models.py b/apps/rp/models.py index 8d35bded7d945e19b6867a09c312f41e5bedd075..bd864af2947f9d593d79ee5c0bb17ea968f53311 100644 --- a/apps/rp/models.py +++ b/apps/rp/models.py @@ -123,7 +123,7 @@ class Article(models.Model): """ Publish a complete draft. """ self.published_at = datetime.now() - @transition(field=status, source='NEW', target='DRAFT', + @transition(field=status, source=['NEW', 'REJECTED'], target='DRAFT', permission="rp.can_change_status") def recover(self): """ Force an article to be considered as _DRAFT_. """ diff --git a/apps/rp/tests/test_article.py b/apps/rp/tests/test_article.py index 1a64bcb07adf873eb9d021916603e9bed844b572..5868b3c639554ea4039faf39847498569172f2f5 100644 --- a/apps/rp/tests/test_article.py +++ b/apps/rp/tests/test_article.py @@ -1,5 +1,5 @@ from django.test import TestCase, Client -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Permission from rest_framework.test import APIClient from rp.models import Article @@ -19,6 +19,15 @@ class TestArticle(TestCase): 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' + class TestArticleViews(TestCase): def setUp(self): @@ -60,7 +69,8 @@ class TestArticleViews(TestCase): assert len(r.context['object_list']) == 0 def test_search_view(self): - article = ArticleFactory(title=u'Zog Zog chez les schtroumphs', lang='FR') + article = ArticleFactory(title=u'Zog Zog chez les schtroumphs', + lang='FR') article.save() r = self.client.get('/rp/', {'q': 'Zog Zog'}) @@ -84,6 +94,9 @@ class TestArticleApi(TestCase): 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() @@ -146,3 +159,30 @@ class TestArticleApi(TestCase): 'tags': '' }) assert [t.name for t in a.tags.all()] == 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