2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2017-01-21 08:19:03 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
|
2022-04-14 23:58:15 +02:00
|
|
|
from zerver.actions.create_realm import do_create_realm
|
2022-04-14 23:57:26 +02:00
|
|
|
from zerver.actions.realm_domains import do_change_realm_domain, do_remove_realm_domain
|
2022-04-14 23:57:15 +02:00
|
|
|
from zerver.actions.realm_settings import do_set_realm_property
|
2022-04-14 23:48:28 +02:00
|
|
|
from zerver.actions.users import do_change_user_role
|
2017-01-21 08:19:03 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.email_validation import email_allowed_for_realm
|
2016-12-26 19:19:02 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import DomainNotAllowedForRealmError, RealmDomain, UserProfile, get_realm
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
|
2017-03-31 16:20:07 +02:00
|
|
|
class RealmDomainTest(ZulipTestCase):
|
2020-03-06 16:22:23 +01:00
|
|
|
def setUp(self) -> None:
|
2023-10-12 18:49:16 +02:00
|
|
|
super().setUp()
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2021-03-01 11:33:24 +01:00
|
|
|
do_set_realm_property(realm, "emails_restricted_to_domains", True, acting_user=None)
|
2020-03-06 16:22:23 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_list_realm_domains(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("iago")
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
RealmDomain.objects.create(realm=realm, domain="acme.com", allow_subdomains=True)
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_get("/json/realm/domains")
|
2022-06-07 01:37:01 +02:00
|
|
|
received = self.assert_json_success(result)["domains"]
|
2021-05-12 19:50:08 +02:00
|
|
|
expected = [
|
|
|
|
{"domain": "zulip.com", "allow_subdomains": False},
|
|
|
|
{"domain": "acme.com", "allow_subdomains": True},
|
|
|
|
]
|
2017-01-21 08:19:03 +01:00
|
|
|
self.assertEqual(received, expected)
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2021-07-29 09:10:33 +02:00
|
|
|
def test_not_realm_owner(self) -> None:
|
|
|
|
self.login("iago")
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_post("/json/realm/domains")
|
2021-07-29 09:10:33 +02:00
|
|
|
self.assert_json_error(result, "Must be an organization owner")
|
2017-01-21 08:19:03 +01:00
|
|
|
result = self.client_patch("/json/realm/domains/15")
|
2021-07-29 09:10:33 +02:00
|
|
|
self.assert_json_error(result, "Must be an organization owner")
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_delete("/json/realm/domains/15")
|
2021-07-29 09:10:33 +02:00
|
|
|
self.assert_json_error(result, "Must be an organization owner")
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_create_realm_domain(self) -> None:
|
2021-07-29 09:10:33 +02:00
|
|
|
self.login("desdemona")
|
2021-02-12 08:19:30 +01:00
|
|
|
data = {
|
2021-05-04 16:31:26 +02:00
|
|
|
"domain": "",
|
2021-02-12 08:20:45 +01:00
|
|
|
"allow_subdomains": orjson.dumps(True).decode(),
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Invalid domain: Domain can't be empty.")
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2021-05-04 16:31:26 +02:00
|
|
|
data["domain"] = "acme.com"
|
2016-12-26 19:19:02 +01:00
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertTrue(
|
|
|
|
RealmDomain.objects.filter(
|
2021-02-12 08:20:45 +01:00
|
|
|
realm=realm, domain="acme.com", allow_subdomains=True
|
2021-02-12 08:19:30 +01:00
|
|
|
).exists()
|
|
|
|
)
|
2016-12-26 19:19:02 +01:00
|
|
|
|
|
|
|
result = self.client_post("/json/realm/domains", info=data)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assert_json_error(
|
2021-02-12 08:20:45 +01:00
|
|
|
result, "The domain acme.com is already a part of your organization."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-05-23 01:27:31 +02:00
|
|
|
mit_user_profile = self.mit_user("sipbtest")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(mit_user_profile)
|
2017-05-23 01:27:31 +02:00
|
|
|
|
2021-07-29 09:10:33 +02:00
|
|
|
do_change_user_role(mit_user_profile, UserProfile.ROLE_REALM_OWNER, acting_user=None)
|
2017-10-02 08:04:12 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
result = self.client_post(
|
|
|
|
"/json/realm/domains", info=data, HTTP_HOST=mit_user_profile.realm.host
|
|
|
|
)
|
2017-10-02 08:04:12 +02:00
|
|
|
self.assert_json_success(result)
|
2017-01-30 19:30:57 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_patch_realm_domain(self) -> None:
|
2021-07-29 09:10:33 +02:00
|
|
|
self.login("desdemona")
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
RealmDomain.objects.create(realm=realm, domain="acme.com", allow_subdomains=False)
|
2017-01-21 08:19:03 +01:00
|
|
|
data = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"allow_subdomains": orjson.dumps(True).decode(),
|
2017-01-21 08:19:03 +01:00
|
|
|
}
|
|
|
|
url = "/json/realm/domains/acme.com"
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertTrue(
|
|
|
|
RealmDomain.objects.filter(
|
2021-02-12 08:20:45 +01:00
|
|
|
realm=realm, domain="acme.com", allow_subdomains=True
|
2021-02-12 08:19:30 +01:00
|
|
|
).exists()
|
|
|
|
)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
|
|
|
url = "/json/realm/domains/non-existent.com"
|
|
|
|
result = self.client_patch(url, data)
|
|
|
|
self.assertEqual(result.status_code, 400)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "No entry found for domain non-existent.com.")
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_realm_domain(self) -> None:
|
2021-07-29 09:10:33 +02:00
|
|
|
self.login("desdemona")
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
RealmDomain.objects.create(realm=realm, domain="acme.com")
|
2017-01-21 09:09:27 +01:00
|
|
|
result = self.client_delete("/json/realm/domains/non-existent.com")
|
|
|
|
self.assertEqual(result.status_code, 400)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "No entry found for domain non-existent.com.")
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-01-21 08:19:03 +01:00
|
|
|
result = self.client_delete("/json/realm/domains/acme.com")
|
2016-12-26 19:19:02 +01:00
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertFalse(RealmDomain.objects.filter(domain="acme.com").exists())
|
2018-07-27 23:26:29 +02:00
|
|
|
self.assertTrue(realm.emails_restricted_to_domains)
|
2017-01-26 10:52:56 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_delete_all_realm_domains(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.login("iago")
|
|
|
|
realm = get_realm("zulip")
|
2017-03-31 16:20:07 +02:00
|
|
|
query = RealmDomain.objects.filter(realm=realm)
|
2017-01-26 10:52:56 +01:00
|
|
|
|
2018-07-27 23:26:29 +02:00
|
|
|
self.assertTrue(realm.emails_restricted_to_domains)
|
2017-03-31 22:21:13 +02:00
|
|
|
for realm_domain in query.all():
|
2021-04-08 12:40:46 +02:00
|
|
|
do_remove_realm_domain(realm_domain, acting_user=None)
|
2017-01-26 10:52:56 +01:00
|
|
|
self.assertEqual(query.count(), 0)
|
2018-07-27 23:26:29 +02:00
|
|
|
# Deleting last realm_domain should set `emails_restricted_to_domains` to False.
|
2017-03-31 22:21:13 +02:00
|
|
|
# This should be tested on a fresh instance, since the cached objects
|
|
|
|
# would not be updated.
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertFalse(get_realm("zulip").emails_restricted_to_domains)
|
2016-12-26 19:19:02 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_email_allowed_for_realm(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm1 = do_create_realm("testrealm1", "Test Realm 1", emails_restricted_to_domains=True)
|
|
|
|
realm2 = do_create_realm("testrealm2", "Test Realm 2", emails_restricted_to_domains=True)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
realm_domain = RealmDomain.objects.create(
|
2021-02-12 08:20:45 +01:00
|
|
|
realm=realm1, domain="test1.com", allow_subdomains=False
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmDomain.objects.create(realm=realm2, domain="test2.test1.com", allow_subdomains=True)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
email_allowed_for_realm("user@test1.com", realm1)
|
2018-03-14 12:54:05 +01:00
|
|
|
with self.assertRaises(DomainNotAllowedForRealmError):
|
2021-02-12 08:20:45 +01:00
|
|
|
email_allowed_for_realm("user@test2.test1.com", realm1)
|
|
|
|
email_allowed_for_realm("user@test2.test1.com", realm2)
|
|
|
|
email_allowed_for_realm("user@test3.test2.test1.com", realm2)
|
2018-03-14 12:54:05 +01:00
|
|
|
with self.assertRaises(DomainNotAllowedForRealmError):
|
2021-02-12 08:20:45 +01:00
|
|
|
email_allowed_for_realm("user@test3.test1.com", realm2)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2022-03-07 15:19:13 +01:00
|
|
|
do_change_realm_domain(realm_domain, True, acting_user=None)
|
2021-02-12 08:20:45 +01:00
|
|
|
email_allowed_for_realm("user@test1.com", realm1)
|
|
|
|
email_allowed_for_realm("user@test2.test1.com", realm1)
|
2018-03-14 12:54:05 +01:00
|
|
|
with self.assertRaises(DomainNotAllowedForRealmError):
|
2021-02-12 08:20:45 +01:00
|
|
|
email_allowed_for_realm("user@test2.com", realm1)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_realm_realm_domains_uniqueness(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("zulip")
|
2017-10-02 08:32:09 +02:00
|
|
|
with self.assertRaises(IntegrityError):
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmDomain.objects.create(realm=realm, domain="zulip.com", allow_subdomains=True)
|
2017-01-21 08:19:03 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_validate_domain(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
invalid_domains = [
|
2021-02-12 08:20:45 +01:00
|
|
|
"",
|
|
|
|
"test",
|
|
|
|
"t.",
|
|
|
|
"test.",
|
|
|
|
".com",
|
|
|
|
"-test",
|
|
|
|
"test...com",
|
|
|
|
"test-",
|
|
|
|
"test_domain.com",
|
|
|
|
"test.-domain.com",
|
|
|
|
"a" * 255 + ".com",
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2017-01-21 08:19:03 +01:00
|
|
|
for domain in invalid_domains:
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
validate_domain(domain)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
valid_domains = ["acme.com", "x-x.y.3.z"]
|
2017-01-21 08:19:03 +01:00
|
|
|
for domain in valid_domains:
|
|
|
|
validate_domain(domain)
|