diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 0fa1f0e..6efa91d 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -300,11 +300,12 @@ class CalendarSocialApp(Flask): @staticmethod @route('/profile/@/follow') + @login_required def follow_user(username): """View for following a user """ - from .models import db, Profile, User, UserFollow, Notification, NotificationAction + from .models import db, Profile, User try: profile = Profile.query.join(User).filter(User.username == username).one() @@ -312,16 +313,7 @@ class CalendarSocialApp(Flask): abort(404) if profile.user != current_user: - follow = UserFollow(follower=current_user.profile, - followed=profile, - accepted_at=datetime.utcnow()) - db.session.add(follow) - - notification = Notification(profile=profile, - actor=current_user.profile, - item=profile, - action=NotificationAction.follow) - db.session.add(notification) + profile.follow(follower=current_user.profile) db.session.commit() diff --git a/tests/test_follow.py b/tests/test_follow.py index 3309e1e..ecd389e 100644 --- a/tests/test_follow.py +++ b/tests/test_follow.py @@ -18,9 +18,9 @@ """ import calsocial -from calsocial.models import db, Notification, NotificationAction, Profile, User +from calsocial.models import db, Notification, NotificationAction, Profile, User, UserFollow -from helpers import database +from helpers import client, database, login def test_profile_follow(database): @@ -53,3 +53,32 @@ def test_profile_follow(database): assert follower in followed.follower_list assert followed in follower.followed_list + + +def test_follow_ui(client): + """Test following on the web interface + """ + + with calsocial.app.app_context(): + follower_user = User(username='follower', + email='follower@example.com', + password='passworder', + active=True) + followed_user = User(username='followed', email='followed@example.com') + follower = Profile(display_name='Follower', user=follower_user) + followed = Profile(display_name='Followed', user=followed_user) + db.session.add_all([follower, followed]) + db.session.commit() + + login(client, 'follower', 'passworder') + + page = client.get('/profile/@followed/follow') + assert page.status_code == 302 + assert page.location == 'http://localhost/profile/%40followed' + + with calsocial.app.app_context(): + db.session.add_all([follower, followed]) + follow = UserFollow.query.one() + assert follow.follower == follower + assert follow.followed == followed + assert follow.accepted_at is not None