Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
demo-orgs: Restrict bot creation until owner has set an email.
Lauryn Menard610af680a268
diff --git a/web/src/settings_bots.ts b/web/src/settings_bots.tsindex 95022c7dd89..cfb48bee66e 100644--- a/web/src/settings_bots.ts+++ b/web/src/settings_bots.ts@@ -13,6 +13,7 @@ import * as bot_helper from "./bot_helper.ts";import * as buttons from "./buttons.ts";import * as channel from "./channel.ts";import {csrf_token} from "./csrf.ts";+import * as demo_organizations_ui from "./demo_organizations_ui.ts";import * as dialog_widget from "./dialog_widget.ts";import * as dropdown_widget from "./dropdown_widget.ts";import {$t, $t_html} from "./i18n.ts";@@ -271,10 +272,12 @@ function bot_owner_full_name(owner_id: number | null): string | undefined {}export function add_a_new_bot(): void {+ const user_has_email_set = !settings_data.user_email_not_configured();const modal_content_html = render_add_new_bot_form({bot_types: get_allowed_bot_types(),realm_embedded_bots: realm.realm_embedded_bots,realm_bot_domain: realm.realm_bot_domain,+ user_has_email_set,});let create_avatar_widget: UploadWidget;@@ -351,6 +354,11 @@ export function add_a_new_bot(): void {create_avatar_widget = avatar.build_bot_create_widget();+ if (!user_has_email_set) {+ $(util.the($<HTMLFormElement>("form#create_bot_form")).elements).prop("disabled", true);+ demo_organizations_ui.show_configure_email_banner();+ }+$("#create_bot_type").on("change", () => {const bot_type = $("#create_bot_type").val();// For "generic bot" or "incoming webhook" both these fields need not be displayed.diff --git a/web/templates/settings/add_new_bot_form.hbs b/web/templates/settings/add_new_bot_form.hbsindex c9d1f99e49b..14c89e4fd2b 100644--- a/web/templates/settings/add_new_bot_form.hbs+++ b/web/templates/settings/add_new_bot_form.hbs@@ -1,4 +1,7 @@<form class="modal-form" id="create_bot_form">+ {{#unless user_has_email_set}}+ <div class="demo-organization-add-email-banner banner-wrapper"></div>+ {{/unless}}<div class="new-bot-form"><div class="input-group"><label for="create_bot_type" class="modal-field-label">diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex 2230d3cb977..de5feb4f992 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -114,6 +114,36 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assert_json_error(result, "Bad name or username")self.assert_num_bots_equal(0)+ def test_add_bot_demo_organization_restrictions(self) -> None:+ demo_owner_account = self.create_demo_organization_owner()+ realm = demo_owner_account.realm+ bot_info = {+ "full_name": "Demo organization bot",+ "short_name": "demobot",+ "bot_type": "1",+ }++ # Confirm owner account has no associated bots.+ result = self.client_get("/json/bots", subdomain=realm.subdomain)+ response_dict = self.assert_json_success(result)+ self.assert_length(response_dict["bots"], 0)++ # Bot creation is restricted before demo organization owner email set.+ result = self.client_post("/json/bots", bot_info, subdomain=realm.subdomain)+ self.assert_json_error(result, "Configure owner account email address.")++ # Set owner email account.+ demo_owner_account.delivery_email = "[email protected]"+ demo_owner_account.save()++ result = self.client_post("/json/bots", bot_info, subdomain=realm.subdomain)+ self.assert_json_success(result)++ # Confirm owner account now has one associated bot.+ result = self.client_get("/json/bots", subdomain=realm.subdomain)+ response_dict = self.assert_json_success(result)+ self.assert_length(response_dict["bots"], 1)+@override_settings(FAKE_EMAIL_DOMAIN="invaliddomain", REALM_HOSTS={"zulip": "127.0.0.1"})def test_add_bot_with_invalid_fake_email_domain(self) -> None:self.login("hamlet")diff --git a/zerver/views/users.py b/zerver/views/users.pyindex b6b39364557..2d586a1e873 100644--- a/zerver/views/users.py+++ b/zerver/views/users.py@@ -42,6 +42,7 @@ from zerver.decorator import require_human_non_guest_user, require_realm_adminfrom zerver.forms import PASSWORD_TOO_WEAK_ERROR, CreateUserFormfrom zerver.lib.avatar import avatar_url, get_avatar_for_inaccessible_user, get_gravatar_urlfrom zerver.lib.bot_config import set_bot_config+from zerver.lib.demo_organizations import check_demo_organization_has_set_emailfrom zerver.lib.email_validation import email_allowed_for_realm, validate_email_not_already_in_realmfrom zerver.lib.exceptions import (CannotDeactivateLastUserError,@@ -646,6 +647,9 @@ def add_bot_backend(service_name: str | None = None,short_name_raw: Annotated[str, ApiParamConfig("short_name")],) -> HttpResponse:+ if user_profile.realm.demo_organization_scheduled_deletion_date is not None:+ check_demo_organization_has_set_email(user_profile.realm)+if config_data is None:config_data = {}try: