<commit_before>package org.cherchgk.test.ui;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.Selectors;
import com.codeborne.selenide.SelenideElement;
import com.icegreen.greenmail.store.FolderException;
import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.ServerSetup;
import org.cherchgk.actions.security.RestorePasswordAction;
import org.cherchgk.domain.security.Role;
import org.cherchgk.services.SecurityService;
import org.cherchgk.services.SettingsService;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.support.GenericXmlContextLoader;

import javax.mail.MessagingException;

import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static org.junit.Assert.assertEquals;

/**
 * UI-тесты для страницы восстановления пароля.
 *
 * @author <NAME>
 */
public class RestorePasswordUITest extends BaseUITest {

    private static final GreenMail mailServer = new GreenMail(ServerSetup.SMTP);

    @BeforeClass
    public static void initContext() throws Exception {
        mailServer.start();
        mailServer.setUser("mail-server-user", "mail-server-user-password");

        System.setProperty("jdbc.url", TestWebAppLauncher.TEST_APPLICATION_DATABASE_CONNECTION_URL);
        System.setProperty("hibernate.showSql", "false");
        GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
        ApplicationContext applicationContext = xmlContextLoader.loadContext("/WEB-INF/applicationContext.xml");

        SettingsService settingsService = applicationContext.getBean(SettingsService.class);
        settingsService.saveHostName("test");
        settingsService.saveMailServerHostName(mailServer.getSmtp().getBindTo());
        settingsService.saveMailServerPort(String.valueOf(mailServer.getSmtp().getPort()));
        settingsService.saveMailServerUser("mail-server-user");
        settingsService.saveMailServerPassword("<PASSWORD>-server-user-password");

        SecurityService securityService = applicationContext.getBean(SecurityService.class);
        Role role = securityService.getRoleByName("organizer");
        securityService.createUser("testUser", "12345", "<EMAIL>", role, false);
    }

    @AfterClass
    public static void destroyContext() {
        mailServer.stop();
    }

    @Before
    public void clearMailServer() throws FolderException {
        mailServer.purgeEmailFromAllMailboxes();
    }

    @Test
    public void testUserNotFound() {
        openRestorePasswordPage();
        restorePassword("<EMAIL>");
        $(Selectors.withText(RestorePasswordAction.USER_NOT_FOUND)).shouldBe(Condition.visible);
    }

    @Test
    public void testSuccessfulRestorePassword() throws MessagingException {
        openRestorePasswordPage();
        restorePassword("<EMAIL>");
        $(Selectors.withText(RestorePasswordAction.SUCCESS_MESSAGE)).shouldBe(Condition.visible);
        assertEquals(1, mailServer.getReceivedMessages().length);
        assertEquals("<EMAIL>", mailServer.getReceivedMessages()[0].getAllRecipients()[0].toString());
    }

    private void openRestorePasswordPage() {
        open("");
        $(By.id("loginAction")).shouldBe(Condition.visible);
        $(By.id("loginAction")).click();
        $(By.linkText("Забыли пароль?")).click();
    }

    private void restorePassword(String loginOrEmail) {
        SelenideElement loginOrEmailField = $(By.name("loginOrEmail"));
        loginOrEmailField.shouldBe(Condition.visible);
        loginOrEmailField.setValue(loginOrEmail);
        SelenideElement submitButton = $(By.id("restore-password-button"));
        submitButton.shouldBe(Condition.visible);
        submitButton.click();
    }
}<commit_msg>1. Запуск тестового SMTP-сервера GreenMail теперь выполняется на порту 12345. 2. Добавлен тест, проверяющий корректность работы механизма восстановления пароля в случае недоступности почтового сервера.<commit_after>package org.cherchgk.test.ui;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.Selectors;
import com.codeborne.selenide.SelenideElement;
import com.icegreen.greenmail.store.FolderException;
import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.ServerSetup;
import org.cherchgk.actions.security.RestorePasswordAction;
import org.cherchgk.domain.security.Role;
import org.cherchgk.services.SecurityService;
import org.cherchgk.services.SettingsService;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.support.GenericXmlContextLoader;

import javax.mail.MessagingException;

import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static org.junit.Assert.assertEquals;

/**
 * UI-тесты для страницы восстановления пароля.
 *
 * @author <NAME>
 */
public class RestorePasswordUITest extends BaseUITest {

    private static final int mailServerPort = 12345;
    private static final String mailServerHostName = "127.0.0.1";
    private static final GreenMail mailServer = new GreenMail(new ServerSetup(mailServerPort, mailServerHostName, ServerSetup.PROTOCOL_SMTP));

    @BeforeClass
    public static void initContext() throws Exception {
        System.setProperty("jdbc.url", TestWebAppLauncher.TEST_APPLICATION_DATABASE_CONNECTION_URL);
        System.setProperty("hibernate.showSql", "false");
        GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
        ApplicationContext applicationContext = xmlContextLoader.loadContext("/WEB-INF/applicationContext.xml");

        SettingsService settingsService = applicationContext.getBean(SettingsService.class);
        settingsService.saveHostName("test");
        settingsService.saveMailServerHostName(mailServerHostName);
        settingsService.saveMailServerPort(String.valueOf(mailServerPort));
        settingsService.saveMailServerUser("mail-server-user");
        settingsService.saveMailServerPassword("<PASSWORD>");

        SecurityService securityService = applicationContext.getBean(SecurityService.class);
        Role role = securityService.getRoleByName("organizer");
        securityService.createUser("testUser", "12345", "<EMAIL>", role, false);
    }

    @Before
    public void clearMailServer() throws FolderException {
        mailServer.reset();
    }

    @Test
    public void testUserNotFound() {
        openRestorePasswordPage();
        restorePassword("<EMAIL>");
        $(Selectors.withText(RestorePasswordAction.USER_NOT_FOUND)).shouldBe(Condition.visible);
    }

    @Test
    public void testSuccessfulRestorePassword() throws MessagingException {
        openRestorePasswordPage();
        restorePassword("<EMAIL>");
        $(Selectors.withText(RestorePasswordAction.SUCCESS_MESSAGE)).shouldBe(Condition.visible);
        assertEquals(1, mailServer.getReceivedMessages().length);
        assertEquals("<EMAIL>", mailServer.getReceivedMessages()[0].getAllRecipients()[0].toString());
    }

    @Test
    public void testMailSendingFailure() throws MessagingException {
        mailServer.stop();
        openRestorePasswordPage();
        restorePassword("<EMAIL>");
        $(Selectors.withText(RestorePasswordAction.MAIL_SENDING_FAILURE)).shouldBe(Condition.visible);
    }

    private void openRestorePasswordPage() {
        open("");
        $(By.id("loginAction")).shouldBe(Condition.visible);
        $(By.id("loginAction")).click();
        $(By.linkText("Забыли пароль?")).click();
    }

    private void restorePassword(String loginOrEmail) {
        SelenideElement loginOrEmailField = $(By.name("loginOrEmail"));
        loginOrEmailField.shouldBe(Condition.visible);
        loginOrEmailField.setValue(loginOrEmail);
        SelenideElement submitButton = $(By.id("restore-password-button"));
        submitButton.shouldBe(Condition.visible);
        submitButton.click();
    }
}
{'fraction_non_alphanumeric': 0.10973084886128365, 'fraction_numerical': 0.004140786749482402, 'mean_word_length': 4.559918754231551, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 1, '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': '5450268', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2277, 'n_words': 359}