Skip to content

Commit

Permalink
fixed url names and unit tests for follow
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetfirat23 committed May 16, 2024
1 parent 245981c commit ce1d693
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 4 deletions.
58 changes: 58 additions & 0 deletions backend/main/tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.auth import get_user_model
from django.test import TestCase, Client
from django.urls import reverse
from ..models import Follow

class SetupTestCase(TestCase):
def setUp(self):
Expand Down Expand Up @@ -36,3 +37,60 @@ def tearDown(self):
User = get_user_model()
User.objects.all().delete()
return super().tearDown()


class FollowSetupTestCase(TestCase):
def setUp(self):
self.client = Client()
self.user_one_data = {
'username': 'user_one',
'email': '[email protected]',
'password': 'Password123'
}
self.user_two_data = {
'username': 'user_two',
'email': '[email protected]',
'password': 'Password123'
}
User = get_user_model()
User.objects.create_user(**self.user_one_data)
User.objects.create_user(**self.user_two_data)
return super().setUp()

def tearDown(self):
get_user_model().objects.all().delete()
return super().tearDown()

class UnfollowSetupTestCase(TestCase):
def setUp(self):
self.client = Client()
self.user_one_data = {
'username': 'user_one',
'email': '[email protected]',
'password': 'Password123'
}
self.user_two_data = {
'username': 'user_two',
'email': '[email protected]',
'password': 'Password123'
}

self.user_three_data = {
'username': 'user_three',
'email': '[email protected]',
'password': 'Password123'
}
User = get_user_model()
User.objects.create_user(**self.user_one_data)
User.objects.create_user(**self.user_two_data)
User.objects.create_user(**self.user_three_data)
user_one_id = User.objects.get(username='user_one').user_id
user_two_id = User.objects.get(username='user_two').user_id
user_three_id = User.objects.get(username='user_three').user_id
Follow.objects.create(user_id=user_one_id, followed_user_id=user_two_id)
return super().setUp()

def tearDown(self):
get_user_model().objects.all().delete()
Follow.objects.all().delete()
return super().tearDown()
89 changes: 88 additions & 1 deletion backend/main/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import json
from .test_setup import SetupTestCase
from .test_setup import SetupTestCase, FollowSetupTestCase, UnfollowSetupTestCase
from django.db import transaction
from django.test import TestCase, Client
from django.urls import reverse
from main.models import RegisteredUser, Follow
import json


class SignupLoginTestCase(SetupTestCase):
def test_unique_email(self):
Expand Down Expand Up @@ -34,3 +39,85 @@ def test_login_invalid_credentials(self):

self.assertEqual(response.status_code, 401)
self.assertNotIn('token', response.json()) # Assuming the response doesn't contain a 'token' key


class FollowUserTests(FollowSetupTestCase):
def test_follow_user_success(self):
data = json.dumps({'username': 'user_one', 'followed_user': 'user_two'})
response = self.client.post(reverse('main:follow-user'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'message': 'User followed successfully', 'success': True})


class UnfollowUserTests(UnfollowSetupTestCase):

def test_unfollow_user_success(self):
data = json.dumps({'username': 'user_two', 'followed_user': 'user_one'})
response = self.client.post(reverse('main:unfollow-user'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'message': 'User unfollowed successfully', 'success': True})

class GetFollowUserTest(UnfollowSetupTestCase):
def test_get_following_user_success(self):
data = json.dumps({'username': 'user_two'})
response = self.client.post(reverse('main:user-following-list'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'following': []})

def test_get_followers_user_success(self):
data = json.dumps({'username': 'user_one'})
response = self.client.post(reverse('main:user-followers-list'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'followers': []})

def test_get_following_user_empty(self):
data = json.dumps({'username': 'user_three'})
response = self.client.post(reverse('main:user-following-list'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'following': []})

def test_get_followers_user_empty(self):
data = json.dumps({'username': 'user_three'})
response = self.client.post(reverse('main:user-followers-list'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'followers': []})

def test_is_following_success(self):
data = json.dumps({'username': 'user_one', 'followed_user': 'user_two'})
response = self.client.post(reverse('main:is-following'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'is_following': True})

def test_is_following_failure(self):
data = json.dumps({'username': 'user_two', 'followed_user': 'user_three'})
response = self.client.post(reverse('main:is-following'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'is_following': False})

def test_user_check_success(self):
data = json.dumps({'username': 'user_one'})
response = self.client.post(reverse('main:user-check'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'exists': True})

def test_user_check_failure(self):
data = json.dumps({'username': 'user_four'})
response = self.client.post(reverse('main:user-check'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'exists': False})

def test_user_details_success(self):
data = json.dumps({'username': 'user_one'})
response = self.client.post(reverse('main:user-details'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'gamesLiked': 2, 'reviewCount': 3, 'followers': 0, 'following': 1})

def test_user_details_empty(self):
data = json.dumps({'username': 'user_three'})
response = self.client.post(reverse('main:user-details'), data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {'gamesLiked': 2, 'reviewCount': 3, 'followers': 0, 'following': 0})




6 changes: 3 additions & 3 deletions backend/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
path('game-info/<str:game_slug>/', views.get_game_info, name='game_info'),
path('follow-user', views.follow_user, name='follow-user'),
path('unfollow-user', views.unfollow_user, name='unfollow-user'),
path('user-followers-list', views.get_followers, name='get-followers-list'),
path('user-following-list', views.get_following, name='get-following-list'),
path('user-followers-list', views.get_followers, name='user-followers-list'),
path('user-following-list', views.get_following, name='user-following-list'),
path('is-following', views.is_following, name='is-following'),
path('user-check', views.user_check, name='user-check'),
path('user-details', views.user_details, name='user_details'),
path('user-details', views.user_details, name='user-details'),
]


Expand Down

0 comments on commit ce1d693

Please sign in to comment.