Hoist allow method into AbstractDaemonTest

Change-Id: I4a53a272d8c6d96f5cf40f42f429e52615bd166f
This commit is contained in:
Dave Borowitz
2014-08-05 10:14:03 -07:00
parent 80915206e7
commit 2f0d2664d2
8 changed files with 59 additions and 132 deletions

View File

@@ -27,11 +27,16 @@ import com.google.gerrit.extensions.api.changes.RevisionApi;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.ListChangesOption;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.OutputFormat;
import com.google.gerrit.server.change.ChangeJson;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.Util;
import com.google.gerrit.testutil.ConfigSuite;
import com.google.gson.Gson;
import com.google.gwtorm.server.SchemaFactory;
@@ -77,6 +82,12 @@ public abstract class AbstractDaemonTest {
@Inject
protected PushOneCommit.Factory pushFactory;
@Inject
protected MetaDataUpdate.Server metaDataUpdateFactory;
@Inject
protected ProjectCache projectCache;
protected Git git;
protected GerritServer server;
protected TestAccount admin;
@@ -218,4 +229,22 @@ public abstract class AbstractDaemonTest {
.id(r.getChangeId())
.current();
}
protected void allow(String permission, AccountGroup.UUID id, String ref)
throws Exception {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
Util.allow(cfg, permission, id, ref);
saveProjectConfig(project, cfg);
}
protected void saveProjectConfig(Project.NameKey p, ProjectConfig cfg)
throws Exception {
MetaDataUpdate md = metaDataUpdateFactory.create(p);
try {
cfg.commit(md);
} finally {
md.close();
}
projectCache.evict(cfg.getProject());
}
}

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.acceptance.rest.config;
import static com.google.gerrit.server.config.PostCaches.Operation.FLUSH;
import static com.google.gerrit.server.config.PostCaches.Operation.FLUSH_ALL;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.gerrit.server.project.Util.allow;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -26,13 +25,14 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.config.ListCaches.CacheInfo;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.config.ListCaches.CacheInfo;
import com.google.gerrit.server.config.PostCaches;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.Util;
import com.google.inject.Inject;
import org.apache.http.HttpStatus;
@@ -140,8 +140,8 @@ public class CacheOperationsIT extends AbstractDaemonTest {
ProjectConfig cfg = projectCache.checkedGet(allProjects).getConfig();
AccountGroup.UUID registeredUsers =
SystemGroupBackend.getGroup(REGISTERED_USERS).getUUID();
allow(cfg, GlobalCapability.VIEW_CACHES, registeredUsers);
allow(cfg, GlobalCapability.FLUSH_CACHES, registeredUsers);
Util.allow(cfg, GlobalCapability.VIEW_CACHES, registeredUsers);
Util.allow(cfg, GlobalCapability.FLUSH_CACHES, registeredUsers);
saveProjectConfig(cfg);
RestResponse r = userSession.post("/config/server/caches/",

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.acceptance.rest.config;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.gerrit.server.project.Util.allow;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -30,6 +29,7 @@ import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.Util;
import com.google.inject.Inject;
import org.apache.http.HttpStatus;
@@ -92,8 +92,8 @@ public class FlushCacheIT extends AbstractDaemonTest {
ProjectConfig cfg = projectCache.checkedGet(allProjects).getConfig();
AccountGroup.UUID registeredUsers =
SystemGroupBackend.getGroup(REGISTERED_USERS).getUUID();
allow(cfg, GlobalCapability.VIEW_CACHES, registeredUsers);
allow(cfg, GlobalCapability.FLUSH_CACHES, registeredUsers);
Util.allow(cfg, GlobalCapability.VIEW_CACHES, registeredUsers);
Util.allow(cfg, GlobalCapability.FLUSH_CACHES, registeredUsers);
saveProjectConfig(cfg);
RestResponse r = userSession.post("/config/server/caches/accounts/flush");

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.allow;
import static com.google.gerrit.server.project.Util.block;
import static org.junit.Assert.assertEquals;
@@ -24,27 +23,15 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ProjectCache;
import com.google.inject.Inject;
import org.apache.http.HttpStatus;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class CreateBranchIT extends AbstractDaemonTest {
@Inject
private MetaDataUpdate.Server metaDataUpdateFactory;
@Inject
private ProjectCache projectCache;
@Inject
private AllProjectsName allProjects;
@@ -56,7 +43,7 @@ public class CreateBranchIT extends AbstractDaemonTest {
}
@Test
public void createBranch_Forbidden() throws IOException {
public void createBranch_Forbidden() throws Exception {
RestResponse r =
userSession.put("/projects/" + project.get()
+ "/branches/" + branch.getShortName());
@@ -64,7 +51,7 @@ public class CreateBranchIT extends AbstractDaemonTest {
}
@Test
public void createBranchByAdmin() throws IOException {
public void createBranchByAdmin() throws Exception {
RestResponse r =
adminSession.put("/projects/" + project.get()
+ "/branches/" + branch.getShortName());
@@ -77,7 +64,7 @@ public class CreateBranchIT extends AbstractDaemonTest {
}
@Test
public void branchAlreadyExists_Conflict() throws IOException {
public void branchAlreadyExists_Conflict() throws Exception {
RestResponse r =
adminSession.put("/projects/" + project.get()
+ "/branches/" + branch.getShortName());
@@ -90,8 +77,7 @@ public class CreateBranchIT extends AbstractDaemonTest {
}
@Test
public void createBranchByProjectOwner() throws IOException,
ConfigInvalidException {
public void createBranchByProjectOwner() throws Exception {
grantOwner();
RestResponse r =
@@ -106,8 +92,7 @@ public class CreateBranchIT extends AbstractDaemonTest {
}
@Test
public void createBranchByAdminCreateReferenceBlocked() throws IOException,
ConfigInvalidException {
public void createBranchByAdminCreateReferenceBlocked() throws Exception {
blockCreateReference();
RestResponse r =
adminSession.put("/projects/" + project.get()
@@ -122,7 +107,7 @@ public class CreateBranchIT extends AbstractDaemonTest {
@Test
public void createBranchByProjectOwnerCreateReferenceBlocked_Forbidden()
throws IOException, ConfigInvalidException {
throws Exception {
grantOwner();
blockCreateReference();
RestResponse r =
@@ -131,27 +116,13 @@ public class CreateBranchIT extends AbstractDaemonTest {
assertEquals(HttpStatus.SC_FORBIDDEN, r.getStatusCode());
}
private void blockCreateReference() throws IOException, ConfigInvalidException {
private void blockCreateReference() throws Exception {
ProjectConfig cfg = projectCache.checkedGet(allProjects).getConfig();
block(cfg, Permission.CREATE, ANONYMOUS_USERS, "refs/*");
saveProjectConfig(allProjects, cfg);
projectCache.evict(cfg.getProject());
}
private void grantOwner() throws IOException, ConfigInvalidException {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
allow(cfg, Permission.OWNER, REGISTERED_USERS, "refs/*");
saveProjectConfig(project, cfg);
projectCache.evict(cfg.getProject());
}
private void saveProjectConfig(Project.NameKey p, ProjectConfig cfg)
throws IOException {
MetaDataUpdate md = metaDataUpdateFactory.create(p);
try {
cfg.commit(md);
} finally {
md.close();
}
private void grantOwner() throws Exception {
allow(Permission.OWNER, REGISTERED_USERS, "refs/*");
}
}

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.allow;
import static com.google.gerrit.server.project.Util.block;
import static org.junit.Assert.assertEquals;
@@ -24,28 +23,17 @@ import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ProjectCache;
import com.google.inject.Inject;
import org.apache.http.HttpStatus;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class DeleteBranchIT extends AbstractDaemonTest {
@Inject
private MetaDataUpdate.Server metaDataUpdateFactory;
@Inject
private ProjectCache projectCache;
@Inject
private AllProjectsName allProjects;
@@ -82,8 +70,7 @@ public class DeleteBranchIT extends AbstractDaemonTest {
}
@Test
public void deleteBranchByProjectOwner() throws IOException,
ConfigInvalidException {
public void deleteBranchByProjectOwner() throws Exception {
grantOwner();
RestResponse r =
@@ -99,8 +86,7 @@ public class DeleteBranchIT extends AbstractDaemonTest {
}
@Test
public void deleteBranchByAdminForcePushBlocked() throws IOException,
ConfigInvalidException {
public void deleteBranchByAdminForcePushBlocked() throws Exception {
blockForcePush();
RestResponse r =
adminSession.delete("/projects/" + project.get()
@@ -116,7 +102,7 @@ public class DeleteBranchIT extends AbstractDaemonTest {
@Test
public void deleteBranchByProjectOwnerForcePushBlocked_Forbidden()
throws IOException, ConfigInvalidException {
throws Exception {
grantOwner();
blockForcePush();
RestResponse r =
@@ -126,26 +112,13 @@ public class DeleteBranchIT extends AbstractDaemonTest {
r.consume();
}
private void blockForcePush() throws IOException, ConfigInvalidException {
private void blockForcePush() throws Exception {
ProjectConfig cfg = projectCache.checkedGet(allProjects).getConfig();
block(cfg, Permission.PUSH, ANONYMOUS_USERS, "refs/heads/*").setForce(true);
saveProjectConfig(allProjects, cfg);
projectCache.evict(cfg.getProject());
}
private void grantOwner() throws IOException, ConfigInvalidException {
ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
allow(cfg, Permission.OWNER, REGISTERED_USERS, "refs/*");
saveProjectConfig(project, cfg);
projectCache.evict(cfg.getProject());
}
private void saveProjectConfig(Project.NameKey p, ProjectConfig cfg) throws IOException {
MetaDataUpdate md = metaDataUpdateFactory.create(p);
try {
cfg.commit(md);
} finally {
md.close();
}
private void grantOwner() throws Exception {
allow(Permission.OWNER, REGISTERED_USERS, "refs/*");
}
}

View File

@@ -25,10 +25,8 @@ import com.google.gerrit.extensions.common.CommitInfo;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ListBranches.BranchInfo;
import com.google.gerrit.server.project.ProjectCache;
import com.google.inject.Inject;
import org.apache.http.HttpStatus;
@@ -38,16 +36,9 @@ import org.junit.Test;
import java.io.IOException;
public class GetCommitIT extends AbstractDaemonTest {
@Inject
private ProjectCache projectCache;
@Inject
private AllProjectsName allProjects;
@Inject
private MetaDataUpdate.Server metaDataUpdateFactory;
@Test
public void getCommit() throws IOException {
RestResponse r =
@@ -81,7 +72,7 @@ public class GetCommitIT extends AbstractDaemonTest {
}
@Test
public void getNonVisibleCommit_NotFound() throws IOException {
public void getNonVisibleCommit_NotFound() throws Exception {
RestResponse r =
adminSession.get("/projects/" + project.get() + "/branches/"
+ IdString.fromDecoded(RefNames.REFS_CONFIG).encoded());
@@ -92,20 +83,10 @@ public class GetCommitIT extends AbstractDaemonTest {
ProjectConfig cfg = projectCache.checkedGet(allProjects).getConfig();
cfg.getAccessSection("refs/*", false).removePermission(Permission.READ);
saveProjectConfig(cfg);
projectCache.evict(cfg.getProject());
saveProjectConfig(allProjects, cfg);
r = adminSession.get("/projects/" + project.get() + "/commits/"
+ branchInfo.revision);
assertEquals(HttpStatus.SC_NOT_FOUND, r.getStatusCode());
}
private void saveProjectConfig(ProjectConfig cfg) throws IOException {
MetaDataUpdate md = metaDataUpdateFactory.create(allProjects);
try {
cfg.commit(md);
} finally {
md.close();
}
}
}

View File

@@ -26,20 +26,14 @@ import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ListBranches.BranchInfo;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gson.reflect.TypeToken;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.jcraft.jsch.JSchException;
import org.apache.http.HttpStatus;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.junit.Test;
import java.io.IOException;
@@ -47,13 +41,6 @@ import java.util.Collections;
import java.util.List;
public class ListBranchesIT extends AbstractDaemonTest {
@Inject
private MetaDataUpdate.Server metaDataUpdateFactory;
@Inject
private ProjectCache projectCache;
@Test
public void listBranchesOfNonExistingProject_NotFound() throws IOException {
assertEquals(HttpStatus.SC_NOT_FOUND,
@@ -61,8 +48,7 @@ public class ListBranchesIT extends AbstractDaemonTest {
}
@Test
public void listBranchesOfNonVisibleProject_NotFound() throws IOException,
OrmException, JSchException, ConfigInvalidException {
public void listBranchesOfNonVisibleProject_NotFound() throws Exception {
blockRead(project, "refs/*");
assertEquals(HttpStatus.SC_NOT_FOUND,
userSession.get("/projects/" + project.get() + "/branches").getStatusCode());
@@ -106,8 +92,7 @@ public class ListBranchesIT extends AbstractDaemonTest {
}
@Test
public void listBranchesSomeHidden() throws IOException, GitAPIException,
ConfigInvalidException, OrmException, JSchException {
public void listBranchesSomeHidden() throws Exception {
blockRead(project, "refs/heads/dev");
pushTo("refs/heads/master");
String masterCommit = git.getRepository().getRef("master").getTarget().getObjectId().getName();
@@ -123,8 +108,7 @@ public class ListBranchesIT extends AbstractDaemonTest {
}
@Test
public void listBranchesHeadHidden() throws IOException, GitAPIException,
ConfigInvalidException, OrmException, JSchException {
public void listBranchesHeadHidden() throws Exception {
blockRead(project, "refs/heads/master");
pushTo("refs/heads/master");
pushTo("refs/heads/dev");
@@ -139,12 +123,10 @@ public class ListBranchesIT extends AbstractDaemonTest {
return adminSession.get(endpoint);
}
private void blockRead(Project.NameKey project, String ref)
throws RepositoryNotFoundException, IOException, ConfigInvalidException {
private 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);
projectCache.evict(cfg.getProject());
}
private static List<BranchInfo> toBranchInfoList(RestResponse r)
@@ -160,13 +142,4 @@ public class ListBranchesIT extends AbstractDaemonTest {
PushOneCommit push = pushFactory.create(db, admin.getIdent());
return push.to(git, ref);
}
private void saveProjectConfig(Project.NameKey p, ProjectConfig cfg) throws IOException {
MetaDataUpdate md = metaDataUpdateFactory.create(p);
try {
cfg.commit(md);
} finally {
md.close();
}
}
}

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.acceptance.server.project;
import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS;
import static com.google.gerrit.server.project.Util.allow;
import static com.google.gerrit.server.project.Util.category;
import static com.google.gerrit.server.project.Util.value;
import static org.junit.Assert.assertEquals;
@@ -37,6 +36,7 @@ import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.Util;
import com.google.inject.Inject;
import org.junit.Before;
@@ -64,7 +64,7 @@ public class CustomLabelIT extends AbstractDaemonTest {
ProjectConfig cfg = projectCache.checkedGet(allProjects).getConfig();
AccountGroup.UUID anonymousUsers =
SystemGroupBackend.getGroup(ANONYMOUS_USERS).getUUID();
allow(cfg, Permission.forLabel(Q.getName()), -1, 1, anonymousUsers,
Util.allow(cfg, Permission.forLabel(Q.getName()), -1, 1, anonymousUsers,
"refs/heads/*");
saveProjectConfig(cfg);
}