<commit_before>/**
 * © <NAME> 2015
 */
package io.spacedog.test.sendpulse;

import java.util.Collections;

import org.joda.time.DateTime;

import io.spacedog.model.SendPulseSettings;
import io.spacedog.rest.SpaceRequest;
import io.spacedog.rest.SpaceTarget;
import io.spacedog.rest.SpaceTest;
import io.spacedog.sdk.SpaceDog;

public class SendPulseResourceTest extends SpaceTest {

	public void testSendPulseResource() {

		// this must come before preparing the test
		// or else the test flag is set
		// and the sendpulse backend files are not reset
		initSendPulseTestWebApp();

		// prepare
		prepareTest();
		SpaceRequest.env().target(SpaceTarget.local);
		SpaceDog test = resetTestBackend();
		SpaceDog fred = signUp("test", "fred", "hi fred");

		// set sendpulse test settings
		SendPulseSettings settings = new SendPulseSettings();
		settings.clientId = SpaceRequest.env().get("spacedog.test.sendpulse.client.id");
		settings.clientSecret = SpaceRequest.env().get("spacedog.test.sendpulse.client.secret");
		settings.authorizedRoles = Collections.singleton("user");
		test.settings().save(settings);

		// fred can get all websites
		SpaceRequest.get("/1/sendpulse/push/websites").auth(fred).go(200);

		// fred can create a web push to sendpulse.www.spacedog.io
		SpaceRequest.post("/1/sendpulse/push/tasks").auth(fred)//
				.formField("title", "Hello")//
				.formField("body", DateTime.now().toString())//
				.go(400)//
				.assertEquals("sendpulse-1255", "error.code");

		// fred can create a web push to sendpulse.www.spacedog.io
		SpaceRequest.post("/1/sendpulse/push/tasks").auth(fred)//
				.formField("title", "Hello")//
				.formField("body", DateTime.now().toString())//
				.formField("website_id", "22573")//
				.formField("ttl", "300")// 5 minutes
				.go(200);

		// settings forbid admins to get all websites nor create push tasks
		SpaceRequest.get("/1/sendpulse/push/websites").auth(test).go(403);
		SpaceRequest.post("/1/sendpulse/push/tasks").auth(test).go(403);
	}

	private void initSendPulseTestWebApp() {

		// prepare
		SpaceRequest.env().target(SpaceTarget.production);

		// if sendpulde test web app already initialized then return
		if (SpaceRequest.get("/1/file/www/index.html").backend("sendpulse")//
				.go(200, 404).status() == 200)
			return;

		SpaceDog sendpulse = resetBackend("sendpulse", "sendpulse", "hi sendpulse");

		// upload web site
		upload(sendpulse, "index.html");
		upload(sendpulse, "sp-push-manifest.json");
		upload(sendpulse, "sp-push-worker.js");
	}

	private void upload(SpaceDog backend, String name) {
		SpaceRequest.put("/1/file/www/" + name)//
		.bodyResource(this.getClass(), name).auth(backend)//
				.go(200);
	}
}
<commit_msg>Refactors and fixes SendPulseResourceTest<commit_after>/**
 * © <NAME> 2015
 */
package io.spacedog.test.sendpulse;

import java.util.Collections;

import org.joda.time.DateTime;

import io.spacedog.model.SendPulseSettings;
import io.spacedog.rest.SpaceEnv;
import io.spacedog.rest.SpaceRequestException;
import io.spacedog.rest.SpaceTest;
import io.spacedog.sdk.SpaceDog;
import io.spacedog.utils.Utils;

public class SendPulseResourceTest extends SpaceTest {

	// @Test
	public void testSendPulseResource() {

		// prepare
		prepareTest();

		// this must come before preparing the test
		// or else the test flag is set
		// and the sendpulse backend files are not reset
		initSendPulseTestWebApp();

		// reset test backend
		SpaceDog superadmin = resetTestBackend();
		SpaceDog fred = signUp("test", "fred", "hi fred");
		SpaceDog vince = signUp("test", "vince", "hi vince");
		superadmin.credentials().setRole(fred.id(), "pusher");

		// set sendpulse test settings
		SendPulseSettings settings = new SendPulseSettings();
		settings.clientId = SpaceEnv.defaultEnv()//
				.get("spacedog.test.sendpulse.client.id");
		settings.clientSecret = SpaceEnv.defaultEnv()//
				.get("spacedog.test.sendpulse.client.secret");
		settings.authorizedRoles = Collections.singleton("pusher");
		superadmin.settings().save(settings);

		// fred can get all websites
		fred.get("/1/sendpulse/push/websites").go(200);

		// fred fails to web push to sendpulse.www.spacedog.io
		// since no website_id parameter
		fred.post("/1/sendpulse/push/tasks")//
				.formField("title", "Hello")//
				.formField("body", DateTime.now().toString())//
				.go(400)//
				.assertEquals("sendpulse-1255", "error.code");

		// fred web pushes to sendpulse.www.spacedog.io
		fred.post("/1/sendpulse/push/tasks")//
				.formField("title", "Hello")//
				.formField("body", DateTime.now().toString())//
				.formField("website_id", "22573")//
				.formField("ttl", "300")// 5 minutes
				.go(200);

		// vince fails to get all websites nor push tasks
		// since he hasn't got the 'pusher' role
		vince.get("/1/sendpulse/push/websites").go(403);
		vince.post("/1/sendpulse/push/tasks").go(403);
	}

	private void initSendPulseTestWebApp() {

		// prepare
		String password = SpaceEnv.defaultEnv()//
				.get("spacedog.test.sendpulse.backend.password");
		SpaceDog sendpulse = SpaceDog.backend("https://sendpulse.spacedog.io")//
				.username("sendpulse");

		try {
			sendpulse.login(password);
		} catch (SpaceRequestException e) {
			if (e.httpStatus() == 401) {
				sendpulse.admin().createBackend(//
						"sendpulse", password, "<EMAIL>", false);
				sendpulse.login(password);
			} else
				throw e;
		}

		// upload web site
		upload(sendpulse, "index.html");
		upload(sendpulse, "sp-push-manifest.json");
		upload(sendpulse, "sp-push-worker.js");
	}

	private void upload(SpaceDog dog, String name) {
		dog.file().save("/www/" + name, Utils.readResource(this.getClass(), name));
	}
}

{'fraction_non_alphanumeric': 0.1598729486500794, 'fraction_numerical': 0.01605787894829716, 'mean_word_length': 3.945898778359511, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 7, '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': '984025', 'n_tokens_mistral': 2153, 'n_tokens_neox': 1839, 'n_words': 383}