<commit_before>package ch.sportchef.business.authentication.control;

import ch.sportchef.business.configuration.control.ConfigurationService;
import ch.sportchef.business.configuration.entity.Configuration;
import ch.sportchef.business.user.control.UserService;
import ch.sportchef.business.user.entity.User;
import ch.sportchef.business.user.entity.UserBuilder;
import com.dumbster.smtp.ServerOptions;
import com.dumbster.smtp.SmtpServer;
import com.dumbster.smtp.SmtpServerFactory;
import org.junit.Rule;
import org.junit.Test;
import org.needle4j.annotation.ObjectUnderTest;
import org.needle4j.junit.NeedleBuilders;
import org.needle4j.junit.NeedleRule;
import org.needle4j.mock.EasyMockProvider;
import org.picketlink.Identity;
import org.picketlink.credential.DefaultLoginCredentials;
import org.picketlink.idm.model.Account;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.Optional;

import static java.lang.Boolean.FALSE;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class AuthenticationServiceTest {

    private static final Long TEST_USER_ID = 1L;
    private static final String TEST_USER_FIRSTNAME = "John";
    private static final String TEST_USER_LASTNAME = "Doe";
    private static final String TEST_USER_PHONE = "+41 79 555 00 01";
    private static final String TEST_USER_EMAIL = "<EMAIL>";

    @Rule
    public NeedleRule needleRule = NeedleBuilders.needleMockitoRule().build();

    @ObjectUnderTest(postConstruct = true)
    private AuthenticationService authenticationService;

    @Inject
    private EasyMockProvider mockProvider;

    @Inject
    private UserService userServiceMock;

    @Inject
    private ConfigurationService configurationServiceMock;

    private User createTestUser() {
        return UserBuilder.anUser()
                .withUserId(TEST_USER_ID)
                .withFirstName(TEST_USER_FIRSTNAME)
                .withLastName(TEST_USER_LASTNAME)
                .withPhone(TEST_USER_PHONE)
                .withEmail(TEST_USER_EMAIL)
                .build();
    }

    private Configuration createConfigurationMock() {
        final Configuration configurationMock = mock(Configuration.class);
        expect(configurationMock.getSMTPServer()).andReturn("localhost");
        expect(configurationMock.getSMTPPort()).andReturn(4444);
        expect(configurationMock.getSMTPUser()).andReturn("test");
        expect(configurationMock.getSMTPPassword()).andReturn("<PASSWORD>");
        expect(configurationMock.getSMTPSSL()).andReturn(FALSE);
        expect(configurationMock.getSMTPFrom()).andReturn("<EMAIL>");
        replay(configurationMock);
        return configurationMock;
    }

    private SmtpServer createSmtpServerMock() {
        final ServerOptions smtpServerOptions = new ServerOptions();
        smtpServerOptions.port = 4444;
        final SmtpServer smtpServer = SmtpServerFactory.startServer(smtpServerOptions);
        return smtpServer;
    }

    @Test
    public void requestChallengeNotOk() {
        // arrange
        expect(userServiceMock.findByEmail(TEST_USER_EMAIL)).andReturn(Optional.empty());
        mockProvider.replayAll();

        // act
        final boolean ok = authenticationService.requestChallenge(TEST_USER_EMAIL);

        // assert
        assertThat(ok, is(false));
        mockProvider.verifyAll();
    }

    @Test
    public void requestAndValidateChallenge() {
        // arrange

        // act
        final String challenge = requestChallengeOk();
        final String token = validateChallenge(challenge);

        // assert
        System.out.println(token);
    }

    private String requestChallengeOk() {
        // arrange
        final Optional<User> userOptional = Optional.of(createTestUser());
        expect(userServiceMock.findByEmail(TEST_USER_EMAIL)).andReturn(userOptional);
        expectLastCall().times(2);<commit_msg>#186 Added missing assertions to complete a forgotten test<commit_after>package ch.sportchef.business.authentication.control;

import ch.sportchef.business.configuration.control.ConfigurationService;
import ch.sportchef.business.configuration.entity.Configuration;
import ch.sportchef.business.user.control.UserService;
import ch.sportchef.business.user.entity.User;
import ch.sportchef.business.user.entity.UserBuilder;
import com.dumbster.smtp.ServerOptions;
import com.dumbster.smtp.SmtpServer;
import com.dumbster.smtp.SmtpServerFactory;
import org.junit.Rule;
import org.junit.Test;
import org.needle4j.annotation.ObjectUnderTest;
import org.needle4j.junit.NeedleBuilders;
import org.needle4j.junit.NeedleRule;
import org.needle4j.mock.EasyMockProvider;
import org.picketlink.Identity;
import org.picketlink.credential.DefaultLoginCredentials;
import org.picketlink.idm.model.Account;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.Optional;

import static java.lang.Boolean.FALSE;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class AuthenticationServiceTest {

    private static final Long TEST_USER_ID = 1L;
    private static final String TEST_USER_FIRSTNAME = "John";
    private static final String TEST_USER_LASTNAME = "Doe";
    private static final String TEST_USER_PHONE = "+41 79 555 00 01";
    private static final String TEST_USER_EMAIL = "<EMAIL>";

    @Rule
    public NeedleRule needleRule = NeedleBuilders.needleMockitoRule().build();

    @ObjectUnderTest(postConstruct = true)
    private AuthenticationService authenticationService;

    @Inject
    private EasyMockProvider mockProvider;

    @Inject
    private UserService userServiceMock;

    @Inject
    private ConfigurationService configurationServiceMock;

    private User createTestUser() {
        return UserBuilder.anUser()
                .withUserId(TEST_USER_ID)
                .withFirstName(TEST_USER_FIRSTNAME)
                .withLastName(TEST_USER_LASTNAME)
                .withPhone(TEST_USER_PHONE)
                .withEmail(TEST_USER_EMAIL)
                .build();
    }

    private Configuration createConfigurationMock() {
        final Configuration configurationMock = mock(Configuration.class);
        expect(configurationMock.getSMTPServer()).andReturn("localhost");
        expect(configurationMock.getSMTPPort()).andReturn(4444);
        expect(configurationMock.getSMTPUser()).andReturn("test");
        expect(configurationMock.getSMTPPassword()).andReturn("<PASSWORD>");
        expect(configurationMock.getSMTPSSL()).andReturn(FALSE);
        expect(configurationMock.getSMTPFrom()).andReturn("<EMAIL>");
        replay(configurationMock);
        return configurationMock;
    }

    private SmtpServer createSmtpServerMock() {
        final ServerOptions smtpServerOptions = new ServerOptions();
        smtpServerOptions.port = 4444;
        final SmtpServer smtpServer = SmtpServerFactory.startServer(smtpServerOptions);
        return smtpServer;
    }

    @Test
    public void requestChallengeNotOk() {
        // arrange
        expect(userServiceMock.findByEmail(TEST_USER_EMAIL)).andReturn(Optional.empty());
        mockProvider.replayAll();

        // act
        final boolean ok = authenticationService.requestChallenge(TEST_USER_EMAIL);

        // assert
        assertThat(ok, is(false));
        mockProvider.verifyAll();
    }

    @Test
    public void requestAndValidateChallenge() {
        // arrange

        // act
        final String challenge = requestChallengeOk();
        final String token = validateChallenge(challenge);

        // assert
        assertThat(token.matches(".{20}\\..{38}\\..{86}"), is(true));
    }

    private String requestChallengeOk() {
        // arrange
        final Optional<User> userOptional = Optional.of(createTestUser());
        expect(userServiceMock.findByEmail(TEST_USER_EMAIL)).andReturn(userOptional);
        expectLastCall().times(2);
{'fraction_non_alphanumeric': 0.09352349777881692, 'fraction_numerical': 0.006897357961187748, 'mean_word_length': 3.8278781038374716, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1378252', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2372, 'n_words': 434}