feat: add seed_sandbox_data management command - #429
Conversation
|
Thanks for the pull request, @efortish! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
Automates data seeding for the Sandbox environment (closes openedx-authz#382). Reads a JSON fixture of organizations, users, and authz role assignments and inserts it idempotently via get_or_create, so it's safe to rerun. --reset deletes previously seeded users first for a clean slate. Logs a summary of created/skipped/failed items and raises CommandError if any assignment fails, so failures show up clearly instead of silently passing. Course/library content creation is intentionally out of scope here since role assignment doesn't require the scoped object to exist yet (see openedx-authz#352); the bundled fixture points at role assignments over a demo org/course that already exist in the target Sandbox.
The bundled fixture pointed course-scoped role assignments at course-v1:OpenedX+DemoX+Demo_Course, which doesn't exist. Verified against a real devstack: the demo course actually imported there is course-v1:OpenedX+DemoX+DemoCourse (no underscore), and the mismatch made CourseScope.get_or_create_for_external_key raise CourseOverview.DoesNotExist for every course-scoped user (course_staff, course_editor, course_auditor), failing 3 of the 6 seeded role assignments.
8ddc000 to
93be64c
Compare
…xcept) CI's quality check failed on the new command: missing-function-docstring on the two helper methods, and broad-exception-caught on the per-item try/except blocks. Added docstrings and disabled broad-exception-caught with a comment, since catching Exception there is deliberate: one bad org/user/role shouldn't stop the rest of the fixture from seeding.
Codecov's project check failed after the last push since the org-creation and role-assignment except blocks, plus the final failed-summary/CommandError branch, had no test exercising them. Adds two tests that mock a failure in each path and assert on the counts, the log, and the raised CommandError. seed_sandbox_data.py is now at 100% coverage.
What this does
Closes #382. Right now testing course-authoring permissions on the Sandbox means manually creating orgs, users, and role assignments before you can even start. This adds a
seed_sandbox_datamanagement command that does that setup in one shot, and is safe to rerun whenever you need a fresh environment.openedx_authz/management/commands/data/sandbox_seed_data.jsonships a default org and 6 users, each with one role/scope pair covering the main course-authoring and library roles (platform-wide, org-wide, course-level, library-level).python manage.py cms seed_sandbox_datareads that fixture, creates the org/users withget_or_create, assigns roles through the realassign_role_to_user_in_scopeAPI, then reloads the Casbin policy. Running it again just skips what's already there instead of duplicating it.--resetdeletes the fixture's users first, for a clean slate.--data-file /path/to/other.jsonswaps in a different fixture without touching the bundled one.created/skipped/failedsummary and raisesCommandErrorif anything failed, instead of finishing green with half the data missing.Two things I left out on purpose. First, creating real course/library content: the fixture just points at ids that should already exist in the target Sandbox. Since #369 made role assignment work even when the scoped object doesn't exist yet, pointing at a not-yet-created course id won't error either, but it won't be much use for clicking around Studio until that course actually shows up. Second, wiring this into a CI/CD step (the optional part of the issue's acceptance criteria), that belongs to the Sandbox deploy pipeline, not this repo.
How to test this manually
pip install -e /path/to/openedx-authzinside the container after checking out the branch).Seeding complete: 7 created, 0 skipped, 0 failed.(the org plus the 6 users). If you'd already seeded before, the ones that exist show up asskippedinstead.skipped, not create duplicates.python manage.py cms seed_sandbox_data --reset. The 6 users get deleted and recreated (their DB id will change if you check before/after).edxby default) and verify what that role can and can't do: check that the course/library ids in the fixture match something real in your Sandbox first. The bundled default (course-v1:OpenedX+DemoX+DemoCourse) is just whatever demo course I had imported locally, it's very likely not the one in your environment.