<commit_before>var common = require('../casper_lib/common.js').common;

common.start_and_log_in();

casper.then(function () {
    var menu_selector = '#settings-dropdown';
    casper.waitUntilVisible(menu_selector, function () {
        casper.click(menu_selector);
    });
});

casper.then(function () {
    casper.test.info('Organization page');
    casper.click('a[href^="#organization"]');
});

casper.waitForSelector('#settings_overlay_container.show', function () {
    casper.test.info('Organization page is active');
    casper.test.assertUrlMatch(/^http:\/\/[^/]+\/#organization/, 'URL suggests we are on organization page');
});

casper.then(function () {
    casper.click("li[data-section='organization-settings']");
});

// Test changing notifications stream
casper.then(function () {
    casper.test.info('Changing notifications stream to Verona by filtering with "verona"');
    casper.click("#id_realm_notifications_stream > button.dropdown-toggle");

    casper.waitUntilVisible('#id_realm_notifications_stream ul.dropdown-menu', function () {
        casper.sendKeys('#id_realm_notifications_stream .dropdown-search > input[type=text]', 'verona');
        casper.click("#id_realm_notifications_stream .dropdown-list-body li.stream_name");
    });

    casper.waitUntilVisible('#admin-realm-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-notifications-stream-status',
                                          'Notifications stream changed!');
        casper.test.assertSelectorHasText('#realm_notifications_stream_name', '#Verona');
    });
});

casper.then(function () {
    casper.click(".notifications-stream-disable");
    casper.waitUntilVisible('#admin-realm-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-notifications-stream-status',
                                          'Notifications stream disabled!');
        casper.test.assertSelectorHasText('#realm_notifications_stream_name', 'Disabled');
    });
});

// Test changing signup notifications stream
casper.then(function () {
    casper.test.info('Changing signup notifications stream to Verona by filtering with "verona"');
    casper.click("#id_realm_signup_notifications_stream > button.dropdown-toggle");

    casper.waitUntilVisible('#id_realm_signup_notifications_stream ul.dropdown-menu', function () {
        casper.sendKeys('#id_realm_signup_notifications_stream .dropdown-search > input[type=text]', 'verona');
        casper.click("#id_realm_signup_notifications_stream .dropdown-list-body li.stream_name");
    });

    casper.waitUntilVisible('#admin-realm-signup-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-signup-notifications-stream-status',
                                          'Signup notifications stream changed!');
        casper.test.assertSelectorHasText('#realm_signup_notifications_stream_name', '#Verona');
    });
});

casper.then(function () {
    casper.click(".signup-notifications-stream-disable");
    casper.waitUntilVisible('#admin-realm-signup-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-signup-notifications-stream-status',
                                          'Signup notifications stream disabled!');
        casper.test.assertSelectorHasText('#realm_signup_notifications_stream_name', 'Disabled');
    });
});

// Test permissions setting
casper.then(function () {
    casper.click("li[data-section='organization-permissions']");
});

function submit_permissions_change() {
    casper.test.assertSelectorHasText('#org-submit-other-permissions', "Save");
    casper.click('#org-submit-other-permissions');
}

// Test setting limiting stream creation to administrators
casper.then(function () {
    casper.test.info("Test setting limiting stream creation to administrators");
    casper.waitUntilVisible("#id_realm_create_stream_permission", function () {
        casper.evaluate(function () {
            $("#id_realm_create_stream_permission").val("by_admins_only").change();
        });
        submit_permissions_change();
    });
});

casper.then(function () {
    // Test setting was activated
    casper.waitUntilVisible('#org-submit-other-permissions[data-status="saved"]', function () {
        casper.test.assertSelectorHasText('#org-submit-other-permissions',
                                          'Saved');
    });
});

casper.then(function () {
    casper.waitUntilVisible("#id_realm_create_stream_permission", function () {
        casper.evaluate(function () {
            $("#id_realm_create_stream_permission").val("by_admin_user_with_custom_time").change();
            $("#id_realm_waiting_period_threshold").val('6');
        });
        submit_permissions_change();
    });
});

casper.then(function () {
    // Test setting was activated
    casper.waitUntilVisible('#org-submit-other-permissions[data-status="saved"]', function () {
        casper.test.assertSelectorHasText('#org-submit-other-permissions',
                                          'Saved');
    });
});

casper.then(function () {
    // Leave the page and return
    casper.click('#settings-dropdown');
    casper.click('a[href^="#streams"]');
    casper.click('#settings-dropdown');
    casper.click('a[href^="#organization"]');
});

casper.waitUntilVisible('#id_realm_create_stream_permission', function () {
    // Test Setting was saved
    casper.test.assertEval(function () {
        return $('input[type="text"][id="id_realm_waiting_period_threshold"]').val() === '6';
    }, 'Waiting period threshold set to 6 days');


    // Deactivate setting

    casper.evaluate(function () {
        $("#id_realm_create_stream_permission").val("by_admins_only").change();
    });
    submit_permissions_change();
});

casper.then(function () {
    // Test setting was activated
    casper.waitUntilVisible('#org-submit-other-permissions[data-status="saved"]', function () {
        casper.test.assertSelectorHasText('#org-submit-other-permissions',
                                          'Saved');
    });
});

casper.then(function () {
    // Test custom realm emoji
    casper.click("li[data-section='emoji-settings']");
    casper.waitUntilVisible('.admin-emoji-form', function () {
        casper.fill('form.admin-emoji-form', {
            name: 'mouseface',
            emoji_file_input: 'static/images/logo/zulip-icon-128x128.png',
        }, true);
    });
});

casper.then(function () {
    casper.waitUntilVisible('div#admin-emoji-status', function () {
        casper.test.assertSelectorHasText('div#admin-emoji-status', 'Custom emoji added!');
    });
});

casper.then(function () {
    casper.waitUntilVisible('tr#emoji_mouseface', function () {
        casper.test.assertSelectorHasText('tr#emoji_mouseface .emoji_name', 'mouseface');
        casper.test.assertExists('tr#emoji_mouseface img');
        casper.click('tr#emoji_mouseface button.delete');
    });
});

casper.then(function () {
    casper.waitWhileVisible('tr#emoji_mouseface', function () {
        casper.test.assertDoesntExist('tr#emoji_mouseface');
    });
});

// Test custom profile fields
casper.test.info("Testing custom profile fields");
casper.thenClick("li[data-section='profile-field-settings']");
casper.then(function () {
    casper.waitUntilVisible('.admin-profile-field-form', function () {
        casper.fill('form.admin-profile-field-form', {
            name: 'Teams',
            field_type: '3',
        });
        casper.click('form.admin-profile-field-form button.button');
    });
});

casper.then(function () {
    casper.waitUntilVisible('div#admin-profile-field-status', function () {
        casper.test.assertSelectorHasText('div#admin-profile-field-status',<commit_msg>casper: Fix tests for custom profile fields.<commit_after>var common = require('../casper_lib/common.js').common;

common.start_and_log_in();

casper.then(function () {
    var menu_selector = '#settings-dropdown';
    casper.waitUntilVisible(menu_selector, function () {
        casper.click(menu_selector);
    });
});

casper.then(function () {
    casper.test.info('Organization page');
    casper.click('a[href^="#organization"]');
});

casper.waitForSelector('#settings_overlay_container.show', function () {
    casper.test.info('Organization page is active');
    casper.test.assertUrlMatch(/^http:\/\/[^/]+\/#organization/, 'URL suggests we are on organization page');
});

casper.then(function () {
    casper.click("li[data-section='organization-settings']");
});

// Test changing notifications stream
casper.then(function () {
    casper.test.info('Changing notifications stream to Verona by filtering with "verona"');
    casper.click("#id_realm_notifications_stream > button.dropdown-toggle");

    casper.waitUntilVisible('#id_realm_notifications_stream ul.dropdown-menu', function () {
        casper.sendKeys('#id_realm_notifications_stream .dropdown-search > input[type=text]', 'verona');
        casper.click("#id_realm_notifications_stream .dropdown-list-body li.stream_name");
    });

    casper.waitUntilVisible('#admin-realm-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-notifications-stream-status',
                                          'Notifications stream changed!');
        casper.test.assertSelectorHasText('#realm_notifications_stream_name', '#Verona');
    });
});

casper.then(function () {
    casper.click(".notifications-stream-disable");
    casper.waitUntilVisible('#admin-realm-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-notifications-stream-status',
                                          'Notifications stream disabled!');
        casper.test.assertSelectorHasText('#realm_notifications_stream_name', 'Disabled');
    });
});

// Test changing signup notifications stream
casper.then(function () {
    casper.test.info('Changing signup notifications stream to Verona by filtering with "verona"');
    casper.click("#id_realm_signup_notifications_stream > button.dropdown-toggle");

    casper.waitUntilVisible('#id_realm_signup_notifications_stream ul.dropdown-menu', function () {
        casper.sendKeys('#id_realm_signup_notifications_stream .dropdown-search > input[type=text]', 'verona');
        casper.click("#id_realm_signup_notifications_stream .dropdown-list-body li.stream_name");
    });

    casper.waitUntilVisible('#admin-realm-signup-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-signup-notifications-stream-status',
                                          'Signup notifications stream changed!');
        casper.test.assertSelectorHasText('#realm_signup_notifications_stream_name', '#Verona');
    });
});

casper.then(function () {
    casper.click(".signup-notifications-stream-disable");
    casper.waitUntilVisible('#admin-realm-signup-notifications-stream-status', function () {
        casper.test.assertSelectorHasText('#admin-realm-signup-notifications-stream-status',
                                          'Signup notifications stream disabled!');
        casper.test.assertSelectorHasText('#realm_signup_notifications_stream_name', 'Disabled');
    });
});

// Test permissions setting
casper.then(function () {
    casper.click("li[data-section='organization-permissions']");
});

function submit_permissions_change() {
    casper.test.assertSelectorHasText('#org-submit-other-permissions', "Save");
    casper.click('#org-submit-other-permissions');
}

// Test setting limiting stream creation to administrators
casper.then(function () {
    casper.test.info("Test setting limiting stream creation to administrators");
    casper.waitUntilVisible("#id_realm_create_stream_permission", function () {
        casper.evaluate(function () {
            $("#id_realm_create_stream_permission").val("by_admins_only").change();
        });
        submit_permissions_change();
    });
});

casper.then(function () {
    // Test setting was activated
    casper.waitUntilVisible('#org-submit-other-permissions[data-status="saved"]', function () {
        casper.test.assertSelectorHasText('#org-submit-other-permissions',
                                          'Saved');
    });
});

casper.then(function () {
    casper.waitUntilVisible("#id_realm_create_stream_permission", function () {
        casper.evaluate(function () {
            $("#id_realm_create_stream_permission").val("by_admin_user_with_custom_time").change();
            $("#id_realm_waiting_period_threshold").val('6');
        });
        submit_permissions_change();
    });
});

casper.then(function () {
    // Test setting was activated
    casper.waitUntilVisible('#org-submit-other-permissions[data-status="saved"]', function () {
        casper.test.assertSelectorHasText('#org-submit-other-permissions',
                                          'Saved');
    });
});

casper.then(function () {
    // Leave the page and return
    casper.click('#settings-dropdown');
    casper.click('a[href^="#streams"]');
    casper.click('#settings-dropdown');
    casper.click('a[href^="#organization"]');
});

casper.waitUntilVisible('#id_realm_create_stream_permission', function () {
    // Test Setting was saved
    casper.test.assertEval(function () {
        return $('input[type="text"][id="id_realm_waiting_period_threshold"]').val() === '6';
    }, 'Waiting period threshold set to 6 days');


    // Deactivate setting

    casper.evaluate(function () {
        $("#id_realm_create_stream_permission").val("by_admins_only").change();
    });
    submit_permissions_change();
});

casper.then(function () {
    // Test setting was activated
    casper.waitUntilVisible('#org-submit-other-permissions[data-status="saved"]', function () {
        casper.test.assertSelectorHasText('#org-submit-other-permissions',
                                          'Saved');
    });
});

casper.then(function () {
    // Test custom realm emoji
    casper.click("li[data-section='emoji-settings']");
    casper.waitUntilVisible('.admin-emoji-form', function () {
        casper.fill('form.admin-emoji-form', {
            name: 'mouseface',
            emoji_file_input: 'static/images/logo/zulip-icon-128x128.png',
        }, true);
    });
});

casper.then(function () {
    casper.waitUntilVisible('div#admin-emoji-status', function () {
        casper.test.assertSelectorHasText('div#admin-emoji-status', 'Custom emoji added!');
    });
});

casper.then(function () {
    casper.waitUntilVisible('tr#emoji_mouseface', function () {
        casper.test.assertSelectorHasText('tr#emoji_mouseface .emoji_name', 'mouseface');
        casper.test.assertExists('tr#emoji_mouseface img');
        casper.click('tr#emoji_mouseface button.delete');
    });
});

casper.then(function () {
    casper.waitWhileVisible('tr#emoji_mouseface', function () {
        casper.test.assertDoesntExist('tr#emoji_mouseface');
    });
});

// Test custom profile fields
casper.test.info("Testing custom profile fields");
casper.thenClick("li[data-section='profile-field-settings']");
casper.then(function () {
    casper.waitUntilVisible('.admin-profile-field-form', function () {
        casper.fill('form.admin-profile-field-form', {
            name: 'Teams',
            field_type: '1',
        });
        casper.click('form.admin-profile-field-form button.button');
    });
});

casper.then(function () {
    casper.waitUntilVisible('div#admin-profile-field-status', function () {
        casper.test.assertSelectorHasText('div#admin-profile-field-status',
{'fraction_non_alphanumeric': 0.15341452528215266, 'fraction_numerical': 0.0012752662118217179, 'mean_word_length': 3.612941176470588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3818031', 'n_tokens_mistral': 4577, 'n_tokens_neox': 4520, 'n_words': 559}