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}, )
def test_strings_to_roles_success(): roles = strings_to_roles(["ProjectDeveloper"]) assert len(roles) == 1 assert roles[0] == ProjectDeveloper
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"])
def test_strings_to_roles_success(): roles = strings_to_roles(["ProjectDeveloper"], "jobserver.models.ProjectMembership") assert len(roles) == 1 assert roles[0] == ProjectDeveloper
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")
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")