feat: added users app
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
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
|
||||
@@ -0,0 +1,79 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
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('/api/v1/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('/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')
|
||||
response = self.client.post('/api/v1/auth/verify/', {
|
||||
'token': jwt_token,
|
||||
})
|
||||
|
||||
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'JWT {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
|
||||
|
||||
def test_login_failed(self):
|
||||
response = self.client.post('/api/v1/auth/login/', {
|
||||
'username': self.user.username,
|
||||
'password': 'wrongpassword',
|
||||
})
|
||||
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
@@ -0,0 +1,37 @@
|
||||
import pytest
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Group
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class TestUsers(TestCase):
|
||||
|
||||
def test_create_user(self):
|
||||
User.objects.create_user({
|
||||
'username': 'user 1',
|
||||
'password': 'password1'
|
||||
})
|
||||
User.objects.all().count() == 1
|
||||
|
||||
def test_new_user_invalid_username(self):
|
||||
"""Test creating user with no username raises error"""
|
||||
with pytest.raises(ValueError):
|
||||
User.objects.create_user(None, 'test123')
|
||||
|
||||
def test_create_new_superuser(self):
|
||||
"""Test creating a new superuser"""
|
||||
# Creation with standard method
|
||||
user = User.objects.create_superuser(
|
||||
'testsuperuser@adminemail.com',
|
||||
'testadmin123'
|
||||
)
|
||||
assert user.is_superuser
|
||||
assert user.is_staff
|
||||
|
||||
def test_create_group(self):
|
||||
Group.objects.create(name='Group1')
|
||||
assert Group.objects.count() == 1
|
||||
Reference in New Issue
Block a user