<commit_before>package web.sso;

import com.google.inject.Injector;
import controllers.sso.auth.SignInController;
import controllers.sso.web.Escapers;
import ninja.NinjaFluentLeniumTest;
import ninja.Router;
import ninja.utils.NinjaProperties;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.slf4j.Logger;
import services.sso.CaptchaTokenService;
import services.sso.token.ExpirableTokenEncryptor;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * Selenium tests for sign in.
 */
public class SignInTest extends NinjaFluentLeniumTest {

    Router router;
    ExpirableTokenEncryptor encryptor;
    CaptchaTokenService captchaTokenService;
    NinjaProperties properties;
    Logger logger;

    @Before
    public void setUp() {
        Injector injector = this.getInjector();

        this.router = injector.getBinding(Router.class).getProvider().get();
        this.encryptor = injector.getBinding(ExpirableTokenEncryptor.class).getProvider().get();
        this.captchaTokenService = injector.getBinding(CaptchaTokenService.class).getProvider().get();
        this.properties = injector.getBinding(NinjaProperties.class).getProvider().get();
        this.logger = injector.getBinding(Logger.class).getProvider().get();

        webDriver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    }

    @Test
    public void testSignIn() {
        goTo(getSignInUrl());

        assertTrue("No error notification", webDriver.findElements(By.className("alert-danger")).isEmpty());
        assertTrue("No success notifications", webDriver.findElements(By.className("alert-success")).isEmpty());
        assertTrue("No captcha with the first hit", webDriver.findElements(By.name("captchaCode")).isEmpty());

        webDriver.findElement(By.name("emailOrUsername")).sendKeys("<EMAIL>");
        webDriver.findElement(By.name("password")).sendKeys("<EMAIL>");

        click("#signInSubmit");

        assertNotNull("Error notification exists", webDriver.findElement(By.className("alert-danger")));

        // In test mode all continue URLs are allowed.
        goTo(getSignInUrl(getServerAddress() + "?successful_sign_in=true", null));

        assertTrue("No captcha", webDriver.findElements(By.name("captchaCode")).isEmpty());

        // Apply existing user.
        webDriver.findElement(By.name("emailOrUsername")).sendKeys("root");
        webDriver.findElement(By.name("password")).sendKeys("password");

        click("#signInSubmit");

        assertTrue("Redirected to continue URL", webDriver.getCurrentUrl().contains("successful_sign_in"));
    }

    @Test
    public void testSignInWithCaptcha() {
        int numberOfSafeRequests = properties.getIntegerWithDefault("counters.ip.numberOfSafeRequests", 5);

        // Reach IP limit hits so the sign in page shows captcha.
        for (int i = 0; i < numberOfSafeRequests; i++) {
            goTo(getSignInUrl());
        }
        goTo(getSignInUrl(getServerAddress() + "?successful_sign_in=true", null));
        logger.warn(webDriver.getPageSource());
        assertTrue("No error notification", webDriver.findElements(By.className("alert-danger")).isEmpty());
        assertTrue("No success notifications", webDriver.findElements(By.className("alert-success")).isEmpty());<commit_msg>Adds Sign In web-driver test: tests authentication in simple and captcha case<commit_after>package web.sso;

import com.google.inject.Injector;
import controllers.sso.auth.SignInController;
import controllers.sso.web.Escapers;
import ninja.NinjaFluentLeniumTest;
import ninja.Router;
import ninja.utils.NinjaProperties;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.slf4j.Logger;
import services.sso.CaptchaTokenService;
import services.sso.token.ExpirableTokenEncryptor;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * Selenium tests for sign in.
 */
public class SignInTest extends NinjaFluentLeniumTest {

    Router router;
    ExpirableTokenEncryptor encryptor;
    CaptchaTokenService captchaTokenService;
    NinjaProperties properties;
    Logger logger;

    @Before
    public void setUp() {
        Injector injector = this.getInjector();

        this.router = injector.getBinding(Router.class).getProvider().get();
        this.encryptor = injector.getBinding(ExpirableTokenEncryptor.class).getProvider().get();
        this.captchaTokenService = injector.getBinding(CaptchaTokenService.class).getProvider().get();
        this.properties = injector.getBinding(NinjaProperties.class).getProvider().get();
        this.logger = injector.getBinding(Logger.class).getProvider().get();

        webDriver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    }

    @Test
    public void testSignIn() {
        goTo(getSignInUrl());

        assertTrue("No error notification", webDriver.findElements(By.className("alert-danger")).isEmpty());
        assertTrue("No success notifications", webDriver.findElements(By.className("alert-success")).isEmpty());
        assertTrue("No captcha with the first hit", webDriver.findElements(By.name("captchaCode")).isEmpty());

        webDriver.findElement(By.name("emailOrUsername")).sendKeys("<EMAIL>");
        webDriver.findElement(By.name("password")).sendKeys("<EMAIL>");

        click("#signInSubmit");

        assertNotNull("Error notification exists", webDriver.findElement(By.className("alert-danger")));

        // In test mode all continue URLs are allowed.
        goTo(getSignInUrl(getServerAddress() + "?successful_sign_in=true", null));

        assertTrue("No captcha", webDriver.findElements(By.name("captchaCode")).isEmpty());

        // Apply existing user.
        webDriver.findElement(By.name("emailOrUsername")).sendKeys("root");
        webDriver.findElement(By.name("password")).sendKeys("password");

        click("#signInSubmit");

        assertTrue("Redirected to continue URL", webDriver.getCurrentUrl().contains("successful_sign_in"));
    }

    @Test
    public void testSignInWithCaptcha() {
        int numberOfSafeRequests = properties.getIntegerWithDefault("counters.ip.numberOfSafeRequests", 5);

        // Reach IP limit hits so the sign in page shows captcha.
        for (int i = 0; i < numberOfSafeRequests; i++) {
            goTo(getSignInUrl());
        }
        goTo(getSignInUrl(getServerAddress() + "?successful_sign_in=true", null));

        assertTrue("No error notification", webDriver.findElements(By.className("alert-danger")).isEmpty());
        assertTrue("No success notifications", webDriver.findElements(By.className("alert-success")).isEmpty());
{'fraction_non_alphanumeric': 0.12998675106727514, 'fraction_numerical': 0.0014721036360959812, 'mean_word_length': 4.254447022428461, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '2541152', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1913, 'n_words': 337}