Skip to content

feat: add seed_sandbox_data management command - #429

Open
efortish wants to merge 4 commits into
openedx:mainfrom
eduNEXT:ks/issue-382-seed-sandbox-data
Open

feat: add seed_sandbox_data management command#429
efortish wants to merge 4 commits into
openedx:mainfrom
eduNEXT:ks/issue-382-seed-sandbox-data

Conversation

@efortish

@efortish efortish commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

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_data management command that does that setup in one shot, and is safe to rerun whenever you need a fresh environment.

  • Fixture: openedx_authz/management/commands/data/sandbox_seed_data.json ships 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).
  • Command: python manage.py cms seed_sandbox_data reads that fixture, creates the org/users with get_or_create, assigns roles through the real assign_role_to_user_in_scope API, then reloads the Casbin policy. Running it again just skips what's already there instead of duplicating it.
  • --reset deletes the fixture's users first, for a clean slate.
  • --data-file /path/to/other.json swaps in a different fixture without touching the bundled one.
  • Every org/user/role-assignment failure is logged individually and counted. The command prints a created/skipped/failed summary and raises CommandError if 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

  1. Get a shell in a CMS process running this branch (in an eduNEXT devstack, that's pip install -e /path/to/openedx-authz inside the container after checking out the branch).
  2. Run the seeder:
    python manage.py cms seed_sandbox_data
    
    On a fresh environment you should see 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 as skipped instead.
  3. Check what landed:
    python manage.py cms shell -c "
    from django.contrib.auth import get_user_model
    from openedx_authz.api.users import get_user_role_assignments_filtered
    for u in get_user_model().objects.filter(username__startswith='authz_'):
        for a in get_user_role_assignments_filtered(user_external_key=u.username):
            for role in a.roles:
                print(u.username, '->', role.external_key, '@', a.scope.external_key)
    "
    
    You should see all 6 users with their expected role and scope.
  4. Run the command again with no flags. It should report everything as skipped, not create duplicates.
  5. Run 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).
  6. To actually log into Studio/LMS as one of these users (password edx by 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.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Aug 31, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @efortish!

This repository is currently maintained by @openedx/committers-openedx-authz.

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 approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To 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:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where 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:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 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.
@efortish
efortish force-pushed the ks/issue-382-seed-sandbox-data branch from 8ddc000 to 93be64c Compare September 1, 2026 16:35
…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.
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Ready for Review in Contributions Sep 2, 2026
@mphilbrick211
mphilbrick211 requested a review from a team September 2, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

Automate Data Seeding in the Sandbox Environment

3 participants