From 1224df6449932a5723504a89248193e049fc61e6 Mon Sep 17 00:00:00 2001 From: Kolja Date: Wed, 20 Nov 2024 10:51:49 +0100 Subject: [PATCH] Adding docs how to add a user in a test It was not clear how to test endpoints with a user --- docs/docs/guides/testing.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/docs/guides/testing.md b/docs/docs/guides/testing.md index ee17450f5..6c4c9bc5e 100644 --- a/docs/docs/guides/testing.md +++ b/docs/docs/guides/testing.md @@ -64,3 +64,12 @@ It is also possible to specify cookies, both from the TestCase instantiation and # The request will be made with {"A": "na", "B": "b", "C": "nc"} cookies response = client.get("/test-cookies", COOKIES={"A": "na", "C": "nc"}) ``` + +### Users +It is also possible to specify a User for the request: +```python + user = User.objects.create(...) + client = TestClient(router) + # The request will be made with user logged in + response = client.get("/test-with-user", user=user) +```