2018-12-14 20:10:20 +01:00
|
|
|
import re
|
|
|
|
|
2021-03-30 12:15:39 +02:00
|
|
|
from zerver.lib.actions import do_add_linkifier
|
2016-02-13 19:17:15 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2019-05-04 04:47:44 +02:00
|
|
|
from zerver.models import RealmFilter, get_realm
|
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")
|
|
|
|
realm = get_realm("zulip")
|
2021-03-30 12:15:39 +02:00
|
|
|
do_add_linkifier(realm, "#(?P<id>[123])", "https://realm.com/my_realm_filter/%(id)s")
|
2016-02-13 19:17:15 +01:00
|
|
|
result = self.client_get("/json/realm/filters")
|
|
|
|
self.assert_json_success(result)
|
|
|
|
self.assertEqual(200, result.status_code)
|
2017-08-16 09:51:46 +02:00
|
|
|
self.assertEqual(len(result.json()["filters"]), 1)
|
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")
|
|
|
|
realm = get_realm("zulip")
|
2021-03-30 12:15:39 +02:00
|
|
|
linkifier_id = do_add_linkifier(
|
2021-02-12 08:19:30 +01:00
|
|
|
realm, "#(?P<id>[123])", "https://realm.com/my_realm_filter/%(id)s"
|
|
|
|
)
|
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-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Filter 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)
|