Files
upstream-zulip/zerver/tests/test_realm_playgrounds.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

184 lines
7.0 KiB
Python

from zerver.actions.realm_playgrounds import check_add_realm_playground
from zerver.actions.realm_settings import do_set_realm_property
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmPlayground
from zerver.models.realms import get_realm
class RealmPlaygroundTests(ZulipTestCase):
def test_create_one_playground_entry(self) -> None:
iago = self.example_user("iago")
payload = {
"name": "Python playground",
"pygments_language": "Python",
"url_template": "https://python.example.com{code}",
}
# Now send a POST request to the API endpoint.
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_success(resp)
# Check if the actual object exists
realm = get_realm("zulip")
self.assertTrue(
RealmPlayground.objects.filter(realm=realm, name="Python playground").exists()
)
def test_create_multiple_playgrounds_for_same_language(self) -> None:
iago = self.example_user("iago")
data = [
{
"name": "Python playground 1",
"pygments_language": "Python",
"url_template": "https://python.example.com{code}",
},
{
"name": "Python playground 2",
"pygments_language": "Python",
"url_template": "https://python2.example.com{code}",
},
]
for payload in data:
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_success(resp)
realm = get_realm("zulip")
self.assertTrue(
RealmPlayground.objects.filter(realm=realm, name="Python playground 1").exists()
)
self.assertTrue(
RealmPlayground.objects.filter(realm=realm, name="Python playground 2").exists()
)
def test_invalid_params(self) -> None:
iago = self.example_user("iago")
payload = {
"name": "Invalid characters in pygments language",
"pygments_language": "a$b$c",
"url_template": "https://template.com{code}",
}
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(
resp,
"Invalid character in language: $",
)
payload = {
"name": "Template with an unexpected variable",
"pygments_language": "Python",
"url_template": "https://template.com{?test,code}",
}
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(
resp, '"code" should be the only variable present in the URL template'
)
payload = {
"name": "Invalid URL template",
"pygments_language": "Python",
"url_template": "https://template.com?test={test",
}
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(resp, "Invalid URL template.")
payload = {
"name": "Template without the required variable",
"pygments_language": "Python",
"url_template": "https://template.com{?test}",
}
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(resp, 'Missing the required variable "code" in the URL template')
def test_add_realm_playground_validation(self) -> None:
iago = self.example_user("iago")
# Test to check that spaces are rejected
payload = {
"name": "Bad Name",
"pygments_language": "rust lang",
"url_template": "https://example.com",
}
result = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(
result,
"Invalid character in language: ",
)
# Test to check that restricted keywords are rejected
payload = {
"name": "Bad Keyword",
"pygments_language": "math",
"url_template": "https://example.com",
}
result = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(result, "Language 'math' is not allowed.")
def test_create_already_existing_playground(self) -> None:
iago = self.example_user("iago")
payload = {
"name": "Python playground",
"pygments_language": "Python",
"url_template": "https://python.example.com{code}",
}
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_success(resp)
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(
resp, "Realm playground with this Realm, Pygments language and Name already exists."
)
def test_not_realm_admin(self) -> None:
hamlet = self.example_user("hamlet")
resp = self.api_post(hamlet, "/api/v1/realm/playgrounds")
self.assert_json_error(resp, "Must be an organization administrator")
resp = self.api_delete(hamlet, "/api/v1/realm/playgrounds/1")
self.assert_json_error(resp, "Must be an organization administrator")
def test_delete_realm_playground(self) -> None:
iago = self.example_user("iago")
realm = get_realm("zulip")
playground_id = check_add_realm_playground(
realm,
acting_user=iago,
name="Python playground",
pygments_language="Python",
url_template="https://python.example.com{code}",
)
self.assertTrue(RealmPlayground.objects.filter(name="Python playground").exists())
result = self.api_delete(iago, f"/api/v1/realm/playgrounds/{playground_id + 1}")
self.assert_json_error(result, "Invalid playground")
result = self.api_delete(iago, f"/api/v1/realm/playgrounds/{playground_id}")
self.assert_json_success(result)
self.assertFalse(RealmPlayground.objects.filter(name="Python").exists())
def test_delete_default_code_block_language_playground(self) -> None:
iago = self.example_user("iago")
realm = get_realm("zulip")
playground_id = check_add_realm_playground(
realm,
acting_user=iago,
name="Python playground",
pygments_language="Python",
url_template="https://python.example.com{code}",
)
self.assertTrue(RealmPlayground.objects.filter(name="Python playground").exists())
# Set the default code block language to the playground's language
do_set_realm_property(realm, "default_code_block_language", "Python", acting_user=iago)
self.assertEqual(realm.default_code_block_language, "Python")
result = self.api_delete(iago, f"/api/v1/realm/playgrounds/{playground_id}")
self.assert_json_success(result)
realm.refresh_from_db()
self.assertFalse(RealmPlayground.objects.filter(name="Python").exists())
self.assertEqual(realm.default_code_block_language, "")