fix: record email optin preference against target user instead of caller - #487
Open
ssurendrannair wants to merge 1 commit into
Open
ssurendrannair wants to merge 1 commit into
ssurendrannair wants to merge 1 commit into
Conversation
ssurendrannair
requested review from
kramakrushna,
nakhan-sonata-afk and
subhashree-sahu31
September 22, 2026 07:31
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The targeted fix uses the already-authorized learner object and includes focused regression coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Corrects Enrollment API email opt-in attribution for staff/service-initiated enrollments.
Changes:
- Records preferences against the enrolled learner.
- Adds regression coverage ensuring the caller remains unaffected.
| File | Description |
|---|---|
openedx/core/djangoapps/enrollments/views.py |
Uses the resolved target user for email preferences. |
openedx/core/djangoapps/enrollments/tests/test_views.py |
Tests enrollment by a different staff caller. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
subhashree-sahu31
approved these changes
Sep 22, 2026
subhashree-sahu31
left a comment
There was a problem hiding this comment.
Small, well-scoped, single-purpose fix with focused test coverage — no concerns.
LGTM🚀.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes a bug in the Enrollment REST API (POST /api/enrollment/v1/enrollment) where the email_opt_in preference was being recorded against the API caller (request.user) instead of the learner actually being enrolled.
In EnrollmentListView.post(), the view already looks up the correct target user via User.objects.get(username=username) (stored in the user variable) specifically because request.user may differ from the learner being enrolled — for example, when a staff member or a server-to-server (API key) caller enrolls a different student. However, the call to update_email_opt_in() was mistakenly passed request.user instead of user, so the org-wide email preference was written to the caller's account rather than the enrolled learner's.
Impact:
Learner: their email opt-in/opt-out preference was silently never recorded when someone else (staff or a service) enrolled them with email_opt_in set.
Course Author / Operator: any staff-driven enrollment flow that also sets email_opt_in (e.g. bulk enrollment tooling, support tooling, integrations) was incorrectly writing a UserOrgTag preference against the staff/service account instead of the student.
Developer: no API contract change — the fix only corrects which user record is written to internally.
Supporting information
Jira: AUT-315
Testing instructions
Log in as a global staff user (or use a server-to-server request with API key permissions).
Send a POST to /api/enrollment/v1/enrollment enrolling a different user, including "email_opt_in": true and that user's "user" (username) in the payload, e.g.:
Verify a UserOrgTag record with key="email-optin" and value="True" now exists for <other_username> (the enrolled learner), and that no such record is created for the staff/service caller.
Automated coverage: openedx/core/djangoapps/enrollments/tests/test_views.py::EnrollmentTest::test_email_opt_in_recorded_for_enrolled_user_not_caller — a new regression test that fails on the old code and passes with the fix.
Deadline
None.
Other information
No database migration required.
No dependency on other in-flight changes.
Single-line behavioral fix; no API request/response shape changes.