tests: Simplify blockRead and block

The same pattern was repeated in many places. Centralize it and reuse
from there.

Change-Id: I3731876805271e9247a36b06b860915344b9044b
This commit is contained in:
Nasser Grainawi 2015-11-10 12:54:52 -08:00
parent 5e2585f6a1
commit 5f889d3101
8 changed files with 23 additions and 30 deletions

View File

@ -17,7 +17,6 @@ package com.google.gerrit.acceptance;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.acceptance.GitUtil.initSsh;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.gerrit.server.project.Util.block;
import static com.google.gerrit.testutil.GerritServerTests.isNoteDbTestEnabled;
import com.google.common.base.Function;
@ -542,6 +541,14 @@ public abstract class AbstractDaemonTest {
saveProjectConfig(project, cfg);
}
protected PermissionRule block(String permission, AccountGroup.UUID id, String ref)
throws Exception {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
PermissionRule rule = Util.block(cfg, permission, id, ref);
saveProjectConfig(project, cfg);
return rule;
}
protected void saveProjectConfig(Project.NameKey p, ProjectConfig cfg)
throws Exception {
MetaDataUpdate md = metaDataUpdateFactory.create(p);
@ -574,16 +581,14 @@ public abstract class AbstractDaemonTest {
projectCache.evict(config.getProject());
}
protected void blockRead(Project.NameKey project, String ref) throws Exception {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
block(cfg, Permission.READ, REGISTERED_USERS, ref);
saveProjectConfig(project, cfg);
protected void blockRead(String ref) throws Exception {
block(Permission.READ, REGISTERED_USERS, ref);
}
protected void blockForgeCommitter(Project.NameKey project, String ref)
throws Exception {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
block(cfg, Permission.FORGE_COMMITTER, REGISTERED_USERS, ref);
Util.block(cfg, Permission.FORGE_COMMITTER, REGISTERED_USERS, ref);
saveProjectConfig(project, cfg);
}

View File

@ -15,13 +15,11 @@
package com.google.gerrit.acceptance.git;
import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS;
import static com.google.gerrit.server.project.Util.block;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.server.git.ProjectConfig;
import org.junit.Before;
import org.junit.Test;
@ -31,9 +29,7 @@ public class DraftChangeBlockedIT extends AbstractDaemonTest {
@Before
public void setUp() throws Exception {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
block(cfg, Permission.PUSH, ANONYMOUS_USERS, "refs/drafts/*");
saveProjectConfig(project, cfg);
block(Permission.PUSH, ANONYMOUS_USERS, "refs/drafts/*");
}
@Test

View File

@ -30,7 +30,7 @@ public class IndexChangeIT extends AbstractDaemonTest {
@Test
public void indexChangeOnNonVisibleBranch() throws Exception {
String changeId = createChange().getChangeId();
blockRead(project, "refs/heads/master");
blockRead("refs/heads/master");
userSession
.post("/changes/" + changeId + "/index/")
.assertNotFound();

View File

@ -17,7 +17,6 @@ package com.google.gerrit.acceptance.rest.project;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.gerrit.server.project.Util.block;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
@ -29,7 +28,6 @@ import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.server.git.ProjectConfig;
import org.eclipse.jgit.lib.Constants;
import org.junit.Before;
@ -84,9 +82,7 @@ public class CreateBranchIT extends AbstractDaemonTest {
}
private void blockCreateReference() throws Exception {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
block(cfg, Permission.CREATE, ANONYMOUS_USERS, "refs/*");
saveProjectConfig(project, cfg);
block(Permission.CREATE, ANONYMOUS_USERS, "refs/*");
}
private void grantOwner() throws Exception {

View File

@ -16,7 +16,6 @@ package com.google.gerrit.acceptance.rest.project;
import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.gerrit.server.project.Util.block;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
@ -26,7 +25,6 @@ import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.server.git.ProjectConfig;
import org.junit.Before;
import org.junit.Test;
@ -76,9 +74,7 @@ public class DeleteBranchIT extends AbstractDaemonTest {
}
private void blockForcePush() throws Exception {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
block(cfg, Permission.PUSH, ANONYMOUS_USERS, "refs/heads/*").setForce(true);
saveProjectConfig(project, cfg);
block(Permission.PUSH, ANONYMOUS_USERS, "refs/heads/*").setForce(true);
}
private void grantOwner() throws Exception {

View File

@ -39,7 +39,7 @@ public class GetCommitIT extends AbstractDaemonTest {
@Before
public void setUp() throws Exception {
repo = GitUtil.newTestRepository(repoManager.openRepository(project));
blockRead(project, "refs/*");
blockRead("refs/*");
}
@After

View File

@ -37,7 +37,7 @@ public class ListBranchesIT extends AbstractDaemonTest {
@Test
public void listBranchesOfNonVisibleProject_NotFound() throws Exception {
blockRead(project, "refs/*");
blockRead("refs/*");
setApiUser(user);
exception.expect(ResourceNotFoundException.class);
gApi.projects().name(project.get()).branches().get();
@ -65,7 +65,7 @@ public class ListBranchesIT extends AbstractDaemonTest {
@Test
public void listBranchesSomeHidden() throws Exception {
blockRead(project, "refs/heads/dev");
blockRead("refs/heads/dev");
String master = pushTo("refs/heads/master").getCommit().name();
pushTo("refs/heads/dev");
setApiUser(user);
@ -78,7 +78,7 @@ public class ListBranchesIT extends AbstractDaemonTest {
@Test
public void listBranchesHeadHidden() throws Exception {
blockRead(project, "refs/heads/master");
blockRead("refs/heads/master");
pushTo("refs/heads/master");
String dev = pushTo("refs/heads/dev").getCommit().name();
setApiUser(user);

View File

@ -60,7 +60,7 @@ public class TagsIT extends AbstractDaemonTest {
@Test
public void listTagsOfNonVisibleProject() throws Exception {
blockRead(project, "refs/*");
blockRead("refs/*");
userSession
.get("/projects/" + project.get() + "/tags")
.assertNotFound();
@ -68,7 +68,7 @@ public class TagsIT extends AbstractDaemonTest {
@Test
public void listTagsOfNonVisibleProjectWithApi() throws Exception {
blockRead(project, "refs/*");
blockRead("refs/*");
setApiUser(user);
exception.expect(ResourceNotFoundException.class);
gApi.projects().name(project.get()).tags().get();
@ -76,7 +76,7 @@ public class TagsIT extends AbstractDaemonTest {
@Test
public void getTagOfNonVisibleProjectWithApi() throws Exception {
blockRead(project, "refs/*");
blockRead("refs/*");
exception.expect(ResourceNotFoundException.class);
gApi.projects().name(project.get()).tag("tag").get();
}
@ -211,7 +211,7 @@ public class TagsIT extends AbstractDaemonTest {
assertThat(result.get(1).ref).isEqualTo("refs/tags/" + tag2.name);
assertThat(result.get(1).revision).isEqualTo(r2.getCommitId().getName());
blockRead(project, "refs/heads/hidden");
blockRead("refs/heads/hidden");
result = getTags().get();
assertThat(result).hasSize(1);
assertThat(result.get(0).ref).isEqualTo("refs/tags/" + tag1.name);