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

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.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collection;

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

import ca.corefacility.bioinformatics.irida.model.Project;
import ca.corefacility.bioinformatics.irida.model.Role;
import ca.corefacility.bioinformatics.irida.model.Sample;
import ca.corefacility.bioinformatics.irida.model.User;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin;
import ca.corefacility.bioinformatics.irida.repositories.ProjectRepository;
import ca.corefacility.bioinformatics.irida.repositories.SampleRepository;
import ca.corefacility.bioinformatics.irida.repositories.UserRepository;

/**
 * Tests for {@link ReadSamplePermission}.
 * 
 * @author <NAME> <<EMAIL>>
 * 
 */
public class ReadSamplePermissionTest {
	private ReadSamplePermission readSamplePermission;
	private UserRepository userRepository;
	private ProjectRepository projectRepository;
	private SampleRepository sampleRepository;

	@Before
	public void setUp() {
		ApplicationContext applicationContext = mock(ApplicationContext.class);
		userRepository = mock(UserRepository.class);
		projectRepository = mock(ProjectRepository.class);
		sampleRepository = mock(SampleRepository.class);
		readSamplePermission = new ReadSamplePermission();
		readSamplePermission.setApplicationContext(applicationContext);

		when(applicationContext.getBean(UserRepository.class)).thenReturn(userRepository);
		when(applicationContext.getBean(ProjectRepository.class)).thenReturn(projectRepository);
		when(applicationContext.getBean("sampleRepository")).thenReturn(sampleRepository);
	}

	@Test
	public void testGrantPermission() {
		String username = "fbristow";
		User u = new User();
		u.setUsername(username);
		Project p = new Project();
		Sample s = new Sample();
		Collection<Join<Project, User>> projectUsers = new ArrayList<>();
		projectUsers.add(new ProjectUserJoin(p, u));
		ProjectSampleJoin projectSample = new ProjectSampleJoin(p, s);

		when(userRepository.getUserByUsername(username)).thenReturn(u);
		when(projectRepository.getProjectForSample(s)).thenReturn(projectSample);
		when(sampleRepository.read(1l)).thenReturn(s);
		when(userRepository.getUsersForProject(p)).thenReturn(projectUsers);

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

		assertTrue("permission was not granted.", readSamplePermission.isAllowed(auth, 1l));

		verify(userRepository).getUserByUsername(username);
		verify(sampleRepository).read(1l);
		verify(projectRepository).getProjectForSample(s);
		verify(userRepository).getUsersForProject(p);
	}

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

		Collection<Join<Project, User>> projectUsers = new ArrayList<>();
		projectUsers.add(new ProjectUserJoin(p, new User()));

		when(userRepository.getUserByUsername(username)).thenReturn(u);
		when(projectRepository.getProjectForSample(s)).thenReturn(projectSample);
		when(sampleRepository.read(1l)).thenReturn(s);
		when(userRepository.getUsersForProject(p)).thenReturn(projectUsers);

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

		assertFalse("permission was granted.", readSamplePermission.isAllowed(auth, 1l));
<commit_msg>Further testing to increase coverage.<commit_after>package ca.corefacility.bioinformatics.irida.security.permissions;

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.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collection;

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

import ca.corefacility.bioinformatics.irida.model.Project;
import ca.corefacility.bioinformatics.irida.model.Role;
import ca.corefacility.bioinformatics.irida.model.Sample;
import ca.corefacility.bioinformatics.irida.model.User;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin;
import ca.corefacility.bioinformatics.irida.repositories.ProjectRepository;
import ca.corefacility.bioinformatics.irida.repositories.SampleRepository;
import ca.corefacility.bioinformatics.irida.repositories.UserRepository;

/**
 * Tests for {@link ReadSamplePermission}.
 * 
 * @author <NAME> <<EMAIL>>
 * 
 */
public class ReadSamplePermissionTest {
	private ReadSamplePermission readSamplePermission;
	private UserRepository userRepository;
	private ProjectRepository projectRepository;
	private SampleRepository sampleRepository;

	@Before
	public void setUp() {
		ApplicationContext applicationContext = mock(ApplicationContext.class);
		userRepository = mock(UserRepository.class);
		projectRepository = mock(ProjectRepository.class);
		sampleRepository = mock(SampleRepository.class);
		readSamplePermission = new ReadSamplePermission();
		readSamplePermission.setApplicationContext(applicationContext);

		when(applicationContext.getBean(UserRepository.class)).thenReturn(userRepository);
		when(applicationContext.getBean(ProjectRepository.class)).thenReturn(projectRepository);
		when(applicationContext.getBean("sampleRepository")).thenReturn(sampleRepository);
	}

	@Test
	public void testGrantPermission() {
		String username = "fbristow";
		User u = new User();
		u.setUsername(username);
		Project p = new Project();
		Sample s = new Sample();
		Collection<Join<Project, User>> projectUsers = new ArrayList<>();
		projectUsers.add(new ProjectUserJoin(p, u));
		ProjectSampleJoin projectSample = new ProjectSampleJoin(p, s);

		when(userRepository.getUserByUsername(username)).thenReturn(u);
		when(projectRepository.getProjectForSample(s)).thenReturn(projectSample);
		when(sampleRepository.read(1l)).thenReturn(s);
		when(userRepository.getUsersForProject(p)).thenReturn(projectUsers);

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

		assertTrue("permission was not granted.", readSamplePermission.isAllowed(auth, 1l));

		verify(userRepository).getUserByUsername(username);
		verify(sampleRepository).read(1l);
		verify(projectRepository).getProjectForSample(s);
		verify(userRepository).getUsersForProject(p);
	}

	@Test
	public void testGrantPermissionWithDomainObject() {
		String username = "fbristow";
		User u = new User();
		u.setUsername(username);
		Project p = new Project();
		Sample s = new Sample();
		Collection<Join<Project, User>> projectUsers = new ArrayList<>();
		projectUsers.add(new ProjectUserJoin(p, u));
		ProjectSampleJoin projectSample = new ProjectSampleJoin(p, s);

		when(userRepository.getUserByUsername(username)).thenReturn(u);
		when(projectRepository.getProjectForSample(s)).thenReturn(projectSample);
		when(sampleRepository.read(1l)).thenReturn(s);
		when(userRepository.getUsersForProject(p)).thenReturn(projectUsers);

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

		assertTrue("permission was not granted.", readSamplePermission.isAllowed(auth, s));

		verify(userRepository).getUserByUsername(username);
		verify(projectRepository).getProjectForSample(s);
		verify(userRepository).getUsersForProject(p);
		// we didn't need to load the domain object for this test.
		verifyZeroInteractions(sampleRepository);
	}

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

		Collection<Join<Project, User>> projectUsers = new ArrayList<>();
		projectUsers.add(new ProjectUserJoin(p, new User()));

		when(userRepository.getUserByUsername(username)).thenReturn(u);
		when(projectRepository.getProjectForSample(s)).thenReturn(projectSample);
		when(sampleRepository.read(1l)).thenReturn(s);
		when(userRepository.getUsersForProject(p)).thenReturn(projectUsers);

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

		assertFalse("permission was granted.", readSamplePermission.isAllowed(auth, 1l));

{'fraction_non_alphanumeric': 0.11031124285411814, 'fraction_numerical': 0.0011645140800338768, 'mean_word_length': 7.382431233362911, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7156115', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2572, 'n_words': 463}