<commit_before>var common = require('../casper_lib/common.js').common;
var test_credentials = require('../../var/casper/test_credentials.js').test_credentials;
var REALMS_HAVE_SUBDOMAINS = casper.cli.get('subdomains');

common.start_and_log_in();

var form_sel = 'form[action^="/json/settings/change"]';
var regex_zuliprc = /^data:application\/octet-stream;charset=utf-8,\[api\]\nemail=.+\nkey=.+\nsite=.+\n$/;

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

casper.then(function () {
    casper.waitUntilVisible('a[href^="#settings"]', function () {
        casper.test.info('Settings page');
        casper.click('a[href^="#settings"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible("#settings_content .account-settings-form", function () {
        casper.test.assertUrlMatch(/^http:\/\/[^/]+\/#settings/, 'URL suggests we are on settings page');
        casper.test.assertVisible('.account-settings-form', 'Settings page is active');

        casper.test.assertNotVisible("#pw_change_controls");

        casper.click(".change_password_button");
    });
});

casper.then(function () {
    casper.waitUntilVisible("#pw_change_controls", function () {
        casper.waitForResource("zxcvbn.js", function () {
            casper.test.assertVisible("#old_password");
            casper.test.assertVisible("#new_password");
            casper.test.assertVisible("#confirm_password");

            casper.test.assertEqual(casper.getFormValues(form_sel).full_name, "Iago");

            casper.fill(form_sel, {
                full_name: "IagoNew",
                old_password: test_credentials.default_user.password,
                new_password: "<PASSWORD>",
                confirm_password: "<PASSWORD>",
            });
            casper.click('button[name="change_settings"]');
        });
    });
});

casper.then(function () {
    casper.waitUntilVisible('#account-settings-status', function () {
        casper.test.assertSelectorHasText('#account-settings-status', 'Updated settings!');

        casper.click('[data-section="your-bots"]');
        casper.click('#api_key_button');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#get_api_key_password', function () {
        casper.fill('form[action^="/json/fetch_api_key"]', {password:'<PASSWORD>'});
        casper.click('input[name="view_api_key"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#show_api_key_box', function () {
        casper.test.assertMatch(casper.fetchText('#api_key_value'), /[a-zA-Z0-9]{32}/, "Looks like an API key");

        // Change it all back so the next test can still log in
        casper.fill(form_sel, {
            full_name: "Iago",
            old_password: "<PASSWORD>",
            new_password: <PASSWORD>,
            confirm_password: <PASSWORD>,
        });
        casper.click('button[name="change_settings"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#show_api_key_box', function () {
        casper.test.assertExists('#download_zuliprc', '~/.zuliprc button exists');
        casper.click('#download_zuliprc');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#download_zuliprc[href^="data:application"]', function () {
        casper.test.assertMatch(
            decodeURIComponent(casper.getElementsAttribute('#download_zuliprc', 'href')),
            regex_zuliprc,
            'Looks like a zuliprc file');
    });
});

casper.then(function create_bot() {
    casper.test.info('Filling out the create bot form');

    casper.fill('#create_bot_form',{
        bot_name: 'Bot 1',
        bot_short_name: '1',
    });

    casper.test.info('Submitting the create bot form');
    casper.click('#create_bot_button');
});

var bot_email;
if (REALMS_HAVE_SUBDOMAINS) {
    bot_email = '<EMAIL>';
} else {
    bot_email = '<EMAIL>';
}

casper.then(function () {
    var button_sel = '.download_bot_zuliprc[data-email="' + bot_email + '"]';

    casper.waitUntilVisible(button_sel, function () {
        casper.click(button_sel);

        casper.waitUntilVisible(button_sel + '[href^="data:application"]', function () {
            casper.test.assertMatch(
                decodeURIComponent(casper.getElementsAttribute(button_sel, 'href')),
                regex_zuliprc,
                'Looks like a bot ~/.zuliprc file');
        });
    });
});

casper.then(function () {
    casper.waitUntilVisible('.open_edit_bot_form[data-email="' + bot_email + '"]', function open_edit_bot_form() {
        casper.test.info('Opening edit bot form');
        casper.click('.open_edit_bot_form[data-email="' + bot_email + '"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible('.edit_bot_form[data-email="' + bot_email + '"]', function test_edit_bot_form_values() {
        var form_sel = '.edit_bot_form[data-email="' + bot_email + '"]';
        casper.test.info('Testing edit bot form values');

    //     casper.test.assertEqual(
    //         common.get_form_field_value(form_sel + ' [name=bot_name]'),
    //         'Bot 1');
    //     casper.test.assertEqual(
    //         common.get_form_field_value(form_sel + ' [name=bot_default_sending_stream]'),
    //         'Denmark');
    //     casper.test.assertEqual(
    //         common.get_form_field_value(form_sel + ' [name=bot_default_events_register_stream]'),
    //         'Rome');
        casper.test.assertEqual(
            common.get_form_field_value(form_sel + ' [name=bot_name]'),
            'Bot 1');
    });
});

/*
   This test needs a modification. As it stands now, it will cause a race
   condition with all subsequent tests which access the UserProfile object
   this test modifies. Currently, if we modify alert words, we don't get
   any notification from the server, issue reported at
   https://github.com/zulip/zulip/issues/1269. Consequently, we can't wait
   on any condition to avoid the race condition.

casper.waitUntilVisible('#create_alert_word_form', function () {
    casper.test.info('Attempting to submit an empty alert word');
    casper.click('#create_alert_word_button');
    casper.test.info('Checking that an error is displayed');
    casper.test.assertVisible('#empty_alert_word_error');

    casper.test.info('Closing the error message');
    casper.click('.close-empty-alert-word-error');
    casper.test.info('Checking the error is hidden');
    casper.test.assertNotVisible('#empty_alert_word_error');

    casper.test.info('Filling out the alert word input');
    casper.sendKeys('#create_alert_word_name', 'some phrase');
    casper.click('#create_alert_word_button');

    casper.test.info('Checking that an element was created');
    casper.test.assertExists('div.alert-word-information-box');
    casper.test.assertSelectorHasText('span.value', 'some phrase');

    casper.test.info('Deleting element');
    casper.click('button.remove-alert-word');
    casper.test.info('Checking that the element was deleted');
    casper.test.assertDoesntExist('div.alert-word-information-box');
});
*/

casper.then(function change_default_language() {
    casper.test.info('Changing the default language');
    casper.click('[data-section="display-settings"]');
    casper.waitUntilVisible('#default_language');
});

casper.thenClick('#default_language');

casper.waitUntilVisible('#default_language_modal');

casper.thenClick('a[data-code="zh_Hans"]');

casper.waitUntilVisible('#display-settings-status', function () {
    casper.test.assertSelectorHasText('#display-settings-status', 'Chinese Simplified is now the default language');
    casper.test.info("Reloading the page.");
    casper.reload();
});

casper.then(function () {<commit_msg>casper_tests: Fix the broken alert words casper tests.<commit_after>var common = require('../casper_lib/common.js').common;
var test_credentials = require('../../var/casper/test_credentials.js').test_credentials;
var REALMS_HAVE_SUBDOMAINS = casper.cli.get('subdomains');

common.start_and_log_in();

var form_sel = 'form[action^="/json/settings/change"]';
var regex_zuliprc = /^data:application\/octet-stream;charset=utf-8,\[api\]\nemail=.+\nkey=.+\nsite=.+\n$/;

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

casper.then(function () {
    casper.waitUntilVisible('a[href^="#settings"]', function () {
        casper.test.info('Settings page');
        casper.click('a[href^="#settings"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible("#settings_content .account-settings-form", function () {
        casper.test.assertUrlMatch(/^http:\/\/[^/]+\/#settings/, 'URL suggests we are on settings page');
        casper.test.assertVisible('.account-settings-form', 'Settings page is active');

        casper.test.assertNotVisible("#pw_change_controls");

        casper.click(".change_password_button");
    });
});

casper.then(function () {
    casper.waitUntilVisible("#pw_change_controls", function () {
        casper.waitForResource("zxcvbn.js", function () {
            casper.test.assertVisible("#old_password");
            casper.test.assertVisible("#new_password");
            casper.test.assertVisible("#confirm_password");

            casper.test.assertEqual(casper.getFormValues(form_sel).full_name, "Iago");

            casper.fill(form_sel, {
                full_name: "IagoNew",
                old_password: <PASSWORD>,
                new_password: "<PASSWORD>",
                confirm_password: "<PASSWORD>",
            });
            casper.click('button[name="change_settings"]');
        });
    });
});

casper.then(function () {
    casper.waitUntilVisible('#account-settings-status', function () {
        casper.test.assertSelectorHasText('#account-settings-status', 'Updated settings!');

        casper.click('[data-section="your-bots"]');
        casper.click('#api_key_button');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#get_api_key_password', function () {
        casper.fill('form[action^="/json/fetch_api_key"]', {password:'<PASSWORD>'});
        casper.click('input[name="view_api_key"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#show_api_key_box', function () {
        casper.test.assertMatch(casper.fetchText('#api_key_value'), /[a-zA-Z0-9]{32}/, "Looks like an API key");

        // Change it all back so the next test can still log in
        casper.fill(form_sel, {
            full_name: "Iago",
            old_password: "<PASSWORD>",
            new_password: <PASSWORD>,
            confirm_password: <PASSWORD>,
        });
        casper.click('button[name="change_settings"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#show_api_key_box', function () {
        casper.test.assertExists('#download_zuliprc', '~/.zuliprc button exists');
        casper.click('#download_zuliprc');
    });
});

casper.then(function () {
    casper.waitUntilVisible('#download_zuliprc[href^="data:application"]', function () {
        casper.test.assertMatch(
            decodeURIComponent(casper.getElementsAttribute('#download_zuliprc', 'href')),
            regex_zuliprc,
            'Looks like a zuliprc file');
    });
});

casper.then(function create_bot() {
    casper.test.info('Filling out the create bot form');

    casper.fill('#create_bot_form',{
        bot_name: 'Bot 1',
        bot_short_name: '1',
    });

    casper.test.info('Submitting the create bot form');
    casper.click('#create_bot_button');
});

var bot_email;
if (REALMS_HAVE_SUBDOMAINS) {
    bot_email = '<EMAIL>';
} else {
    bot_email = '<EMAIL>';
}

casper.then(function () {
    var button_sel = '.download_bot_zuliprc[data-email="' + bot_email + '"]';

    casper.waitUntilVisible(button_sel, function () {
        casper.click(button_sel);

        casper.waitUntilVisible(button_sel + '[href^="data:application"]', function () {
            casper.test.assertMatch(
                decodeURIComponent(casper.getElementsAttribute(button_sel, 'href')),
                regex_zuliprc,
                'Looks like a bot ~/.zuliprc file');
        });
    });
});

casper.then(function () {
    casper.waitUntilVisible('.open_edit_bot_form[data-email="' + bot_email + '"]', function open_edit_bot_form() {
        casper.test.info('Opening edit bot form');
        casper.click('.open_edit_bot_form[data-email="' + bot_email + '"]');
    });
});

casper.then(function () {
    casper.waitUntilVisible('.edit_bot_form[data-email="' + bot_email + '"]', function test_edit_bot_form_values() {
        var form_sel = '.edit_bot_form[data-email="' + bot_email + '"]';
        casper.test.info('Testing edit bot form values');

    //     casper.test.assertEqual(
    //         common.get_form_field_value(form_sel + ' [name=bot_name]'),
    //         'Bot 1');
    //     casper.test.assertEqual(
    //         common.get_form_field_value(form_sel + ' [name=bot_default_sending_stream]'),
    //         'Denmark');
    //     casper.test.assertEqual(
    //         common.get_form_field_value(form_sel + ' [name=bot_default_events_register_stream]'),
    //         'Rome');
        casper.test.assertEqual(
            common.get_form_field_value(form_sel + ' [name=bot_name]'),
            'Bot 1');
    });
});

casper.then(function () {
    casper.click('[data-section="alert-words"]');
    casper.waitUntilVisible('#create_alert_word_form', function () {
        casper.test.info('Attempting to submit an empty alert word');
        casper.click('#create_alert_word_button');
        casper.waitUntilVisible('#alert_word_status', function () {
            casper.test.info('Checking that an error is displayed');
            casper.test.assertSelectorHasText('.alert_word_status_text', 'Alert word can\'t be empty!');
            casper.test.info('Closing the error message');
            casper.click('.close-alert-word-status');
            casper.test.info('Checking the error is hidden');
            casper.test.assertNotVisible('#alert_word_status');
        });
    });
});

casper.then(function () {
    casper.test.info('Filling out the alert word input');
    casper.sendKeys('#create_alert_word_name', 'some phrase');
    casper.click('#create_alert_word_button');
    casper.test.info('Checking that a success message is displayed');
    casper.waitUntilVisible('#alert_word_status', function () {
        casper.test.assertSelectorHasText('.alert_word_status_text', 'Alert word added successfully!');
        casper.test.info('Closing the status message');
        casper.click('.close-alert-word-status');
        casper.test.info('Checking the status message is hidden');
        casper.test.assertNotVisible('#alert_word_status');
    });
});

casper.then(function () {
    casper.test.info('Checking that an element was created');
    casper.waitUntilVisible(".alert-word-item[data-word='some phrase']", function () {
        casper.test.assertExists('div.alert-word-information-box');
        casper.test.assertSelectorHasText('span.value', 'some phrase');
    });
});

casper.then(function () {
    casper.test.info('Deleting alert word');
    casper.click('button.remove-alert-word');
    casper.test.info('Checking that a success message is displayed');
    casper.waitUntilVisible('#alert_word_status', function () {
        casper.test.assertSelectorHasText('.alert_word_status_text', 'Alert word removed successfully!');
        casper.test.info('Closing the status message');
        casper.click('.close-alert-word-status');
        casper.test.info('Checking the status message is hidden');
        casper.test.assertNotVisible('#alert_word_status');
    });
    casper.test.info('Checking that the element was deleted');
    casper.waitWhileVisible(".alert-word-item[data-word='some phrase']", function () {
        casper.test.assertDoesntExist('div.alert-word-information-box');
        casper.test.info('Element deleted successfully');
    });
});

casper.then(function change_default_language() {
    casper.test.info('Changing the default language');
    casper.click('[data-section="display-settings"]');
    casper.waitUntilVisible('#default_language');
});

casper.thenClick('#default_language');

casper.waitUntilVisible('#default_language_modal');

casper.thenClick('a[data-code="zh_Hans"]');

casper.waitUntilVisible('#display-settings-status', function () {
    casper.test.assertSelectorHasText('#display-settings-status', 'Chinese Simplified is now the default language');
    casper.test.info("Reloading the page.");
    casper.reload();
});

casper.then(function () {
{'fraction_non_alphanumeric': 0.16072917907780293, 'fraction_numerical': 0.001310615989515072, 'mean_word_length': 3.432796408766834, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2223181', 'n_tokens_mistral': 5242, 'n_tokens_neox': 5150, 'n_words': 760}