<commit_before>package com.gentics.mesh.core.user;

import static com.gentics.mesh.test.TestSize.PROJECT_AND_NODE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import com.gentics.madl.tx.Tx;
import com.gentics.mesh.core.data.User;
import com.gentics.mesh.core.rest.common.GenericMessageResponse;
import com.gentics.mesh.etc.config.AuthenticationOptions;
import com.gentics.mesh.rest.client.MeshRestClient;
import com.gentics.mesh.test.context.AbstractMeshTest;
import com.gentics.mesh.test.context.MeshTestContext;
import com.gentics.mesh.test.context.MeshTestSetting;

import io.reactivex.Single;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

@RunWith(Parameterized.class)
@MeshTestSetting(testSize = PROJECT_AND_NODE, startServer = true)
public class JWTConfigurationTest extends AbstractMeshTest {

	@Parameterized.Parameter(0)
	public AuthenticationOptions authOptions;

	@Parameterized.Parameters(name = "{index}: authOptions={0}")
	public static Collection<Object[]> paramData() {
		AuthenticationOptions defaultOptions = new AuthenticationOptions();
		defaultOptions.overrideWithEnv();

		return Arrays.asList(new Object[][] {
			{ defaultOptions },
			{ new AuthenticationOptions()
				.setIssuer("HELLO WORLD TEST")
				.setAudience(Arrays.asList("www.example.com", "auth.example.com"))
			}
		});
	}

	@Override
	public MeshTestContext getTestContext() {
		return testContext.setOptionChanger(options -> {
			options.setAuthenticationOptions(authOptions);
		});
	}

	@Test
	public void testJWTPayload() throws IOException {
		try (Tx tx = tx()) {
			User user = user();
			String username = user.getUsername();

			MeshRestClient client = MeshRestClient.create("localhost", port(), false);
			client.setLogin(username, data().getUserInfo().getPassword());

			Single<GenericMessageResponse> future = client.login();

			GenericMessageResponse loginResponse = future.blockingGet();
			assertNotNull(loginResponse);
			assertEquals("OK", loginResponse.getMessage());

			String token = client.me().getResponse().blockingGet().getHeader("Set-Cookie").orElse(null);
			assertNotNull(token);

			JsonObject payload = new JsonObject(new String(Base64.getDecoder().decode(token.split("\\.")[1])));
			assertEquals(payload.getString("iss"), authOptions.getIssuer());
			assertEquals(payload.getJsonArray("aud"), new JsonArray(authOptions.getAudience()));
		}
	}
}<commit_msg>Updated test to handle tokens better<commit_after>package com.gentics.mesh.core.user;

import static com.gentics.mesh.test.TestSize.PROJECT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.function.Consumer;

import com.gentics.madl.tx.Tx;
import com.gentics.mesh.core.data.User;
import com.gentics.mesh.core.rest.common.GenericMessageResponse;
import com.gentics.mesh.etc.config.MeshOptions;
import com.gentics.mesh.rest.client.MeshRestClient;
import com.gentics.mesh.test.context.AbstractMeshTest;
import com.gentics.mesh.test.context.MeshTestContext;
import com.gentics.mesh.test.context.MeshTestHelper;
import com.gentics.mesh.test.context.MeshTestSetting;

import io.reactivex.Single;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
@MeshTestSetting(testSize = PROJECT, startServer = true)
public class JWTConfigurationTest extends AbstractMeshTest {

	@Parameterized.Parameter(0)
	public Consumer<MeshOptions> optionChanger;

	@Parameterized.Parameters(name = "{index}: authOptions={0}")
	public static Collection<Object[]> paramData() {
		return Arrays.asList(new Object[][] {
			{ MeshTestHelper.noopConsumer() },
			{(Consumer<MeshOptions>) meshOptions -> {
				meshOptions.getAuthenticationOptions()
					.setIssuer("HELLO WORLD")
					.setAudience(Arrays.asList("www.example.com", "auth.example.com"));
			}}
		});
	}

	@Override
	public MeshTestContext getTestContext() {
		return testContext.setOptionChanger(optionChanger);
	}

	@Test
	public void testJWTPayload() throws IOException {
		try (Tx tx = tx()) {
			User user = user();
			String username = user.getUsername();

			MeshRestClient client = MeshRestClient.create("localhost", port(), false);
			client.setLogin(username, data().getUserInfo().getPassword());

			Single<GenericMessageResponse> future = client.login();

			GenericMessageResponse loginResponse = future.blockingGet();
			assertNotNull(loginResponse);
			assertEquals("OK", loginResponse.getMessage());

			String tokenCookie = client.me().getResponse().blockingGet().getHeader("Set-Cookie").orElse(null);
			assertNotNull(tokenCookie);
			String token = tokenCookie.substring(11, tokenCookie.indexOf(';'));
			assertNotNull(token);

			JsonObject payload = new JsonObject(new String(Base64.getDecoder().decode(token.split("\\.")[1])));
			MeshOptions options = this.options();
			this.optionChanger.accept(options);

			assertEquals(payload.getString("iss"), options.getAuthenticationOptions().getIssuer());
			assertEquals(payload.getJsonArray("aud"), new JsonArray(options.getAuthenticationOptions().getAudience()));
		}
	}
}
{'fraction_non_alphanumeric': 0.13196322336398053, 'fraction_numerical': 0.002884442040742744, 'mean_word_length': 5.824108241082411, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4157851', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1639, 'n_words': 286}