<commit_before>package ca.intelliware.ihtsdo.mlds.security.ihtsdo;

import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Arrays;

import org.apache.http.client.ClientProtocolException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@RunWith(MockitoJUnitRunner.class)
public class HttpAuthAuthenticationProviderTest {
	HttpAuthAuthenticationProvider authenticationProvider = new HttpAuthAuthenticationProvider();
	
	@Mock
	HttpAuthAdaptor httpAuthAdaptorMock;
	
	CentralAuthUserPermission adminUserPermission = new CentralAuthUserPermission();
	CentralAuthUserInfo remoteUserInfo = new CentralAuthUserInfo();

	@Before
	public void setUp() {
		authenticationProvider.httpAuthAdaptor = httpAuthAdaptorMock;
		
		adminUserPermission.setApp("MLDS");
		adminUserPermission.setMember("INTL");
		adminUserPermission.setRole("Admin");
	}

	@Test
	public void providerSupportsUsernamePassword() throws Exception {
		assertTrue(authenticationProvider.supports(UsernamePasswordAuthenticationToken.class));
	}
	
	@Test
	public void successReturnsAnAuthentication() throws Exception {
		stubUserLookup("username", remoteUserInfo);
		stubUserPasswordCheckResult("username", "password", true);
		
		Authentication authentication = authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "password"));
		
		assertNotNull(authentication);
	}
	
	@Test()
	public void badPasswordThrowsAnAuthException() throws Exception {
		stubUserLookup("username", remoteUserInfo);
		stubUserPasswordCheckResult("username", "badpassword", false);
		
		try {
			authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "<PASSWORD>"));
		} catch (BadCredentialsException e) {
			// expected
		}
	}

	@Test()
	public void nouserThrowsUsernameNotFoundException() throws Exception {
		stubUserLookup("username", null);
		stubUserPasswordCheckResult("username", "badpassword", false);
		
		try {
			authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "<PASSWORD>"));
		} catch (UsernameNotFoundException e) {
			// expected
		}
	}
	
	@Test
	public void successfulAuthenticationHasAuthorities() throws Exception {
		stubUserLookup("username", remoteUserInfo);
		stubUserPasswordCheckResult("username", "password", true);
		Mockito.stub(httpAuthAdaptorMock.getUserPermissions("username")).toReturn(Arrays.asList(adminUserPermission));
		
		Authentication authentication = authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "password"));
		
		assertThat(authentication.getAuthorities(), not(empty()));
	}


	private void stubUserLookup(String username, CentralAuthUserInfo centralAuthUserInfo) throws IOException {
		Mockito.stub(httpAuthAdaptorMock.getUserInfo(username)).toReturn(centralAuthUserInfo);
	}
<commit_msg>MLDS-170 Update test to match new error on empty perms list<commit_after>package ca.intelliware.ihtsdo.mlds.security.ihtsdo;

import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@RunWith(MockitoJUnitRunner.class)
public class HttpAuthAuthenticationProviderTest {
	HttpAuthAuthenticationProvider authenticationProvider = new HttpAuthAuthenticationProvider();
	
	@Mock
	HttpAuthAdaptor httpAuthAdaptorMock;
	
	CentralAuthUserPermission adminUserPermission = new CentralAuthUserPermission();
	CentralAuthUserInfo remoteUserInfo = new CentralAuthUserInfo();

	@Before
	public void setUp() {
		authenticationProvider.httpAuthAdaptor = httpAuthAdaptorMock;
		
		adminUserPermission.setApp("MLDS");
		adminUserPermission.setMember("INTL");
		adminUserPermission.setRole("Admin");
	}

	@Test
	public void providerSupportsUsernamePassword() throws Exception {
		assertTrue(authenticationProvider.supports(UsernamePasswordAuthenticationToken.class));
	}
	
	@Test
	public void successReturnsAnAuthentication() throws Exception {
		stubUserLookup("username", remoteUserInfo);
		stubUserPasswordCheckResult("username", "password", true);
		stubUserPerms("username", adminUserPermission);

		Authentication authentication = authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "password"));
		
		assertNotNull(authentication);
	}
	
	@Test()
	public void badPasswordThrowsAnAuthException() throws Exception {
		stubUserLookup("username", remoteUserInfo);
		stubUserPasswordCheckResult("username", "badpassword", false);
		
		try {
			authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "<PASSWORD>"));
		} catch (BadCredentialsException e) {
			// expected
		}
	}

	@Test()
	public void nouserThrowsUsernameNotFoundException() throws Exception {
		stubUserLookup("username", null);
		stubUserPasswordCheckResult("username", "badpassword", false);
		
		try {
			authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "<PASSWORD>"));
		} catch (UsernameNotFoundException e) {
			// expected
		}
	}
	
	@Test
	public void successfulAuthenticationHasAuthorities() throws Exception {
		stubUserLookup("username", remoteUserInfo);
		stubUserPasswordCheckResult("username", "password", true);
		stubUserPerms("username", adminUserPermission);
		
		Authentication authentication = authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "password"));
		
		assertThat(authentication.getAuthorities(), not(empty()));
	}

	private void stubUserPerms(String username, CentralAuthUserPermission... perms)
			throws ClientProtocolException, IOException {
		Mockito.stub(httpAuthAdaptorMock.getUserPermissions(username)).toReturn(Arrays.asList(perms));
	}


	private void stubUserLookup(String username, CentralAuthUserInfo centralAuthUserInfo) throws IOException {
		Mockito.stub(httpAuthAdaptorMock.getUserInfo(username)).toReturn(centralAuthUserInfo);
	}

{'fraction_non_alphanumeric': 0.09540341619219553, 'fraction_numerical': 0.00041660880433273156, 'mean_word_length': 7.533175355450237, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7255056', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1884, 'n_words': 347}