Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance.testsuite.request; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.gerrit.acceptance.AcceptanceTestRequestScope; 21 : import com.google.gerrit.acceptance.GerritServer.TestSshServerAddress; 22 : import com.google.gerrit.acceptance.testsuite.account.AccountOperations; 23 : import com.google.gerrit.acceptance.testsuite.account.TestAccount; 24 : import com.google.gerrit.acceptance.testsuite.account.TestSshKeys; 25 : import com.google.gerrit.common.Nullable; 26 : import com.google.gerrit.entities.Account; 27 : import com.google.gerrit.server.AnonymousUser; 28 : import com.google.gerrit.server.CurrentUser; 29 : import com.google.gerrit.server.IdentifiedUser; 30 : import com.google.gerrit.server.IdentifiedUser.GenericFactory; 31 : import com.google.gerrit.server.InternalUser; 32 : import com.google.gerrit.server.account.AccountCache; 33 : import com.google.inject.Inject; 34 : import com.google.inject.Provider; 35 : import com.google.inject.Singleton; 36 : import java.net.InetSocketAddress; 37 : 38 : /** 39 : * The implementation of {@code RequestScopeOperations}. 40 : * 41 : * <p>There is only one implementation of {@code RequestScopeOperations}. Nevertheless, we keep the 42 : * separation between interface and implementation to enhance clarity. 43 : */ 44 : @Singleton 45 : public class RequestScopeOperationsImpl implements RequestScopeOperations { 46 : private final AcceptanceTestRequestScope atrScope; 47 : private final AccountCache accountCache; 48 : private final AccountOperations accountOperations; 49 : private final IdentifiedUser.GenericFactory userFactory; 50 : private final Provider<AnonymousUser> anonymousUserProvider; 51 : private final InternalUser.Factory internalUserFactory; 52 : private final InetSocketAddress sshAddress; 53 : private final TestSshKeys testSshKeys; 54 : 55 : @Inject 56 : RequestScopeOperationsImpl( 57 : AcceptanceTestRequestScope atrScope, 58 : AccountCache accountCache, 59 : AccountOperations accountOperations, 60 : GenericFactory userFactory, 61 : Provider<AnonymousUser> anonymousUserProvider, 62 : InternalUser.Factory internalUserFactory, 63 : @Nullable @TestSshServerAddress InetSocketAddress sshAddress, 64 138 : TestSshKeys testSshKeys) { 65 138 : this.atrScope = atrScope; 66 138 : this.accountCache = accountCache; 67 138 : this.accountOperations = accountOperations; 68 138 : this.userFactory = userFactory; 69 138 : this.anonymousUserProvider = anonymousUserProvider; 70 138 : this.internalUserFactory = internalUserFactory; 71 138 : this.sshAddress = sshAddress; 72 138 : this.testSshKeys = testSshKeys; 73 138 : } 74 : 75 : @Override 76 : public AcceptanceTestRequestScope.Context setApiUser(Account.Id accountId) { 77 132 : return setApiUser(accountOperations.account(accountId).get()); 78 : } 79 : 80 : @Override 81 : public AcceptanceTestRequestScope.Context setApiUser(TestAccount testAccount) { 82 132 : return atrScope.set( 83 132 : atrScope.newContext( 84 132 : SshSessionFactory.createSession(testSshKeys, sshAddress, testAccount), 85 132 : createIdentifiedUser(testAccount.accountId()))); 86 : } 87 : 88 : @Override 89 : public AcceptanceTestRequestScope.Context resetCurrentApiUser() { 90 3 : CurrentUser user = atrScope.get().getUser(); 91 : // More special cases for anonymous users etc. can be added as needed. 92 3 : checkState(user.isIdentifiedUser(), "can only reset IdentifiedUser, not %s", user); 93 3 : return setApiUser(user.getAccountId()); 94 : } 95 : 96 : @Override 97 : public AcceptanceTestRequestScope.Context setApiUserAnonymous() { 98 14 : return atrScope.set(atrScope.newContext(null, anonymousUserProvider.get())); 99 : } 100 : 101 : @Override 102 : public AcceptanceTestRequestScope.Context setApiUserInternal() { 103 1 : return atrScope.set(atrScope.newContext(null, internalUserFactory.create())); 104 : } 105 : 106 : private IdentifiedUser createIdentifiedUser(Account.Id accountId) { 107 132 : return userFactory.create( 108 : accountCache 109 132 : .get(requireNonNull(accountId)) 110 132 : .orElseThrow( 111 0 : () -> new IllegalArgumentException("account does not exist: " + accountId))); 112 : } 113 : }