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

import com.atlassian.security.auth.trustedapps.ApplicationCertificate;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.user.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.security.Principal;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class TrustedApplicationsUserResolverTest {

    @Mock
    private UserService userService;

    @InjectMocks
    private TrustedApplicationsUserResolver userResolver;

    @Test
    public void testResolve() throws Exception {
        ApplicationCertificate applicationCertificate = mock(ApplicationCertificate.class);
        when(applicationCertificate.getUserName()).thenReturn("user");

        ApplicationUser stashUser = mock(ApplicationUser.class);
        when(userService.getUserByName("user")).thenReturn(stashUser);

        Principal actual = userResolver.resolve(applicationCertificate);
        assertSame(stashUser, actual);
    }
}
