djangodjango-viewsdjango-testingdjango-tests

Django Tests - TemplateSyntaxError: 'socialaccount' is not a registered tag library while running tests


I'm trying to run Django tests but I'm encountering with this error - django.template.exceptions.TemplateSyntaxError: 'socialaccount' is not a registered tag library., but normally it have to work because I'm using custom settings while running tests.

I have 2 authentication methods: default Django's auth and allauth's "Login with google". I wanna test only Django's default auth and not allauth login. For that, I'am using tests_settings.py:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'widget_tweaks', # I'm using here only needed apps
'apps.userauth',
'apps.spacefy',
]


IS_TEST_CONFIG = True # I just override existing settings.py

Here is my view I wanna test:

class CustomLoginView(LoginView):
form_class = CustomAuthenticationForm
redirect_authenticated_user = True
template_name = 'apps.userauth/login.html'
success_url = "home"

def get_context_data(self, **kwargs):
    context = super(CustomLoginView, self).get_context_data(**kwargs)
    context['is_test'] = settings.IS_TEST_CONFIG
    return context


def form_invalid(self, form):
    messages.error(self.request, message=(
        "Please enter a correct email and password.Note that both fields may be "
        "case-sensitive. (You can also try to login with google)"))
    return self.render_to_response(self.get_context_data(form=form))

Actually my tests::

class TestCustomLoginView(TestCase):
def setUp(self):
    CustomUser.objects.create_user(
        email='test@gmail.com',
        password='<PASSWORD>'
    )

    self.email = "test@gmail.com"

def test_form_invalid(self):
    response = self.client.post(path='/auth/login/', data={
        'email': self.email,
        'password': 'invalid_password'
    })
    self.assert ...

Also I'm using if statement in my template to prevent the errors like I'm encountering (but it doesn't work :( ):

{% if not is_test %}{% load socialaccount %}{% endif %}

Finally I run my tests using this command -

python3 manage.py test tests.userauth-tests.test_views --settings=tests.tests_settings

My login.html

    {% extends "base.html" %}
{% load static %}
{% load widget_tweaks %}
{% if is_test is not True %}{% load socialaccount %}{% endif %}

{% block extrahead %}
    <meta charset="UTF-8">
    <title>Spacefy</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" 
    integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="shortcut icon" href="{% static 'spacefy/images/logo-1.png' %}" type="image/*">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Coustard&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />
    <link rel="stylesheet" href="{% static 'userauth/css/login.css' %}">
{% endblock %}


{% block content %}
<body class="bg-dark">

{% if form.errors %}
    {% for error in messages %}
        <div class="alert alert-danger" role="alert">
            {{error}}
          </div>
    {% endfor %}
{% endif %}
<div class="bg-light position-absolute top-50 start-50 translate-middle" style="width: 30%;">
    <div class="d-flex justify-content-center align-items-center">
        <a class="navbar-brand spacefy-font" href="{% url "home" %}"><h1>Spacefy</h1></a>
    </div>
        <div class="d-flex justify-content-center align-items-center">
            <h2>Login</h2>
        </div>
        <form method="post">
            {% csrf_token %}
            <div class="d-block">
                <label for="email" style="margin-left: 5px;"><b>Email:</b></label>
                {{form.username}}
            </div>
            <div class="d-block">
                <label for="password" style="margin-left: 5px;"><b>Password:</b></label>
                {{form.password}}
            </div>
            <div class="d-flex justify-content-center align-items-center">
                <button class="btn btn-dark text-light" style="margin-bottom: 5px;" type="submit">Submit</button>
            </div>
        </form>
        <div class="d-block" >
            <b>Log in with:</b> <div class="float-end" style="margin-right: 5px;"><a class="text-primary" href="{% url "password-reset" %}" style="text-decoration: none;">Forgot password?</a></div>
                <a href="{% if is_test is not True %}{% provider_login_url 'google'  method='oauth2' %}{% else %}#{% endif %}" style="text-decoration:none;">
                    <div class="d-flex justify-content-center align-items-center">
                        <button class="gsi-material-button" style="margin-bottom: 5px;">
                            <div class="gsi-material-button-state"></div>
                            <div class="gsi-material-button-content-wrapper">
                            <div class="gsi-material-button-icon">
                                <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" xmlns:xlink="http://www.w3.org/1999/xlink" style="display: block;">
                                  <path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"></path>
                                  <path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"></path>
                                  <path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"></path>
                                  <path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"></path>
                                  <path fill="none" d="M0 0h48v48H0z"></path>
                                </svg>
                              </div>
                              <span class="gsi-material-button-contents">Sign in with Google</span>
                              <span style="display: none;">Sign in with Google</span>
                                </div>
                          </button>
                    </div>
                </a>
        </div>
        <div class="d-block" style="margin-bottom: 5px;">
            <b>Don't have account? :</b>
            <a href="{% url "signup" %}" class="text-primary" style="text-decoration: none;">Sign up</a>
        </div>
</div>


</body>
{% endblock content %}

Could you help me please?


Solution

  • 1)

    you should not load template tag libraries conditionally, it makes the codepaths unpredictable and prone to errors. https://stackoverflow.com/a/77742918/13861187

    2)

    your code.

    
    from core import settings
    # import settings.py from core app
    #then you set 'is_test' from your narmal settings.py
    context['is_test'] = settings.IS_TEST_CONFIG
    

    instead, use as below.

    from django.conf import settings
    
    context['is_test'] = settings.IS_TEST_CONFIG
    
    

    3)

    You should return the context like below.

    def get_context_data(self, **kwargs):
        context = super(CustomLoginView, self).get_context_data(**kwargs)
    
        context['is_test'] = settings.IS_TEST_CONFIG
        return context
    
    
    1) use it like this.
    {% if is_test is not True %}{% load socialaccount %}{% endif %}
    

    Or try like this.

    {% if is_test != "True" %}{% load socialaccount %}{% endif %}
    

    Reference : - Doc

    remove below lines from tests_settings.py

    'allauth.account.middleware.AccountMiddleware',
    
    
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        'allauth.socialaccount.providers.google',
    
    

    if still doesn't work try below.

    1. delete these folders.

    spacefy-main/apps/spacefy/__pycache__

    spacefy-main/apps/userauth/__pycache__

    spacefy-main/core/__pycache__

    2. if it didn't work, try deleting

    • any other __pycache__ folder.

    it's okay to delete them since they are just cache.