Ejemplo n.º 1
0
    def test_realm_playground_entries(self) -> None:
        user = self.example_user("iago")
        intial_playgrounds = get_realm_playgrounds(user.realm)
        now = timezone_now()
        playground_id = do_add_realm_playground(
            user.realm,
            acting_user=user,
            name="Python playground",
            pygments_language="Python",
            url_prefix="https://python.example.com",
        )
        added_playground = RealmPlaygroundDict(
            id=playground_id,
            name="Python playground",
            pygments_language="Python",
            url_prefix="https://python.example.com",
        )
        expected_extra_data = {
            "realm_playgrounds": intial_playgrounds + [added_playground],
            "added_playground": added_playground,
        }
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=RealmAuditLog.REALM_PLAYGROUND_ADDED,
                event_time__gte=now,
                acting_user=user,
                extra_data=orjson.dumps(expected_extra_data).decode(),
            ).count(),
            1,
        )

        now = timezone_now()
        realm_playground = RealmPlayground.objects.get(id=playground_id)
        do_remove_realm_playground(
            user.realm,
            realm_playground,
            acting_user=user,
        )
        removed_playground = {
            "name": "Python playground",
            "pygments_language": "Python",
            "url_prefix": "https://python.example.com",
        }
        expected_extra_data = {
            "realm_playgrounds": intial_playgrounds,
            "removed_playground": removed_playground,
        }
        self.assertEqual(
            RealmAuditLog.objects.filter(
                realm=user.realm,
                event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,
                event_time__gte=now,
                acting_user=user,
                extra_data=orjson.dumps(expected_extra_data).decode(),
            ).count(),
            1,
        )
def remove_realm_playground() -> Dict[str, object]:
    playground_info = dict(
        name="Python playground",
        pygments_language="Python",
        url_prefix="https://python.example.com",
    )
    playground_id = do_add_realm_playground(get_realm("zulip"), **playground_info)
    return {
        "playground_id": playground_id,
    }
Ejemplo n.º 3
0
def add_realm_playground(
    request: HttpRequest,
    user_profile: UserProfile,
    name: str = REQ(),
    url_prefix: str = REQ(str_validator=check_url),
    pygments_language: str = REQ(str_validator=check_pygments_language),
) -> HttpResponse:
    try:
        playground_id = do_add_realm_playground(
            realm=user_profile.realm,
            name=name.strip(),
            pygments_language=pygments_language.strip(),
            url_prefix=url_prefix.strip(),
        )
    except ValidationError as e:
        raise ValidationFailureError(e)
    return json_success(request, data={"id": playground_id})
Ejemplo n.º 4
0
    def test_delete_realm_playground(self) -> None:
        iago = self.example_user("iago")
        realm = get_realm("zulip")

        playground_info = dict(
            name="Python playground",
            pygments_language="Python",
            url_prefix="https://python.example.com",
        )
        playground_id = do_add_realm_playground(realm, **playground_info)
        self.assertTrue(
            RealmPlayground.objects.filter(name="Python playground").exists())

        result = self.api_delete(
            iago, f"/json/realm/playgrounds/{playground_id + 1}")
        self.assert_json_error(result, "Invalid playground")

        result = self.api_delete(iago,
                                 f"/json/realm/playgrounds/{playground_id}")
        self.assert_json_success(result)
        self.assertFalse(
            RealmPlayground.objects.filter(name="Python").exists())