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-10-09 21:21:41 +02:00
|
|
|
const {mock_esm, with_overrides, zrequire} = require("./lib/namespace");
|
2023-12-14 23:51:33 +01:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2023-02-22 23:04:10 +01:00
|
|
|
const $ = require("./lib/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const loading = mock_esm("../src/loading");
|
2021-02-28 00:36:14 +01:00
|
|
|
|
2018-08-15 11:35:18 +02:00
|
|
|
const SHORT_TEXT_ID = 1;
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2021-03-24 11:41:29 +01:00
|
|
|
const SELECT_ID = 3;
|
2019-05-27 10:59:55 +02:00
|
|
|
const EXTERNAL_ACCOUNT_ID = 7;
|
2022-08-17 14:16:15 +02:00
|
|
|
const LONG_TEXT_ID = 2;
|
|
|
|
const USER_FIELD_ID = 6;
|
2019-05-27 10:59:55 +02:00
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
const SHORT_TEXT_NAME = "Short text";
|
2021-03-24 11:41:29 +01:00
|
|
|
const SELECT_NAME = "Select";
|
2019-05-27 10:59:55 +02:00
|
|
|
const EXTERNAL_ACCOUNT_NAME = "External account";
|
2022-08-17 14:16:15 +02:00
|
|
|
const LONG_TEXT_NAME = "Long text";
|
|
|
|
const USER_FIELD_NAME = "Person";
|
2018-08-15 11:35:18 +02:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
const custom_profile_field_types = {
|
2018-08-15 11:35:18 +02:00
|
|
|
SHORT_TEXT: {
|
|
|
|
id: SHORT_TEXT_ID,
|
2019-05-27 10:59:55 +02:00
|
|
|
name: SHORT_TEXT_NAME,
|
2018-08-15 11:35:18 +02:00
|
|
|
},
|
2021-03-24 11:41:29 +01:00
|
|
|
SELECT: {
|
|
|
|
id: SELECT_ID,
|
|
|
|
name: SELECT_NAME,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
EXTERNAL_ACCOUNT: {
|
|
|
|
id: EXTERNAL_ACCOUNT_ID,
|
|
|
|
name: EXTERNAL_ACCOUNT_NAME,
|
2018-08-15 11:35:18 +02:00
|
|
|
},
|
2022-08-17 14:16:15 +02:00
|
|
|
LONG_TEXT: {
|
|
|
|
id: LONG_TEXT_ID,
|
|
|
|
name: LONG_TEXT_NAME,
|
|
|
|
},
|
|
|
|
USER: {
|
|
|
|
id: USER_FIELD_ID,
|
|
|
|
name: USER_FIELD_NAME,
|
|
|
|
},
|
2018-08-15 11:35:18 +02:00
|
|
|
};
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2024-05-08 20:48:47 +02:00
|
|
|
const Sortable = {create: noop};
|
|
|
|
|
|
|
|
mock_esm("sortablejs", {default: Sortable});
|
2021-03-10 06:36:44 +01:00
|
|
|
|
2024-06-23 05:40:39 +02:00
|
|
|
mock_esm("../src/list_widget", {
|
|
|
|
generic_sort_functions: noop,
|
|
|
|
create(_container, custom_profile_data, opts) {
|
|
|
|
for (const item of custom_profile_data) {
|
|
|
|
opts.modifier_html(item);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-02-24 22:31:50 +01:00
|
|
|
const settings_profile_fields = zrequire("settings_profile_fields");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_current_user, set_realm} = zrequire("state_data");
|
|
|
|
|
|
|
|
const current_user = {};
|
|
|
|
set_current_user(current_user);
|
|
|
|
const realm = {};
|
|
|
|
set_realm(realm);
|
2018-08-15 13:17:15 +02:00
|
|
|
|
2021-06-14 11:20:47 +02:00
|
|
|
function test_populate(opts, template_data) {
|
2024-10-09 21:21:41 +02:00
|
|
|
with_overrides(({override}) => {
|
|
|
|
const fields_data = opts.fields_data;
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "custom_profile_field_types", custom_profile_field_types);
|
2024-10-09 21:21:41 +02:00
|
|
|
override(current_user, "is_admin", opts.is_admin);
|
|
|
|
const $table = $("#admin_profile_fields_table");
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
$table[0] = "stub";
|
2021-02-22 17:01:29 +01:00
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
loading.destroy_indicator = noop;
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
settings_profile_fields.do_populate_profile_fields(fields_data);
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
assert.deepEqual(template_data, opts.expected_template_data);
|
|
|
|
});
|
2018-06-11 22:32:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
run_test("populate_profile_fields", ({mock_template, override}) => {
|
|
|
|
override(realm, "custom_profile_fields", {});
|
|
|
|
override(realm, "realm_default_external_accounts", JSON.stringify({}));
|
2021-04-03 19:07:13 +02:00
|
|
|
|
2023-12-14 23:51:33 +01:00
|
|
|
$("#admin_profile_fields_table .display_in_profile_summary_false").toggleClass = noop;
|
2022-08-17 14:16:15 +02:00
|
|
|
|
2021-06-14 11:20:47 +02:00
|
|
|
const template_data = [];
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("settings/admin_profile_field_list.hbs", false, (data) => {
|
2021-06-14 11:20:47 +02:00
|
|
|
template_data.push(data);
|
2024-04-04 00:53:08 +02:00
|
|
|
return "<admin-profile-field-list-stub>";
|
2021-06-14 11:20:47 +02:00
|
|
|
});
|
|
|
|
|
2018-06-11 22:32:11 +02:00
|
|
|
const fields_data = [
|
|
|
|
{
|
2018-08-15 11:35:18 +02:00
|
|
|
type: SHORT_TEXT_ID,
|
2018-06-11 22:32:11 +02:00
|
|
|
id: 10,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "favorite color",
|
|
|
|
hint: "blue?",
|
|
|
|
field_data: "",
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: false,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
{
|
2021-03-24 11:41:29 +01:00
|
|
|
type: SELECT_ID,
|
2018-06-11 22:32:11 +02:00
|
|
|
id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "meal",
|
|
|
|
hint: "lunch",
|
2024-05-08 20:48:47 +02:00
|
|
|
field_data: JSON.stringify({
|
|
|
|
0: {
|
2020-07-15 01:29:15 +02:00
|
|
|
text: "lunch",
|
2024-05-08 20:48:47 +02:00
|
|
|
order: "0",
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
2024-05-08 20:48:47 +02:00
|
|
|
1: {
|
2020-07-15 01:29:15 +02:00
|
|
|
text: "dinner",
|
2024-05-08 20:48:47 +02:00
|
|
|
order: "1",
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
2024-05-08 20:48:47 +02:00
|
|
|
}),
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: false,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
2019-05-27 10:59:55 +02:00
|
|
|
{
|
|
|
|
type: EXTERNAL_ACCOUNT_ID,
|
|
|
|
id: 20,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "github profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
field_data: JSON.stringify({
|
2020-07-15 01:29:15 +02:00
|
|
|
subtype: "github",
|
2019-05-27 10:59:55 +02:00
|
|
|
}),
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: true,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: EXTERNAL_ACCOUNT_ID,
|
|
|
|
id: 21,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "zulip profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
field_data: JSON.stringify({
|
2020-07-15 01:29:15 +02:00
|
|
|
subtype: "custom",
|
|
|
|
url_pattern: "https://chat.zulip.com/%(username)s",
|
2019-05-27 10:59:55 +02:00
|
|
|
}),
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: true,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
2018-06-11 22:32:11 +02:00
|
|
|
];
|
|
|
|
const expected_template_data = [
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 10,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "favorite color",
|
|
|
|
hint: "blue?",
|
2019-05-27 10:59:55 +02:00
|
|
|
type: SHORT_TEXT_NAME,
|
2018-06-11 22:32:11 +02:00
|
|
|
choices: [],
|
2021-03-24 11:41:29 +01:00
|
|
|
is_select_field: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
is_external_account_field: false,
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: false,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
can_modify: true,
|
2024-02-13 02:08:24 +01:00
|
|
|
realm_default_external_accounts: realm.realm_default_external_accounts,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "meal",
|
|
|
|
hint: "lunch",
|
2021-03-24 11:41:29 +01:00
|
|
|
type: SELECT_NAME,
|
2018-06-11 22:32:11 +02:00
|
|
|
choices: [
|
2024-05-08 20:48:47 +02:00
|
|
|
{order: "0", value: "0", text: "lunch"},
|
|
|
|
{order: "1", value: "1", text: "dinner"},
|
2018-06-11 22:32:11 +02:00
|
|
|
],
|
2021-03-24 11:41:29 +01:00
|
|
|
is_select_field: true,
|
2019-05-27 10:59:55 +02:00
|
|
|
is_external_account_field: false,
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: false,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
can_modify: true,
|
2024-02-13 02:08:24 +01:00
|
|
|
realm_default_external_accounts: realm.realm_default_external_accounts,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 20,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "github profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
type: EXTERNAL_ACCOUNT_NAME,
|
|
|
|
choices: [],
|
2021-03-24 11:41:29 +01:00
|
|
|
is_select_field: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
is_external_account_field: true,
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: true,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
can_modify: true,
|
2024-02-13 02:08:24 +01:00
|
|
|
realm_default_external_accounts: realm.realm_default_external_accounts,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 21,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "zulip profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
type: EXTERNAL_ACCOUNT_NAME,
|
|
|
|
choices: [],
|
2021-03-24 11:41:29 +01:00
|
|
|
is_select_field: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
is_external_account_field: true,
|
2022-08-17 14:16:15 +02:00
|
|
|
display_in_profile_summary: true,
|
|
|
|
valid_to_display_in_summary: true,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: false,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
can_modify: true,
|
2024-02-13 02:08:24 +01:00
|
|
|
realm_default_external_accounts: realm.realm_default_external_accounts,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-06-14 11:20:47 +02:00
|
|
|
test_populate(
|
|
|
|
{
|
|
|
|
fields_data,
|
|
|
|
expected_template_data,
|
|
|
|
is_admin: true,
|
|
|
|
},
|
|
|
|
template_data,
|
|
|
|
);
|
2018-06-11 22:32:11 +02:00
|
|
|
});
|