package com.atlassian.bitbucket.internal.key.ssh.condition;

import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.bitbucket.internal.ssh.server.SshServer;
import com.atlassian.bitbucket.ssh.SshKeyService;
import com.atlassian.bitbucket.user.ApplicationUser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ShowUserSshCloneUrlConditionTest {

    @Mock
    private AuthenticationContext authenticationContext;
    @InjectMocks
    private ShowUserSshCloneUrlCondition condition;
    @Mock
    private SshKeyService sshKeyService;
    @Mock
    private SshServer sshServer;
    @Mock
    private ApplicationUser user;

    @Before
    public void setUp() throws Exception {
        condition.init(Collections.<String, String>emptyMap());
        when(sshServer.isRunning()).thenReturn(true);
        when(authenticationContext.getCurrentUser()).thenReturn(user);
        when(sshKeyService.hasSshKey(user)).thenReturn(false);
    }

    @Test
    public void testCurrentUserHasKeys() {
        when(sshKeyService.hasSshKey(user)).thenReturn(true);
        assertTrue(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testNoCurrentUser() {
        when(authenticationContext.getCurrentUser()).thenReturn(null);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testSshNotRunning() {
        when(sshServer.isRunning()).thenReturn(false);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }
}
