Files
shoppy/users/tests/test_change_password.py
T
2024-05-13 00:26:49 +02:00

86 lines
3.1 KiB
Python

from rest_framework import status
from rest_framework.test import APITestCase
from django.contrib.auth import get_user_model
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('/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'JWT {jwt_token}')
response = self.client.put('/api/v1/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('/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'JWT {jwt_token}')
response = self.client.put('/api/v1/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('/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'JWT {jwt_token}')
response = self.client.put('/api/v1/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('/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'JWT {jwt_token}')
response = self.client.put('/api/v1/auth/change-password/', {
'new_password': self.password,
'new_password2': self.password,
'old_password': self.password,
})
assert response.status_code == status.HTTP_400_BAD_REQUEST