package com.atlassian.bitbucket.web.conditions;

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionService;
import com.atlassian.bitbucket.user.UserAdminService;
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.*;

@RunWith(MockitoJUnitRunner.class)
public class CanCreateUsersConditionTest {

    @Mock
    private UserAdminService crowdService;
    @InjectMocks
    private CanCreateUsersCondition condition;
    @Mock
    private PermissionService permissionService;

    @Test
    public void testInit() {
        condition.init(Collections.<String, String>emptyMap());
    }

    @Test
    public void testShouldDisplay() throws Exception {
        when(crowdService.canCreateUsers()).thenReturn(false, true);
        when(permissionService.hasGlobalPermission(same(Permission.ADMIN))).thenReturn(false, true);

        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
        assertTrue(condition.shouldDisplay(Collections.<String, Object>emptyMap()));

        verify(crowdService, times(2)).canCreateUsers();
        verifyNoMoreInteractions(crowdService);
        verify(permissionService, times(3)).hasGlobalPermission(same(Permission.ADMIN));
        verifyNoMoreInteractions(permissionService);
    }
}
