2022-04-14 23:32:18 +02:00
|
|
|
from zerver.actions.realm_playgrounds import do_add_realm_playground
|
2020-10-27 02:14:56 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.models import RealmPlayground, 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",
|
2023-05-27 05:04:50 +02:00
|
|
|
"url_template": "https://python.example.com{code}",
|
2020-10-27 02:14:56 +01:00
|
|
|
}
|
|
|
|
# Now send a POST request to the API endpoint.
|
2022-11-04 20:26:18 +01:00
|
|
|
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
|
2020-10-27 02:14:56 +01:00
|
|
|
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",
|
2023-05-27 05:04:50 +02:00
|
|
|
"url_template": "https://python.example.com{code}",
|
2020-10-27 02:14:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Python playground 2",
|
|
|
|
"pygments_language": "Python",
|
2023-05-27 05:04:50 +02:00
|
|
|
"url_template": "https://python2.example.com{code}",
|
2020-10-27 02:14:56 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
for payload in data:
|
2022-11-04 20:26:18 +01:00
|
|
|
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
|
2020-10-27 02:14:56 +01:00
|
|
|
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 = {
|
2023-05-27 05:04:50 +02:00
|
|
|
"name": "Invalid characters in pygments language",
|
|
|
|
"pygments_language": "a$b$c",
|
|
|
|
"url_template": "https://template.com{code}",
|
2020-10-27 02:14:56 +01:00
|
|
|
}
|
2022-11-04 20:26:18 +01:00
|
|
|
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
|
2020-10-27 02:14:56 +01:00
|
|
|
self.assert_json_error(resp, "Invalid characters in pygments language")
|
|
|
|
|
2023-05-27 03:29:40 +02:00
|
|
|
payload = {
|
|
|
|
"name": "Template with an unexpected variable",
|
|
|
|
"pygments_language": "Python",
|
2023-05-27 05:04:50 +02:00
|
|
|
"url_template": "https://template.com{?test,code}",
|
2023-05-27 03:29:40 +02:00
|
|
|
}
|
|
|
|
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",
|
2023-05-27 05:04:50 +02:00
|
|
|
"url_template": "https://template.com?test={test",
|
2023-05-27 03:29:40 +02:00
|
|
|
}
|
|
|
|
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
|
|
|
|
self.assert_json_error(resp, "Invalid URL template.")
|
|
|
|
|
2023-05-27 05:04:50 +02:00
|
|
|
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')
|
|
|
|
|
2020-10-27 02:14:56 +01:00
|
|
|
def test_create_already_existing_playground(self) -> None:
|
|
|
|
iago = self.example_user("iago")
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"name": "Python playground",
|
|
|
|
"pygments_language": "Python",
|
2023-05-27 05:04:50 +02:00
|
|
|
"url_template": "https://python.example.com{code}",
|
2020-10-27 02:14:56 +01:00
|
|
|
}
|
2022-11-04 20:26:18 +01:00
|
|
|
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
|
2020-10-27 02:14:56 +01:00
|
|
|
self.assert_json_success(resp)
|
|
|
|
|
2022-11-04 20:26:18 +01:00
|
|
|
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
|
2021-05-13 03:39:29 +02:00
|
|
|
self.assert_json_error(
|
|
|
|
resp, "Realm playground with this Realm, Pygments language and Name already exists."
|
|
|
|
)
|
2020-10-27 02:14:56 +01:00
|
|
|
|
|
|
|
def test_not_realm_admin(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
|
2022-11-04 20:26:18 +01:00
|
|
|
resp = self.api_post(hamlet, "/api/v1/realm/playgrounds")
|
2020-10-27 02:14:56 +01:00
|
|
|
self.assert_json_error(resp, "Must be an organization administrator")
|
2020-10-27 02:21:22 +01:00
|
|
|
|
2022-11-04 20:26:18 +01:00
|
|
|
resp = self.api_delete(hamlet, "/api/v1/realm/playgrounds/1")
|
2020-10-27 02:21:22 +01:00
|
|
|
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")
|
|
|
|
|
2023-05-27 01:33:01 +02:00
|
|
|
playground_id = do_add_realm_playground(
|
|
|
|
realm,
|
|
|
|
acting_user=iago,
|
2020-10-27 02:21:22 +01:00
|
|
|
name="Python playground",
|
|
|
|
pygments_language="Python",
|
2023-05-27 05:04:50 +02:00
|
|
|
url_template="https://python.example.com{code}",
|
2020-10-27 02:21:22 +01:00
|
|
|
)
|
|
|
|
self.assertTrue(RealmPlayground.objects.filter(name="Python playground").exists())
|
|
|
|
|
2022-11-04 20:26:18 +01:00
|
|
|
result = self.api_delete(iago, f"/api/v1/realm/playgrounds/{playground_id + 1}")
|
2020-10-27 02:21:22 +01:00
|
|
|
self.assert_json_error(result, "Invalid playground")
|
|
|
|
|
2022-11-04 20:26:18 +01:00
|
|
|
result = self.api_delete(iago, f"/api/v1/realm/playgrounds/{playground_id}")
|
2020-10-27 02:21:22 +01:00
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertFalse(RealmPlayground.objects.filter(name="Python").exists())
|