111 lines
3.5 KiB
Python
111 lines
3.5 KiB
Python
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 TestChangePassword(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_then_change_password(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.put(
|
|
reverse("auth:change_password"),
|
|
{
|
|
"new_password": "barad-dur",
|
|
"new_password2": "barad-dur",
|
|
"old_password": self.password,
|
|
},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
def test_login_then_change_password_with_wrong_password(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.put(
|
|
reverse("auth:change_password"),
|
|
{
|
|
"new_password": "barad-dur",
|
|
"new_password2": "barad-dur",
|
|
"old_password": "incorrectoldpassword",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
def test_login_then_change_password_mismatch_password(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.put(
|
|
reverse("auth:change_password"),
|
|
{
|
|
"new_password": "barad-dur",
|
|
"new_password2": "mountdoom",
|
|
"old_password": self.password,
|
|
},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
|
|
def test_login_then_change_password_but_its_the_same(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.put(
|
|
reverse("auth:change_password"),
|
|
{
|
|
"new_password": self.password,
|
|
"new_password2": self.password,
|
|
"old_password": self.password,
|
|
},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|