<commit_before>package org.subshare.core.user;

import static co.codewizards.cloudstore.core.oio.OioFileFactory.*;
import static org.assertj.core.api.Assertions.*;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.subshare.core.gpg.GnuPgTest;
import org.subshare.core.pgp.Pgp;
import org.subshare.core.pgp.PgpAuthenticationCallback;
import org.subshare.core.pgp.PgpKey;
import org.subshare.core.pgp.PgpKeyId;
import org.subshare.core.pgp.PgpKeyValidity;
import org.subshare.core.pgp.PgpOwnerTrust;
import org.subshare.core.pgp.PgpRegistry;
import org.subshare.core.pgp.gnupg.GnuPgDir;

import co.codewizards.cloudstore.core.Uid;
import co.codewizards.cloudstore.core.config.Config;
import co.codewizards.cloudstore.core.config.ConfigDir;
import co.codewizards.cloudstore.core.oio.File;
import co.codewizards.cloudstore.core.util.IOUtil;

public class UserRegistryTest {

	@BeforeClass
	public static void beforeClass() {
		final Uid jvmInstanceId = new Uid(); // for parallel test execution ;-)
		System.setProperty(ConfigDir.SYSTEM_PROPERTY_CONFIG_DIR, "build/" + jvmInstanceId + "/.cloudstore");
		System.setProperty(Config.SYSTEM_PROPERTY_PREFIX + GnuPgDir.CONFIG_KEY_GNU_PG_DIR, "build/" + jvmInstanceId + "/.gnupg");
		createFile("build/" + jvmInstanceId).mkdir();
	}

	@Before
	public void before() throws Exception {
		createFile(ConfigDir.getInstance().getFile(), UserRegistry.USER_REGISTRY_FILE_NAME).delete();
		initPgp();
	}

	@After
	public void after() throws Exception {
		PgpRegistry.getInstance().clearCache();
	}

	@Test
	public void initUserRegistryFromGpgKeys() throws Exception {
		final UserRegistry userRegistry = new UserRegistryImpl();
		final Collection<User> users = userRegistry.getUsers();
		assertThat(users).isNotEmpty();

		User marcoAtCodeWizards = null;
		User marcoAtNightLabs = null;

		for (final User user : users) {
			if (user.getEmails().contains("<EMAIL>"))
				marcoAtCodeWizards = user;
			else if (user.getEmails().contains("<EMAIL>"))
				marcoAtNightLabs = user;
		}

		assertThat(marcoAtCodeWizards).isNotNull();
		assertThat(marcoAtNightLabs).isNotNull();

		marcoAtCodeWizards.createUserRepoKey(UUID.randomUUID());
	}

	@Test
	public void testGpgKeyTrustLevels() throws Exception {
		final UserRegistry userRegistry = new UserRegistryImpl();
		final Pgp pgp = PgpRegistry.getInstance().getPgpOrFail();

		final Map<String, PgpKeyValidity> email2ExpectedPgpKeyTrustLevel = new HashMap<String, PgpKeyValidity>();
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.ULTIMATE);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.FULL);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.FULL);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.NOT_TRUSTED);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.FULL); // PgpKeyValidity.NOT_TRUSTED);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.EXPIRED);

		User marco = userRegistry.getUsersByEmail("<EMAIL>").iterator().next();
		pgp.setOwnerTrust(marco.getPgpKeys().iterator().next(), PgpOwnerTrust.ULTIMATE);
		pgp.updateTrustDb();

		for (final Map.Entry<String, PgpKeyValidity> me : email2ExpectedPgpKeyTrustLevel.entrySet()) {
			final String email = me.getKey();<commit_msg>Fixed test which failed due to expired PGP-key.<commit_after>package org.subshare.core.user;

import static co.codewizards.cloudstore.core.oio.OioFileFactory.*;
import static org.assertj.core.api.Assertions.*;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.subshare.core.gpg.GnuPgTest;
import org.subshare.core.pgp.Pgp;
import org.subshare.core.pgp.PgpAuthenticationCallback;
import org.subshare.core.pgp.PgpKey;
import org.subshare.core.pgp.PgpKeyId;
import org.subshare.core.pgp.PgpKeyValidity;
import org.subshare.core.pgp.PgpOwnerTrust;
import org.subshare.core.pgp.PgpRegistry;
import org.subshare.core.pgp.gnupg.GnuPgDir;

import co.codewizards.cloudstore.core.Uid;
import co.codewizards.cloudstore.core.config.Config;
import co.codewizards.cloudstore.core.config.ConfigDir;
import co.codewizards.cloudstore.core.oio.File;
import co.codewizards.cloudstore.core.util.IOUtil;

public class UserRegistryTest {

	@BeforeClass
	public static void beforeClass() {
		final Uid jvmInstanceId = new Uid(); // for parallel test execution ;-)
		System.setProperty(ConfigDir.SYSTEM_PROPERTY_CONFIG_DIR, "build/" + jvmInstanceId + "/.cloudstore");
		System.setProperty(Config.SYSTEM_PROPERTY_PREFIX + GnuPgDir.CONFIG_KEY_GNU_PG_DIR, "build/" + jvmInstanceId + "/.gnupg");
		createFile("build/" + jvmInstanceId).mkdir();
	}

	@Before
	public void before() throws Exception {
		createFile(ConfigDir.getInstance().getFile(), UserRegistry.USER_REGISTRY_FILE_NAME).delete();
		initPgp();
	}

	@After
	public void after() throws Exception {
		PgpRegistry.getInstance().clearCache();
	}

	@Test
	public void initUserRegistryFromGpgKeys() throws Exception {
		final UserRegistry userRegistry = new UserRegistryImpl();
		final Collection<User> users = userRegistry.getUsers();
		assertThat(users).isNotEmpty();

		User marcoAtCodeWizards = null;
		User marcoAtNightLabs = null;

		for (final User user : users) {
			if (user.getEmails().contains("<EMAIL>"))
				marcoAtCodeWizards = user;
			else if (user.getEmails().contains("<EMAIL>"))
				marcoAtNightLabs = user;
		}

		assertThat(marcoAtCodeWizards).isNotNull();
		assertThat(marcoAtNightLabs).isNotNull();

		marcoAtCodeWizards.createUserRepoKey(UUID.randomUUID());
	}

	@Test
	public void testGpgKeyTrustLevels() throws Exception {
		final UserRegistry userRegistry = new UserRegistryImpl();
		final Pgp pgp = PgpRegistry.getInstance().getPgpOrFail();

		final Map<String, PgpKeyValidity> email2ExpectedPgpKeyTrustLevel = new HashMap<String, PgpKeyValidity>();
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.ULTIMATE);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.FULL);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.FULL);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.EXPIRED); // PgpKeyValidity.NOT_TRUSTED);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.FULL); // PgpKeyValidity.NOT_TRUSTED);
		email2ExpectedPgpKeyTrustLevel.put("<EMAIL>", PgpKeyValidity.EXPIRED);

		User marco = userRegistry.getUsersByEmail("<EMAIL>").iterator().next();
		pgp.setOwnerTrust(marco.getPgpKeys().iterator().next(), PgpOwnerTrust.ULTIMATE);
		pgp.updateTrustDb();

		for (final Map.Entry<String, PgpKeyValidity> me : email2ExpectedPgpKeyTrustLevel.entrySet()) {
			final String email = me.getKey();
{'fraction_non_alphanumeric': 0.12582686223756112, 'fraction_numerical': 0.0023008340523439745, 'mean_word_length': 6.060913705583756, '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': '5329958', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2330, 'n_words': 383}