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

import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.bitbucket.ssh.SshKey;
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.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.Matchers.eq;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class UserHasSshKeyConditionTest {
    private UserHasSshKeyCondition condition;

    @Mock
    private ApplicationUser user = null;
    @Mock
    private SshKey sshKey = null;
    @Mock
    private SshKeyService sshKeyService = null;
    @Mock
    private AuthenticationContext authenticationContext = null;

    @Before
    public void setUp() throws Exception {
        condition = new UserHasSshKeyCondition(sshKeyService, authenticationContext);
        condition.init(Collections.<String, String>emptyMap());
    }

    @Test
    public void testNotAuthenticated() throws Exception {
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testNoSshKeyEntries() throws Exception {
        when(authenticationContext.getCurrentUser()).thenReturn(user);
        when(sshKeyService.hasSshKey(eq(user))).thenReturn(false);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testHasSshKeyEntries() throws Exception {
        when(authenticationContext.getCurrentUser()).thenReturn(user);
        when(sshKeyService.hasSshKey(eq(user))).thenReturn(true);
        assertTrue(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }
}
