AccountApi: Add getGroups method

Add a method to get the groups to which an account belongs.

Note that the test user for some reason does not get included into
the "Anonymous Users" and "Registered Users" groups, while the admin
user does. Fixing this is outside the scope of this commit so it's
left as-is with a TODO to update the test later.

Bug: Issue 7065
Change-Id: If4c2d878adb90b020d74369316953796dbfcb712
This commit is contained in:
David Pursehouse
2017-08-23 08:54:06 +09:00
parent 53147ab906
commit b151e1eb80
5 changed files with 50 additions and 6 deletions

View File

@@ -1134,6 +1134,12 @@ public abstract class AbstractDaemonTest {
return name;
}
protected String createAccount(String name, String group) throws Exception {
name = name(name);
accountCreator.create(name, group);
return name;
}
protected RevCommit getHead(Repository repo, String name) throws Exception {
try (RevWalk rw = new RevWalk(repo)) {
Ref r = repo.exactRef(name);

View File

@@ -31,6 +31,7 @@ import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GPG
import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import static org.junit.Assert.fail;
@@ -1313,6 +1314,26 @@ public class AccountIT extends AbstractDaemonTest {
}
}
@Test
public void groups() throws Exception {
assertGroups(
admin.username, ImmutableList.of("Anonymous Users", "Registered Users", "Administrators"));
//TODO: update when test user is fixed to be included in "Anonymous Users" and
// "Registered Users" groups
assertGroups(user.username, ImmutableList.of());
String group = createGroup("group");
String newUser = createAccount("user1", group);
assertGroups(newUser, ImmutableList.of(group));
}
private void assertGroups(String user, List<String> expected) throws Exception {
List<String> actual =
gApi.accounts().id(user).getGroups().stream().map(g -> g.name).collect(toList());
assertThat(actual).containsExactlyElementsIn(expected);
}
private void assertSequenceNumbers(List<SshKeyInfo> sshKeys) {
int seq = 1;
for (SshKeyInfo key : sshKeys) {

View File

@@ -687,12 +687,6 @@ public class GroupsIT extends AbstractDaemonTest {
return groupCache.get(new AccountGroup.NameKey(name));
}
private String createAccount(String name, String group) throws Exception {
name = name(name);
accountCreator.create(name, group);
return name;
}
private void setCreatedOnToNull(AccountGroup.UUID groupUuid) throws Exception {
groupsUpdateProvider.get().updateGroup(db, groupUuid, group -> group.setCreatedOn(null));
}

View File

@@ -25,6 +25,7 @@ import com.google.gerrit.extensions.common.AgreementInfo;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.common.GpgKeyInfo;
import com.google.gerrit.extensions.common.GroupInfo;
import com.google.gerrit.extensions.common.SshKeyInfo;
import com.google.gerrit.extensions.restapi.NotImplementedException;
import com.google.gerrit.extensions.restapi.RestApiException;
@@ -69,6 +70,8 @@ public interface AccountApi {
List<ChangeInfo> getStarredChanges() throws RestApiException;
List<GroupInfo> getGroups() throws RestApiException;
List<EmailInfo> getEmails() throws RestApiException;
void addEmail(EmailInput input) throws RestApiException;
@@ -196,6 +199,11 @@ public interface AccountApi {
throw new NotImplementedException();
}
@Override
public List<GroupInfo> getGroups() throws RestApiException {
throw new NotImplementedException();
}
@Override
public List<EmailInfo> getEmails() throws RestApiException {
throw new NotImplementedException();

View File

@@ -33,6 +33,7 @@ import com.google.gerrit.extensions.common.AgreementInput;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.common.GpgKeyInfo;
import com.google.gerrit.extensions.common.GroupInfo;
import com.google.gerrit.extensions.common.SshKeyInfo;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.Response;
@@ -54,6 +55,7 @@ import com.google.gerrit.server.account.GetDiffPreferences;
import com.google.gerrit.server.account.GetEditPreferences;
import com.google.gerrit.server.account.GetEmails;
import com.google.gerrit.server.account.GetExternalIds;
import com.google.gerrit.server.account.GetGroups;
import com.google.gerrit.server.account.GetPreferences;
import com.google.gerrit.server.account.GetSshKeys;
import com.google.gerrit.server.account.GetWatchedProjects;
@@ -70,6 +72,7 @@ import com.google.gerrit.server.account.StarredChanges;
import com.google.gerrit.server.account.Stars;
import com.google.gerrit.server.change.ChangeResource;
import com.google.gerrit.server.change.ChangesCollection;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.util.List;
@@ -116,6 +119,7 @@ public class AccountApiImpl implements AccountApi {
private final GetExternalIds getExternalIds;
private final DeleteExternalIds deleteExternalIds;
private final PutStatus putStatus;
private final GetGroups getGroups;
@Inject
AccountApiImpl(
@@ -153,6 +157,7 @@ public class AccountApiImpl implements AccountApi {
GetExternalIds getExternalIds,
DeleteExternalIds deleteExternalIds,
PutStatus putStatus,
GetGroups getGroups,
@Assisted AccountResource account) {
this.account = account;
this.accountLoaderFactory = ailf;
@@ -189,6 +194,7 @@ public class AccountApiImpl implements AccountApi {
this.getExternalIds = getExternalIds;
this.deleteExternalIds = deleteExternalIds;
this.putStatus = putStatus;
this.getGroups = getGroups;
}
@Override
@@ -362,6 +368,15 @@ public class AccountApiImpl implements AccountApi {
}
}
@Override
public List<GroupInfo> getGroups() throws RestApiException {
try {
return getGroups.apply(account);
} catch (OrmException e) {
throw asRestApiException("Cannot get groups", e);
}
}
@Override
public List<EmailInfo> getEmails() {
return getEmails.apply(account);