feat: login view
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.shortcuts import reverse
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -18,7 +19,7 @@ class TestChangePassword(APITestCase):
|
||||
|
||||
def test_login_then_change_password(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
@@ -29,7 +30,7 @@ class TestChangePassword(APITestCase):
|
||||
jwt_token = response.data.get("access")
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt_token}")
|
||||
response = self.client.put(
|
||||
"/api/v1/auth/change-password/",
|
||||
reverse("auth:change_password"),
|
||||
{
|
||||
"new_password": "barad-dur",
|
||||
"new_password2": "barad-dur",
|
||||
@@ -41,7 +42,7 @@ class TestChangePassword(APITestCase):
|
||||
|
||||
def test_login_then_change_password_with_wrong_password(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
@@ -52,7 +53,7 @@ class TestChangePassword(APITestCase):
|
||||
jwt_token = response.data.get("access")
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt_token}")
|
||||
response = self.client.put(
|
||||
"/api/v1/auth/change-password/",
|
||||
reverse("auth:change_password"),
|
||||
{
|
||||
"new_password": "barad-dur",
|
||||
"new_password2": "barad-dur",
|
||||
@@ -64,7 +65,7 @@ class TestChangePassword(APITestCase):
|
||||
|
||||
def test_login_then_change_password_mismatch_password(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
@@ -75,7 +76,7 @@ class TestChangePassword(APITestCase):
|
||||
jwt_token = response.data.get("access")
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt_token}")
|
||||
response = self.client.put(
|
||||
"/api/v1/auth/change-password/",
|
||||
reverse("auth:change_password"),
|
||||
{
|
||||
"new_password": "barad-dur",
|
||||
"new_password2": "mountdoom",
|
||||
@@ -87,7 +88,7 @@ class TestChangePassword(APITestCase):
|
||||
|
||||
def test_login_then_change_password_but_its_the_same(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
@@ -98,7 +99,7 @@ class TestChangePassword(APITestCase):
|
||||
jwt_token = response.data.get("access")
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt_token}")
|
||||
response = self.client.put(
|
||||
"/api/v1/auth/change-password/",
|
||||
reverse("auth:change_password"),
|
||||
{
|
||||
"new_password": self.password,
|
||||
"new_password2": self.password,
|
||||
@@ -0,0 +1,101 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.shortcuts import reverse
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class TestLogin(APITestCase):
|
||||
def setUp(self) -> None:
|
||||
self.password = "theonering"
|
||||
self.user = User.objects.create(
|
||||
username="sauron",
|
||||
email="sauron@mordor.middleearth",
|
||||
)
|
||||
|
||||
self.user.set_password(self.password)
|
||||
self.user.save()
|
||||
|
||||
def test_login(self):
|
||||
response = self.client.post(
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("access") is not None
|
||||
assert response.data.get("refresh") is not None
|
||||
|
||||
def test_login_then_verify(self):
|
||||
response = self.client.post(
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
jwt_token = response.data.get("access")
|
||||
response = self.client.post(
|
||||
reverse("auth:verify_jwt"),
|
||||
{
|
||||
"token": jwt_token,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_login_then_refresh(self):
|
||||
response = self.client.post(
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
jwt_token = response.data.get("refresh")
|
||||
response = self.client.post(
|
||||
reverse("auth:refresh_jwt"),
|
||||
{
|
||||
"refresh": jwt_token,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("access") is not None
|
||||
assert response.data.get("access") != jwt_token
|
||||
|
||||
def test_login_then_get_user_info(self):
|
||||
response = self.client.post(
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
jwt_token = response.data.get("access")
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt_token}")
|
||||
response = self.client.get(reverse("auth:user_info"))
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("email") == self.user.email
|
||||
|
||||
def test_login_failed(self):
|
||||
response = self.client.post(
|
||||
reverse("auth:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": "wrongpassword",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
+38
-56
@@ -1,6 +1,7 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.shortcuts import reverse
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -18,83 +19,64 @@ class TestLogin(APITestCase):
|
||||
|
||||
def test_login(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
reverse("users:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("access") is not None
|
||||
assert response.data.get("refresh") is not None
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("shop:index")
|
||||
|
||||
def test_login_then_verify(self):
|
||||
def test_login_get(self):
|
||||
response = self.client.get(reverse("users:login"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_login_already_logged_in(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
reverse("users:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
jwt_token = response.data.get("access")
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/verify/",
|
||||
{
|
||||
"token": jwt_token,
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("shop:index")
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
def test_login_then_refresh(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
jwt_token = response.data.get("refresh")
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/refresh/",
|
||||
{
|
||||
"refresh": jwt_token,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("access") is not None
|
||||
assert response.data.get("access") != jwt_token
|
||||
|
||||
def test_login_then_get_user_info(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
jwt_token = response.data.get("access")
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {jwt_token}")
|
||||
response = self.client.get("/api/v1/auth/me/")
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.data.get("email") == self.user.email
|
||||
response = self.client.get(reverse("users:login"))
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("shop:index")
|
||||
|
||||
def test_login_failed(self):
|
||||
response = self.client.post(
|
||||
"/api/v1/auth/login/",
|
||||
reverse("users:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": "wrongpassword",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
|
||||
def test_login_then_logout(self):
|
||||
response = self.client.post(
|
||||
reverse("users:login"),
|
||||
{
|
||||
"username": self.user.username,
|
||||
"password": self.password,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("shop:index")
|
||||
|
||||
response = self.client.post(reverse("users:logout"))
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("shop:index")
|
||||
|
||||
def test_logout_already_logged_out(self):
|
||||
response = self.client.post(reverse("users:logout"))
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert response.url == reverse("shop:index")
|
||||
|
||||
Reference in New Issue
Block a user