package dev.peerat.backend.routes;

import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import dev.peerat.backend.Main;
import dev.peerat.backend.WebClient;
import dev.peerat.framework.utils.json.JsonMap;

@TestInstance(Lifecycle.PER_CLASS)
public class ScoreTests {

	private Thread server;
	private WebClient client;
	
	@BeforeAll
	void init(){
		server = new Thread(new Runnable(){
			@Override
			public void run(){
				try {
					Main.main(null);
				} catch (Exception e) {
					e.printStackTrace();
				};
			}
		});
		server.start();
		client = new WebClient("localhost", 80);
	}
	
	@AfterAll
	void close(){
		server.interrupt();
	}
	
	@Test
	void testOnDeployed(){
		try{
			JsonMap group = new JsonMap();
			group.set("name", "GroupTest");
			group.set("chapter", 2);
			
			client.register("Test1", "Test2", "Test3@Test7.be", "Test4", "Test5", "Test6");
			client.assertResponseCode(200);
			
			client.route("/groupCreate", "POST", group.toString());
			client.assertResponseCode(200);
			
			client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7");
			client.route("/puzzleResponse/14", "POST", WebClient.buildMultiPartData("seven"));
			client.assertResponseCode(200);
			client.sendHeaders.clear();
			
			client.disconnect();
			
			client.auth("JeffCheasey88", "TheoPueDesPieds");
			client.route("/groupJoin", "POST", group.toString());
			client.assertResponseCode(200);
			
			client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7");
			client.route("/puzzleResponse/14", "POST", WebClient.buildMultiPartData("seven"));
			client.assertResponseCode(403);
			client.sendHeaders.clear();
		}catch(Exception e){
			e.printStackTrace();
			fail(e);
		}
	}
}
