API & CLI - refacto + new commands to update users
This commit is contained in:
@ -3,10 +3,14 @@ from unittest.mock import patch
|
||||
import pytest
|
||||
from flask import Flask
|
||||
|
||||
from fittrackee import bcrypt
|
||||
from fittrackee.tests.utils import random_string
|
||||
from fittrackee.users.exceptions import UserNotFoundException
|
||||
from fittrackee.users.exceptions import (
|
||||
InvalidEmailException,
|
||||
UserNotFoundException,
|
||||
)
|
||||
from fittrackee.users.models import User
|
||||
from fittrackee.users.utils.admin import set_admin_rights
|
||||
from fittrackee.users.utils.admin import UserManagerService
|
||||
from fittrackee.users.utils.controls import (
|
||||
check_password,
|
||||
check_username,
|
||||
@ -14,37 +18,166 @@ from fittrackee.users.utils.controls import (
|
||||
register_controls,
|
||||
)
|
||||
|
||||
from ..utils import random_email
|
||||
|
||||
class TestSetAdminRights:
|
||||
|
||||
class TestUserManagerService:
|
||||
def test_it_raises_exception_if_user_does_not_exist(
|
||||
self, app: Flask
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(username=random_string())
|
||||
|
||||
with pytest.raises(UserNotFoundException):
|
||||
set_admin_rights(random_string())
|
||||
user_manager_service.update()
|
||||
|
||||
def test_it_does_not_update_user_when_no_args_provided(
|
||||
self, app: Flask, user_1: User
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
_, user_updated, _ = user_manager_service.update()
|
||||
|
||||
assert user_updated is False
|
||||
|
||||
def test_it_returns_user(self, app: Flask, user_1: User) -> None:
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
user, _, _ = user_manager_service.update()
|
||||
|
||||
assert user == user_1
|
||||
|
||||
def test_it_sets_admin_right_for_a_given_user(
|
||||
self, app: Flask, user_1: User
|
||||
) -> None:
|
||||
set_admin_rights(user_1.username)
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
user_manager_service.update(is_admin=True)
|
||||
|
||||
assert user_1.admin is True
|
||||
|
||||
def test_it_return_updated_user_flag_to_true(
|
||||
self, app: Flask, user_1: User
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
_, user_updated, _ = user_manager_service.update(is_admin=True)
|
||||
|
||||
assert user_updated is True
|
||||
|
||||
def test_it_does_not_raise_exception_when_user_has_already_admin_right(
|
||||
self, app: Flask, user_1_admin: User
|
||||
) -> None:
|
||||
set_admin_rights(user_1_admin.username)
|
||||
user_manager_service = UserManagerService(
|
||||
username=user_1_admin.username
|
||||
)
|
||||
|
||||
user_manager_service.update(is_admin=True)
|
||||
|
||||
assert user_1_admin.admin is True
|
||||
|
||||
def test_it_activates_account_if_user_is_inactive(
|
||||
self, app: Flask, inactive_user: User
|
||||
) -> None:
|
||||
set_admin_rights(inactive_user.username)
|
||||
user_manager_service = UserManagerService(
|
||||
username=inactive_user.username
|
||||
)
|
||||
|
||||
user_manager_service.update(is_admin=True)
|
||||
|
||||
assert inactive_user.admin is True
|
||||
assert inactive_user.is_active is True
|
||||
assert inactive_user.confirmation_token is None
|
||||
|
||||
def test_it_activates_given_user_account(
|
||||
self, app: Flask, inactive_user: User
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(
|
||||
username=inactive_user.username
|
||||
)
|
||||
|
||||
user_manager_service.update(activate=True)
|
||||
|
||||
assert inactive_user.is_active is True
|
||||
|
||||
def test_it_empties_confirmation_token(
|
||||
self, app: Flask, inactive_user: User
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(
|
||||
username=inactive_user.username
|
||||
)
|
||||
|
||||
user_manager_service.update(activate=True)
|
||||
|
||||
assert inactive_user.confirmation_token is None
|
||||
|
||||
def test_it_does_not_raise_error_if_user_account_already_activated(
|
||||
self, app: Flask, user_1: User
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
user_manager_service.update(activate=True)
|
||||
|
||||
assert user_1.is_active is True
|
||||
|
||||
def test_it_resets_user_password(self, app: Flask, user_1: User) -> None:
|
||||
previous_password = user_1.password
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
user_manager_service.update(reset_password=True)
|
||||
|
||||
assert user_1.password != previous_password
|
||||
|
||||
def test_new_password_is_encrypted(self, app: Flask, user_1: User) -> None:
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
_, _, new_password = user_manager_service.update(reset_password=True)
|
||||
|
||||
assert bcrypt.check_password_hash(user_1.password, new_password)
|
||||
|
||||
def test_it_raises_exception_if_provided_email_is_invalid(
|
||||
self, app: Flask, user_1: User
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
with pytest.raises(
|
||||
InvalidEmailException, match='valid email must be provided'
|
||||
):
|
||||
user_manager_service.update(new_email=random_string())
|
||||
|
||||
def test_it_raises_exception_if_provided_email_is_current_user_email(
|
||||
self, app: Flask, user_1: User
|
||||
) -> None:
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
with pytest.raises(
|
||||
InvalidEmailException,
|
||||
match='new email must be different than curent email',
|
||||
):
|
||||
user_manager_service.update(new_email=user_1.email)
|
||||
|
||||
def test_it_updates_user_email_to_confirm(
|
||||
self, app: Flask, user_1: User
|
||||
) -> None:
|
||||
new_email = random_email()
|
||||
current_email = user_1.email
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
user_manager_service.update(new_email=new_email)
|
||||
|
||||
assert user_1.email == current_email
|
||||
assert user_1.email_to_confirm == new_email
|
||||
assert user_1.confirmation_token is not None
|
||||
|
||||
def test_it_updates_user_email(self, app: Flask, user_1: User) -> None:
|
||||
new_email = random_email()
|
||||
user_manager_service = UserManagerService(username=user_1.username)
|
||||
|
||||
user_manager_service.update(
|
||||
new_email=new_email, with_confirmation=False
|
||||
)
|
||||
|
||||
assert user_1.email == new_email
|
||||
assert user_1.email_to_confirm is None
|
||||
assert user_1.confirmation_token is None
|
||||
|
||||
|
||||
class TestIsValidEmail:
|
||||
@pytest.mark.parametrize(
|
||||
|
Reference in New Issue
Block a user