2018-12-14 20:10:20 +01:00
|
|
|
import re
|
|
|
|
|
2016-02-13 19:17:15 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2021-04-15 19:41:12 +02:00
|
|
|
from zerver.models import RealmFilter
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2016-02-13 19:17:15 +01:00
|
|
|
class RealmFilterTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("iago")
|
2021-04-15 19:41:12 +02:00
|
|
|
data = {
|
|
|
|
"pattern": "#(?P<id>[123])",
|
|
|
|
"url_format_string": "https://realm.com/my_realm_filter/%(id)s",
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
2021-03-30 12:51:54 +02:00
|
|
|
result = self.client_get("/json/realm/linkifiers")
|
2016-02-13 19:17:15 +01:00
|
|
|
self.assert_json_success(result)
|
2021-04-15 19:41:12 +02:00
|
|
|
linkifiers = result.json()["linkifiers"]
|
|
|
|
self.assertEqual(len(linkifiers), 1)
|
|
|
|
self.assertEqual(linkifiers[0]["pattern"], "#(?P<id>[123])")
|
|
|
|
self.assertEqual(linkifiers[0]["url_format"], "https://realm.com/my_realm_filter/%(id)s")
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_create(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("iago")
|
2016-02-13 19:17:15 +01:00
|
|
|
data = {"pattern": "", "url_format_string": "https://realm.com/my_realm_filter/%(id)s"}
|
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "This field cannot be blank.")
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = "$a"
|
2016-02-13 19:17:15 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
2021-03-30 12:08:03 +02:00
|
|
|
result, "Invalid linkifier pattern. Valid characters are [ a-zA-Z_#=/:+!-]."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"ZUL-(?P<id>\d++)"
|
2016-02-13 19:17:15 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
2021-03-30 12:08:03 +02:00
|
|
|
result, "Invalid linkifier pattern. Valid characters are [ a-zA-Z_#=/:+!-]."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"ZUL-(?P<id>\d+)"
|
|
|
|
data["url_format_string"] = "$fgfg"
|
2016-02-13 19:17:15 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Enter a valid URL.")
|
2017-07-22 01:08:26 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"ZUL-(?P<id>\d+)"
|
|
|
|
data["url_format_string"] = "https://realm.com/my_realm_filter/"
|
2017-07-22 01:08:26 +02:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Invalid URL format string.")
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["url_format_string"] = "https://realm.com/my_realm_filter/#hashtag/%(id)s"
|
2016-02-13 19:17:15 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "ZUL-15"))
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"ZUL2-(?P<id>\d+)"
|
|
|
|
data["url_format_string"] = "https://realm.com/my_realm_filter/?value=%(id)s"
|
2017-03-26 01:13:34 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "ZUL2-15"))
|
2018-12-14 20:10:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"_code=(?P<id>[0-9a-zA-Z]+)"
|
|
|
|
data["url_format_string"] = "https://example.com/product/%(id)s/details"
|
2018-12-14 20:10:20 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "_code=123abcdZ"))
|
2018-12-14 20:10:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"PR (?P<id>[0-9]+)"
|
2021-02-12 08:19:30 +01:00
|
|
|
data[
|
2021-02-12 08:20:45 +01:00
|
|
|
"url_format_string"
|
|
|
|
] = "https://example.com/~user/web#view_type=type&model=model&action=12345&id=%(id)s"
|
2018-12-14 20:10:20 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "PR 123"))
|
2018-12-14 20:10:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"lp/(?P<id>[0-9]+)"
|
|
|
|
data["url_format_string"] = "https://realm.com/my_realm_filter/?value=%(id)s&sort=reverse"
|
2018-12-14 20:10:20 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "lp/123"))
|
2018-12-14 20:10:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"lp:(?P<id>[0-9]+)"
|
|
|
|
data["url_format_string"] = "https://realm.com/my_realm_filter/?sort=reverse&value=%(id)s"
|
2018-12-14 20:10:20 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "lp:123"))
|
2018-12-14 20:10:20 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"!(?P<id>[0-9]+)"
|
2021-02-12 08:19:30 +01:00
|
|
|
data[
|
2021-02-12 08:20:45 +01:00
|
|
|
"url_format_string"
|
|
|
|
] = "https://realm.com/index.pl?Action=AgentTicketZoom;TicketNumber=%(id)s"
|
2018-12-14 20:10:20 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "!123"))
|
2017-03-26 01:13:34 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data["pattern"] = r"(?P<org>[a-zA-Z0-9_-]+)/(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>[0-9]+)"
|
|
|
|
data["url_format_string"] = "https://github.com/%(org)s/%(repo)s/issue/%(id)s"
|
2018-11-21 04:34:28 +01:00
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
2018-12-17 21:12:52 +01:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "zulip/zulip#123"))
|
2018-11-21 04:34:28 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_not_realm_admin(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("hamlet")
|
2016-02-13 19:17:15 +01:00
|
|
|
result = self.client_post("/json/realm/filters")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Must be an organization administrator")
|
2016-02-13 19:17:15 +01:00
|
|
|
result = self.client_delete("/json/realm/filters/15")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Must be an organization administrator")
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("iago")
|
2021-04-15 19:41:12 +02:00
|
|
|
data = {
|
|
|
|
"pattern": "#(?P<id>[123])",
|
|
|
|
"url_format_string": "https://realm.com/my_realm_filter/%(id)s",
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
linkifier_id = result.json()["id"]
|
2021-03-30 12:15:39 +02:00
|
|
|
linkifiers_count = RealmFilter.objects.count()
|
|
|
|
result = self.client_delete(f"/json/realm/filters/{linkifier_id + 1}")
|
2021-04-14 04:21:49 +02:00
|
|
|
self.assert_json_error(result, "Linkifier not found.")
|
2016-02-13 19:17:15 +01:00
|
|
|
|
2021-03-30 12:15:39 +02:00
|
|
|
result = self.client_delete(f"/json/realm/filters/{linkifier_id}")
|
2016-02-13 19:17:15 +01:00
|
|
|
self.assert_json_success(result)
|
2021-03-30 12:15:39 +02:00
|
|
|
self.assertEqual(RealmFilter.objects.count(), linkifiers_count - 1)
|
2021-04-15 19:51:36 +02:00
|
|
|
|
|
|
|
def test_update(self) -> None:
|
|
|
|
self.login("iago")
|
|
|
|
data = {
|
|
|
|
"pattern": "#(?P<id>[123])",
|
|
|
|
"url_format_string": "https://realm.com/my_realm_filter/%(id)s",
|
|
|
|
}
|
|
|
|
result = self.client_post("/json/realm/filters", info=data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
|
|
|
|
linkifier_id = result.json()["id"]
|
|
|
|
data = {
|
|
|
|
"pattern": "#(?P<id>[0-9]+)",
|
|
|
|
"url_format_string": "https://realm.com/my_realm_filter/issues/%(id)s",
|
|
|
|
}
|
|
|
|
result = self.client_patch(f"/json/realm/filters/{linkifier_id}", info=data)
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertIsNotNone(re.match(data["pattern"], "#1234"))
|
|
|
|
|
|
|
|
# Verify that the linkifier is updated accordingly.
|
|
|
|
result = self.client_get("/json/realm/linkifiers")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
linkifier = result.json()["linkifiers"]
|
|
|
|
self.assertEqual(len(linkifier), 1)
|
|
|
|
self.assertEqual(linkifier[0]["pattern"], "#(?P<id>[0-9]+)")
|
|
|
|
self.assertEqual(
|
|
|
|
linkifier[0]["url_format"], "https://realm.com/my_realm_filter/issues/%(id)s"
|
|
|
|
)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"pattern": r"ZUL-(?P<id>\d++)",
|
|
|
|
"url_format_string": "https://realm.com/my_realm_filter/%(id)s",
|
|
|
|
}
|
|
|
|
result = self.client_patch(f"/json/realm/filters/{linkifier_id}", info=data)
|
|
|
|
self.assert_json_error(
|
|
|
|
result, "Invalid linkifier pattern. Valid characters are [ a-zA-Z_#=/:+!-]."
|
|
|
|
)
|
|
|
|
|
|
|
|
data["pattern"] = r"ZUL-(?P<id>\d+)"
|
|
|
|
data["url_format_string"] = "$fgfg"
|
|
|
|
result = self.client_patch(f"/json/realm/filters/{linkifier_id}", info=data)
|
|
|
|
self.assert_json_error(result, "Enter a valid URL.")
|
|
|
|
|
|
|
|
data["pattern"] = r"#(?P<id>[123])"
|
|
|
|
data["url_format_string"] = "https://realm.com/my_realm_filter/%(id)s"
|
|
|
|
result = self.client_patch(f"/json/realm/filters/{linkifier_id + 1}", info=data)
|
|
|
|
self.assert_json_error(result, "Linkifier not found.")
|