I create pytest test and want to get rendered field which logic can change depending from user. It can be tricky due to a singleton problem in the admin How to solve the singleton problem in Django ModelAdmin. - DEV Community. The code is like:
from django.test import Client
from models import Message
@pytest.fixture(scope="session")
def manager1_browser_client(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
manager = User.objects.get(username='manager1')
client = Client()
client.manager_user = manager
client.login(username=manager.username, password=settings.ADMIN_PASSWORD)
yield client
def test_manager_cannot_see_sensitive_data(manager1_browser_client):
...
msg = Message.objects.create()
url = reverse(f"admin:{app_name}_{message_model_name}_change", args=[msg.pk])
response = manager1_browser_client.get(url)
# How to assert, that restricted data is not in field of msg.data in the admin pannel, but can be in another rendered fields of this object?
I get from manager1_browser_client.get(url) the response object. And this not works response.context.keys() with error
{TypeError}TypeError(“unhashable type: ‘ContextDict’”)
Why does it happen? I would like to get all possible keys, because I can’t get my field value by response.context[‘data’] that was discussed here How to access context from a custom django-admin command? .
I want to get the value that was returned in the reality in one of fileds for test. And I don’t want to call ModelAdmin object directly or use beautifullsoup for that. Can I do it via .context? Or are any other good ways?