<commit_before>package org.openur.remoting.client.ws.rs.secure_api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.openur.module.domain.security.secure_api.PermissionConstraints.REMOTE_READ;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.shiro.realm.Realm;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import org.mockito.Mockito;
import org.openur.domain.testfixture.testobjects.TestObjectContainer;
import org.openur.module.domain.userstructure.person.Person;
import org.openur.module.domain.utils.compare.PersonComparer;
import org.openur.module.integration.security.shiro.OpenUrRdbmsRealmMock;
import org.openur.module.service.security.IAuthorizationServices;
import org.openur.module.service.userstructure.IUserServices;
import org.openur.module.util.exception.EntityNotFoundException;
import org.openur.remoting.resource.errorhandling.EntityNotFoundExceptionMapper;
import org.openur.remoting.resource.secure_api.SecurityFilter_UsernamePw;
import org.openur.remoting.resource.userstructure.UserResource;
import org.openur.remoting.xchange.rest.providers.json.PersonProvider;

public class SecurityClientFilterTest
{
	private static abstract class AbstractSecurityClientFilterTest
		extends JerseyTest
	{
		protected String applicationName = "Demo-Application";
		protected Boolean hashCredentials;

		protected OpenUrRdbmsRealmMock realmMock;
		protected IAuthorizationServices authorizationServicesMock;
		protected IUserServices userServicesMock;

		@Override
		protected Application configure()
		{
			realmMock = new OpenUrRdbmsRealmMock();
			authorizationServicesMock = Mockito.mock(IAuthorizationServices.class);
			userServicesMock = Mockito.mock(IUserServices.class);
			hashCredentials = (hashCredentials == null ? Boolean.FALSE : hashCredentials);

			AbstractBinder binder = new AbstractBinder()
			{
				@Override
				protected void configure()
				{
					bind(realmMock).to(Realm.class);
					bind(hashCredentials).to(Boolean.class);
					bind(authorizationServicesMock).to(IAuthorizationServices.class);
					bind(userServicesMock).to(IUserServices.class);
				}
			};

			ResourceConfig config = new ResourceConfig(UserResource.class).register(PersonProvider.class).register(SecurityFilter_UsernamePw.class).register(EntityNotFoundExceptionMapper.class).register(binder);

			return config;
		}

		protected Invocation.Builder buildInvocationTargetBuilder(String user, String password, boolean hashCredentials)
		{
			ClientConfig clientConfig = new ClientConfig();
			clientConfig.register(PersonProvider.class);
			clientConfig.register(new SecurityClientFilter_UsernamePw(user, password, hashCredentials));
			Client client = ClientBuilder.newClient(clientConfig);
			WebTarget webTarget = client.target("http://localhost:9998/").path(UserResource.USER_RESOURCE_PATH).path(UserResource.PERSON_PER_ID_RESOURCE_PATH).path(TestObjectContainer.PERSON_UUID_1);

			Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
			invocationBuilder.header(SecurityFilter_UsernamePw.APPLICATION_NAME_PROPERTY, applicationName);

			return invocationBuilder;
		}
	}

	public static class SecurityClientFilterPlainUsernamePwTest
		extends AbstractSecurityClientFilterTest
	{
		@Override
		protected Application configure()
		{
			hashCredentials = Boolean.FALSE;

			return super.configure();
		}

		@Test
		public void testFilterValidCredentials()
			throws EntityNotFoundException
		{
			Mockito.when(authorizationServicesMock.hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName)).thenReturn(Boolean.TRUE);
			Mockito.when(userServicesMock.findPersonById(TestObjectContainer.PERSON_UUID_1)).thenReturn(TestObjectContainer.PERSON_1);

			Invocation.Builder invocationBuilder = buildInvocationTargetBuilder(OpenUrRdbmsRealmMock.USER_NAME_2, OpenUrRdbmsRealmMock.PASSWORD_2, false);
			Response response = invocationBuilder.get();
			assertEquals(200, response.getStatus());
			System.out.println(response.getStatus());

			Person p = response.readEntity(Person.class);
			assertNotNull(p);
			System.out.println(p);
			assertTrue(new PersonComparer().objectsAreEqual(TestObjectContainer.PERSON_1, p));

			assertEquals(1, realmMock.getAuthCounter());
			verify(authorizationServicesMock, times(1)).hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName);
		}
	}

	public static class SecurityClientFilterHashedUsernamePwTest
		extends AbstractSecurityClientFilterTest
	{
		@Override
		protected Application configure()
		{
			hashCredentials = Boolean.TRUE;
			
			return super.configure();
		}

		@Test
		public void testFilterValidCredentials()
			throws EntityNotFoundException
		{
			Mockito.when(authorizationServicesMock.hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName)).thenReturn(Boolean.TRUE);
			Mockito.when(userServicesMock.findPersonById(TestObjectContainer.PERSON_UUID_1)).thenReturn(TestObjectContainer.PERSON_1);

			Invocation.Builder invocationBuilder = buildInvocationTargetBuilder(OpenUrRdbmsRealmMock.USER_NAME_2, OpenUrRdbmsRealmMock.PASSWORD_2, false);
			Response response = invocationBuilder.get();
			assertEquals(200, response.getStatus());
			System.out.println(response.getStatus());

			Person p = response.readEntity(Person.class);
			assertNotNull(p);
			System.out.println(p);
			assertTrue(new PersonComparer().objectsAreEqual(TestObjectContainer.PERSON_1, p));

			assertEquals(1, realmMock.getAuthCounter());
			verify(authorizationServicesMock, times(1)).hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName);
		}
	}
}
<commit_msg>add two SecurityClientFilterUsernamePw-tests, for plain and for hashed credentials<commit_after>package org.openur.remoting.client.ws.rs.secure_api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.openur.module.domain.security.secure_api.PermissionConstraints.REMOTE_READ;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.shiro.realm.Realm;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.openur.domain.testfixture.testobjects.TestObjectContainer;
import org.openur.module.domain.userstructure.person.Person;
import org.openur.module.domain.utils.compare.PersonComparer;
import org.openur.module.integration.security.shiro.OpenUrRdbmsRealmMock;
import org.openur.module.service.security.IAuthorizationServices;
import org.openur.module.service.userstructure.IUserServices;
import org.openur.module.util.exception.EntityNotFoundException;
import org.openur.remoting.resource.errorhandling.EntityNotFoundExceptionMapper;
import org.openur.remoting.resource.secure_api.SecurityFilter_UsernamePw;
import org.openur.remoting.resource.userstructure.UserResource;
import org.openur.remoting.xchange.rest.providers.json.PersonProvider;

@RunWith(Enclosed.class)
public class SecurityClientFilterUsernamePwTest
{
	private static abstract class AbstractSecurityClientFilterUsernamePwTest
		extends JerseyTest
	{
		protected String applicationName = "Demo-Application";
		protected Boolean hashCredentials;

		protected OpenUrRdbmsRealmMock realmMock;
		protected IAuthorizationServices authorizationServicesMock;
		protected IUserServices userServicesMock;

		@Override
		protected Application configure()
		{
			realmMock = new OpenUrRdbmsRealmMock();
			authorizationServicesMock = Mockito.mock(IAuthorizationServices.class);
			userServicesMock = Mockito.mock(IUserServices.class);
			hashCredentials = (hashCredentials == null ? Boolean.FALSE : hashCredentials);

			AbstractBinder binder = new AbstractBinder()
			{
				@Override
				protected void configure()
				{
					bind(realmMock).to(Realm.class);
					bind(hashCredentials).to(Boolean.class);
					bind(authorizationServicesMock).to(IAuthorizationServices.class);
					bind(userServicesMock).to(IUserServices.class);
				}
			};

			ResourceConfig config = new ResourceConfig(UserResource.class).register(PersonProvider.class).register(SecurityFilter_UsernamePw.class).register(EntityNotFoundExceptionMapper.class).register(binder);

			return config;
		}

		protected Invocation.Builder buildInvocationTargetBuilder(String user, String password, boolean hashCredentials)
		{
			ClientConfig clientConfig = new ClientConfig();
			clientConfig.register(PersonProvider.class);
			clientConfig.register(new SecurityClientFilter_UsernamePw(user, password, hashCredentials));
			Client client = ClientBuilder.newClient(clientConfig);
			WebTarget webTarget = client.target("http://localhost:9998/").path(UserResource.USER_RESOURCE_PATH).path(UserResource.PERSON_PER_ID_RESOURCE_PATH).path(TestObjectContainer.PERSON_UUID_1);

			Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
			invocationBuilder.header(SecurityFilter_UsernamePw.APPLICATION_NAME_PROPERTY, applicationName);

			return invocationBuilder;
		}
	}

	public static class SecurityClientFilterPlainUsernamePwTest
		extends AbstractSecurityClientFilterUsernamePwTest
	{
		@Override
		protected Application configure()
		{
			hashCredentials = Boolean.FALSE;

			return super.configure();
		}

		@Test
		public void testFilterValidCredentials()
			throws EntityNotFoundException
		{
			Mockito.when(authorizationServicesMock.hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName)).thenReturn(Boolean.TRUE);
			Mockito.when(userServicesMock.findPersonById(TestObjectContainer.PERSON_UUID_1)).thenReturn(TestObjectContainer.PERSON_1);;

			// do NOT hash credentials:
			Invocation.Builder invocationBuilder = buildInvocationTargetBuilder(OpenUrRdbmsRealmMock.USER_NAME_2, OpenUrRdbmsRealmMock.PASSWORD_2, false);
			Response response = invocationBuilder.get();
			assertEquals(200, response.getStatus());
			System.out.println(response.getStatus());

			Person p = response.readEntity(Person.class);
			assertNotNull(p);
			System.out.println(p);
			assertTrue(new PersonComparer().objectsAreEqual(TestObjectContainer.PERSON_1, p));

			assertEquals(1, realmMock.getAuthCounter());
			verify(authorizationServicesMock, times(1)).hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName);
		}
	}

	public static class SecurityClientFilterHashedUsernamePwTest
		extends AbstractSecurityClientFilterUsernamePwTest
	{
		@Override
		protected Application configure()
		{
			hashCredentials = Boolean.TRUE;
			
			return super.configure();
		}

		@Test
		public void testFilterValidCredentials()
			throws EntityNotFoundException
		{
			Mockito.when(authorizationServicesMock.hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName)).thenReturn(Boolean.TRUE);
			Mockito.when(userServicesMock.findPersonById(TestObjectContainer.PERSON_UUID_1)).thenReturn(TestObjectContainer.PERSON_1);

			// hash credentials:
			Invocation.Builder invocationBuilder = buildInvocationTargetBuilder(OpenUrRdbmsRealmMock.USER_NAME_2, OpenUrRdbmsRealmMock.PASSWORD_2, true);
			Response response = invocationBuilder.get();
			assertEquals(200, response.getStatus());
			System.out.println(response.getStatus());

			Person p = response.readEntity(Person.class);
			assertNotNull(p);
			System.out.println(p);
			assertTrue(new PersonComparer().objectsAreEqual(TestObjectContainer.PERSON_1, p));

			assertEquals(1, realmMock.getAuthCounter());
			verify(authorizationServicesMock, times(1)).hasPermissionTechUser(OpenUrRdbmsRealmMock.TECH_USER_UUID_2, REMOTE_READ, applicationName);
		}
	}
}

{'fraction_non_alphanumeric': 0.09243040350328433, 'fraction_numerical': 0.0046918986549890525, 'mean_word_length': 6.607971445568114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5656270', 'n_tokens_mistral': 4081, 'n_tokens_neox': 3697, 'n_words': 535}