<commit_before>package io.spacedog.watchdog;

import java.util.Arrays;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.collect.Sets;
import com.google.common.io.Resources;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import io.spacedog.client.SpaceDogHelper;
import io.spacedog.client.SpaceDogHelper.Backend;
import io.spacedog.client.SpaceDogHelper.User;
import io.spacedog.client.SpaceRequest;
import io.spacedog.utils.Json;
import io.spacedog.watchdog.SpaceSuite.TestOncePerDay;

@TestOncePerDay
public class ShareResourceTest {

	private static final String FILE_CONTENT = "This is a test file!";

	@Test
	public void shareListAndGetFiles() throws Exception {

		// prepare
		Backend testBackend = SpaceDogHelper.resetTestBackend();
		User vince = SpaceDogHelper.createUser(testBackend, "vince", "hi vince", "<EMAIL>");
		User fred = SpaceDogHelper.createUser(testBackend, "fred", "hi fred", "<EMAIL>");

		// only admin can get all shared locations
		SpaceRequest.get("/1/share").go(401);
		SpaceRequest.get("/1/share").backend(testBackend).go(401);
		SpaceRequest.get("/1/share").userAuth(vince).go(401);

		// this account is brand new, no shared files
		SpaceRequest.get("/1/share").adminAuth(testBackend).go(200)//
				.assertSizeEquals(0, "results");

		// only users and admins are allowed to share files
		SpaceRequest.put("/1/share/tweeter.png").go(401);
		SpaceRequest.put("/1/share/tweeter.png").backend(testBackend).go(401);

		// share a small png file
		byte[] pngBytes = Resources.toByteArray(//
				Resources.getResource("io/spacedog/watchdog/tweeter.png"));

		JsonNode json = SpaceRequest.put("/1/share/tweeter.png")//
				.backend(testBackend)//
				.userAuth(vince)//
				.body(pngBytes)//
				.go(200).jsonNode();

		String pngPath = json.get("path").asText();
		String pngLocation = json.get("location").asText();
		String pngS3Location = json.get("s3").asText();

		// list all shared files should return tweeter.png path only
		SpaceRequest.get("/1/share").adminAuth(testBackend).go(200)//
				.assertSizeEquals(1, "results")//
				.assertEquals(pngPath, "results.0.path");

		// Anonymous are allowed to access shared files if they get the location
		// but they need to provide the backend key until the backendId is part
		// of the host
		SpaceRequest.get(pngLocation).go(401);

		// download shared png file
		byte[] downloadedBytes = SpaceRequest.get(pngLocation)//
				.backend(testBackend)//
				.go(200)//
				// .assertHeaderEquals("image/png", "content-type")//
				.assertHeaderEquals("vince", "x-amz-meta-owner")//
				.assertHeaderEquals("USER", "x-amz-meta-owner-type")//
				.bytes();

		Assert.assertTrue(Arrays.equals(pngBytes, downloadedBytes));

		// download shared png file through S3 direct access

		downloadedBytes = SpaceRequest.get(pngS3Location).go(200)//
				// .assertHeaderEquals("image/png", "content-type")//<commit_msg>Fixes user schema init and shared file anonymous access<commit_after>package io.spacedog.watchdog;

import java.util.Arrays;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.collect.Sets;
import com.google.common.io.Resources;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import io.spacedog.client.SpaceDogHelper;
import io.spacedog.client.SpaceDogHelper.Backend;
import io.spacedog.client.SpaceDogHelper.User;
import io.spacedog.client.SpaceRequest;
import io.spacedog.utils.Json;
import io.spacedog.watchdog.SpaceSuite.TestOncePerDay;

@TestOncePerDay
public class ShareResourceTest {

	private static final String FILE_CONTENT = "This is a test file!";

	@Test
	public void shareListAndGetFiles() throws Exception {

		// prepare
		Backend testBackend = SpaceDogHelper.resetTestBackend();
		SpaceDogHelper.initUserDefaultSchema(testBackend);
		User vince = SpaceDogHelper.createUser(testBackend, "vince", "hi vince", "<EMAIL>");
		User fred = SpaceDogHelper.createUser(testBackend, "fred", "hi fred", "<EMAIL>");

		// only admin can get all shared locations
		SpaceRequest.get("/1/share").go(401);
		SpaceRequest.get("/1/share").backend(testBackend).go(401);
		SpaceRequest.get("/1/share").userAuth(vince).go(401);

		// this account is brand new, no shared files
		SpaceRequest.get("/1/share").adminAuth(testBackend).go(200)//
				.assertSizeEquals(0, "results");

		// only users and admins are allowed to share files
		SpaceRequest.put("/1/share/tweeter.png").go(401);
		SpaceRequest.put("/1/share/tweeter.png").backend(testBackend).go(401);

		// share a small png file
		byte[] pngBytes = Resources.toByteArray(//
				Resources.getResource("io/spacedog/watchdog/tweeter.png"));

		JsonNode json = SpaceRequest.put("/1/share/tweeter.png")//
				.backend(testBackend)//
				.userAuth(vince)//
				.body(pngBytes)//
				.go(200).jsonNode();

		String pngPath = json.get("path").asText();
		String pngLocation = json.get("location").asText();
		String pngS3Location = json.get("s3").asText();

		// list all shared files should return tweeter.png path only
		SpaceRequest.get("/1/share").adminAuth(testBackend).go(200)//
				.assertSizeEquals(1, "results")//
				.assertEquals(pngPath, "results.0.path");

		// Anonymous are allowed to access shared files if they get the location
		byte[] downloadedBytes = SpaceRequest.get(pngLocation).go(200)//
				// .assertHeaderEquals("image/png", "content-type")//
				.assertHeaderEquals("vince", "x-amz-meta-owner")//
				.assertHeaderEquals("USER", "x-amz-meta-owner-type")//
				.bytes();

		Assert.assertTrue(Arrays.equals(pngBytes, downloadedBytes));

		// download shared png file through S3 direct access

		downloadedBytes = SpaceRequest.get(pngS3Location).go(200)//
				// .assertHeaderEquals("image/png", "content-type")//
{'fraction_non_alphanumeric': 0.1462623641965299, 'fraction_numerical': 0.015080265931571267, 'mean_word_length': 4.482666666666667, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4740666', 'n_tokens_mistral': 2219, 'n_tokens_neox': 1984, 'n_words': 393}