Commit 951dcb3b authored by okhin's avatar okhin 🚴

Correcting managing the recover method, adding more tests and start status

parent 9aca3d39
Pipeline #2588 passed with stages
in 3 minutes and 1 second
......@@ -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_. """
......
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment