From 8aabc8ecceb24c6d00c8398a7051dcc840169879 Mon Sep 17 00:00:00 2001 From: rafalp Date: Sat, 9 Dec 2023 22:38:49 +0100 Subject: [PATCH] Fix tests --- misago/oauth2/tests/conftest.py | 9 ++++- .../tests/test_user_creation_from_data.py | 33 +++++++------------ .../oauth2/tests/test_user_data_validation.py | 8 +++-- .../tests/test_user_update_with_data.py | 33 +++++++------------ 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/misago/oauth2/tests/conftest.py b/misago/oauth2/tests/conftest.py index 772b84df2f..18cd0d48d0 100644 --- a/misago/oauth2/tests/conftest.py +++ b/misago/oauth2/tests/conftest.py @@ -1,6 +1,13 @@ +from unittest.mock import patch + import pytest +def noop_filter_user_data(request, user, user_data): + return user_data + + @pytest.fixture def disable_user_data_filters(): - pass + with patch("misago.oauth2.validation.filter_user_data", noop_filter_user_data): + yield diff --git a/misago/oauth2/tests/test_user_creation_from_data.py b/misago/oauth2/tests/test_user_creation_from_data.py index 2ad6d4f2a9..2698adb848 100644 --- a/misago/oauth2/tests/test_user_creation_from_data.py +++ b/misago/oauth2/tests/test_user_creation_from_data.py @@ -1,4 +1,4 @@ -from unittest.mock import Mock, patch +from unittest.mock import Mock import pytest from django.contrib.auth import get_user_model @@ -92,29 +92,20 @@ def test_user_is_created_with_admin_activation_from_valid_data(db, dynamic_setti assert user.requires_activation == User.ACTIVATION_ADMIN -def user_noop_filter(*args): - pass - - def test_user_name_conflict_during_creation_from_valid_data_is_handled( - user, dynamic_settings + user, dynamic_settings, disable_user_data_filters ): with pytest.raises(OAuth2UserDataValidationError) as excinfo: - # Custom filters disable build in filters - with patch( - "misago.oauth2.validation.oauth2_user_data_filters", - [user_noop_filter], - ): - get_user_from_data( - Mock(settings=dynamic_settings, user_ip="83.0.0.1"), - { - "id": "1234", - "name": user.username, - "email": "test@example.com", - "avatar": None, - }, - {}, - ) + get_user_from_data( + Mock(settings=dynamic_settings, user_ip="83.0.0.1"), + { + "id": "1234", + "name": user.username, + "email": "test@example.com", + "avatar": None, + }, + {}, + ) assert excinfo.value.error_list == [ "Your username returned by the provider is not available for use on this site." diff --git a/misago/oauth2/tests/test_user_data_validation.py b/misago/oauth2/tests/test_user_data_validation.py index 0531e52543..14ed16f4fe 100644 --- a/misago/oauth2/tests/test_user_data_validation.py +++ b/misago/oauth2/tests/test_user_data_validation.py @@ -48,7 +48,9 @@ def test_existing_user_valid_data_is_validated(user, dynamic_settings): } -def test_error_was_raised_for_user_data_with_without_name(db, dynamic_settings): +def test_error_was_raised_for_user_data_with_without_name( + db, dynamic_settings, disable_user_data_filters +): with pytest.raises(OAuth2UserDataValidationError) as excinfo: validate_user_data( Mock(settings=dynamic_settings), @@ -70,7 +72,9 @@ def test_error_was_raised_for_user_data_with_without_name(db, dynamic_settings): ] -def test_error_was_raised_for_user_data_with_invalid_name(db, dynamic_settings): +def test_error_was_raised_for_user_data_with_invalid_name( + db, dynamic_settings, disable_user_data_filters +): with pytest.raises(OAuth2UserDataValidationError) as excinfo: validate_user_data( Mock(settings=dynamic_settings), diff --git a/misago/oauth2/tests/test_user_update_with_data.py b/misago/oauth2/tests/test_user_update_with_data.py index a522de62a8..23259ae124 100644 --- a/misago/oauth2/tests/test_user_update_with_data.py +++ b/misago/oauth2/tests/test_user_update_with_data.py @@ -1,4 +1,4 @@ -from unittest.mock import Mock, patch +from unittest.mock import Mock import pytest from django.contrib.auth import get_user_model @@ -72,31 +72,22 @@ def test_user_is_not_updated_with_unchanged_valid_data(user, dynamic_settings): assert user_by_email.id == user.id -def user_noop_filter(*args): - pass - - def test_user_name_conflict_during_update_with_valid_data_is_handled( - user, other_user, dynamic_settings + user, other_user, dynamic_settings, disable_user_data_filters ): Subject.objects.create(sub="1234", user=user) with pytest.raises(OAuth2UserDataValidationError) as excinfo: - # Custom filters disable build in filters - with patch( - "misago.oauth2.validation.oauth2_user_data_filters", - [user_noop_filter], - ): - get_user_from_data( - Mock(settings=dynamic_settings, user_ip="83.0.0.1"), - { - "id": "1234", - "name": other_user.username, - "email": "test@example.com", - "avatar": None, - }, - {}, - ) + get_user_from_data( + Mock(settings=dynamic_settings, user_ip="83.0.0.1"), + { + "id": "1234", + "name": other_user.username, + "email": "test@example.com", + "avatar": None, + }, + {}, + ) assert excinfo.value.error_list == [ "Your username returned by the provider is not available "