This commit is contained in:
AdrienLSH
2024-05-14 08:50:37 +02:00
parent 95f0097ce5
commit e308e8f012
231 changed files with 70 additions and 22 deletions

View File

@ -0,0 +1,2 @@
from .blocks import *
from .profiles import *

View File

@ -0,0 +1,46 @@
from django.test import TestCase
from django.http import HttpResponse
from django.contrib.auth.models import User
from rest_framework import status
from profiles.serializers.ProfileSerializer import ProfileSerializer
from profiles.models.ProfileModel import ProfileModel
class BlockTest(TestCase):
def setUp(self):
self.blocker_password = 'hello_world'
self.blocker: User = User.objects.create_user('blocker', password=self.blocker_password)
self.blocked: User = User.objects.create_user('blocked', password='password')
self.blocker.save()
self.blocked.save()
def test_normal(self):
self.client.login(username=self.blocker.username, password=self.blocker_password)
response: HttpResponse = self.client.post(f'/api/profiles/block/{self.blocked.pk}')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
blocked_profile = ProfileModel.objects.get(user=self.blocked)
response = self.client.get('/api/profiles/block')
self.assertListEqual(response.json(), [ProfileSerializer(blocked_profile).data])
def test_yourself(self):
self.client.login(username=self.blocker.username, password=self.blocker_password)
response: HttpResponse = self.client.post(f'/api/profiles/block/{self.blocker.pk}')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = self.client.get('/api/profiles/block')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertListEqual(response.json(), [])
def test_user_not_found(self):
self.client.login(username=self.blocker.username, password=self.blocker_password)
response: HttpResponse = self.client.post(f'/api/profiles/block/23')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response = self.client.get('/api/profiles/block')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertListEqual(response.json(), [])

View File

@ -0,0 +1,52 @@
from django.test import TestCase
from django.http import HttpResponse
from django.contrib.auth.models import User
from rest_framework import status
from profiles.serializers.ProfileSerializer import ProfileSerializer
from profiles.models.ProfileModel import ProfileModel
class FriendTest(TestCase):
def setUp(self):
self.user_password = 'hello_world'
self.user: User = User.objects.create_user('blocker', password=self.user_password)
self.friend_password = "password"
self.friend: User = User.objects.create_user('blocked', password=self.friend_password)
self.user.save()
self.friend.save()
self.user_profile = ProfileModel.objects.get(user=self.user)
self.friend_profile = ProfileModel.objects.get(user=self.friend)
def test_normal(self):
self.client.login(username=self.user.username, password=self.user_password)
response: HttpResponse = self.client.post(f'/api/profiles/friends/{self.friend.pk}')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
response = self.client.get('/api/profiles/friends')
self.assertListEqual(response.json(), [ProfileSerializer(self.friend_profile).data])
self.client.login(username=self.friend.username, password=self.friend_password)
response = self.client.get('/api/profiles/friends')
self.assertListEqual(response.json(), [ProfileSerializer(self.user_profile).data])
def test_yourself(self):
self.client.login(username=self.user.username, password=self.user_password)
response: HttpResponse = self.client.post(f'/api/profiles/friends/{self.user.pk}')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = self.client.get('/api/profiles/friends')
self.assertListEqual(response.json(), [])
def test_user_not_found(self):
self.client.login(username=self.user.username, password=self.user_password)
response: HttpResponse = self.client.post(f'/api/profiles/friends/32')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response = self.client.get('/api/profiles/friends')
self.assertListEqual(response.json(), [])

View File

@ -0,0 +1,16 @@
from django.test import TestCase
from django.http import HttpResponse
from django.contrib.auth.models import User
class ProfileTest(TestCase):
def setUp(self):
self.user: User = User.objects.create_user('bozo', password='password')
self.user.save()
self.expected_response = {'avatar': '/static/avatars/default.avif', 'user_id': 1, 'username': 'bozo'}
self.url = "/api/profiles/user/"
def test_profile_create_on_user_created(self):
response: HttpResponse = self.client.get(self.url + self.user.username)
self.assertDictEqual(self.expected_response, response.json())