package com.atlassian.bitbucket.internal.crowd.sso;

import com.atlassian.crowd.embedded.api.Directory;
import com.atlassian.crowd.integration.Constants;
import com.atlassian.crowd.service.client.ClientPropertiesImpl;
import com.atlassian.bitbucket.stp.SimpleSupportInfoBuilder;
import org.junit.Test;

import java.util.Map;
import java.util.Properties;

import static com.atlassian.bitbucket.internal.crowd.sso.SsoSupportInfoAppender.STP_DIRECTORY_SSO;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class SsoSupportInfoAppenderTest {

    @Test
    public void testHappyPath() {
        Directory directory = mock(Directory.class);
        when(directory.getId()).thenReturn(1L);

        Properties properties = new Properties();

        properties.setProperty(Constants.PROPERTIES_FILE_APPLICATION_NAME, "stash");
        properties.setProperty(Constants.PROPERTIES_FILE_APPLICATION_PASSWORD, "super secret");
        properties.setProperty(Constants.PROPERTIES_FILE_APPLICATION_LOGIN_URL, "http://stash.atlassian.com/login");
        properties.setProperty(Constants.PROPERTIES_FILE_COOKIE_TOKENKEY, "abcdefgthghgsfhdsfk");
        properties.setProperty(Constants.PROPERTIES_FILE_SESSIONKEY_TOKENKEY, "3def6ghdg7ashscccdc");

        properties.setProperty(Constants.PROPERTIES_FILE_HTTP_PROXY_HOST, "newgrounds.com");
        properties.setProperty(Constants.PROPERTIES_FILE_HTTP_PROXY_PORT, "8080");
        properties.setProperty(Constants.PROPERTIES_FILE_HTTP_PROXY_USERNAME, "stash");
        properties.setProperty(Constants.PROPERTIES_FILE_HTTP_PROXY_PASSWORD, "youshouldntbereadingthis");

        properties.setProperty(Constants.PROPERTIES_FILE_COOKIE_DOMAIN, "ssoDomain.com");
        properties.setProperty(Constants.PROPERTIES_FILE_BASE_URL, "http://stash.atlassian.com:8080");


        ClientPropertiesImpl clientProperties = ClientPropertiesImpl.newInstanceFromProperties(properties);
        SsoConfiguration config = new SsoConfiguration(null, null, directory, clientProperties);

        SimpleSupportInfoBuilder appender = new SimpleSupportInfoBuilder();
        SsoSupportInfoAppender.addSsoSupportInfo(appender, directory, config);

        Map<String, Object> expectedDirectory = (Map<String, Object>)
                appender.getValuesAndSubCategories().get(STP_DIRECTORY_SSO);

        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".authentication.url"), "http://stash.atlassian.com/login");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".application.name"), "stash");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".base.url"), "http://stash.atlassian.com:8080");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".cookie.tokenkey"), "abcdefgthghgsfhdsfk");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".http.proxy.host"), "newgrounds.com");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".http.proxy.port"), "8080");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".http.proxy.username"), "stash");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".session.tokenkey"), "3def6ghdg7ashscccdc");
        assertEquals(expectedDirectory.get(STP_DIRECTORY_SSO + ".sso.domain"), "ssoDomain.com");

        for (Map.Entry<String, Object> entry : expectedDirectory.entrySet()) {
            if (entry.getValue() != null) {
                assertFalse(entry.getValue().toString().contains("super secret"));
                assertFalse(entry.getValue().toString().contains("youshouldntbereadingthis"));
            }
        }

    }

    @Test
    public void testNotSSO() {
        Directory directory = mock(Directory.class);
        when(directory.getId()).thenReturn(1L);

        Directory directory2 = mock(Directory.class);
        when(directory2.getId()).thenReturn(2L);

        SsoConfiguration config = new SsoConfiguration(null, null, directory2, null);

        SimpleSupportInfoBuilder appender = new SimpleSupportInfoBuilder();
        SsoSupportInfoAppender.addSsoSupportInfo(appender, directory, config);

        Map<String, Object> expectedDirectory = (Map<String, Object>)
                appender.getValuesAndSubCategories().get(STP_DIRECTORY_SSO);
        assertNull(expectedDirectory);
    }

    @Test
    public void testNoConfig() {
        Directory directory = mock(Directory.class);
        when(directory.getId()).thenReturn(1L);

        SimpleSupportInfoBuilder appender = new SimpleSupportInfoBuilder();
        SsoSupportInfoAppender.addSsoSupportInfo(appender, directory, null);

        Map<String, Object> expectedDirectory = (Map<String, Object>)
                appender.getValuesAndSubCategories().get(STP_DIRECTORY_SSO);
        assertNull(expectedDirectory);
    }
}
