use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class AccountIT method updateNonExistingAccount.
@Test
public void updateNonExistingAccount() throws Exception {
Account.Id nonExistingAccountId = Account.id(999999);
AtomicBoolean consumerCalled = new AtomicBoolean();
Optional<AccountState> accountState = accountsUpdateProvider.get().update("Update Non-Existing Account", nonExistingAccountId, a -> consumerCalled.set(true));
assertThat(accountState).isEmpty();
assertThat(consumerCalled.get()).isFalse();
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class AccountIT method checkMetaId.
@Test
public void checkMetaId() throws Exception {
// metaId is set when account is loaded
assertThat(accounts.get(admin.id()).get().account().metaId()).isEqualTo(getMetaId(admin.id()));
// metaId is set when account is created
AccountsUpdate au = accountsUpdateProvider.get();
Account.Id accountId = Account.id(seq.nextAccountId());
AccountState accountState = au.insert("Create Test Account", accountId, u -> {
});
assertThat(accountState.account().metaId()).isEqualTo(getMetaId(accountId));
// metaId is set when account is updated
Optional<AccountState> updatedAccountState = au.update("Set Full Name", accountId, u -> u.setFullName("foo"));
assertThat(updatedAccountState).isPresent();
Account updatedAccount = updatedAccountState.get().account();
assertThat(accountState.account().metaId()).isNotEqualTo(updatedAccount.metaId());
assertThat(updatedAccount.metaId()).isEqualTo(getMetaId(accountId));
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class AccountIT method externalIdBatchUpdates.
@Test
public void externalIdBatchUpdates() throws Exception {
String extId1String = "foo:bar";
String extId2String = "foo:baz";
ExternalId extId1 = externalIdFactory.createWithEmail(externalIdKeyFactory.parse(extId1String), admin.id(), "1@foo.com");
ExternalId extId2 = externalIdFactory.createWithEmail(externalIdKeyFactory.parse(extId2String), user.id(), "2@foo.com");
ObjectId revBefore;
try (Repository repo = repoManager.openRepository(allUsers)) {
revBefore = repo.exactRef(RefNames.REFS_EXTERNAL_IDS).getObjectId();
}
AccountsUpdate.UpdateArguments ua1 = new AccountsUpdate.UpdateArguments("Add External ID", admin.id(), (a, u) -> u.addExternalId(extId1));
AccountsUpdate.UpdateArguments ua2 = new AccountsUpdate.UpdateArguments("Add External ID", user.id(), (a, u) -> u.addExternalId(extId2));
ImmutableList<Optional<AccountState>> accountStates = accountsUpdateProvider.get().updateBatch(ImmutableList.of(ua1, ua2));
assertThat(accountStates).hasSize(2);
assertThat(accountStates.get(0).get().externalIds()).contains(extId1);
assertThat(accountStates.get(1).get().externalIds()).contains(extId2);
assertThat(gApi.accounts().id(admin.id().get()).getExternalIds().stream().map(e -> e.identity).collect(toSet())).contains(extId1String);
assertThat(gApi.accounts().id(user.id().get()).getExternalIds().stream().map(e -> e.identity).collect(toSet())).contains(extId2String);
// Ensure that we only applied one single commit.
try (Repository repo = repoManager.openRepository(allUsers);
RevWalk rw = new RevWalk(repo)) {
RevCommit after = rw.parseCommit(repo.exactRef(RefNames.REFS_EXTERNAL_IDS).getObjectId());
assertThat(after.getParent(0).toObjectId()).isEqualTo(revBefore);
}
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class AccountIT method validateAccountActivation.
@Test
public void validateAccountActivation() throws Exception {
Account.Id activatableAccountId = accountOperations.newAccount().inactive().preferredEmail("foo@activatable.com").create();
Account.Id deactivatableAccountId = accountOperations.newAccount().preferredEmail("foo@deactivatable.com").create();
AccountActivationValidationListener validationListener = new AccountActivationValidationListener() {
@Override
public void validateActivation(AccountState account) throws ValidationException {
String preferredEmail = account.account().preferredEmail();
if (preferredEmail == null || !preferredEmail.endsWith("@activatable.com")) {
throw new ValidationException("not allowed to active account");
}
}
@Override
public void validateDeactivation(AccountState account) throws ValidationException {
String preferredEmail = account.account().preferredEmail();
if (preferredEmail == null || !preferredEmail.endsWith("@deactivatable.com")) {
throw new ValidationException("not allowed to deactive account");
}
}
};
AccountActivationListener listener = mock(AccountActivationListener.class);
try (Registration registration = extensionRegistry.newRegistration().add(validationListener).add(listener)) {
/* Test account that can be activated, but not deactivated */
// Deactivate account that is already inactive
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(activatableAccountId.get()).setActive(false));
assertThat(thrown).hasMessageThat().isEqualTo("account not active");
assertThat(accountOperations.account(activatableAccountId).get().active()).isFalse();
verifyNoInteractions(listener);
// Activate account that can be activated
gApi.accounts().id(activatableAccountId.get()).setActive(true);
assertThat(accountOperations.account(activatableAccountId).get().active()).isTrue();
verify(listener).onAccountActivated(activatableAccountId.get());
verifyNoMoreInteractions(listener);
// Activate account that is already active
gApi.accounts().id(activatableAccountId.get()).setActive(true);
assertThat(accountOperations.account(activatableAccountId).get().active()).isTrue();
verifyNoMoreInteractions(listener);
// Try deactivating account that cannot be deactivated
thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(activatableAccountId.get()).setActive(false));
assertThat(thrown).hasMessageThat().isEqualTo("not allowed to deactive account");
assertThat(accountOperations.account(activatableAccountId).get().active()).isTrue();
verifyNoMoreInteractions(listener);
/* Test account that can be deactivated, but not activated */
// Activate account that is already inactive
gApi.accounts().id(deactivatableAccountId.get()).setActive(true);
assertThat(accountOperations.account(deactivatableAccountId).get().active()).isTrue();
verifyNoMoreInteractions(listener);
// Deactivate account that can be deactivated
gApi.accounts().id(deactivatableAccountId.get()).setActive(false);
assertThat(accountOperations.account(deactivatableAccountId).get().active()).isFalse();
verify(listener).onAccountDeactivated(deactivatableAccountId.get());
verifyNoMoreInteractions(listener);
// Deactivate account that is already inactive
thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(deactivatableAccountId.get()).setActive(false));
assertThat(thrown).hasMessageThat().isEqualTo("account not active");
assertThat(accountOperations.account(deactivatableAccountId).get().active()).isFalse();
verifyNoMoreInteractions(listener);
// Try activating account that cannot be activated
thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(deactivatableAccountId.get()).setActive(true));
assertThat(thrown).hasMessageThat().isEqualTo("not allowed to active account");
assertThat(accountOperations.account(deactivatableAccountId).get().active()).isFalse();
verifyNoMoreInteractions(listener);
}
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class AbstractDaemonTest method verifyNoAccountDetailsInChangeNotes.
/**
* Verify that NoteDB commits do not persist user-sensitive information, by running checks for all
* commits in {@link RefNames#changeMetaRef} for all changes, created during the test.
*
* <p>These tests prevent regression, assuming appropriate test coverage for new features. The
* verification is disabled by default and can be enabled using {@link VerifyNoPiiInChangeNotes}
* annotation either on test class or method.
*/
protected void verifyNoAccountDetailsInChangeNotes() throws RestApiException, IOException {
List<ChangeInfo> allChanges = gApi.changes().query().get();
List<AccountState> allAccounts = accounts.all();
for (ChangeInfo change : allChanges) {
try (Repository repo = repoManager.openRepository(Project.nameKey(change.project))) {
String metaRefName = RefNames.changeMetaRef(Change.Id.tryParse(change._number.toString()).get());
ObjectId metaTip = repo.getRefDatabase().exactRef(metaRefName).getObjectId();
ChangeNotesRevWalk revWalk = ChangeNotesCommit.newRevWalk(repo);
revWalk.reset();
revWalk.markStart(revWalk.parseCommit(metaTip));
ChangeNotesCommit commit;
while ((commit = revWalk.next()) != null) {
String fullMessage = commit.getFullMessage();
for (AccountState accountState : allAccounts) {
Account account = accountState.account();
assertThat(fullMessage).doesNotContain(account.getName());
if (account.fullName() != null) {
assertThat(fullMessage).doesNotContain(account.fullName());
}
if (account.displayName() != null) {
assertThat(fullMessage).doesNotContain(account.displayName());
}
if (account.preferredEmail() != null) {
assertThat(fullMessage).doesNotContain(account.preferredEmail());
}
if (accountState.userName().isPresent()) {
assertThat(fullMessage).doesNotContain(accountState.userName().get());
}
List<String> allEmails = accountState.externalIds().stream().map(ExternalId::email).filter(Objects::nonNull).collect(toImmutableList());
for (String email : allEmails) {
assertThat(fullMessage).doesNotContain(email);
}
}
}
}
}
}
Aggregations