mirror of
https://gitlab.com/megazordpobeda/DataRush.git
synced 2026-05-23 01:37:11 +00:00
add tests on sign-in
This commit is contained in:
@@ -14,10 +14,11 @@ class User(BaseModel):
|
|||||||
username = models.SlugField(unique=True, verbose_name="юзернейм")
|
username = models.SlugField(unique=True, verbose_name="юзернейм")
|
||||||
password = models.TextField(verbose_name="пароль")
|
password = models.TextField(verbose_name="пароль")
|
||||||
|
|
||||||
def make_password(self):
|
@staticmethod
|
||||||
return make_password(self.password)
|
def make_password(password: str):
|
||||||
|
return make_password(password)
|
||||||
|
|
||||||
def check_password(self, password):
|
def check_password(self, password: str):
|
||||||
return check_password(self.password, password)
|
return check_password(self.password, password)
|
||||||
|
|
||||||
status = models.CharField(
|
status = models.CharField(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
from django.contrib.auth.hashers import make_password
|
||||||
|
|
||||||
from apps.user.models import User
|
from apps.user.models import User
|
||||||
|
|
||||||
@@ -58,3 +59,81 @@ class SignUpAPITestCase(TestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 409)
|
self.assertEqual(response.status_code, 409)
|
||||||
self.assertIn("detail", response.json())
|
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"
|
||||||
|
)
|
||||||
|
print(self.user.password)
|
||||||
|
self.valid_payload = {
|
||||||
|
"email": "valid@example.com",
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
print(make_password(self.valid_payload["password"]))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertIn("token", response.json())
|
||||||
|
|
||||||
|
def test_missing_credentials(self):
|
||||||
|
# Test missing email
|
||||||
|
response = self.client.post(
|
||||||
|
"/api/v1/sign-in",
|
||||||
|
data=json.dumps({"password": "pass"}),
|
||||||
|
content_type="application/json"
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
|
||||||
|
# Test missing password
|
||||||
|
response = self.client.post(
|
||||||
|
"/api/v1/sign-in",
|
||||||
|
data=json.dumps({"email": "test@example.com"}),
|
||||||
|
content_type="application/json"
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
|
||||||
|
def test_invalid_email_format(self):
|
||||||
|
payload = {
|
||||||
|
"email": "invalid-email",
|
||||||
|
"password": "password123"
|
||||||
|
}
|
||||||
|
response = self.client.post(
|
||||||
|
"/api/v1/sign-in",
|
||||||
|
data=json.dumps(payload),
|
||||||
|
content_type="application/json"
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 401)
|
||||||
|
|
||||||
|
def test_incorrect_password(self):
|
||||||
|
payload = {
|
||||||
|
"email": "valid@example.com",
|
||||||
|
"password": "wrongpassword"
|
||||||
|
}
|
||||||
|
response = self.client.post(
|
||||||
|
"/api/v1/sign-in",
|
||||||
|
data=json.dumps(payload),
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
response = self.client.post(
|
||||||
|
"/api/v1/sign-in",
|
||||||
|
data=json.dumps(payload),
|
||||||
|
content_type="application/json"
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 401)
|
||||||
|
self.assertEqual(response.json()["detail"], "Unauthorized")
|
||||||
|
|||||||
@@ -485,6 +485,7 @@ LANGUAGE_COOKIE_AGE = 31449600
|
|||||||
|
|
||||||
PASSWORD_HASHERS = [
|
PASSWORD_HASHERS = [
|
||||||
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
||||||
|
"django.contrib.auth.hashers.ScryptPasswordHasher",
|
||||||
]
|
]
|
||||||
|
|
||||||
LANGUAGE_COOKIE_DOMAIN = None
|
LANGUAGE_COOKIE_DOMAIN = None
|
||||||
|
|||||||
Reference in New Issue
Block a user