// Copyright (C) 2017 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.server.project;

import static com.google.common.truth.Truth.assertThat;

import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.UseLocalDisk;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.api.projects.BranchApi;
import com.google.gerrit.extensions.api.projects.ReflogEntryInfo;
import java.util.List;
import org.junit.Test;

@UseLocalDisk
public class ReflogIT extends AbstractDaemonTest {
  @Test
  public void reflogUpdatedBySubmittingChange() throws Exception {
    BranchApi branchApi = gApi.projects().name(project.get()).branch("master");
    List<ReflogEntryInfo> reflog = branchApi.reflog();
    assertThat(reflog).isNotEmpty();

    // Current number of entries in the reflog
    int refLogLen = reflog.size();

    // Create and submit a change
    PushOneCommit.Result r = createChange();
    String changeId = r.getChangeId();
    String revision = r.getCommit().name();
    ReviewInput in = ReviewInput.approve();
    gApi.changes().id(changeId).revision(revision).review(in);
    gApi.changes().id(changeId).revision(revision).submit();

    // Submitting the change causes a new entry in the reflog
    reflog = branchApi.reflog();
    assertThat(reflog).hasSize(refLogLen + 1);
  }
}

@@ -870,16 +870,65 @@
 alDisk;%0A
+import com.google.gerrit.common.data.Permission;%0A
 import c
@@ -980,16 +980,73 @@
 wInput;%0A
+import com.google.gerrit.extensions.api.groups.GroupApi;%0A
 import c
@@ -1163,16 +1163,227 @@
 ryInfo;%0A
+import com.google.gerrit.extensions.restapi.AuthException;%0Aimport com.google.gerrit.reviewdb.client.AccountGroup;%0Aimport com.google.gerrit.server.git.ProjectConfig;%0Aimport com.google.gerrit.server.project.Util;%0A
 import j
@@ -1425,22 +1425,8 @@
 t;%0A%0A
-@UseLocalDisk%0A
 publ
@@ -1480,16 +1480,32 @@
   @Test%0A
+  @UseLocalDisk%0A
   public
@@ -2295,11 +2295,908 @@
  1);%0A  %7D
+%0A%0A  @Test%0A  @UseLocalDisk%0A  public void regularUserIsNotAllowedToGetReflog() throws Exception %7B%0A    setApiUser(user);%0A    exception.expect(AuthException.class);%0A    gApi.projects().name(project.get()).branch(%22master%22).reflog();%0A  %7D%0A%0A  @Test%0A  @UseLocalDisk%0A  public void ownerUserIsAllowedToGetReflog() throws Exception %7B%0A    GroupApi groupApi = gApi.groups().create(name(%22get-reflog%22));%0A    groupApi.addMembers(%22user%22);%0A%0A    ProjectConfig cfg = projectCache.checkedGet(project).getConfig();%0A    Util.allow(cfg, Permission.OWNER, new AccountGroup.UUID(groupApi.get().id), %22refs/*%22);%0A    saveProjectConfig(project, cfg);%0A%0A    setApiUser(user);%0A    gApi.projects().name(project.get()).branch(%22master%22).reflog();%0A  %7D%0A%0A  @Test%0A  @UseLocalDisk%0A  public void adminUserIsAllowedToGetReflog() throws Exception %7B%0A    setApiUser(admin);%0A    gApi.projects().name(project.get()).branch(%22master%22).reflog();%0A  %7D
 %0A%7D%0A

Add test coverage for reflog permissions