Skip to content

Commit 177e7dc

Browse files
joemullajrbyers
authored andcommitted
Document force_login in view tests
1 parent 18d9134 commit 177e7dc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/md/testing-views.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ When writing tests for Janeway views, rely on the Django test client (`django.te
77

88
Here are tips for avoiding common pitfalls and saving debugging time.
99

10+
## Setting up a user for login-protected views
11+
12+
Use `force_login` to log the test user in before accessing a protected view.
13+
14+
> [!NOTE]
15+
> The user must be active (`is_active=True`) for this to work.
16+
17+
```py
18+
from django.test import TestCase
19+
20+
class MyTest(TestCase):
21+
22+
def my_test(self):
23+
user.is_active = True
24+
user.save()
25+
self.client.force_login(user)
26+
```
27+
1028
## Use domain mode explicitly
1129

1230
To call the Django test client, you have to write out the URL. But in Janeway, URLs are interpreted differently based on the `URL_CONFIG` setting. `URL_CONFIG` can be set to `"path"` or `"domain"`. In most cases, it is best to set `"domain"` using a setting override in a decorator, so you do not have to write out the journal code or repository short name in the URL.

0 commit comments

Comments
 (0)