feat: added review module

This commit is contained in:
Андрей Сумин
2025-03-01 23:58:14 +03:00
parent 27afa70207
commit cc2693986d
22 changed files with 283 additions and 431 deletions
+29 -38
View File
@@ -1,8 +1,8 @@
import json
import uuid
from django.test import TestCase
from django.contrib.auth.hashers import make_password
from django.test import TestCase
from apps.user.models import User
@@ -47,7 +47,9 @@ class SignUpAPITestCase(TestCase):
def test_existing_user_conflict(self):
User.objects.create(
email="existing@example.com", password="existingpass123", username="testing"
email="existing@example.com",
password="existingpass123",
username="testing",
)
payload = {
"email": "existing@example.com",
@@ -62,23 +64,24 @@ class SignUpAPITestCase(TestCase):
self.assertEqual(response.status_code, 409)
self.assertIn("detail", response.json())
class SignInAPITestCase(TestCase):
def setUp(self):
self.user = User.objects.create(
email="valid@example.com",
password=make_password("securepassword123"),
username="testuser"
username="testuser",
)
self.valid_payload = {
"email": "valid@example.com",
"password": "securepassword123"
"password": "securepassword123",
}
def test_successful_sign_in(self):
response = self.client.post(
"/api/v1/sign-in",
data=json.dumps(self.valid_payload),
content_type="application/json"
content_type="application/json",
)
self.assertEqual(response.status_code, 200)
self.assertIn("token", response.json())
@@ -88,7 +91,7 @@ class SignInAPITestCase(TestCase):
response = self.client.post(
"/api/v1/sign-in",
data=json.dumps({"password": "pass"}),
content_type="application/json"
content_type="application/json",
)
self.assertEqual(response.status_code, 400)
@@ -96,44 +99,35 @@ class SignInAPITestCase(TestCase):
response = self.client.post(
"/api/v1/sign-in",
data=json.dumps({"email": "test@example.com"}),
content_type="application/json"
content_type="application/json",
)
self.assertEqual(response.status_code, 400)
def test_invalid_email_format(self):
payload = {
"email": "invalid-email",
"password": "password123"
}
payload = {"email": "invalid-email", "password": "password123"}
response = self.client.post(
"/api/v1/sign-in",
data=json.dumps(payload),
content_type="application/json"
content_type="application/json",
)
self.assertEqual(response.status_code, 401)
def test_incorrect_password(self):
payload = {
"email": "valid@example.com",
"password": "wrongpassword"
}
payload = {"email": "valid@example.com", "password": "wrongpassword"}
response = self.client.post(
"/api/v1/sign-in",
data=json.dumps(payload),
content_type="application/json"
content_type="application/json",
)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.json()["detail"], "Unauthorized")
def test_nonexistent_user(self):
payload = {
"email": "notexist@example.com",
"password": "password123"
}
payload = {"email": "notexist@example.com", "password": "password123"}
response = self.client.post(
"/api/v1/sign-in",
data=json.dumps(payload),
content_type="application/json"
content_type="application/json",
)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.json()["detail"], "Unauthorized")
@@ -145,21 +139,25 @@ class UserMeEndpointTestCase(TestCase):
self.user = User.objects.create(
email="johndoe@example.com",
username="johndoe",
password=make_password("securepassword123")
password=make_password("securepassword123"),
)
resp = self.client.post(
"/api/v1/sign-in",
data=json.dumps({"email": "johndoe@example.com", "password": "securepassword123"}),
content_type="application/json"
data=json.dumps(
{
"email": "johndoe@example.com",
"password": "securepassword123",
}
),
content_type="application/json",
).json()
self.token = resp['token']
self.token = resp["token"]
self.url = "/api/v1/me"
def test_get_authenticated_user_data(self):
"""Test authenticated user can retrieve their profile (200 OK)"""
response = self.client.get(
self.url,
HTTP_AUTHORIZATION=f"Bearer {self.token}"
self.url, HTTP_AUTHORIZATION=f"Bearer {self.token}"
)
self.assertEqual(response.status_code, 200)
@@ -191,8 +189,7 @@ class UserMeEndpointTestCase(TestCase):
def test_invalid_auth_scheme(self):
"""Test invalid authentication scheme returns 401"""
response = self.client.get(
self.url,
HTTP_AUTHORIZATION=f"InvalidScheme {self.token}"
self.url, HTTP_AUTHORIZATION=f"InvalidScheme {self.token}"
)
self.assertEqual(response.status_code, 401)
@@ -200,18 +197,12 @@ class UserMeEndpointTestCase(TestCase):
def test_malformed_token(self):
"""Test malformed token returns 401"""
test_cases = [
"invalid.token.123",
"Bearer",
"",
"123456"
]
test_cases = ["invalid.token.123", "Bearer", "", "123456"]
for token in test_cases:
with self.subTest(token=token):
response = self.client.get(
self.url,
HTTP_AUTHORIZATION=f"Bearer {token}"
self.url, HTTP_AUTHORIZATION=f"Bearer {token}"
)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.json()["detail"], "Unauthorized")