Commit 51f50f9d authored by cynddl's avatar cynddl

Fix API tests and serialization

parent 7fd952c6
...@@ -6,4 +6,4 @@ from rp.models import Article ...@@ -6,4 +6,4 @@ from rp.models import Article
class ArticleSerializer(serializers.ModelSerializer): class ArticleSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Article model = Article
fields = "__all__" fields = ('id', 'url', 'title')
...@@ -3,3 +3,4 @@ from .views import ArticleViewSet ...@@ -3,3 +3,4 @@ from .views import ArticleViewSet
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r"articles", ArticleViewSet) router.register(r"articles", ArticleViewSet)
urlpatterns = router.urls
...@@ -12,6 +12,7 @@ from datetime import datetime ...@@ -12,6 +12,7 @@ from datetime import datetime
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from project.settings import env from project.settings import env
from rp.utils import cleanup_url
ARTICLE_SCORE_THRESHOLD = 3 ARTICLE_SCORE_THRESHOLD = 3
...@@ -84,33 +85,33 @@ class Article(VoteMixin): ...@@ -84,33 +85,33 @@ class Article(VoteMixin):
# Finite state logic # Finite state logic
@transition(field=status, source='DRAFT', target='PUBLISHED', @transition(field=status, source='DRAFT', target='PUBLISHED',
permission="can_change_status") permission="rp.can_change_status")
def publish(self): def publish(self):
self.published_at = datetime.now() self.published_at = datetime.now()
@transition(field=status, source='NEW', target='DRAFT', @transition(field=status, source='NEW', target='DRAFT',
permission="can_change_status") permission="rp.can_change_status")
def recover(self): def recover(self):
pass pass
@transition(field=status, source=['NEW', 'DRAFT'], target='REJECTED', @transition(field=status, source=['NEW', 'DRAFT'], target='REJECTED',
permission="can_change_status") permission="rp.can_change_status")
def reject(self): def reject(self):
pass pass
@transition(field=status, source='DRAFT', target='DRAFT', @transition(field=status, source='DRAFT', target='DRAFT',
permission="can_change_priority") permission="rp.can_change_priority")
def set_priority(self): def set_priority(self):
self.priority = True self.priority = True
@transition(field=status, source='DRAFT', target='DRAFT', @transition(field=status, source='DRAFT', target='DRAFT',
permission="can_change_priority") permission="rp.can_change_priority")
def unset_priority(self): def unset_priority(self):
self.priority = False self.priority = False
@transition(field=status, source='DRAFT', target='DRAFT') @transition(field=status, source='DRAFT', target='DRAFT')
@transition(field=status, source='NEW', @transition(field=status, source='NEW',
target=RETURN_VALUE('NEW', 'DRAFT'), permission="can_vote") target=RETURN_VALUE('NEW', 'DRAFT'), permission="rp.can_vote")
def upvote(self, by=None): def upvote(self, by=None):
super(Article, self).upvote(by) super(Article, self).upvote(by)
if self.und_score >= ARTICLE_SCORE_THRESHOLD: if self.und_score >= ARTICLE_SCORE_THRESHOLD:
...@@ -118,21 +119,39 @@ class Article(VoteMixin): ...@@ -118,21 +119,39 @@ class Article(VoteMixin):
else: else:
return self.status return self.status
@transition(field=status, source='NEW', target='NEW', permission="can_vote") @transition(field=status, source='NEW', target='NEW', permission="rp.can_vote")
@transition(field=status, source='DRAFT', target='DRAFT', @transition(field=status, source='DRAFT', target='DRAFT',
permission="can_vote") permission="rp.can_vote")
def downvote(self, by=None): def downvote(self, by=None):
super(Article, self).downvote(by) super(Article, self).downvote(by)
def add_new_url(url, by=None):
url = cleanup_url(url)
article, _ = Article.objects.get_or_create(url=url)
if article.created_by is None:
article.created_by = by
if by is not None:
article.upvote(by)
article.save()
return article
# Content extraction # Content extraction
def fetch_content(self): def fetch_content(self):
lang_lower = self.lang.lower() if self.lang != "NA" else None if self.lang != "NA":
article = ArticleParser(url=self.url, language=lang_lower) article = ArticleParser(url=self.url, language=lang_lower)
else:
article = ArticleParser(url=self.url)
article.download() article.download()
article.parse() article.parse()
self.title = article.title self.title = article.title
self.extracts = article.text self.extracts = article.text
self.save()
def fetch_screenshot(self): def fetch_screenshot(self):
from selenium import webdriver from selenium import webdriver
......
from django.urls import reverse from django.urls import reverse
from django.test import TestCase from django.test import TestCase
from django.contrib.auth.models import Permission, User
from rest_framework.test import APIClient
from rest_framework.test import APIRequestFactory, force_authenticate
from userprofile.factories import ProfileFactory from userprofile.factories import ProfileFactory
from rp.factories import ArticleFactory from rp.factories import ArticleFactory
from rp.models import Article from rp.models import Article
from rp.api.views import ArticleViewSet
class VoteViewTestCase(TestCase): class VoteViewTestCase(TestCase):
...@@ -13,7 +17,15 @@ class VoteViewTestCase(TestCase): ...@@ -13,7 +17,15 @@ class VoteViewTestCase(TestCase):
self.profile = ProfileFactory() self.profile = ProfileFactory()
self.user = self.profile.user self.user = self.profile.user
self.client.force_login(self.user)
p = Permission.objects.get(codename="can_vote")
self.user.user_permissions.add(p)
p = Permission.objects.get(codename="can_change_status")
self.user.user_permissions.add(p)
self.user.save()
self.client = APIClient()
self.client.force_authenticate(user=self.user)
def test_votes(self): def test_votes(self):
url_upvote = reverse("api:article-upvote", kwargs={ url_upvote = reverse("api:article-upvote", kwargs={
...@@ -25,12 +37,12 @@ class VoteViewTestCase(TestCase): ...@@ -25,12 +37,12 @@ class VoteViewTestCase(TestCase):
}) })
response = self.client.post(url_upvote) response = self.client.post(url_upvote)
assert response.status_code == 200 self.assertEqual(response.status_code, 200)
article_db = Article.objects.get(id=self.article.id) article_db = Article.objects.get(id=self.article.id)
assert article_db.und_score == 1 assert article_db.und_score == 1
response = self.client.post(url_downvote) response = self.client.post(url_downvote)
assert response.status_code == 200 self.assertEqual(response.status_code, 200)
article_db = Article.objects.get(id=self.article.id) article_db = Article.objects.get(id=self.article.id)
assert article_db.und_score == -1 assert article_db.und_score == -1
...@@ -40,17 +52,21 @@ class VoteViewTestCase(TestCase): ...@@ -40,17 +52,21 @@ class VoteViewTestCase(TestCase):
}) })
response = self.client.post(url_publish) response = self.client.post(url_publish)
assert response.status_code == 200 self.assertEqual(response.status_code, 200)
article_db = Article.objects.get(id=self.article_draft.id) article_db = Article.objects.get(id=self.article_draft.id)
assert article_db.status == "PUBLISHED" assert article_db.status == "PUBLISHED"
def test_reject(self): def test_reject(self):
url_reject = reverse("api:article-reject", kwargs={ url_reject = reverse("api:article-reject", kwargs={
"pk": self.article.id "pk": self.article.id
}) })
response = self.client.post(url_reject) factory = APIRequestFactory()
assert response.status_code == 200 request = factory.post(url_reject)
force_authenticate(request, user=self.user)
view = ArticleViewSet.as_view({"post": "reject"})
response = view(request, pk=self.article.id)
self.assertEqual(response.status_code, 200)
article_db = Article.objects.get(id=self.article.id) article_db = Article.objects.get(id=self.article.id)
assert article_db.status == "REJECTED" assert article_db.status == "REJECTED"
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