<commit_before>package ca.corefacility.bioinformatics.irida.security.permissions.project;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import ca.corefacility.bioinformatics.irida.model.enums.ProjectRole;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.user.Role;
import ca.corefacility.bioinformatics.irida.model.user.User;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroup;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin;
import ca.corefacility.bioinformatics.irida.repositories.ProjectRepository;
import ca.corefacility.bioinformatics.irida.repositories.joins.project.ProjectUserJoinRepository;
import ca.corefacility.bioinformatics.irida.repositories.joins.project.UserGroupProjectJoinRepository;
import ca.corefacility.bioinformatics.irida.repositories.user.UserRepository;

import com.google.common.collect.ImmutableList;

/**
 * Tests for {@link ReadProjectPermission}.
 * 
 * 
 */
public class ReadProjectPermissionTest {
	private ReadProjectPermission readProjectPermission;
	private UserRepository userRepository;
	private ProjectRepository projectRepository;
	private ProjectUserJoinRepository pujRepository;
	private UserGroupProjectJoinRepository ugpjRepository;

	@Before
	public void setUp() {
		userRepository = mock(UserRepository.class);
		projectRepository = mock(ProjectRepository.class);
		pujRepository = mock(ProjectUserJoinRepository.class);
		ugpjRepository = mock(UserGroupProjectJoinRepository.class);
		readProjectPermission = new ReadProjectPermission(projectRepository, userRepository, pujRepository,
				ugpjRepository);
	}

	@Test
	public void testGrantPermission() {
		String username = "fbristow";
		User u = new User();
		u.setUsername(username);
		Project p = new Project();
		ProjectUserJoin projectUser = new ProjectUserJoin(p, u, ProjectRole.PROJECT_USER);

		when(userRepository.loadUserByUsername(username)).thenReturn(u);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(p));
		when(pujRepository.getProjectJoinForUser(p, u)).thenReturn(projectUser);

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>");

		assertTrue("permission was not granted.", readProjectPermission.isAllowed(auth, 1L));

		verify(userRepository).loadUserByUsername(username);
		verify(projectRepository).findById(1L);
		verify(pujRepository).getProjectJoinForUser(p, u);
	}

	@Test
	public void testRejectPermission() {
		String username = "fbristow";
		User u = new User();
		u.setUsername(username);
		Project p = new Project();

		when(userRepository.loadUserByUsername(username)).thenReturn(u);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(p));
		when(pujRepository.getProjectJoinForUser(p, u)).thenReturn(null);
		when(ugpjRepository.findGroupsForProjectAndUser(p, u)).thenReturn(ImmutableList.of());

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>");

		assertFalse("permission was granted.", readProjectPermission.isAllowed(auth, 1L));

		verify(userRepository).loadUserByUsername(username);
		verify(projectRepository).findById(1L);
		verify(pujRepository).getProjectJoinForUser(p, u);
		verify(ugpjRepository).findGroupsForProjectAndUser(p, u);
	}

	@Test
	public void testPermitAdmin() {
		Collection<GrantedAuthority> roles = new ArrayList<>();
		roles.add(Role.ROLE_ADMIN);

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>", roles);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(new Project()));

		assertTrue("permission should be granted to admin.", readProjectPermission.isAllowed(auth, 1L));

		// we should fast pass through to permission granted for administrators.
		verifyNoInteractions(userRepository);
	}

	@Test
	public void testGrantPermissionByGroup() {
		final String username = "fbristow";
		final User u = new User();
		u.setUsername(username);
		final Project p = new Project();
		final UserGroup g = new UserGroup("The group");

		final List<UserGroupProjectJoin> projectGroups = new ArrayList<>();
		projectGroups.add(new UserGroupProjectJoin(p, g, ProjectRole.PROJECT_USER));

		when(userRepository.loadUserByUsername(username)).thenReturn(u);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(p));
		when(pujRepository.getProjectJoinForUser(p, u)).thenReturn(null);
		when(ugpjRepository.findGroupsForProjectAndUser(p, u)).thenReturn(projectGroups);

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>");

		assertTrue("permission should be granted by user group.", readProjectPermission.isAllowed(auth, 1L));

		verify(userRepository).loadUserByUsername(username);
		verify(projectRepository).findById(1L);
		verify(pujRepository).getProjectJoinForUser(p, u);
		verify(ugpjRepository).findGroupsForProjectAndUser(p, u);
	}
}
<commit_msg>more useless imports removed<commit_after>package ca.corefacility.bioinformatics.irida.security.permissions.project;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import ca.corefacility.bioinformatics.irida.model.enums.ProjectRole;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.user.Role;
import ca.corefacility.bioinformatics.irida.model.user.User;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroup;
import ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin;
import ca.corefacility.bioinformatics.irida.repositories.ProjectRepository;
import ca.corefacility.bioinformatics.irida.repositories.joins.project.ProjectUserJoinRepository;
import ca.corefacility.bioinformatics.irida.repositories.joins.project.UserGroupProjectJoinRepository;
import ca.corefacility.bioinformatics.irida.repositories.user.UserRepository;

import com.google.common.collect.ImmutableList;

/**
 * Tests for {@link ReadProjectPermission}.
 * 
 * 
 */
public class ReadProjectPermissionTest {
	private ReadProjectPermission readProjectPermission;
	private UserRepository userRepository;
	private ProjectRepository projectRepository;
	private ProjectUserJoinRepository pujRepository;
	private UserGroupProjectJoinRepository ugpjRepository;

	@Before
	public void setUp() {
		userRepository = mock(UserRepository.class);
		projectRepository = mock(ProjectRepository.class);
		pujRepository = mock(ProjectUserJoinRepository.class);
		ugpjRepository = mock(UserGroupProjectJoinRepository.class);
		readProjectPermission = new ReadProjectPermission(projectRepository, userRepository, pujRepository,
				ugpjRepository);
	}

	@Test
	public void testGrantPermission() {
		String username = "fbristow";
		User u = new User();
		u.setUsername(username);
		Project p = new Project();
		ProjectUserJoin projectUser = new ProjectUserJoin(p, u, ProjectRole.PROJECT_USER);

		when(userRepository.loadUserByUsername(username)).thenReturn(u);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(p));
		when(pujRepository.getProjectJoinForUser(p, u)).thenReturn(projectUser);

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>");

		assertTrue("permission was not granted.", readProjectPermission.isAllowed(auth, 1L));

		verify(userRepository).loadUserByUsername(username);
		verify(projectRepository).findById(1L);
		verify(pujRepository).getProjectJoinForUser(p, u);
	}

	@Test
	public void testRejectPermission() {
		String username = "fbristow";
		User u = new User();
		u.setUsername(username);
		Project p = new Project();

		when(userRepository.loadUserByUsername(username)).thenReturn(u);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(p));
		when(pujRepository.getProjectJoinForUser(p, u)).thenReturn(null);
		when(ugpjRepository.findGroupsForProjectAndUser(p, u)).thenReturn(ImmutableList.of());

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>");

		assertFalse("permission was granted.", readProjectPermission.isAllowed(auth, 1L));

		verify(userRepository).loadUserByUsername(username);
		verify(projectRepository).findById(1L);
		verify(pujRepository).getProjectJoinForUser(p, u);
		verify(ugpjRepository).findGroupsForProjectAndUser(p, u);
	}

	@Test
	public void testPermitAdmin() {
		Collection<GrantedAuthority> roles = new ArrayList<>();
		roles.add(Role.ROLE_ADMIN);

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>", roles);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(new Project()));

		assertTrue("permission should be granted to admin.", readProjectPermission.isAllowed(auth, 1L));

		// we should fast pass through to permission granted for administrators.
		verifyNoInteractions(userRepository);
	}

	@Test
	public void testGrantPermissionByGroup() {
		final String username = "fbristow";
		final User u = new User();
		u.setUsername(username);
		final Project p = new Project();
		final UserGroup g = new UserGroup("The group");

		final List<UserGroupProjectJoin> projectGroups = new ArrayList<>();
		projectGroups.add(new UserGroupProjectJoin(p, g, ProjectRole.PROJECT_USER));

		when(userRepository.loadUserByUsername(username)).thenReturn(u);
		when(projectRepository.findById(1L)).thenReturn(Optional.of(p));
		when(pujRepository.getProjectJoinForUser(p, u)).thenReturn(null);
		when(ugpjRepository.findGroupsForProjectAndUser(p, u)).thenReturn(projectGroups);

		Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "<PASSWORD>");

		assertTrue("permission should be granted by user group.", readProjectPermission.isAllowed(auth, 1L));

		verify(userRepository).loadUserByUsername(username);
		verify(projectRepository).findById(1L);
		verify(pujRepository).getProjectJoinForUser(p, u);
		verify(ugpjRepository).findGroupsForProjectAndUser(p, u);
	}
}

{'fraction_non_alphanumeric': 0.10922737306843266, 'fraction_numerical': 0.0019426048565121413, 'mean_word_length': 7.273192111029949, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5049710', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3230, 'n_words': 561}