<commit_before>package uk.ac.ox.zoo.seeg.abraid.mp.publicsite.validator;

import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.junit.Test;
import uk.ac.ox.zoo.seeg.abraid.mp.common.domain.Expert;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.ExpertService;
import uk.ac.ox.zoo.seeg.abraid.mp.publicsite.domain.JsonExpertBasic;

import javax.servlet.ServletRequest;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

/**
 * Tests for ExpertForRegistrationValidator.
 * Copyright (c) 2014 University of Oxford
 */
public class ExpertForRegistrationValidatorTest {
    private ExpertForRegistrationValidator target =
            new ExpertForRegistrationValidator(mock(ReCaptcha.class), mock(ExpertService.class));

    @Test
    public void createValidationCaptchaCallsReCaptchaMethodWithCorrectTheme() throws Exception {
        // Arrange
        ReCaptcha mockCaptcha = mock(ReCaptcha.class);
        when(mockCaptcha.createRecaptchaHtml(anyString(), anyString(), anyInt())).thenReturn("expected");
        ExpertForRegistrationValidator localTarget =
                new ExpertForRegistrationValidator(mockCaptcha, null);

        // Act
        String result = localTarget.createValidationCaptcha();

        // Assert
        assertThat(result).isEqualTo("expected");
        verify(mockCaptcha, times(1)).createRecaptchaHtml(eq((String) null), eq("clean"), eq((Integer) null));
    }

    @Test
    public void validateBasicFieldsRejectsNullEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn(null);

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsEmptyEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn("");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsTooLongEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        char[] chars = new char[158];
        Arrays.fill(chars, 'a');
        when(expert.getEmail()).thenReturn(new String(chars) + "@" + new String(chars) + ".com");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address must less than 320 letters in length.");
    }

    @Test
    public void validateBasicFieldsRejectsInvalidEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn("a_at_b.com");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address not valid.");
    }

    @Test
    public void validateBasicFieldsRejectsPreexistingEmails() throws Exception {
        // Arrange
        ExpertService mockExpertService = mock(ExpertService.class);
        ExpertForRegistrationValidator localTarget =
                new ExpertForRegistrationValidator(mock(ReCaptcha.class), mockExpertService);
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn("<EMAIL>");
        when(mockExpertService.getExpertByEmail("<EMAIL>")).thenReturn(mock(Expert.class));

        // Act
        List<String> result = localTarget.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address already has an associated account.");
    }

    @Test
    public void validateBasicFieldsRejectsNullPassword() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getPassword()).thenReturn(null);

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Password must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsEmptyPassword() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getPassword()).thenReturn("");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Password must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsInvalidPasswords() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getPassword()).thenReturn("abc");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Password not sufficiently complex.");
    }

    @Test
    public void validateDetailsFieldsRejectsNullName() throws Exception {
        // Arrange
        Expert expert = mockExpertDetails();
        when(expert.getName()).thenReturn(null);

        // Act
        List<String> result = target.validateDetailsFields(expert);

        // Assert
        assertThat(result).contains("Name must be provided.");
    }

    @Test
    public void validateDetailsFieldsRejectsEmptyName() throws Exception {
        // Arrange
        Expert expert = mockExpertDetails();
        when(expert.getName()).thenReturn("");

        // Act
        List<String> result = target.validateDetailsFields(expert);

        // Assert<commit_msg>Happy path tests for regexes.<commit_after>package uk.ac.ox.zoo.seeg.abraid.mp.publicsite.validator;

import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.junit.Test;
import uk.ac.ox.zoo.seeg.abraid.mp.common.domain.Expert;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.ExpertService;
import uk.ac.ox.zoo.seeg.abraid.mp.publicsite.domain.JsonExpertBasic;

import javax.servlet.ServletRequest;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

/**
 * Tests for ExpertForRegistrationValidator.
 * Copyright (c) 2014 University of Oxford
 */
public class ExpertForRegistrationValidatorTest {
    private ExpertForRegistrationValidator target =
            new ExpertForRegistrationValidator(mock(ReCaptcha.class), mock(ExpertService.class));

    @Test
    public void createValidationCaptchaCallsReCaptchaMethodWithCorrectTheme() throws Exception {
        // Arrange
        ReCaptcha mockCaptcha = mock(ReCaptcha.class);
        when(mockCaptcha.createRecaptchaHtml(anyString(), anyString(), anyInt())).thenReturn("expected");
        ExpertForRegistrationValidator localTarget =
                new ExpertForRegistrationValidator(mockCaptcha, null);

        // Act
        String result = localTarget.createValidationCaptcha();

        // Assert
        assertThat(result).isEqualTo("expected");
        verify(mockCaptcha, times(1)).createRecaptchaHtml(eq((String) null), eq("clean"), eq((Integer) null));
    }

    @Test
    public void validateBasicFieldsRejectsNullEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn(null);

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsEmptyEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn("");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsTooLongEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        char[] chars = new char[158];
        Arrays.fill(chars, 'a');
        when(expert.getEmail()).thenReturn(new String(chars) + "@" + new String(chars) + ".com");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address must less than 320 letters in length.");
    }

    @Test
    public void validateBasicFieldsRejectsInvalidEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn("a_at_b.com");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address not valid.");
    }

    @Test
    public void validateBasicFieldsAcceptsValidEmails() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn("<EMAIL>");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).isEmpty();
    }

    @Test
    public void validateBasicFieldsRejectsPreexistingEmails() throws Exception {
        // Arrange
        ExpertService mockExpertService = mock(ExpertService.class);
        ExpertForRegistrationValidator localTarget =
                new ExpertForRegistrationValidator(mock(ReCaptcha.class), mockExpertService);
        Expert expert = mockExpertBasic();
        when(expert.getEmail()).thenReturn("<EMAIL>");
        when(mockExpertService.getExpertByEmail("<EMAIL>")).thenReturn(mock(Expert.class));

        // Act
        List<String> result = localTarget.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Email address already has an associated account.");
    }

    @Test
    public void validateBasicFieldsRejectsNullPassword() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getPassword()).thenReturn(null);

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Password must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsEmptyPassword() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getPassword()).thenReturn("");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Password must be provided.");
    }

    @Test
    public void validateBasicFieldsRejectsInvalidPasswords() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getPassword()).thenReturn("<PASSWORD>");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).contains("Password not sufficiently complex.");
    }

    @Test
    public void validateBasicFieldsAcceptsValidPasswords() throws Exception {
        // Arrange
        Expert expert = mockExpertBasic();
        when(expert.getPassword()).thenReturn("<PASSWORD>");

        // Act
        List<String> result = target.validateBasicFields(expert);

        // Assert
        assertThat(result).isEmpty();
    }

    @Test
    public void validateDetailsFieldsRejectsNullName() throws Exception {
        // Arrange
        Expert expert = mockExpertDetails();
        when(expert.getName()).thenReturn(null);

        // Act
        List<String> result = target.validateDetailsFields(expert);

        // Assert
        assertThat(result).contains("Name must be provided.");
    }

    @Test
    public void validateDetailsFieldsRejectsEmptyName() throws Exception {
        // Arrange
        Expert expert = mockExpertDetails();
        when(expert.getName()).thenReturn("");

        // Act
        List<String> result = target.validateDetailsFields(expert);

        // Assert
{'fraction_non_alphanumeric': 0.11040527500804118, 'fraction_numerical': 0.0017690575747828885, 'mean_word_length': 2.9233438485804415, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '3234529', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3283, 'n_words': 653}