Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of custom permissions between scenarios #117

Open
danielbynight opened this issue Nov 26, 2020 · 1 comment
Open

Cleanup of custom permissions between scenarios #117

danielbynight opened this issue Nov 26, 2020 · 1 comment
Labels

Comments

@danielbynight
Copy link

In a Django project, I have a feature with three scenarios. These scenarios pass when run individually, two of them fail when running the whole feature and all fail when running all features in the project.

The failure happens in a step common to all three scenarios, in which a custom permission created in a database migration is attempted to be assigned to a user. The step looks something along the lines of

from django.contrib.auth.models import Permission

def step_implementation(context):
    context.test.user.user_permissions.add(
        Permission.objects.get(codename='custom_permission'),
    )
    # ...

In the cases where this step fails, the error seems to be caused by not finding the permission in the database: django.contrib.auth.models.Permission.DoesNotExist: Permission matching query does not exist..

It seems as if the custom permissions added during the database migration are removed after each scenario runs, which explains the observations. I've looked into the code in the migration, but it doesn't look like anything fishy is happening there:

from django.db import migrations

def forward_func(apps, schema_editor):
    Permission = apps.get_model('auth', 'Permission')
    ContentType = apps.get_model('contenttypes', 'ContentType')
    content_type = ContentType.objects.get_for_model(apps.get_model('app_name', 'CustomUser'))
    permission, _ = Permission.objects.get_or_create(
        codename='custom_permission',
        defaults={
            'name': 'custom permission',
            'content_type': content_type
        },
    )

# backward_func defined here

class Migration(migrations.Migration):
    dependencies = [
        # dependencies on the migrations of other apps
    ]

    operations = [
        migrations.RunPython(forward_func, backward_func),
    ]

When running Django tests I don't experience this issue, although these custom permissions are used in many test cases and there is a database cleanup happening per test case supposedly similar to the cleanup happening per feature scenario.

@bittner
Copy link
Member

bittner commented Nov 30, 2020

The documentation says, re Test Isolation:

Each scenario is run inside a database transaction, just like your regular TestCases.

Hence, you should load your fixtures before each scenario. Please also see the related note in the Fixture Loading chapter.

This approach ensures proper isolation. Is this counter-intuitive or otherwise problematic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants