<commit_before>package org.innovateuk.ifs.finance.security;

import org.innovateuk.ifs.BaseServiceSecurityTest;
import org.innovateuk.ifs.application.resource.ApplicationResource;
import org.innovateuk.ifs.application.security.ApplicationLookupStrategy;
import org.innovateuk.ifs.application.security.ApplicationPermissionRules;
import org.innovateuk.ifs.application.security.FormInputResponseFileUploadLookupStrategies;
import org.innovateuk.ifs.application.security.FormInputResponseFileUploadRules;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.finance.domain.FinanceRow;
import org.innovateuk.ifs.finance.handler.item.FinanceRowHandler;
import org.innovateuk.ifs.finance.resource.ProjectFinanceResource;
import org.innovateuk.ifs.finance.resource.cost.FinanceRowItem;
import org.innovateuk.ifs.finance.transactional.ProjectFinanceRowService;
import org.innovateuk.ifs.project.security.ProjectFinancePermissionRules;
import org.innovateuk.ifs.user.resource.RoleResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.resource.UserRoleType;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.method.P;

import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static junit.framework.TestCase.fail;
import static org.innovateuk.ifs.application.builder.ApplicationResourceBuilder.newApplicationResource;
import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;
import static org.innovateuk.ifs.finance.builder.ProjectFinanceResourceBuilder.newProjectFinanceResource;
import static org.innovateuk.ifs.user.builder.RoleResourceBuilder.newRoleResource;
import static org.innovateuk.ifs.user.builder.UserResourceBuilder.newUserResource;
import static org.innovateuk.ifs.user.resource.UserRoleType.PROJECT_FINANCE;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ProjectFinanceRowPermissionRulesTest extends BaseServiceSecurityTest<ProjectFinanceRowService> {

    private ProjectFinancePermissionRules projectFinancePermissionRules;

    @Before
    public void lookupPermissionRules() {

        projectFinancePermissionRules = getMockPermissionRulesBean(ProjectFinancePermissionRules.class);

    }

    /**
     * Note: Although only project finance users should be allowed to see finance data, because its is comp admin
     * who actually is expected to usually decide on funding of applications (and generate project), it is necessary
     * to provide service level access to these methods to comp admin user.
     *
     * This is inadvertently removed and caused regression issues INFUND-7981 and INFUND-7972
     */
    @Test
    public void testFinanceChecksDetails(){
        final long applicationId = 1L;
        assertAccessDenied(
                () -> classUnderTest.financeChecksDetails(1L, 2L),
                () -> {
                    verify(projectFinancePermissionRules).internalUserCanSeeProjectFinancesForOrganisations(isA(ProjectFinanceResource.class), isA(UserResource.class));
                    verify(projectFinancePermissionRules).partnersCanSeeTheProjectFinancesForTheirOrganisation(isA(ProjectFinanceResource.class), isA(UserResource.class));
                }
        );



//        asList(UserRoleType.values()).forEach(role -> {
//            RoleResource roleResource = newRoleResource().withType(role).build();
//            UserResource userWithRole = newUserResource().withRolesGlobal(singletonList(roleResource)).build();
//            setLoggedInUser(userWithRole);
//            if (PROJECT_FINANCE.equals(role) || UserRoleType.COMP_ADMIN.equals(role)) {
//                classUnderTest.financeChecksDetails(1L, 2L);
//            } else {
//                try{
//                    classUnderTest.financeChecksDetails(1L, 2L);
//                    fail("Should have thrown an AccessDeniedException for any non-Finance Team members");
//                } catch (AccessDeniedException e) {
//                    // expected behaviour
//                }
//            }
//        });
    }

    /**
     * Comp admin is allowed access to update project inance details costs for same reason as above financeChecksDetails
     * method.
     */<commit_msg>INFUND-7574 Updating tests to match new rules<commit_after>package org.innovateuk.ifs.finance.security;

import org.innovateuk.ifs.BaseServiceSecurityTest;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.finance.domain.FinanceRow;
import org.innovateuk.ifs.finance.handler.item.FinanceRowHandler;
import org.innovateuk.ifs.finance.resource.ProjectFinanceResource;
import org.innovateuk.ifs.finance.resource.cost.FinanceRowItem;
import org.innovateuk.ifs.finance.transactional.ProjectFinanceRowService;
import org.innovateuk.ifs.project.security.ProjectFinancePermissionRules;
import org.innovateuk.ifs.user.resource.RoleResource;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.resource.UserRoleType;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.method.P;

import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static junit.framework.TestCase.fail;
import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;
import static org.innovateuk.ifs.finance.builder.ProjectFinanceResourceBuilder.newProjectFinanceResource;
import static org.innovateuk.ifs.user.builder.RoleResourceBuilder.newRoleResource;
import static org.innovateuk.ifs.user.builder.UserResourceBuilder.newUserResource;
import static org.innovateuk.ifs.user.resource.UserRoleType.PROJECT_FINANCE;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;

public class ProjectFinanceRowPermissionRulesTest extends BaseServiceSecurityTest<ProjectFinanceRowService> {

    private ProjectFinancePermissionRules projectFinancePermissionRules;

    @Before
    public void lookupPermissionRules() {

        projectFinancePermissionRules = getMockPermissionRulesBean(ProjectFinancePermissionRules.class);

    }

    @Test
    public void testFinanceChecksDetails(){
        assertAccessDenied(
                () -> classUnderTest.financeChecksDetails(1L, 2L),
                () -> {
                    verify(projectFinancePermissionRules).internalUserCanSeeProjectFinancesForOrganisations(isA(ProjectFinanceResource.class), isA(UserResource.class));
                    verify(projectFinancePermissionRules).partnersCanSeeTheProjectFinancesForTheirOrganisation(isA(ProjectFinanceResource.class), isA(UserResource.class));
                }
        );
    }

    /**
     * Comp admin is allowed access to update project inance details costs for same reason as above financeChecksDetails
     * method.
     */
{'fraction_non_alphanumeric': 0.08552169639095633, 'fraction_numerical': 0.0029490240134812527, 'mean_word_length': 4.366993217784477, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5331246', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1937, 'n_words': 330}