package com.atlassian.stash.internal.auth.trusted;

import com.atlassian.bitbucket.license.LicenseService;
import com.atlassian.bitbucket.auth.AuthenticationContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.servlet.http.HttpServletRequest;

import java.security.Principal;

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

@RunWith(MockitoJUnitRunner.class)
public class TrustedApplicationsAuthControllerTest {

    @Mock
    private AuthenticationContext authenticationContext;
    @Mock
    private LicenseService licenseService;

    @InjectMocks
    private TrustedApplicationsAuthController authController;

    @Test
    public void testShouldAttemptAuthentication() throws Exception {
        assertTrue(authController.shouldAttemptAuthentication(mock(HttpServletRequest.class)));
        when(authenticationContext.isAuthenticated()).thenReturn(true);
        assertFalse(authController.shouldAttemptAuthentication(mock(HttpServletRequest.class)));
    }

    @Test
    public void testCanLogin() throws Exception {
        Principal principal = mock(Principal.class);
        assertFalse(authController.canLogin(principal, mock(HttpServletRequest.class)));
        when(licenseService.canLogin(same(principal))).thenReturn(true);
        assertTrue(authController.canLogin(principal, mock(HttpServletRequest.class)));
    }

}
