You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.2 KiB
124 lines
3.2 KiB
import mock
|
|
import pytest
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.urls import reverse
|
|
|
|
from rest_framework import status
|
|
from rest_framework.generics import get_object_or_404
|
|
|
|
from factories.users import USER_PASSWORD
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@mock.patch('django.core.mail.EmailMessage.send')
|
|
def test_generate_password_by_manager(mocked_send_mail, staff_client,
|
|
student_client, user_student):
|
|
"""
|
|
Test generate new password from admin area by manager
|
|
"""
|
|
assert staff_client.get(
|
|
reverse('users:management-password'),
|
|
status=status.HTTP_405_METHOD_NOT_ALLOWED
|
|
)
|
|
assert student_client.get(
|
|
reverse('users:management-password'),
|
|
status=status.HTTP_403_FORBIDDEN
|
|
)
|
|
data = {
|
|
'email': user_student.email,
|
|
}
|
|
assert staff_client.post(
|
|
reverse('users:management-password'),
|
|
data=data,
|
|
status=status.HTTP_201_CREATED
|
|
)
|
|
test_user = get_object_or_404(get_user_model(), email=user_student.email)
|
|
assert not test_user.check_password('test')
|
|
assert mocked_send_mail.call_count == 1
|
|
assert student_client.post(
|
|
reverse('users:management-password'),
|
|
data=data,
|
|
status=status.HTTP_403_FORBIDDEN
|
|
)
|
|
wrong_data = {
|
|
'email': 'no_user@example.com',
|
|
}
|
|
assert staff_client.post(
|
|
reverse('users:management-password'),
|
|
data=wrong_data,
|
|
status=status.HTTP_404_NOT_FOUND
|
|
)
|
|
wrong_email = {
|
|
'email': 'no_user@example',
|
|
}
|
|
assert staff_client.post(
|
|
reverse('users:management-password'),
|
|
data=wrong_email,
|
|
status=status.HTTP_404_NOT_FOUND
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_generate_password_by_manager_for_not_active_student(staff_client,
|
|
user_not_active_student):
|
|
"""
|
|
Test generate new password from admin area by manager for not active student
|
|
"""
|
|
|
|
data = {
|
|
'email': user_not_active_student.email,
|
|
}
|
|
assert staff_client.post(
|
|
reverse('users:management-password'),
|
|
data=data,
|
|
status=status.HTTP_201_CREATED
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_login_user(api_client, user_student):
|
|
"""
|
|
Test login user
|
|
"""
|
|
data = {
|
|
'email': user_student.email,
|
|
'password': USER_PASSWORD
|
|
}
|
|
assert api_client.post(
|
|
reverse('users:login'),
|
|
data=data,
|
|
status=status.HTTP_200_OK
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_login_user_wrong_password(api_client, user_student):
|
|
"""
|
|
Test login user with wrong password
|
|
"""
|
|
data = {
|
|
'email': user_student.email,
|
|
'password': USER_PASSWORD + '1'
|
|
}
|
|
assert api_client.post(
|
|
reverse('users:login'),
|
|
data=data,
|
|
status=status.HTTP_403_FORBIDDEN
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_login_user_wrong_user(api_client, user_student):
|
|
"""
|
|
Test login user with wrong password
|
|
"""
|
|
data = {
|
|
'email': user_student.email + '1',
|
|
'password': USER_PASSWORD
|
|
}
|
|
assert api_client.post(
|
|
reverse('users:login'),
|
|
data=data,
|
|
status=status.HTTP_404_NOT_FOUND
|
|
)
|
|
|