package ca.corefacility.bioinformatics.irida.web.controller.test.integration.user;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.junit.Test;

import static com.jayway.restassured.RestAssured.expect;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;

/**
 * Integration tests for users.
 * 
 * @author Franklin Bristow <franklin.bristow@phac-aspc.gc.ca>
 */
public class UsersIntegrationTest {

	@Test
	public void testGetAllUsers() {
		expect().body("resource.links.rel", hasItems("self", "collection/pages/first")).and()
				.body("resource.resources.username", hasItem("fbristow")).when().get("/users/all");
	}

	@Test
	public void testGetCurrentUser() {
		expect().body("resource.links.rel", hasItems("self", "user/projects")).and()
				.body("resource.username", is("fbristow")).when().get("/users/current");
	}

	@Test
	public void testCreateUserFail() {
		// doesn't matter what the user is, we should fail here when trying to
		// create a user because the current user doesn't have permission to
		// create users.
		given().body(createUser()).expect().response().statusCode(HttpStatus.SC_FORBIDDEN).when().post("/users");
	}

	@Test
	public void testCreateUserSucceed() {
		given().body(createUser()).and().auth().basic("admin", "password1").expect().response()
				.statusCode(HttpStatus.SC_CREATED).when().post("/users");
	}

	@Test
	public void testUpdateOtherAccountFail() {
		given().body(createUser()).expect().response().statusCode(HttpStatus.SC_FORBIDDEN).when().patch("/users/2");
	}

	private Map<String, String> createUser() {
		Map<String, String> user = new HashMap<>();
		user.put("username", "franklin");
		user.put("password", "Password1");
		user.put("email", "fbristow@phac-aspc.gc.ca");
		user.put("firstName", "Franklin");
		user.put("lastName", "Bristow");
		user.put("phoneNumber", "7029");
		return user;
	}
}

@@ -88,109 +88,112 @@
 ort 
-java.util.HashMap;%0Aimport java.util.Map;%0A%0Aimport org.apache.http.HttpStatus;%0Aimport org.junit.Tes
+static com.jayway.restassured.RestAssured.expect;%0Aimport static com.jayway.restassured.RestAssured.ge
 t;%0A
-%0A
 impo
@@ -229,38 +229,37 @@
 red.RestAssured.
-expect
+given
 ;%0Aimport static 
@@ -281,33 +281,39 @@
 assured.
-RestAssured.given
+path.json.JsonPath.from
 ;%0Aimport
@@ -450,16 +450,124 @@
 rs.is;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0Aimport org.apache.http.HttpStatus;%0Aimport org.junit.Test;%0A%0A
 /**%0A * I
@@ -1811,16 +1811,381 @@
 %22);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testUpdateOwnAccountSucceed() %7B%0A%09%09// figure out what the uri is for the current user%0A%09%09String responseBody = get(%22/users/current%22).asString();%0A%09%09String location = from(responseBody).getString(%22resource.links.find%7Bit.rel == 'self'%7D.href%22);%0A%09%09given().body(createUser()).expect().response().statusCode(HttpStatus.SC_OK).when().patch(location);%0A%09%7D%0A%0A
 %09private

Test that a user can modify their own account.