package com.atlassian.bitbucket.internal.branch.web;

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionService;
import com.atlassian.bitbucket.permission.PermissionValidationService;
import com.atlassian.soy.renderer.SoyTemplateRenderer;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class BranchCreationServletTest {

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private BranchCreationData branchCreationData;
    @Mock
    private SoyTemplateRenderer soyTemplateRenderer;
    @Mock
    private PermissionValidationService permissionValidationService;
    @Mock
    private PermissionService permissionService;

    @InjectMocks
    private BranchCreationServlet branchCreationServlet;

    @Mock
    private HttpServletRequest request;
    @Mock
    private HttpServletResponse response;

    @Test
    public void testNoRepoWriteAccessToAnyRepository() throws Exception {
        when(permissionService.hasAnyUserPermission(Permission.REPO_WRITE)).thenReturn(false);
        branchCreationServlet.doGet(request, response);
        verify(permissionValidationService).validateAuthenticated();
        verify(soyTemplateRenderer).render(any(), any(), eq(BranchCreationServlet.NO_ACCESS_TEMPLATE_KEY), any());
    }

    @Test
    public void testRepoWritePermissionToRepository() throws Exception {
        when(permissionService.hasAnyUserPermission(Permission.REPO_WRITE)).thenReturn(true);
        branchCreationServlet.doGet(request, response);
        verify(permissionValidationService).validateAuthenticated();
        verify(branchCreationData).soyResults(any());
        verify(soyTemplateRenderer).render(any(), any(), eq(BranchCreationServlet.BRANCH_CREATION_TEMPLATE_KEY), any());
    }
}
