From 6f4af26585541939b1fe971f860af755e64bf966 Mon Sep 17 00:00:00 2001 From: sahil839 Date: Fri, 30 Apr 2021 12:54:29 +0530 Subject: [PATCH] node_tests: Extract main logic of testing policies in a common function. This commit extracts the logic for testing user_can_invite_others_to_realm user_can_create_streams, and user_can_subscribe_other_users to a single function test_policy and this function test_policy is called passing different policies and validation functions as arguments. This helps in removing a lot of duplicated code. --- frontend_tests/node_tests/settings_data.js | 148 ++++++--------------- 1 file changed, 42 insertions(+), 106 deletions(-) diff --git a/frontend_tests/node_tests/settings_data.js b/frontend_tests/node_tests/settings_data.js index a6d246b4b3..fbcef67bf6 100644 --- a/frontend_tests/node_tests/settings_data.js +++ b/frontend_tests/node_tests/settings_data.js @@ -107,117 +107,53 @@ run_test("user_can_change_logo", () => { assert.equal(can_change_logo(), false); }); -run_test("user_can_invite_others_to_realm", () => { - const can_invite_others_to_realm = settings_data.user_can_invite_others_to_realm; +function test_policy(label, policy, validation_func) { + run_test(label, () => { + page_params.is_admin = true; + page_params[policy] = settings_config.common_policy_values.by_admins_only.code; + assert.equal(validation_func(), true); - page_params.is_admin = true; - page_params.realm_invite_to_realm_policy = - settings_config.common_policy_values.by_admins_only.code; - assert.equal(can_invite_others_to_realm(), true); + page_params.is_admin = false; + assert.equal(validation_func(), false); - page_params.is_admin = false; - assert.equal(can_invite_others_to_realm(), false); + page_params.is_moderator = true; + page_params[policy] = settings_config.common_policy_values.by_moderators_only.code; + assert.equal(validation_func(), true); - page_params.is_moderator = true; - page_params.realm_invite_to_realm_policy = - settings_config.common_policy_values.by_moderators_only.code; - assert.equal(can_invite_others_to_realm(), true); + page_params.is_moderator = false; + assert.equal(validation_func(), false); - page_params.is_moderator = false; - assert.equal(can_invite_others_to_realm(), false); + page_params.is_guest = true; + page_params[policy] = settings_config.common_policy_values.by_members.code; + assert.equal(validation_func(), false); - page_params.is_guest = true; - page_params.realm_invite_to_realm_policy = settings_config.common_policy_values.by_members.code; - assert.equal(can_invite_others_to_realm(), false); + page_params.is_guest = false; + assert.equal(validation_func(), true); - page_params.is_guest = false; - assert.equal(can_invite_others_to_realm(), true); + page_params[policy] = settings_config.common_policy_values.by_full_members.code; + page_params.user_id = 30; + people.add_active_user(isaac); + isaac.date_joined = new Date(Date.now()); + page_params.realm_waiting_period_threshold = 10; + assert.equal(validation_func(), false); - page_params.realm_invite_to_realm_policy = - settings_config.common_policy_values.by_full_members.code; - page_params.user_id = 30; - people.add_active_user(isaac); - isaac.date_joined = new Date(Date.now()); - page_params.realm_waiting_period_threshold = 10; - assert.equal(can_invite_others_to_realm(), false); + isaac.date_joined = new Date(Date.now() - 20 * 86400000); + assert.equal(validation_func(), true); + }); +} - isaac.date_joined = new Date(Date.now() - 20 * 86400000); - assert.equal(can_invite_others_to_realm(), true); -}); - -run_test("user_can_subscribe_other_users", () => { - const can_subscribe_other_users = settings_data.user_can_subscribe_other_users; - - page_params.is_admin = true; - page_params.realm_invite_to_stream_policy = - settings_config.common_policy_values.by_admins_only.code; - assert.equal(can_subscribe_other_users(), true); - - page_params.is_admin = false; - assert.equal(can_subscribe_other_users(), false); - - page_params.is_moderator = true; - page_params.realm_invite_to_stream_policy = - settings_config.common_policy_values.by_moderators_only.code; - assert.equal(can_subscribe_other_users(), true); - - page_params.is_moderator = false; - assert.equal(can_subscribe_other_users(), false); - - page_params.is_guest = true; - page_params.realm_invite_to_stream_policy = - settings_config.common_policy_values.by_members.code; - assert.equal(can_subscribe_other_users(), false); - - page_params.is_guest = false; - assert.equal(can_subscribe_other_users(), true); - - page_params.realm_invite_to_stream_policy = - settings_config.common_policy_values.by_full_members.code; - page_params.user_id = 30; - people.add_active_user(isaac); - isaac.date_joined = new Date(Date.now()); - page_params.realm_waiting_period_threshold = 10; - assert.equal(can_subscribe_other_users(), false); - - isaac.date_joined = new Date(Date.now() - 20 * 86400000); - assert.equal(can_subscribe_other_users(), true); -}); - -run_test("user_can_create_streams", () => { - const can_create_streams = settings_data.user_can_create_streams; - - page_params.is_admin = true; - page_params.realm_create_stream_policy = - settings_config.common_policy_values.by_admins_only.code; - assert.equal(can_create_streams(), true); - - page_params.is_admin = false; - assert.equal(can_create_streams(), false); - - page_params.is_moderator = true; - page_params.realm_create_stream_policy = - settings_config.common_policy_values.by_moderators_only.code; - assert.equal(can_create_streams(), true); - - page_params.is_moderator = false; - assert.equal(can_create_streams(), false); - - page_params.is_guest = true; - page_params.realm_create_stream_policy = settings_config.common_policy_values.by_members.code; - assert.equal(can_create_streams(), false); - - page_params.is_guest = false; - assert.equal(can_create_streams(), true); - - page_params.realm_create_stream_policy = - settings_config.common_policy_values.by_full_members.code; - page_params.user_id = 30; - people.add_active_user(isaac); - isaac.date_joined = new Date(Date.now()); - page_params.realm_waiting_period_threshold = 10; - assert.equal(can_create_streams(), false); - - isaac.date_joined = new Date(Date.now() - 20 * 86400000); - assert.equal(can_create_streams(), true); -}); +test_policy( + "user_can_create_streams", + "realm_create_stream_policy", + settings_data.user_can_create_streams, +); +test_policy( + "user_can_subscribe_other_users", + "realm_invite_to_stream_policy", + settings_data.user_can_subscribe_other_users, +); +test_policy( + "user_can_invite_others_to_realm", + "realm_invite_to_realm_policy", + settings_data.user_can_invite_others_to_realm, +);