Add RequestScopeOperations to manipulate request scope in tests
The only operation for now is setting the current request scope without an SSH session. This approach was chosen over adding a method to AccountOperations since that class is for actually reading and modifying accounts, whereas this operation is about modifying the global request scope state. Change-Id: I98cf69b63792df60778c4caa0213aa8658eda925
This commit is contained in:
@@ -30,6 +30,8 @@ import com.google.gerrit.acceptance.testsuite.group.GroupOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.group.GroupOperationsImpl;
|
||||
import com.google.gerrit.acceptance.testsuite.project.ProjectOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.project.ProjectOperationsImpl;
|
||||
import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperationsImpl;
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.extensions.config.FactoryModule;
|
||||
import com.google.gerrit.lucene.LuceneIndexModule;
|
||||
@@ -502,6 +504,7 @@ public class GerritServer implements AutoCloseable {
|
||||
bind(AccountOperations.class).to(AccountOperationsImpl.class);
|
||||
bind(GroupOperations.class).to(GroupOperationsImpl.class);
|
||||
bind(ProjectOperations.class).to(ProjectOperationsImpl.class);
|
||||
bind(RequestScopeOperations.class).to(RequestScopeOperationsImpl.class);
|
||||
factory(PushOneCommit.Factory.class);
|
||||
install(InProcessProtocol.module());
|
||||
install(new NoSshModule());
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright (C) 2019 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.acceptance.testsuite.request;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
import com.google.gerrit.acceptance.AcceptanceTestRequestScope;
|
||||
import com.google.gerrit.acceptance.testsuite.account.TestAccount;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
|
||||
/**
|
||||
* An aggregation of operations on Guice request scopes for test purposes.
|
||||
*
|
||||
* <p>To execute the operations, no Gerrit permissions are necessary.
|
||||
*/
|
||||
public interface RequestScopeOperations {
|
||||
/**
|
||||
* Sets the Guice request scope to the given account.
|
||||
*
|
||||
* <p>The resulting scope has no SSH session attached.
|
||||
*
|
||||
* @param accountId account ID. Must exist; throws an unchecked exception otherwise.
|
||||
* @return the previous request scope.
|
||||
*/
|
||||
AcceptanceTestRequestScope.Context setApiUser(Account.Id accountId);
|
||||
|
||||
/**
|
||||
* Sets the Guice request scope to the given account.
|
||||
*
|
||||
* <p>The resulting scope has no SSH session attached.
|
||||
*
|
||||
* @param testAccount test account from {@code AccountOperations}.
|
||||
* @return the previous request scope.
|
||||
*/
|
||||
default AcceptanceTestRequestScope.Context setApiUser(TestAccount testAccount) {
|
||||
return setApiUser(requireNonNull(testAccount).accountId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright (C) 2019 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.acceptance.testsuite.request;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
import com.google.gerrit.acceptance.AcceptanceTestRequestScope;
|
||||
import com.google.gerrit.acceptance.AcceptanceTestRequestScope.Context;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.account.AccountCache;
|
||||
import com.google.gerrit.server.account.AccountState;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
/**
|
||||
* The implementation of {@code RequestScopeOperations}.
|
||||
*
|
||||
* <p>There is only one implementation of {@code RequestScopeOperations}. Nevertheless, we keep the
|
||||
* separation between interface and implementation to enhance clarity.
|
||||
*/
|
||||
@Singleton
|
||||
public class RequestScopeOperationsImpl implements RequestScopeOperations {
|
||||
private final AcceptanceTestRequestScope atrScope;
|
||||
private final AccountCache accountCache;
|
||||
private final IdentifiedUser.GenericFactory userFactory;
|
||||
|
||||
@Inject
|
||||
RequestScopeOperationsImpl(
|
||||
AcceptanceTestRequestScope atrScope,
|
||||
AccountCache accountCache,
|
||||
IdentifiedUser.GenericFactory userFactory) {
|
||||
this.atrScope = atrScope;
|
||||
this.accountCache = accountCache;
|
||||
this.userFactory = userFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context setApiUser(Account.Id accountId) {
|
||||
AccountState accountState =
|
||||
accountCache
|
||||
.get(requireNonNull(accountId))
|
||||
.orElseThrow(
|
||||
() -> new IllegalArgumentException("account does not exist: " + accountId));
|
||||
return atrScope.set(atrScope.newContext(null, userFactory.create(accountState)));
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import com.google.gerrit.acceptance.NoHttpd;
|
||||
import com.google.gerrit.acceptance.PushOneCommit;
|
||||
import com.google.gerrit.acceptance.testsuite.account.AccountOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.group.GroupOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
|
||||
import com.google.gerrit.common.RawInputUtil;
|
||||
import com.google.gerrit.common.data.GlobalCapability;
|
||||
import com.google.gerrit.common.data.PermissionRule;
|
||||
@@ -77,6 +78,7 @@ public class GetRelatedIT extends AbstractDaemonTest {
|
||||
|
||||
@Inject private AccountOperations accountOperations;
|
||||
@Inject private GroupOperations groupOperations;
|
||||
@Inject private RequestScopeOperations requestScopeOperations;
|
||||
|
||||
private String systemTimeZone;
|
||||
|
||||
@@ -611,7 +613,7 @@ public class GetRelatedIT extends AbstractDaemonTest {
|
||||
rule.setRange(0, 2);
|
||||
u.save();
|
||||
}
|
||||
atrScope.set(atrScope.newContext(null, identifiedUserFactory.create(accountId)));
|
||||
requestScopeOperations.setApiUser(accountId);
|
||||
|
||||
assertRelated(lastPsId, expected);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
// Copyright (C) 2019 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.acceptance.testsuite.request;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assert_;
|
||||
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.AcceptanceTestRequestScope;
|
||||
import com.google.gerrit.acceptance.testsuite.account.AccountOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.account.TestAccount;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.Sequences;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RequestScopeOperationsImplTest extends AbstractDaemonTest {
|
||||
@Inject private AccountOperations accountOperations;
|
||||
@Inject private Provider<IdentifiedUser> userProvider;
|
||||
@Inject private RequestScopeOperationsImpl requestScopeOperations;
|
||||
@Inject private Sequences sequences;
|
||||
|
||||
@Test
|
||||
public void setApiUserToExistingUserById() throws Exception {
|
||||
checkApiUser(admin.getId());
|
||||
AcceptanceTestRequestScope.Context oldCtx = requestScopeOperations.setApiUser(user.getId());
|
||||
assertThat(oldCtx.getUser().getAccountId()).isEqualTo(admin.getId());
|
||||
checkApiUser(user.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setApiUserToExistingUserByTestAccount() throws Exception {
|
||||
checkApiUser(admin.getId());
|
||||
TestAccount testAccount =
|
||||
accountOperations.account(accountOperations.newAccount().create()).get();
|
||||
AcceptanceTestRequestScope.Context oldCtx = requestScopeOperations.setApiUser(testAccount);
|
||||
assertThat(oldCtx.getUser().getAccountId()).isEqualTo(admin.getId());
|
||||
checkApiUser(testAccount.accountId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setApiUserToNonExistingUser() throws Exception {
|
||||
checkApiUser(admin.getId());
|
||||
try {
|
||||
requestScopeOperations.setApiUser(new Account.Id(sequences.nextAccountId()));
|
||||
assert_().fail("expected RuntimeException");
|
||||
} catch (RuntimeException e) {
|
||||
// Expected.
|
||||
}
|
||||
checkApiUser(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());
|
||||
assertThat(userProvider.get().isIdentifiedUser())
|
||||
.named("user from provider is an IdentifiedUser")
|
||||
.isTrue();
|
||||
assertThat(userProvider.get().getAccountId()).named("user from provider").isEqualTo(expected);
|
||||
AcceptanceTestRequestScope.Context ctx = atrScope.get();
|
||||
assertThat(ctx.getUser().isIdentifiedUser())
|
||||
.named("user from AcceptanceTestRequestScope.Context is an IdentifiedUser")
|
||||
.isTrue();
|
||||
assertThat(ctx.getUser().getAccountId())
|
||||
.named("user from AcceptanceTestRequestScope.Context")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user