def handle(self, *args, **options):
        username = options["username"]
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            self.stderr.write(f"Unknown username: {username}")

        project_slug = options["project"]
        try:
            project = Project.objects.get(slug=project_slug)
        except Project.DoesNotExist:
            self.stderr.write(f"Unknown Project slug: {project_slug}")
            sys.exit(1)

        try:
            selected_roles = strings_to_roles(
                options["roles"], "jobserver.models.ProjectMembership")
        except Exception as e:
            self.stderr.write(str(e))
            sys.exit(1)

        ProjectMembership.objects.update_or_create(
            project=project,
            user=user,
            defaults={"roles": selected_roles},
        )
示例#2
0
def test_strings_to_roles_success():
    roles = strings_to_roles(["ProjectDeveloper"])

    assert len(roles) == 1
    assert roles[0] == ProjectDeveloper
示例#3
0
def test_strings_to_roles_with_unknown_roles():
    msg = "Unknown Roles:\n - DummyRole\nAvailable Roles are:.*"
    with pytest.raises(Exception, match=msg):
        strings_to_roles(["DummyRole"])
示例#4
0
def test_strings_to_roles_success():
    roles = strings_to_roles(["ProjectDeveloper"],
                             "jobserver.models.ProjectMembership")

    assert len(roles) == 1
    assert roles[0] == ProjectDeveloper
示例#5
0
def test_strings_to_roles_with_unknown_roles():
    msg = (
        "Unknown Roles:\n - DummyRole\nAvailable Roles for jobserver.models.User are:.*"
    )
    with pytest.raises(Exception, match=msg):
        strings_to_roles(["DummyRole"], "jobserver.models.User")
示例#6
0
def test_strings_to_roles_with_no_available_roles():
    msg = "No Roles found with a link to 'dummy'.  model_path is a dotted path to a Model, eg: jobserver.models.User"

    with pytest.raises(Exception, match=msg):
        strings_to_roles(["ProjectDeveloper"], "dummy")