Skip to content

Commit

Permalink
Fix follow API
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed May 6, 2014
1 parent 871f673 commit c4760bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
13 changes: 11 additions & 2 deletions udata/core/followers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flask.ext.security import current_user

from udata.api import api, API, marshal
from udata.models import Follow, FollowOrg, FollowDataset
from udata.models import FollowOrg, FollowDataset, Follow, FollowReuse


class FollowAPI(API):
Expand All @@ -30,6 +30,10 @@ def delete(self, id):
return '', 204


class FollowUserAPI(FollowAPI):
model = Follow


class FollowOrgAPI(FollowAPI):
model = FollowOrg

Expand All @@ -38,6 +42,11 @@ class FollowDatasetAPI(FollowAPI):
model = FollowDataset


api.add_resource(FollowAPI, '/follow/<id>/', endpoint=b'api.follow')
class FollowReuseAPI(FollowAPI):
model = FollowReuse


api.add_resource(FollowUserAPI, '/follow/user/<id>/', endpoint=b'api.follow_user')
api.add_resource(FollowOrgAPI, '/follow/org/<id>/', endpoint=b'api.follow_org')
api.add_resource(FollowDatasetAPI, '/follow/dataset/<id>/', endpoint=b'api.follow_dataset')
api.add_resource(FollowReuseAPI, '/follow/reuse/<id>/', endpoint=b'api.follow_reuse')
8 changes: 6 additions & 2 deletions udata/core/followers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from udata.models import db


__all__ = ('Follow', 'FollowOrg', 'FollowDataset')
__all__ = ('Follow', 'FollowOrg', 'FollowDataset', 'FollowReuse')


class FollowQuerySet(db.BaseQuerySet):
Expand All @@ -23,7 +23,7 @@ def is_following(self, user, following):


class Follow(db.Document):
follower = db.ReferenceField('User')
follower = db.ReferenceField('User', required=True)
following = db.ReferenceField('User')
since = db.DateTimeField(required=True, default=datetime.now)
until = db.DateTimeField()
Expand All @@ -50,6 +50,10 @@ class FollowDataset(Follow):
following = db.ReferenceField('Dataset')


class FollowReuse(Follow):
following = db.ReferenceField('Reuse')


@db.post_save.connect_via(Follow)
def emit_new_follower(sender, document, **kwargs):
document.on_new.send(document)
Expand Down
30 changes: 24 additions & 6 deletions udata/tests/api/test_follow_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from flask import url_for

from udata.models import Follow
from udata.models import Follow, FollowOrg, FollowReuse, FollowDataset

from . import APITestCase
from ..factories import UserFactory, OrganizationFactory, DatasetFactory
from ..factories import UserFactory, OrganizationFactory, DatasetFactory, ReuseFactory


class FollowAPITest(APITestCase):
Expand All @@ -15,11 +15,12 @@ def test_follow_user(self):
user = self.login()
to_follow = UserFactory()

response = self.post(url_for('api.follow', id=to_follow.id))
response = self.post(url_for('api.follow_user', id=to_follow.id))
self.assertStatus(response, 201)

self.assertEqual(Follow.objects.following(to_follow).count(), 0)
self.assertEqual(Follow.objects.followers(to_follow).count(), 1)
self.assertIsInstance(Follow.objects.followers(to_follow).first(), Follow)
self.assertEqual(Follow.objects.following(user).count(), 1)
self.assertEqual(Follow.objects.followers(user).count(), 0)

Expand All @@ -33,6 +34,7 @@ def test_follow_org(self):

self.assertEqual(Follow.objects.following(to_follow).count(), 0)
self.assertEqual(Follow.objects.followers(to_follow).count(), 1)
self.assertIsInstance(Follow.objects.followers(to_follow).first(), FollowOrg)
self.assertEqual(Follow.objects.following(user).count(), 1)
self.assertEqual(Follow.objects.followers(user).count(), 0)

Expand All @@ -46,6 +48,22 @@ def test_follow_dataset(self):

self.assertEqual(Follow.objects.following(to_follow).count(), 0)
self.assertEqual(Follow.objects.followers(to_follow).count(), 1)
self.assertIsInstance(Follow.objects.followers(to_follow).first(), FollowDataset)
self.assertEqual(Follow.objects.following(user).count(), 1)
self.assertEqual(Follow.objects.followers(user).count(), 0)


def test_follow_reuse(self):
'''It should follow a reuse on POST'''
user = self.login()
to_follow = ReuseFactory()

response = self.post(url_for('api.follow_reuse', id=to_follow.id))
self.assertStatus(response, 201)

self.assertEqual(Follow.objects.following(to_follow).count(), 0)
self.assertEqual(Follow.objects.followers(to_follow).count(), 1)
self.assertIsInstance(Follow.objects.followers(to_follow).first(), FollowReuse)
self.assertEqual(Follow.objects.following(user).count(), 1)
self.assertEqual(Follow.objects.followers(user).count(), 0)

Expand All @@ -55,7 +73,7 @@ def test_follow_already_followed(self):
to_follow = UserFactory()
Follow.objects.create(follower=user, following=to_follow)

response = self.post(url_for('api.follow', id=to_follow.id))
response = self.post(url_for('api.follow_user', id=to_follow.id))
self.assertStatus(response, 200)

self.assertEqual(Follow.objects.following(to_follow).count(), 0)
Expand All @@ -69,7 +87,7 @@ def test_unfollow(self):
to_follow = UserFactory()
Follow.objects.create(follower=user, following=to_follow)

response = self.delete(url_for('api.follow', id=to_follow.id))
response = self.delete(url_for('api.follow_user', id=to_follow.id))
self.assertStatus(response, 204)

self.assertEqual(Follow.objects.following(to_follow).count(), 0)
Expand All @@ -82,5 +100,5 @@ def test_unfollow_not_existing(self):
self.login()
to_follow = UserFactory()

response = self.delete(url_for('api.follow', id=to_follow.id))
response = self.delete(url_for('api.follow_user', id=to_follow.id))
self.assert404(response)

0 comments on commit c4760bd

Please sign in to comment.