package it.com.atlassian.stash.rest.scm.git.lfs;

import com.atlassian.bitbucket.test.GitLfsTestHelper;
import com.jayway.restassured.RestAssured;
import org.junit.Test;

import static com.atlassian.bitbucket.test.DefaultFuncTestData.getRegularUser;
import static com.atlassian.bitbucket.test.DefaultFuncTestData.getRegularUserPassword;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Tests the /git-lfs/admin interface
 */
public class GitLfsAdminResourceTest extends BaseGitLfsRestTest {

    @Test
    public void testInstanceEnabled() {
        GitLfsTestHelper.enable();
        assertTrue(GitLfsTestHelper.isEnabled());

        GitLfsTestHelper.disable();
        assertFalse(GitLfsTestHelper.isEnabled());

        GitLfsTestHelper.enable();
        assertTrue(GitLfsTestHelper.isEnabled());
    }

    @Test
    public void testInstanceEnableWithNoPerms() {
        RestAssured.given()
                .auth()
                    .preemptive()
                    .basic(getRegularUser(), getRegularUserPassword())
                .when()
                    .put(GitLfsTestHelper.getEnabledUrl())
                .then()
                    .statusCode(FORBIDDEN.getStatusCode())
                    .log().ifValidationFails();
    }

    @Test
    public void testInstanceDisableWithNoPerms() {
        RestAssured.given()
                .auth()
                    .preemptive()
                    .basic(getRegularUser(), getRegularUserPassword())
                .when()
                    .delete(GitLfsTestHelper.getEnabledUrl())
                .then()
                    .statusCode(FORBIDDEN.getStatusCode())
                    .log().ifValidationFails();
    }

    @Test
    public void testRepositoryEnabled() {
        GitLfsTestHelper.enable(TEST_PROJECT, TEST_REPO);
        assertTrue(GitLfsTestHelper.isEnabled(TEST_PROJECT, TEST_REPO));

        GitLfsTestHelper.disable(TEST_PROJECT, TEST_REPO);
        assertFalse(GitLfsTestHelper.isEnabled(TEST_PROJECT, TEST_REPO));

        GitLfsTestHelper.enable(TEST_PROJECT, TEST_REPO);
        assertTrue(GitLfsTestHelper.isEnabled(TEST_PROJECT, TEST_REPO));
    }

    @Test
    public void testRepositoryEnableWithNoPerms() {
        RestAssured.given()
                .auth()
                    .preemptive()
                    .basic(getRegularUser(), getRegularUserPassword())
                .when()
                    .put(GitLfsTestHelper.getEnabledUrl(TEST_PROJECT, TEST_REPO))
                .then()
                    .statusCode(FORBIDDEN.getStatusCode())
                    .log().ifValidationFails();
    }

    @Test
    public void testRepositoryDisableWithNoPerms() {
        RestAssured.given()
                .auth()
                    .preemptive()
                    .basic(getRegularUser(), getRegularUserPassword())
                .when()
                    .delete(GitLfsTestHelper.getEnabledUrl(TEST_PROJECT, TEST_REPO))
                .then()
                    .statusCode(FORBIDDEN.getStatusCode())
                    .log().ifValidationFails();
    }

    @Test
    public void testRepositoryIsEnabledWithNoPerms() {
        RestAssured.given()
                .auth()
                    .preemptive()
                    .basic(getRegularUser(), getRegularUserPassword())
                .when()
                    .get(GitLfsTestHelper.getEnabledUrl(TEST_PROJECT, TEST_REPO))
                .then()
                    .statusCode(FORBIDDEN.getStatusCode())
                    .log().ifValidationFails();
    }
}
