Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,18 @@ Example
response = client.get('/')
assert response.content == 'Foobar'

To use `client` as an authenticated standard user, call its `login()` method before accessing a URL:
To use `client` as an authenticated standard user, call its `force_login()` or
`login()` method before accessing a URL:

::

def test_with_authenticated_client(client, django_user_model):
username = "user1"
password = "bar"
django_user_model.objects.create_user(username=username, password=password)
user = django_user_model.objects.create_user(username=username, password=password)
# Use this:
client.force_login(user)
# Or this:
client.login(username=username, password=password)
response = client.get('/private')
assert response.content == 'Protected Area'
Expand Down
2 changes: 1 addition & 1 deletion pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def admin_client(db, admin_user):
from django.test.client import Client

client = Client()
client.login(username=admin_user.username, password="password")
client.force_login(admin_user)
return client


Expand Down
11 changes: 11 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def test_admin_client_no_db_marker(admin_client):
assert force_str(resp.content) == "You are an admin"


# For test below.
@pytest.fixture
def existing_admin_user(django_user_model):
return django_user_model._default_manager.create_superuser('admin', None, None)


def test_admin_client_existing_user(db, existing_admin_user, admin_user, admin_client):
resp = admin_client.get("/admin-required/")
assert force_str(resp.content) == "You are an admin"


@pytest.mark.django_db
def test_admin_user(admin_user, django_user_model):
assert isinstance(admin_user, django_user_model)
Expand Down