<commit_before>/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.watcher.history;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.MockSecureSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.xpack.core.watcher.execution.ExecutionState;
import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField;
import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchRequestBuilder;
import org.elasticsearch.xpack.watcher.condition.InternalAlwaysCondition;
import org.elasticsearch.xpack.watcher.notification.email.EmailTemplate;
import org.elasticsearch.xpack.watcher.notification.email.support.EmailServer;
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
import org.junit.After;

import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.emailAction;
import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput;
import static org.elasticsearch.xpack.watcher.trigger.TriggerBuilders.schedule;
import static org.elasticsearch.xpack.watcher.trigger.schedule.Schedules.interval;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

/**
 * This test makes sure that the email address fields in the watch_record action result are
 * not analyzed so they can be used in aggregations
 */
public class HistoryTemplateEmailMappingsTests extends AbstractWatcherIntegrationTestCase {

    private EmailServer server;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        server = EmailServer.localhost(logger);
    }

    @After
    public void cleanup() throws Exception {
        server.stop();
    }

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        final MockSecureSettings secureSettings = new MockSecureSettings();
        secureSettings.setString("xpack.notification.email.account.test.smtp.secure_password", EmailServer.PASSWORD);
        return Settings.builder()
                .put(super.nodeSettings(nodeOrdinal))

                // email
                .put("xpack.notification.email.account.test.smtp.auth", true)
                .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME)
                .put("xpack.notification.email.account.test.smtp.port", server.port())
                .put("xpack.notification.email.account.test.smtp.host", "localhost")
                .setSecureSettings(secureSettings)
                .build();
    }

    public void testEmailFields() throws Exception {
        PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder()
                .trigger(schedule(interval("5s")))
                .input(simpleInput())
                .condition(InternalAlwaysCondition.INSTANCE)
                .addAction("_email", emailAction(EmailTemplate.builder()
                        .from("<EMAIL>")
                        .to("<EMAIL>", "<EMAIL>")
                        .cc("<EMAIL>", "<EMAIL>")
                        .bcc("<EMAIL>", "<EMAIL>")
                        .replyTo("<EMAIL>", "<EMAIL>")
                        .subject("_subject")
                        .textBody("_body"))))
                .get();

        assertThat(putWatchResponse.isCreated(), is(true));
        timeWarp().trigger("_id");
        flush();
        refresh();

        // the action should fail as no email server is available
        assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1);

        SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*").setSource(searchSource()
                .aggregation(terms("from").field("result.actions.email.message.from"))
                .aggregation(terms("to").field("result.actions.email.message.to"))
                .aggregation(terms("cc").field("result.actions.email.message.cc"))
                .aggregation(terms("bcc").field("result.actions.email.message.bcc"))
                .aggregation(terms("reply_to").field("result.actions.email.message.reply_to")))
                .get();

        assertThat(response, notNullValue());
        assertThat(response.getHits().getTotalHits().value, is(1L));
        Aggregations aggs = response.getAggregations();
        assertThat(aggs, notNullValue());

        Terms terms = aggs.get("from");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(1));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("to");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("cc");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("bcc");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("reply_to");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
    }
}
<commit_msg>[TEST] Mute HistoryTemplateEmailMappingsTests.testEmailFields #65053 <commit_after>/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.watcher.history;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.MockSecureSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.xpack.core.watcher.execution.ExecutionState;
import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField;
import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchRequestBuilder;
import org.elasticsearch.xpack.watcher.condition.InternalAlwaysCondition;
import org.elasticsearch.xpack.watcher.notification.email.EmailTemplate;
import org.elasticsearch.xpack.watcher.notification.email.support.EmailServer;
import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase;
import org.junit.After;

import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.emailAction;
import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput;
import static org.elasticsearch.xpack.watcher.trigger.TriggerBuilders.schedule;
import static org.elasticsearch.xpack.watcher.trigger.schedule.Schedules.interval;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

/**
 * This test makes sure that the email address fields in the watch_record action result are
 * not analyzed so they can be used in aggregations
 */
public class HistoryTemplateEmailMappingsTests extends AbstractWatcherIntegrationTestCase {

    private EmailServer server;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        server = EmailServer.localhost(logger);
    }

    @After
    public void cleanup() throws Exception {
        server.stop();
    }

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        final MockSecureSettings secureSettings = new MockSecureSettings();
        secureSettings.setString("xpack.notification.email.account.test.smtp.secure_password", EmailServer.PASSWORD);
        return Settings.builder()
                .put(super.nodeSettings(nodeOrdinal))

                // email
                .put("xpack.notification.email.account.test.smtp.auth", true)
                .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME)
                .put("xpack.notification.email.account.test.smtp.port", server.port())
                .put("xpack.notification.email.account.test.smtp.host", "localhost")
                .setSecureSettings(secureSettings)
                .build();
    }

    @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/65052")
    public void testEmailFields() throws Exception {
        PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder()
                .trigger(schedule(interval("5s")))
                .input(simpleInput())
                .condition(InternalAlwaysCondition.INSTANCE)
                .addAction("_email", emailAction(EmailTemplate.builder()
                        .from("<EMAIL>")
                        .to("<EMAIL>", "<EMAIL>")
                        .cc("<EMAIL>", "<EMAIL>")
                        .bcc("<EMAIL>", "<EMAIL>")
                        .replyTo("<EMAIL>", "<EMAIL>")
                        .subject("_subject")
                        .textBody("_body"))))
                .get();

        assertThat(putWatchResponse.isCreated(), is(true));
        timeWarp().trigger("_id");
        flush();
        refresh();

        // the action should fail as no email server is available
        assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1);

        SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*").setSource(searchSource()
                .aggregation(terms("from").field("result.actions.email.message.from"))
                .aggregation(terms("to").field("result.actions.email.message.to"))
                .aggregation(terms("cc").field("result.actions.email.message.cc"))
                .aggregation(terms("bcc").field("result.actions.email.message.bcc"))
                .aggregation(terms("reply_to").field("result.actions.email.message.reply_to")))
                .get();

        assertThat(response, notNullValue());
        assertThat(response.getHits().getTotalHits().value, is(1L));
        Aggregations aggs = response.getAggregations();
        assertThat(aggs, notNullValue());

        Terms terms = aggs.get("from");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(1));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("to");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("to<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("cc");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("bcc");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));

        terms = aggs.get("reply_to");
        assertThat(terms, notNullValue());
        assertThat(terms.getBuckets().size(), is(2));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
        assertThat(terms.getBucketByKey("<EMAIL>"), notNullValue());
        assertThat(terms.getBucketByKey("<EMAIL>").getDocCount(), is(1L));
    }
}

{'fraction_non_alphanumeric': 0.1367787610619469, 'fraction_numerical': 0.0031150442477876107, 'mean_word_length': 3.4958625079567156, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3529864', 'n_tokens_mistral': 3939, 'n_tokens_neox': 3869, 'n_words': 597}