Skip to content

Commit

Permalink
Make all tests pass in BW
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Oct 30, 2023
1 parent 7516f48 commit 4d7afd3
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 138 deletions.
60 changes: 37 additions & 23 deletions follow/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
# Authors:
# See AUTHORS file.
#
from django.test import TestCase

from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from follow.models import FollowingUserItem, FollowingQueryItem


Expand All @@ -30,84 +32,96 @@ def setUp(self):
self.user = User.objects.create_user("testuser", password="testpass")
self.client.force_login(self.user)

def test_old_ng_redirects(self):
# Test that old NG pages redirect to new BW modals
resp = self.client.get(reverse('user-followers', args=['User2']))
self.assertRedirects(resp, reverse('account', args=['User2']) + '?followers=1')

resp = self.client.get(reverse('user-following-users', args=['User2']))
self.assertRedirects(resp, reverse('account', args=['User2']) + '?following=1')

resp = self.client.get(reverse('user-following-tags', args=['User2']))
self.assertRedirects(resp, reverse('account', args=['User2']) + '?followingTags=1')

def test_following_users(self):
# If we get following users for someone who exists, OK
resp = self.client.get("/people/User2/following_users/")
resp = self.client.get(reverse('user-following-users', args=['User2']) + '?ajax=1')
self.assertEqual(resp.status_code, 200)

# Someone who doesn't exist should give 404
resp = self.client.get("/people/nouser/following_users/")
resp = self.client.get(reverse('user-following-users', args=['User32']) + '?ajax=1')
self.assertEqual(resp.status_code, 404)

def test_following_users_oldusername(self):
user = User.objects.get(username='User2')
user.username = "new-username"
user.save()
# If we get following users for someone who exists by it's old username
resp = self.client.get("/people/User2/following_users/")
resp = self.client.get(reverse('user-following-users', args=['User2']) + '?ajax=1')
self.assertEqual(resp.status_code, 301)

def test_followers(self):
def test_followers_modal(self):
# If we get following users for someone who exists, OK
resp = self.client.get("/people/User2/followers/")
resp = self.client.get(reverse('user-followers', args=['User2']) + '?ajax=1')
self.assertEqual(resp.status_code, 200)
self.assertContains(resp, "User2's followers")

# Someone who doesn't exist should give 404
resp = self.client.get("/people/nouser/followers/")
resp = self.client.get(reverse('user-followers', args=['User32']) + '?ajax=1')
self.assertEqual(resp.status_code, 404)

def test_followers_oldusername(self):
user = User.objects.get(username='User2')
user.username = "new-username"
user.save()
# If we get following users for someone who exists by it's old username
resp = self.client.get("/people/User2/followers/")
resp = self.client.get(reverse('user-followers', args=['User2']) + '?ajax=1')
self.assertEqual(resp.status_code, 301)

def test_following_tags(self):
# If we get following tags for someone who exists, OK
resp = self.client.get("/people/User2/following_tags/")
resp = self.client.get(reverse('user-following-tags', args=['User2']) + '?ajax=1')
self.assertEqual(resp.status_code, 200)

# Someone who doesn't exist should give 404
resp = self.client.get("/people/nouser/following_tags/")
resp = self.client.get(reverse('user-following-tags', args=['User32']) + '?ajax=1')
self.assertEqual(resp.status_code, 404)

def test_following_tags_oldusename(self):
user = User.objects.get(username='User2')
user.username = "new-username"
user.save()
# If we get following tags for someone who exists by it's old username
resp = self.client.get("/people/User2/following_tags/")
resp = self.client.get(reverse('user-following-tags', args=['User2']) + '?ajax=1')
self.assertEqual(resp.status_code, 301)

def test_follow_user(self):
# Start following unexisting user
resp = self.client.get("/follow/follow_user/nouser/")
resp = self.client.get(reverse('follow-user', args=['nouser']))
self.assertEqual(resp.status_code, 404)

# Start following existing user
resp = self.client.get("/follow/follow_user/User1/")
resp = self.client.get(reverse('follow-user', args=['User1']))
self.assertEqual(resp.status_code, 200)

# Start following user you already follow
resp = self.client.get("/follow/follow_user/User1/")
resp = self.client.get(reverse('follow-user', args=['User1']))
self.assertEqual(resp.status_code, 200)

# Check that user is actually following the other user
self.assertEqual(
FollowingUserItem.objects.filter(user_from__username='testuser', user_to__username='User1').exists(), True)

# Stop following unexisting user
resp = self.client.get("/follow/unfollow_user/nouser/")
resp = self.client.get(reverse('unfollow-user', args=['nouser']))
self.assertEqual(resp.status_code, 404)

# Stop following user you are not actually following
resp = self.client.get("/follow/unfollow_user/User3/")
resp = self.client.get(reverse('unfollow-user', args=['User1']))
self.assertEqual(resp.status_code, 200)

# Stop following user you follow
resp = self.client.get("/follow/unfollow_user/User1/")
resp = self.client.get(reverse('unfollow-user', args=['User1']))
self.assertEqual(resp.status_code, 200)

# Check that user is no longer following the other user
Expand All @@ -116,23 +130,23 @@ def test_follow_user(self):

def test_follow_tags(self):
# Start following group of tags
resp = self.client.get("/follow/follow_tags/field-recording/another_tag/")
resp = self.client.get(reverse('follow-tags', args=['field-recording/another_tag']))
self.assertEqual(resp.status_code, 200)

# Start following group of tags you already follow
resp = self.client.get("/follow/follow_tags/field-recording/another_tag/")
resp = self.client.get(reverse('follow-tags', args=['field-recording/another_tag']))
self.assertEqual(resp.status_code, 200)

# Check that user is actually following the tags
self.assertEqual(
FollowingQueryItem.objects.filter(user__username='testuser', query='field-recording another_tag').exists(), True)

# Stop following group of tags you do not already follow
resp = self.client.get("/follow/unfollow_tags/a-tag/another_tag/")
resp = self.client.get(reverse('unfollow-tags', args=['a-tag/another_tag']))
self.assertEqual(resp.status_code, 200)

# Stop following group of tags you already follow
resp = self.client.get("/follow/unfollow_tags/field-recording/another_tag/")
resp = self.client.get(reverse('unfollow-tags', args=['field-recording/another_tag']))
self.assertEqual(resp.status_code, 200)

# Check that user is no longer following the tags
Expand All @@ -141,5 +155,5 @@ def test_follow_tags(self):

def test_stream(self):
# Stream should return OK
resp = self.client.get("/home/stream/")
resp = self.client.get(reverse('stream'))
self.assertEqual(resp.status_code, 200)
9 changes: 4 additions & 5 deletions ratings/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,18 @@ def setUp(self):

def test_rating_link_logged_in(self):
"""A logged in user viewing a sound should get links to rate the sound"""

self.client.force_login(self.user1)
resp = self.client.get(self.sound.get_absolute_url())
self.assertContains(resp, f'<a href="/people/Anton/sounds/{self.sound.id}/rate/1/" title="pretty bad :-(" class="one-star">')

self.assertContains(resp, f'<label for="rate-{self.sound.id}-1" data-value="1" aria-label="Rate sound 1 star">')
def test_no_rating_link_logged_out(self):
"""A logged out user doesn't see links to rate a sound"""
resp = self.client.get(self.sound.get_absolute_url())
self.assertNotContains(resp, f'<a href="/people/Anton/sounds/{self.sound.id}/rate/1/" title="pretty bad :-(" class="one-star">')
self.assertNotContains(resp, f'<label for="rate-{self.sound.id}-1" data-value="1" aria-label="Rate sound 1 star">')

def test_no_rating_link_own_sound(self):
"""A user doesn't see links to rate their own sound"""
user = User.objects.get(username="Anton")
self.client.force_login(user)
resp = self.client.get(self.sound.get_absolute_url())
self.assertNotContains(resp, f'<a href="/people/Anton/sounds/{self.sound.id}/rate/1/" title="pretty bad :-(" class="one-star">')
self.assertNotContains(resp, f'<label for="rate-{self.sound.id}-1" data-value="1" aria-label="Rate sound 1 star">')
Loading

0 comments on commit 4d7afd3

Please sign in to comment.