// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.acceptance.git;

import static com.google.common.truth.Truth.assertThat;
import static org.eclipse.jgit.lib.Constants.HEAD;

import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.common.data.Permission;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.RefUpdate;
import org.junit.Test;

public class ForcePushIT extends AbstractDaemonTest {

  @Test
  public void forcePushNotAllowed() throws Exception {
    ObjectId initial = repo().exactRef(HEAD).getLeaf().getObjectId();
    PushOneCommit push1 =
        pushFactory.create(db, admin.getIdent(), testRepo, "change1", "a.txt", "content");
    PushOneCommit.Result r1 = push1.to("refs/heads/master");
    r1.assertOkStatus();

    // Reset HEAD to initial so the new change is a non-fast forward
    RefUpdate ru = repo().updateRef(HEAD);
    ru.setNewObjectId(initial);
    assertThat(ru.forceUpdate()).isEqualTo(RefUpdate.Result.FORCED);

    PushOneCommit push2 =
        pushFactory.create(db, admin.getIdent(), testRepo, "change2", "b.txt", "content");
    push2.setForce(true);
    PushOneCommit.Result r2 = push2.to("refs/heads/master");
    r2.assertErrorStatus("non-fast forward");
  }

  @Test
  public void forcePushAllowed() throws Exception {
    ObjectId initial = repo().exactRef(HEAD).getLeaf().getObjectId();
    grant(Permission.PUSH, project, "refs/*", true);
    PushOneCommit push1 =
        pushFactory.create(db, admin.getIdent(), testRepo, "change1", "a.txt", "content");
    PushOneCommit.Result r1 = push1.to("refs/heads/master");
    r1.assertOkStatus();

    // Reset HEAD to initial so the new change is a non-fast forward
    RefUpdate ru = repo().updateRef(HEAD);
    ru.setNewObjectId(initial);
    assertThat(ru.forceUpdate()).isEqualTo(RefUpdate.Result.FORCED);

    PushOneCommit push2 =
        pushFactory.create(db, admin.getIdent(), testRepo, "change2", "b.txt", "content");
    push2.setForce(true);
    PushOneCommit.Result r2 = push2.to("refs/heads/master");
    r2.assertOkStatus();
  }
}

@@ -719,43 +719,260 @@
 tic 
-org.eclipse.jgit.lib.Constants.HEAD
+com.google.gerrit.acceptance.GitUtil.deleteRef;%0Aimport static org.eclipse.jgit.lib.Constants.HEAD;%0Aimport static org.eclipse.jgit.transport.RemoteRefUpdate.Status.OK;%0Aimport static org.eclipse.jgit.transport.RemoteRefUpdate.Status.REJECTED_OTHER_REASON
 ;%0A%0Ai
@@ -1026,16 +1026,61 @@
 onTest;%0A
+import com.google.gerrit.acceptance.NoHttpd;%0A
 import c
@@ -1171,16 +1171,78 @@
 ission;%0A
+import com.google.gerrit.extensions.api.projects.BranchInput;%0A
 import o
@@ -1310,16 +1310,113 @@
 Update;%0A
+import org.eclipse.jgit.transport.PushResult;%0Aimport org.eclipse.jgit.transport.RemoteRefUpdate;%0A
 import o
@@ -1431,16 +1431,25 @@
 .Test;%0A%0A
+@NoHttpd%0A
 public c
@@ -3098,32 +3098,32 @@
 heads/master%22);%0A
-
     r2.assertOkS
@@ -3134,11 +3134,1045 @@
 s();%0A  %7D
+%0A%0A  @Test%0A  public void deleteNotAllowed() throws Exception %7B%0A    assertDeleteRef(REJECTED_OTHER_REASON);%0A  %7D%0A%0A  @Test%0A  public void deleteNotAllowedWithOnlyPushPermission() throws Exception %7B%0A    grant(Permission.PUSH, project, %22refs/*%22, false);%0A    assertDeleteRef(REJECTED_OTHER_REASON);%0A  %7D%0A%0A  @Test%0A  public void deleteAllowedWithForcePushPermission() throws Exception %7B%0A    grant(Permission.PUSH, project, %22refs/*%22, true);%0A    assertDeleteRef(OK);%0A  %7D%0A%0A  @Test%0A  public void deleteAllowedWithDeletePermission() throws Exception %7B%0A    grant(Permission.DELETE, project, %22refs/*%22, true);%0A    assertDeleteRef(OK);%0A  %7D%0A%0A  private void assertDeleteRef(RemoteRefUpdate.Status expectedStatus) throws Exception %7B%0A    BranchInput in = new BranchInput();%0A    in.ref = %22refs/heads/test%22;%0A    gApi.projects().name(project.get()).branch(in.ref).create(in);%0A    PushResult result = deleteRef(testRepo, in.ref);%0A    RemoteRefUpdate refUpdate = result.getRemoteUpdate(in.ref);%0A    assertThat(refUpdate.getStatus()).isEqualTo(expectedStatus);%0A  %7D
 %0A%7D%0A

Add tests for deleting branch by push