Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Dec 9, 2023
1 parent 4365c32 commit 8aabc8e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
9 changes: 8 additions & 1 deletion misago/oauth2/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 12 additions & 21 deletions misago/oauth2/tests/test_user_creation_from_data.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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": "[email protected]",
"avatar": None,
},
{},
)
get_user_from_data(
Mock(settings=dynamic_settings, user_ip="83.0.0.1"),
{
"id": "1234",
"name": user.username,
"email": "[email protected]",
"avatar": None,
},
{},
)

assert excinfo.value.error_list == [
"Your username returned by the provider is not available for use on this site."
Expand Down
8 changes: 6 additions & 2 deletions misago/oauth2/tests/test_user_data_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
33 changes: 12 additions & 21 deletions misago/oauth2/tests/test_user_update_with_data.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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": "[email protected]",
"avatar": None,
},
{},
)
get_user_from_data(
Mock(settings=dynamic_settings, user_ip="83.0.0.1"),
{
"id": "1234",
"name": other_user.username,
"email": "[email protected]",
"avatar": None,
},
{},
)

assert excinfo.value.error_list == [
"Your username returned by the provider is not available "
Expand Down

0 comments on commit 8aabc8e

Please sign in to comment.