package it.com.atlassian.stash.ssh;

import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.bitbucket.test.SshKeyTestHelper;
import com.atlassian.webdriver.bitbucket.page.admin.ServerSettingsPage;
import com.atlassian.webdriver.bitbucket.page.admin.SshAdminFormFragment;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class SshAdminTest extends BaseBrowserFuncTest {

    private static final String SAMPLE_DSA_KEY
            = "ssh-dss AAAAB3NzaC1kc3MAAACBAIBL0r1uweUBI42s70MYAi8gjsEDArYeCuM33eV1wREB4j3w5TTvKBZCbX1ejcujeU9NZyK+O+"
            + "/6iegzSTPQ9mL7E7BydBFQqEKhLb+/f2NO4blzcHNHbINhPodSBEJgBf27Z9FJBajbjv8gqQzxVmbm1ii2MRYXkZ++ZxWIwg8vAAAA"
            + "FQCDD6psEcl8zWwDxf/5BXLxro406QAAAIB+Ex8DzkLWG7zSFoP/DKe+IuadjqYdwSELSqvVs/iq8LuL20PyV7HCAqsPnAMpywmYAs"
            + "a7G2Usd/BL+SIv5zhyoN2VN+tDceWUZZQ3At+xh9rUmO8JShTYhJ7gMho1Hb9I24OIvpaVD6Ex4qGj+G537tHtxrwQ+Vm8GbRjxk/W"
            + "/wAAAIB/roIr+RyolcGlffAqNsQD2eGwkQrIYiTiyGL2yzqalkTMZnjOpysN/YdwnSi0N4KQBzVV/2JW499eH0QDtUFgS1rilNA0Gb"
            + "z3RZYvolxQZRxERNYnBRZIRcgG/ZqaNcgq1gDX3kXHlWN0sHRztrp9gyGCdqWxkhaYUeJn2M0Tzw== test@test.local";

    @Before
    public void addSshKey() throws Exception {
        SshKeyTestHelper.addSshKey(DefaultFuncTestData.getAdminUser(), SAMPLE_DSA_KEY);
    }

    @After
    public void removeAllSshKeys() throws Exception {
        SshKeyTestHelper.removeSshKeys(DefaultFuncTestData.getAdminUser());
    }

    @Test
    public void testEnablingAndDisablingSshServer() {
        SshAdminFormFragment sshSettings = loginAsAdmin(ServerSettingsPage.class).getSshFormFragment();

        assertTrue("SSH should be enabled by default", sshSettings.isEnabled());
        assertTrue("SSH key access support should be enabled by default", sshSettings.isKeyAccessEnabled());
        assertEquals("SSH base URL should be blank by default", "", sshSettings.getBaseUrl());
        sshSettings = sshSettings
                .toggleEnabled()
                .toggleKeyAccessEnabled()
                .setBaseUrl("ssh://example.com")
                .end()
                .clickSave().getSshFormFragment();

        assertFalse("SSH should now be disabled", sshSettings.isEnabled());
        assertFalse("SSH key access support should now be disabled", sshSettings.isKeyAccessEnabled());
        assertEquals("SSH Base url should have changed", "ssh://example.com", sshSettings.getBaseUrl());

        sshSettings = sshSettings
                .toggleEnabled()
                .setBaseUrl("")
                .end()
                .clickSave().getSshFormFragment();

        assertTrue("SSH should now be enabled", sshSettings.isEnabled());
        assertFalse("SSH key access support should still be disabled", sshSettings.isKeyAccessEnabled());
        assertEquals("SSH Base url should have changed", "", sshSettings.getBaseUrl());

        sshSettings = sshSettings
                .toggleKeyAccessEnabled()
                .end()
                .clickSave().getSshFormFragment();

        assertTrue("SSH should now be enabled", sshSettings.isEnabled());
        assertTrue("SSH key access support should now be enabled", sshSettings.isKeyAccessEnabled());
    }
}
