Skip to content

Commit

Permalink
tests: Small coverage related improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Sep 24, 2023
1 parent 72e2718 commit 611360d
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 29 deletions.
2 changes: 1 addition & 1 deletion allauth/account/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.contrib.auth import (
authenticate,
get_backends,
get_user_model,
login as django_login,
logout as django_logout,
)
Expand All @@ -35,7 +36,6 @@
from allauth.utils import (
build_absolute_uri,
generate_unique_username,
get_user_model,
import_attribute,
)

Expand Down
3 changes: 2 additions & 1 deletion allauth/account/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,10 @@ def PRESERVE_USERNAME_CASING(self):

@property
def USERNAME_VALIDATORS(self):
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured

from allauth.utils import get_user_model, import_attribute
from allauth.utils import import_attribute

path = self._setting("USERNAME_VALIDATORS", None)
if path:
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/auth_backends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from threading import local

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

from ..utils import get_user_model
from . import app_settings
from .app_settings import AuthenticationMethod
from .utils import filter_users_by_email, filter_users_by_username
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.db.models import Count

from allauth.account.models import EmailAddress
from allauth.account.utils import user_email
from allauth.utils import get_user_model


class Command(BaseCommand):
Expand Down
6 changes: 4 additions & 2 deletions allauth/account/tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from allauth.core.exceptions import ImmediateHttpResponse


class TestAccountAdapter(DefaultAccountAdapter):
class PreLoginRedirectAccountAdapter(DefaultAccountAdapter):
def pre_login(self, *args, **kwargs):
raise ImmediateHttpResponse(HttpResponseRedirect("/foo"))


def test_adapter_pre_login(settings, user, user_password, client):
settings.ACCOUNT_ADAPTER = "allauth.account.tests.test_adapter.TestAccountAdapter"
settings.ACCOUNT_ADAPTER = (
"allauth.account.tests.test_adapter.PreLoginRedirectAccountAdapter"
)
resp = client.post(
reverse("account_login"),
{"login": user.username, "password": user_password},
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import json

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.test.utils import override_settings
from django.urls import reverse

from allauth.account import app_settings
from allauth.tests import TestCase
from allauth.utils import get_user_model


class AjaxTests(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_auth_backends.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import absolute_import

from django.contrib.auth import get_user_model
from django.test.utils import override_settings

from allauth.account import app_settings
from allauth.account.auth_backends import AuthenticationBackend
from allauth.tests import TestCase
from allauth.utils import get_user_model


class AuthenticationBackendTests(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_change_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
from unittest.mock import patch

from django.contrib.auth import get_user_model
from django.test.utils import override_settings
from django.urls import reverse

Expand All @@ -12,7 +13,6 @@
from allauth.account.models import EmailAddress, EmailConfirmationHMAC
from allauth.account.utils import user_email
from allauth.tests import TestCase
from allauth.utils import get_user_model


class ChangeEmailTests(TestCase):
Expand Down
7 changes: 7 additions & 0 deletions allauth/account/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.core.management import call_command


def test_unset_multipleprimaryemails(db):
# This command needs to be dropped, in favor of having a conditional
# constraint.
call_command("account_unsetmultipleprimaryemails")
2 changes: 1 addition & 1 deletion allauth/account/tests/test_confirm_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import timedelta

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.http import HttpResponseRedirect
from django.test.client import Client, RequestFactory
Expand All @@ -19,7 +20,6 @@
from allauth.account.signals import user_logged_in
from allauth.account.utils import user_pk_to_url_str
from allauth.tests import Mock, TestCase, patch
from allauth.utils import get_user_model

from .test_models import UUIDUser

Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import django
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.test.utils import override_settings
from django.urls import reverse

from allauth.account import app_settings
from allauth.account.models import EmailAddress
from allauth.tests import TestCase
from allauth.utils import get_user_model


@override_settings(
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_logout.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

from django.contrib.auth import get_user_model
from django.core import validators
from django.test.client import Client
from django.test.utils import override_settings
Expand All @@ -8,7 +9,6 @@
from allauth.account import app_settings
from allauth.account.signals import user_logged_out
from allauth.tests import Mock, TestCase
from allauth.utils import get_user_model


test_username_validators = [
Expand Down
16 changes: 16 additions & 0 deletions allauth/account/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@
from django.contrib.auth.models import AbstractUser
from django.db import models

from allauth.account.models import EmailAddress


class UUIDUser(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

class Meta(AbstractUser.Meta):
swappable = "AUTH_USER_MODEL"


def test_add_new_email(rf, user, settings):
settings.ACCOUNT_CHANGE_EMAIL = True
request = rf.get("/")
assert EmailAddress.objects.filter(user=user).count() == 1
new_email = EmailAddress.objects.add_new_email(request, user, "[email protected]")
assert not new_email.verified
assert not new_email.primary
assert EmailAddress.objects.filter(user=user).count() == 2
EmailAddress.objects.add_new_email(request, user, "[email protected]")
assert EmailAddress.objects.filter(user=user).count() == 2
new_email.refresh_from_db()
assert new_email.email == "[email protected]"
2 changes: 1 addition & 1 deletion allauth/account/tests/test_ratelimit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import absolute_import

from django.contrib.auth import get_user_model
from django.test.utils import override_settings
from django.urls import reverse

from allauth.tests import TestCase
from allauth.utils import get_user_model


@override_settings(
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_reset_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json

from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.core import mail
from django.test.utils import override_settings
Expand All @@ -11,7 +12,6 @@
from allauth.account.forms import ResetPasswordForm
from allauth.account.models import EmailAddress
from allauth.tests import TestCase
from allauth.utils import get_user_model


@override_settings(
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_security.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import absolute_import

from django.contrib.auth import get_user_model
from django.core import mail
from django.test.client import RequestFactory
from django.test.utils import override_settings

from allauth.account.forms import ResetPasswordForm
from allauth.tests import TestCase
from allauth.utils import get_user_model


@override_settings(ACCOUNT_PREVENT_ENUMERATION=False)
Expand Down
3 changes: 2 additions & 1 deletion allauth/account/tests/test_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import django
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
Expand All @@ -19,7 +20,7 @@
from allauth.account.models import EmailAddress
from allauth.core import context
from allauth.tests import TestCase
from allauth.utils import get_user_model, get_username_max_length
from allauth.utils import get_username_max_length


class CustomSignupFormTests(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid

from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.messages.api import get_messages
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
Expand All @@ -23,7 +24,6 @@
)
from allauth.core import context
from allauth.tests import TestCase, patch
from allauth.utils import get_user_model

from .test_models import UUIDUser

Expand Down
2 changes: 1 addition & 1 deletion allauth/mfa/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by Django 3.2.20 on 2023-08-19 14:43

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
Expand Down
8 changes: 0 additions & 8 deletions allauth/socialaccount/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,6 @@ def complete_social_signup(request, sociallogin):
)


# TODO: Factor out callable importing functionality
# See: account.utils.user_display
def import_path(path):
modname, _, attr = path.rpartition(".")
m = __import__(modname, fromlist=[attr])
return getattr(m, attr)


def socialaccount_user_display(socialaccount):
func = app_settings.SOCIALACCOUNT_STR
if not func:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 3.2.20 on 2023-09-03 19:46

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
3 changes: 1 addition & 2 deletions allauth/socialaccount/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import

from django.contrib.auth import authenticate
from django.contrib.auth import authenticate, get_user_model
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import PermissionDenied
from django.db import models
Expand All @@ -12,7 +12,6 @@
from allauth.account.utils import get_next_redirect_url, setup_user_email
from allauth.core import context
from allauth.socialaccount import signals
from allauth.utils import get_user_model

from ..utils import get_request_param
from . import app_settings, providers
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
max-line-length = 88
# Black
ignore = E203, W503, E501, E231


[coverage:run]
omit =
**/test*.py

0 comments on commit 611360d

Please sign in to comment.