forked from dimagi/commcare-connect
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: E2E testing infrastructure with Playwright #34
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 1 commit
Commits
Show all changes
3 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
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
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,83 @@ | ||
| """ | ||
| DEBUG-only view to inject a real OAuth session for Playwright E2E tests. | ||
|
|
||
| Reads the CLI token from TokenManager, introspects it against production, | ||
| fetches org data, and writes labs_oauth into the Django session — exactly | ||
| like BaseLabsURLTest does for the Django test client. | ||
| """ | ||
|
|
||
| import logging | ||
| from datetime import datetime | ||
|
|
||
| from django.conf import settings | ||
| from django.http import JsonResponse | ||
| from django.utils import timezone | ||
| from django.views.decorators.http import require_GET | ||
|
|
||
| from commcare_connect.labs.integrations.connect.cli import TokenManager | ||
| from commcare_connect.labs.integrations.connect.oauth import ( | ||
| fetch_user_organization_data, | ||
| introspect_token, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @require_GET | ||
| def test_auth_view(request): | ||
| """Inject a real OAuth session for E2E testing. DEBUG only.""" | ||
| if not settings.DEBUG: | ||
| return JsonResponse({"error": "Only available in DEBUG mode"}, status=403) | ||
|
|
||
| token_manager = TokenManager() | ||
| token_data = token_manager.load_token() | ||
|
|
||
| if not token_data: | ||
| return JsonResponse({"error": "No CLI token found. Run: python manage.py get_cli_token"}, status=401) | ||
|
|
||
| if token_manager.is_expired(): | ||
| return JsonResponse({"error": "CLI token expired. Run: python manage.py get_cli_token"}, status=401) | ||
|
|
||
| access_token = token_data["access_token"] | ||
|
|
||
| # Introspect token to get user profile | ||
| profile_data = introspect_token( | ||
| access_token=access_token, | ||
| client_id=settings.CONNECT_OAUTH_CLIENT_ID, | ||
| client_secret=settings.CONNECT_OAUTH_CLIENT_SECRET, | ||
| production_url=settings.CONNECT_PRODUCTION_URL, | ||
| ) | ||
| if not profile_data: | ||
| return JsonResponse({"error": "Token introspection failed"}, status=401) | ||
|
|
||
| # Fetch org data | ||
| org_data = fetch_user_organization_data(access_token) | ||
| if not org_data: | ||
| return JsonResponse({"error": "Failed to fetch organization data"}, status=500) | ||
|
|
||
| # Convert expires_at from ISO string to timestamp | ||
| if "expires_at" in token_data and isinstance(token_data["expires_at"], str): | ||
| expires_at = datetime.fromisoformat(token_data["expires_at"]).timestamp() | ||
| else: | ||
| expires_in = token_data.get("expires_in", 1209600) | ||
| expires_at = (timezone.now() + timezone.timedelta(seconds=expires_in)).timestamp() | ||
|
|
||
| # Write session — same structure as the OAuth callback | ||
| request.session["labs_oauth"] = { | ||
| "access_token": access_token, | ||
| "refresh_token": token_data.get("refresh_token", ""), | ||
| "expires_at": expires_at, | ||
| "user_profile": { | ||
| "id": profile_data.get("id"), | ||
| "username": profile_data.get("username"), | ||
| "email": profile_data.get("email"), | ||
| "first_name": profile_data.get("first_name", ""), | ||
| "last_name": profile_data.get("last_name", ""), | ||
| }, | ||
| "organization_data": org_data, | ||
| } | ||
|
|
||
| return JsonResponse({ | ||
| "success": True, | ||
| "username": profile_data.get("username"), | ||
| }) |
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.
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.
Make
/labs/test-auth/truly DEBUG-only.test_auth_viewcheckssettings.DEBUG, but Line 22 still mounts the route in every labs deploy, and the middleware change makes it public before the handler runs. That means non-debug environments get a new unauthenticated code path instead of preserving the existing auth/redirect behavior. Please register this URL only whensettings.DEBUGis true, and gate the matchingpublic_pathsentry the same way.🤖 Prompt for AI Agents