RequestScopeOperations: Implement additional setter methods
The implementations of scope setting methods in AbstractDaemonTest now delegate directly to RequestScopeOperations, and can be inlined in a future change. Change-Id: I50a4096cb51a671ce5be89be32334b0b0094b350
This commit is contained in:
@@ -16,63 +16,100 @@ package com.google.gerrit.acceptance.testsuite.request;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assert_;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.AcceptanceTestRequestScope;
|
||||
import com.google.gerrit.acceptance.UseSsh;
|
||||
import com.google.gerrit.acceptance.testsuite.account.AccountOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.account.TestAccount;
|
||||
import com.google.gerrit.extensions.common.ChangeInput;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.AnonymousUser;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.CurrentUser.PropertyKey;
|
||||
import com.google.gerrit.server.Sequences;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.Test;
|
||||
|
||||
@UseSsh
|
||||
public class RequestScopeOperationsImplTest extends AbstractDaemonTest {
|
||||
private static final AtomicInteger changeCounter = new AtomicInteger();
|
||||
|
||||
@Inject private AccountOperations accountOperations;
|
||||
@Inject private Provider<IdentifiedUser> userProvider;
|
||||
@Inject private Provider<CurrentUser> userProvider;
|
||||
@Inject private RequestScopeOperationsImpl requestScopeOperations;
|
||||
@Inject private Sequences sequences;
|
||||
|
||||
@Test
|
||||
public void setApiUserToExistingUserById() throws Exception {
|
||||
checkApiUser(admin.getId());
|
||||
fastCheckCurrentUser(admin.getId());
|
||||
AcceptanceTestRequestScope.Context oldCtx = requestScopeOperations.setApiUser(user.getId());
|
||||
assertThat(oldCtx.getUser().getAccountId()).isEqualTo(admin.getId());
|
||||
checkApiUser(user.getId());
|
||||
checkCurrentUser(user.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setApiUserToExistingUserByTestAccount() throws Exception {
|
||||
checkApiUser(admin.getId());
|
||||
fastCheckCurrentUser(admin.getId());
|
||||
TestAccount testAccount =
|
||||
accountOperations.account(accountOperations.newAccount().create()).get();
|
||||
accountOperations.account(accountOperations.newAccount().username("tester").create()).get();
|
||||
AcceptanceTestRequestScope.Context oldCtx = requestScopeOperations.setApiUser(testAccount);
|
||||
assertThat(oldCtx.getUser().getAccountId()).isEqualTo(admin.getId());
|
||||
checkApiUser(testAccount.accountId());
|
||||
checkCurrentUser(testAccount.accountId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setApiUserToNonExistingUser() throws Exception {
|
||||
checkApiUser(admin.getId());
|
||||
fastCheckCurrentUser(admin.getId());
|
||||
try {
|
||||
requestScopeOperations.setApiUser(new Account.Id(sequences.nextAccountId()));
|
||||
assert_().fail("expected RuntimeException");
|
||||
} catch (RuntimeException e) {
|
||||
// Expected.
|
||||
}
|
||||
checkApiUser(admin.getId());
|
||||
checkCurrentUser(admin.getId());
|
||||
}
|
||||
|
||||
private void checkApiUser(Account.Id expected) throws Exception {
|
||||
// Test all supported ways that an acceptance test might query the active user.
|
||||
assertThat(gApi.accounts().self().get()._accountId)
|
||||
.named("user from GerritApi")
|
||||
.isEqualTo(expected.get());
|
||||
@Test
|
||||
public void resetCurrentApiUserClearsCachedState() throws Exception {
|
||||
requestScopeOperations.setApiUser(user.getId());
|
||||
PropertyKey<String> key = PropertyKey.create();
|
||||
atrScope.get().getUser().put(key, "foo");
|
||||
assertThat(atrScope.get().getUser().get(key)).hasValue("foo");
|
||||
|
||||
AcceptanceTestRequestScope.Context oldCtx = requestScopeOperations.resetCurrentApiUser();
|
||||
checkCurrentUser(user.getId());
|
||||
assertThat(atrScope.get().getUser().get(key)).isEmpty();
|
||||
assertThat(oldCtx.getUser().get(key)).hasValue("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setApiUserAnonymousSetsAnonymousUser() throws Exception {
|
||||
fastCheckCurrentUser(admin.getId());
|
||||
requestScopeOperations.setApiUserAnonymous();
|
||||
assertThat(userProvider.get()).isInstanceOf(AnonymousUser.class);
|
||||
}
|
||||
|
||||
private void fastCheckCurrentUser(Account.Id expected) {
|
||||
// Check current user quickly, since the full check requires creating changes and is quite slow.
|
||||
assertThat(userProvider.get().isIdentifiedUser())
|
||||
.named("user from provider is an IdentifiedUser")
|
||||
.isTrue();
|
||||
assertThat(userProvider.get().getAccountId()).named("user from provider").isEqualTo(expected);
|
||||
}
|
||||
|
||||
private void checkCurrentUser(Account.Id expected) throws Exception {
|
||||
// Test all supported ways that an acceptance test might query the active user.
|
||||
fastCheckCurrentUser(expected);
|
||||
assertThat(gApi.accounts().self().get()._accountId)
|
||||
.named("user from GerritApi")
|
||||
.isEqualTo(expected.get());
|
||||
AcceptanceTestRequestScope.Context ctx = atrScope.get();
|
||||
assertThat(ctx.getUser().isIdentifiedUser())
|
||||
.named("user from AcceptanceTestRequestScope.Context is an IdentifiedUser")
|
||||
@@ -80,5 +117,31 @@ public class RequestScopeOperationsImplTest extends AbstractDaemonTest {
|
||||
assertThat(ctx.getUser().getAccountId())
|
||||
.named("user from AcceptanceTestRequestScope.Context")
|
||||
.isEqualTo(expected);
|
||||
checkSshUser(expected);
|
||||
}
|
||||
|
||||
private void checkSshUser(Account.Id expected) throws Exception {
|
||||
// No "gerrit whoami" command, so the simplest way to check who the user is over SSH is to query
|
||||
// for owner:self.
|
||||
ChangeInput cin = new ChangeInput();
|
||||
cin.project = project.get();
|
||||
cin.branch = "master";
|
||||
cin.subject = "Test change " + changeCounter.incrementAndGet();
|
||||
String changeId = gApi.changes().create(cin).get().changeId;
|
||||
assertThat(gApi.changes().id(changeId).get().owner._accountId).isEqualTo(expected.get());
|
||||
String queryResults =
|
||||
atrScope.get().getSession().exec("gerrit query owner:self change:" + changeId);
|
||||
assertThat(findDistinct(queryResults, "I[0-9a-f]{40}"))
|
||||
.named("Change-Ids in query results:\n%s", queryResults)
|
||||
.containsExactly(changeId);
|
||||
}
|
||||
|
||||
private static ImmutableSet<String> findDistinct(String input, String pattern) {
|
||||
Matcher m = Pattern.compile(pattern).matcher(input);
|
||||
ImmutableSet.Builder<String> b = ImmutableSet.builder();
|
||||
while (m.find()) {
|
||||
b.add(m.group(0));
|
||||
}
|
||||
return b.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user