-
Notifications
You must be signed in to change notification settings - Fork 84
Document testing views #4720
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
Merged
Merged
Document testing views #4720
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Testing views | ||
|
|
||
| > [!NOTE] | ||
| > This page assumes familiarity with writing automated tests in Python. | ||
|
|
||
| When writing tests for Janeway views, rely on the Django test client (`django.test.Client`) unless the nature of your test requires dealing with requests objects explicitly. | ||
|
|
||
| Here are tips for avoiding common pitfalls and saving debugging time. | ||
|
|
||
| ## Setting up a user for login-protected views | ||
|
|
||
| Use `force_login` to log the test user in before accessing a protected view. | ||
|
|
||
| > [!NOTE] | ||
| > The user must be active (`is_active=True`) for this to work. | ||
|
|
||
| ```py | ||
| from django.test import TestCase | ||
|
|
||
| class MyTest(TestCase): | ||
|
|
||
| def my_test(self): | ||
| user.is_active = True | ||
| user.save() | ||
| self.client.force_login(user) | ||
| ``` | ||
|
|
||
| ## Use domain mode explicitly | ||
|
|
||
| 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. | ||
|
|
||
| ```py | ||
| from django.test import TestCase, override_settings | ||
|
|
||
| class MyTest(TestCase): | ||
| @override_settings(URL_CONFIG="domain") | ||
| def my_test(self): | ||
| url = "/profile/" | ||
| ``` | ||
|
|
||
| ## Name the test server | ||
|
|
||
| When Janeway is using domain mode, the domain of the website you are testing (press, journal, repository) becomes important. So it is a good idea to explicitly pass the name of the target domain as `SERVER_NAME` when calling the client. | ||
|
|
||
| ```py | ||
| from django.test import TestCase | ||
|
|
||
| class MyTest(TestCase): | ||
| def my_test(self): | ||
| response = self.client.get( | ||
| "/profile/", | ||
| SERVER_NAME="www.example.org", | ||
| ) | ||
| ``` | ||
|
|
||
| ## When to clear the "script prefix" | ||
|
|
||
| In path mode, Janeway makes use of `django.urls.base.set_script_prefix` to insert the journal code into request URLs. However, this path prefix does not go away when another request is made in domain mode. You have to manually clear it by using `clear_script_prefix` in the `tearDown` method of any test case where path mode is used. | ||
|
|
||
| ```py | ||
| from django.urls.base import clear_script_prefix | ||
| from django.test import TestCase | ||
|
|
||
| class MyTest(TestCase): | ||
| def tearDown(self): | ||
| clear_script_prefix() | ||
| ``` | ||
|
|
||
| If the offending test cannot be found, you can remove the prefix in the `setUp` of any affected test cases. | ||
|
|
||
| ```py | ||
| def setUp(self): | ||
| clear_script_prefix() | ||
| ``` | ||
|
|
||
| ## How to clear the cache | ||
|
|
||
| Sometimes a test is affected by cached data. If needed you can clear the main | ||
| cache in your setup method or the main test: | ||
|
|
||
| ```py | ||
| from utils.shared import clear_cache | ||
|
|
||
| clear_cache() | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.