-
Notifications
You must be signed in to change notification settings - Fork 16
Add view tests, including testing with OIDC on/off #147
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
792c63d
Added a testing shim for OIDC.
evildmp 7be8aad
Added a series of tests for view functions
evildmp c9aae5f
format code
dwilding 7616b2d
require user when testing anchor URLs
dwilding 40e9271
test public/private pages depending on OIDC
dwilding 0ef5719
fix anchor test to require any user
dwilding 5e4fd7f
move user fixtures to conftest.py
dwilding f7216b9
in CI, run same tests with OIDC configured
dwilding 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
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
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,111 @@ | ||
| import sys | ||
| import types | ||
|
|
||
| import pytest | ||
|
|
||
| from django.contrib.auth.models import Permission, User | ||
| from django.http import HttpResponse | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_without_permissions(client): | ||
| user = User.objects.create_user(username="no_perm", password="password") | ||
| client.login(username="no_perm", password="password") | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_can_change_commitment(client): | ||
| user = User.objects.create_user(username="change_commitment", password="password") | ||
| permission = Permission.objects.get( | ||
| codename="change_commitment", | ||
| content_type__app_label="projects", | ||
| ) | ||
| user.user_permissions.add(permission) | ||
| client.login(username="change_commitment", password="password") | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_can_change_projectobjectivecondition(client): | ||
| user = User.objects.create_user( | ||
| username="change_projectobjectivecondition", password="password" | ||
| ) | ||
| permission = Permission.objects.get( | ||
| codename="change_projectobjectivecondition", | ||
| content_type__app_label="projects", | ||
| ) | ||
| user.user_permissions.add(permission) | ||
| client.login(username="change_projectobjectivecondition", password="password") | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_can_change_projectobjective(client): | ||
| user = User.objects.create_user( | ||
| username="change_projectobjective", password="password" | ||
| ) | ||
| permission = Permission.objects.get( | ||
| codename="change_projectobjective", | ||
| content_type__app_label="projects", | ||
| ) | ||
| user.user_permissions.add(permission) | ||
| client.login(username="change_projectobjective", password="password") | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_can_change_workcycle(client): | ||
| user = User.objects.create_user(username="change_workcycle", password="password") | ||
| permission = Permission.objects.get( | ||
| codename="change_workcycle", | ||
| content_type__app_label="framework", | ||
| ) | ||
| user.user_permissions.add(permission) | ||
| client.login(username="change_workcycle", password="password") | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_can_view_workcycle(client): | ||
| user = User.objects.create_user(username="view_workcycle", password="password") | ||
| permission = Permission.objects.get( | ||
| codename="view_workcycle", | ||
| content_type__app_label="framework", | ||
| ) | ||
| user.user_permissions.add(permission) | ||
| client.login(username="view_workcycle", password="password") | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_is_staff(client): | ||
| user = User.objects.create_user( | ||
| username="staffmember", password="password", is_staff=True | ||
| ) | ||
| client.login(username="staffmember", password="password") | ||
| return user | ||
|
|
||
|
|
||
| # Creat a "fake" mozilla_django_oidc.views so that tests will run, | ||
| # even if mozilla_django_oidc is not available at import time for | ||
| # tests. | ||
|
|
||
| if "mozilla_django_oidc" not in sys.modules: | ||
| oidc_module = types.ModuleType("mozilla_django_oidc") | ||
| oidc_views_module = types.ModuleType("mozilla_django_oidc.views") | ||
|
|
||
| class _DummyOIDCView: | ||
| @classmethod | ||
| def as_view(cls): | ||
| def _view(request, *args, **kwargs): | ||
| return HttpResponse("") | ||
|
|
||
| return _view | ||
|
|
||
| oidc_views_module.OIDCAuthenticationRequestView = _DummyOIDCView | ||
| oidc_views_module.OIDCAuthenticationCallbackView = _DummyOIDCView | ||
| oidc_views_module.OIDCLogoutView = _DummyOIDCView | ||
| oidc_module.views = oidc_views_module | ||
| sys.modules["mozilla_django_oidc"] = oidc_module | ||
| sys.modules["mozilla_django_oidc.views"] = oidc_views_module | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not keen on this approach.
mozilla_django_oidcis listed in our application'srequirements.txt, so we ought to be able to assume it's available in the venv at runtime and testing time.Unless you very strongly object, I'm going to remove this shim.
I think it's important that we run tests with a known combination of dependencies, otherwise it reduces the effectiveness of the tests.
If possible, I recommend the Makefile for running tests :
make test. But I appreciate that the Makefile isn't set up in an offline-friendly way at the moment. So alternatively, when you're online, this will make sure your venv has everything needed for offline work:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. No objections.