Let all tests use AccountCreator to create accounts

For this the AccountCreator must support creating accounts without
username.

This reduces the number of places where accounts are created, which is a
preparation step for migrating the accounts to NoteDb

Change-Id: If67aaf2d713b7599e27ba087ac2f83cc99e23376
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-04-06 15:33:19 +02:00
parent d915ea6b15
commit 6702433385
3 changed files with 31 additions and 32 deletions

View File

@@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.US_ASCII;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
@@ -81,7 +82,12 @@ public class AccountCreator {
}
public synchronized TestAccount create(
String username, String email, String fullName, String... groups) throws Exception {
@Nullable String username,
@Nullable String email,
@Nullable String fullName,
String... groups)
throws Exception {
TestAccount account = accounts.get(username);
if (account != null) {
return account;
@@ -90,8 +96,11 @@ public class AccountCreator {
Account.Id id = new Account.Id(db.nextAccountId());
List<ExternalId> extIds = new ArrayList<>(2);
String httpPass = "http-pass";
extIds.add(ExternalId.createUsername(username, id, httpPass));
String httpPass = null;
if (username != null) {
httpPass = "http-pass";
extIds.add(ExternalId.createUsername(username, id, httpPass));
}
if (email != null) {
extIds.add(ExternalId.createEmail(id, email));
@@ -114,28 +123,36 @@ public class AccountCreator {
}
KeyPair sshKey = null;
if (SshMode.useSsh()) {
if (SshMode.useSsh() && username != null) {
sshKey = genSshKey();
authorizedKeys.addKey(id, publicKey(sshKey, email));
sshKeyCache.evict(username);
}
accountCache.evictByUsername(username);
if (username != null) {
accountCache.evictByUsername(username);
}
byEmailCache.evict(email);
indexer.index(id);
account = new TestAccount(id, username, email, fullName, sshKey, httpPass);
accounts.put(username, account);
if (username != null) {
accounts.put(username, account);
}
return account;
}
}
public TestAccount create(String username, String group) throws Exception {
public TestAccount create(@Nullable String username, String group) throws Exception {
return create(username, null, username, group);
}
public TestAccount create(String username) throws Exception {
public TestAccount create() throws Exception {
return create(null);
}
public TestAccount create(@Nullable String username) throws Exception {
return create(username, null, username, (String[]) null);
}

View File

@@ -18,23 +18,16 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.PutUsername;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import java.util.Collections;
import org.junit.Test;
public class PutUsernameIT extends AbstractDaemonTest {
@Inject private SchemaFactory<ReviewDb> reviewDbProvider;
@Test
public void set() throws Exception {
PutUsername.Input in = new PutUsername.Input();
in.username = "myUsername";
RestResponse r = adminRestSession.put("/accounts/" + createUser().get() + "/username", in);
RestResponse r =
adminRestSession.put("/accounts/" + accounts.create().id.get() + "/username", in);
r.assertOK();
assertThat(newGson().fromJson(r.getReader(), String.class)).isEqualTo(in.username);
}
@@ -43,7 +36,9 @@ public class PutUsernameIT extends AbstractDaemonTest {
public void setExisting_Conflict() throws Exception {
PutUsername.Input in = new PutUsername.Input();
in.username = admin.username;
adminRestSession.put("/accounts/" + createUser().get() + "/username", in).assertConflict();
adminRestSession
.put("/accounts/" + accounts.create().id.get() + "/username", in)
.assertConflict();
}
@Test
@@ -57,13 +52,4 @@ public class PutUsernameIT extends AbstractDaemonTest {
public void delete_MethodNotAllowed() throws Exception {
adminRestSession.put("/accounts/" + admin.username + "/username").assertMethodNotAllowed();
}
private Account.Id createUser() throws Exception {
try (ReviewDb db = reviewDbProvider.open()) {
Account.Id id = new Account.Id(db.nextAccountId());
Account a = new Account(id, TimeUtil.nowTs());
db.accounts().insert(Collections.singleton(a));
return id;
}
}
}

View File

@@ -23,7 +23,6 @@ import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.Sandboxed;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.api.changes.StarsInput;
@@ -39,7 +38,6 @@ import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.testutil.FakeEmailSender.Message;
import com.google.inject.Inject;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
@@ -560,9 +558,7 @@ public class ProjectWatchIT extends AbstractDaemonTest {
@Test
public void deleteAllProjectWatchesIfWatchConfigIsTheOnlyFileInUserBranch() throws Exception {
// Create account that has no files in its refs/users/ branch.
Account.Id id = new Account.Id(db.nextAccountId());
Account a = new Account(id, TimeUtil.nowTs());
db.accounts().insert(Collections.singleton(a));
Account.Id id = accounts.create().id;
// Add a project watch so that a watch.config file in the refs/users/ branch is created.
Map<ProjectWatchKey, Set<NotifyType>> watches = new HashMap<>();