package it.com.atlassian.stash.rest;

import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.bitbucket.test.RepositoryTestHelper;
import com.atlassian.bitbucket.permission.Permission;
import com.jayway.restassured.RestAssured;
import it.com.atlassian.stash.BaseRestTest;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static javax.ws.rs.core.Response.Status.OK;
import static org.hamcrest.Matchers.equalTo;

/**
 * Basic smoke test to check basic REST operations on personal project repos that are rewritten by Tuckey
 */
public class PersonalReposTest extends BaseRestTest {

    public static final String USERNAME = "PersonalReposTest";

    @Before
    public void setUp() throws Exception {
        testContext.user(USERNAME, USERNAME, USERNAME, "user@resource.test")
                   .globalPermission(USERNAME, Permission.ADMIN);
    }

    @Test
    public void testGetPersonalRepos() throws Exception {
        RestAssured
            .given()
            .auth().preemptive().basic(USERNAME, USERNAME)
            .expect()
            .statusCode(OK.getStatusCode())
            .body("size", equalTo(0))
            .body("isLastPage", equalTo(true))
            .body("start", equalTo(0))
            .when().get(DefaultFuncTestData.getRestURL() + "/users/" + USERNAME + "/repos");
    }

    @Test
    @Ignore
    public void testCreatePersonalRepo() throws Exception {
        try {
            RepositoryTestHelper.createPersonalRepositoryAndWait(USERNAME, DefaultFuncTestData.getProject1Repository1(), 2500L);
        } finally {
            RepositoryTestHelper.deleteRepository("~" + USERNAME, DefaultFuncTestData.getProject1Repository1());
        }
    }
}
