2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-01-11 12:16:28 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {run_test} = require("./lib/test");
|
2024-02-13 02:08:24 +01:00
|
|
|
const {current_user, realm} = require("./lib/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const bot_data_params = {
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_bots: [
|
|
|
|
{
|
|
|
|
api_key: "QadL788EkiottHmukyhHgePUFHREiu8b",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
avatar_url: "",
|
|
|
|
bot_type: 1, // DEFAULT_BOT
|
|
|
|
default_all_public_streams: true,
|
|
|
|
default_events_register_stream: "register stream 1",
|
|
|
|
default_sending_stream: "sending stream 1",
|
2020-07-15 00:34:28 +02:00
|
|
|
email: "error-bot@zulip.org",
|
|
|
|
full_name: "Error bot",
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
is_active: true,
|
|
|
|
owner: "someone 4",
|
|
|
|
owner_id: 4,
|
2020-07-15 00:34:28 +02:00
|
|
|
user_id: 1,
|
|
|
|
services: [],
|
ts: Migrate `bot_data.js` to TypeScript.
Add type annotations. Create custom types for Bot and Service.
Add zod data validation for incoming bot data from server.
Based on `zerver/openapi/zulip.yaml` description, `add` operation
(`op`) carries data that follows `Bot` structure. So taking
reference from `bot` structure, I create `ServerAddBotData` zod
schema and infer its type. Similarly, `update` operation carries
data that follows `BasicBot`, so I create `ServerUpdateBotData`.
Note that `Bot` inherits from `BasicBot`.
`zerver/openapi/zulip.yaml` describes that `services` in `BasicBot`
can be one of two objects, one with `{base_url, token, interface}`,
another with `{service_name, config_data}`. Therefore, I create
two corresponding schema and infer their types.
Fix two test cases `bot_data.test.js` and `settings_bots.test.js`
whose synthetic objects should have had followed the schema.
2023-07-21 10:41:35 +02:00
|
|
|
extra: "This field should be ignored",
|
2020-07-15 00:34:28 +02:00
|
|
|
},
|
2018-06-01 11:27:02 +02:00
|
|
|
],
|
2020-02-25 12:16:26 +01:00
|
|
|
};
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2021-02-24 22:31:50 +01:00
|
|
|
const bot_data = zrequire("bot_data");
|
|
|
|
const settings_bots = zrequire("settings_bots");
|
2018-06-27 09:35:30 +02:00
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
bot_data.initialize(bot_data_params);
|
2018-06-01 11:27:02 +02:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_url", "https://chat.example.com");
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_embedded_bots = [
|
2021-04-03 19:07:13 +02:00
|
|
|
{name: "converter", config: {}},
|
|
|
|
{name: "giphy", config: {key: "12345678"}},
|
|
|
|
{name: "foobot", config: {bar: "baz", qux: "quux"}},
|
|
|
|
];
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-04-03 19:07:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-09 05:22:23 +02:00
|
|
|
test("generate_zuliprc_url", () => {
|
|
|
|
const url = settings_bots.generate_zuliprc_url(1);
|
2020-07-15 00:34:28 +02:00
|
|
|
const expected =
|
|
|
|
"data:application/octet-stream;charset=utf-8," +
|
|
|
|
encodeURIComponent(
|
|
|
|
"[api]\nemail=error-bot@zulip.org\n" +
|
|
|
|
"key=QadL788EkiottHmukyhHgePUFHREiu8b\n" +
|
|
|
|
"site=https://chat.example.com\n",
|
|
|
|
);
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2023-04-09 05:22:23 +02:00
|
|
|
assert.equal(url, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2016-12-07 18:38:59 +01:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("generate_zuliprc_content", () => {
|
2020-02-01 00:04:23 +01:00
|
|
|
const bot_user = bot_data.get(1);
|
|
|
|
const content = settings_bots.generate_zuliprc_content(bot_user);
|
2020-07-15 00:34:28 +02:00
|
|
|
const expected =
|
|
|
|
"[api]\nemail=error-bot@zulip.org\n" +
|
|
|
|
"key=QadL788EkiottHmukyhHgePUFHREiu8b\n" +
|
|
|
|
"site=https://chat.example.com\n";
|
2016-12-07 18:38:59 +01:00
|
|
|
|
|
|
|
assert.equal(content, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-30 16:12:02 +02:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
test("generate_botserverrc_content", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user = {
|
2017-05-30 16:12:02 +02:00
|
|
|
email: "vabstest-bot@zulip.com",
|
|
|
|
api_key: "nSlA0mUm7G42LP85lMv7syqFTzDE2q34",
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const service = {
|
2018-05-30 11:09:35 +02:00
|
|
|
token: "abcd1234",
|
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
const content = settings_bots.generate_botserverrc_content(
|
|
|
|
user.email,
|
|
|
|
user.api_key,
|
|
|
|
service.token,
|
|
|
|
);
|
|
|
|
const expected =
|
|
|
|
"[]\nemail=vabstest-bot@zulip.com\n" +
|
|
|
|
"key=nSlA0mUm7G42LP85lMv7syqFTzDE2q34\n" +
|
|
|
|
"site=https://chat.example.com\n" +
|
|
|
|
"token=abcd1234\n";
|
2017-05-30 16:12:02 +02:00
|
|
|
|
|
|
|
assert.equal(content, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-20 00:32:20 +02:00
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
test("can_create_new_bots", ({override}) => {
|
|
|
|
override(current_user, "is_admin", true);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(settings_bots.can_create_new_bots());
|
2018-06-13 16:59:15 +02:00
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
override(current_user, "is_admin", false);
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_bot_creation_policy", 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(settings_bots.can_create_new_bots());
|
2018-06-13 16:59:15 +02:00
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_bot_creation_policy", 3);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!settings_bots.can_create_new_bots());
|
2018-06-13 16:59:15 +02:00
|
|
|
});
|