Search in sources :

Example 81 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class AccountIT method deletePreferredEmail.

@Test
public void deletePreferredEmail() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        String previous = gApi.accounts().self().get().email;
        String email = "foo.bar.baz@example.com";
        EmailInput input = new EmailInput();
        input.email = email;
        input.noConfirmation = true;
        input.preferred = true;
        gApi.accounts().self().addEmail(input);
        // Account is reindexed twice; once on adding the new email,
        // and then again on setting the email preferred.
        accountIndexedCounter.assertReindexOf(admin, 2);
        // The new preferred email is set
        assertThat(gApi.accounts().self().get().email).isEqualTo(email);
        accountIndexedCounter.clear();
        gApi.accounts().self().deleteEmail(input.email);
        accountIndexedCounter.assertReindexOf(admin);
        requestScopeOperations.resetCurrentApiUser();
        assertThat(getEmails()).containsExactly(previous);
        assertThat(gApi.accounts().self().get().email).isNull();
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 82 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class AccountIT method sshKeys.

@Test
@UseSsh
public void sshKeys() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        // The test account should initially have exactly one ssh key
        List<SshKeyInfo> info = gApi.accounts().self().listSshKeys();
        assertThat(info).hasSize(1);
        assertSequenceNumbers(info);
        SshKeyInfo key = info.get(0);
        KeyPair keyPair = sshKeys.getKeyPair(admin);
        String initial = TestSshKeys.publicKey(keyPair, admin.email());
        assertThat(key.sshPublicKey).isEqualTo(initial);
        accountIndexedCounter.assertNoReindex();
        // Add a new key
        sender.clear();
        String newKey = TestSshKeys.publicKey(SshSessionFactory.genSshKey(), admin.email());
        gApi.accounts().self().addSshKey(newKey);
        info = gApi.accounts().self().listSshKeys();
        assertThat(info).hasSize(2);
        assertSequenceNumbers(info);
        accountIndexedCounter.assertReindexOf(admin);
        assertThat(sender.getMessages()).hasSize(1);
        assertThat(sender.getMessages().get(0).body()).contains("new SSH keys have been added");
        // Add an existing key (the request succeeds, but the key isn't added again)
        sender.clear();
        gApi.accounts().self().addSshKey(initial);
        info = gApi.accounts().self().listSshKeys();
        assertThat(info).hasSize(2);
        assertSequenceNumbers(info);
        accountIndexedCounter.assertNoReindex();
        // TODO: Issue 10769: Adding an already existing key should not result in a notification email
        assertThat(sender.getMessages()).hasSize(1);
        assertThat(sender.getMessages().get(0).body()).contains("new SSH keys have been added");
        // Add another new key
        sender.clear();
        String newKey2 = TestSshKeys.publicKey(SshSessionFactory.genSshKey(), admin.email());
        gApi.accounts().self().addSshKey(newKey2);
        info = gApi.accounts().self().listSshKeys();
        assertThat(info).hasSize(3);
        assertSequenceNumbers(info);
        accountIndexedCounter.assertReindexOf(admin);
        assertThat(sender.getMessages()).hasSize(1);
        assertThat(sender.getMessages().get(0).body()).contains("new SSH keys have been added");
        // Delete second key
        sender.clear();
        gApi.accounts().self().deleteSshKey(2);
        info = gApi.accounts().self().listSshKeys();
        assertThat(info).hasSize(2);
        assertThat(info.get(0).seq).isEqualTo(1);
        assertThat(info.get(1).seq).isEqualTo(3);
        accountIndexedCounter.assertReindexOf(admin);
        assertThat(sender.getMessages()).hasSize(1);
        assertThat(sender.getMessages().get(0).body()).contains("SSH keys have been deleted");
        // Mark first key as invalid
        assertThat(info.get(0).valid).isTrue();
        authorizedKeys.markKeyInvalid(admin.id(), 1);
        info = gApi.accounts().self().listSshKeys();
        assertThat(info).hasSize(2);
        assertThat(info.get(0).seq).isEqualTo(1);
        assertThat(info.get(0).valid).isFalse();
        assertThat(info.get(1).seq).isEqualTo(3);
        accountIndexedCounter.assertReindexOf(admin);
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) KeyPair(java.security.KeyPair) SshKeyInfo(com.google.gerrit.extensions.common.SshKeyInfo) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test) UseSsh(com.google.gerrit.acceptance.UseSsh)

Example 83 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class AccountIT method reindexPermissions.

// reindex is tested by {@link AbstractQueryAccountsTest#reindex}
@Test
public void reindexPermissions() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        // admin can reindex any account
        requestScopeOperations.setApiUser(admin.id());
        gApi.accounts().id(user.username()).index();
        accountIndexedCounter.assertReindexOf(user);
        // user can reindex own account
        requestScopeOperations.setApiUser(user.id());
        gApi.accounts().self().index();
        accountIndexedCounter.assertReindexOf(user);
        // user cannot reindex any account
        AuthException thrown = assertThrows(AuthException.class, () -> gApi.accounts().id(admin.username()).index());
        assertThat(thrown).hasMessageThat().contains("modify account not permitted");
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AuthException(com.google.gerrit.extensions.restapi.AuthException) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 84 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class AccountIT method addExternalIdEmail.

private void addExternalIdEmail(TestAccount account, String email) throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        requireNonNull(email);
        accountsUpdateProvider.get().update("Add Email", account.id(), u -> u.addExternalId(externalIdFactory.createWithEmail(name("test"), email, account.id(), email)));
        accountIndexedCounter.assertReindexOf(account);
        requestScopeOperations.setApiUser(account.id());
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration)

Example 85 with Registration

use of com.google.gerrit.acceptance.ExtensionRegistry.Registration in project gerrit by GerritCodeReview.

the class AccountIT method shouldAllowQueryByEmailForInactiveUser.

@Test
public void shouldAllowQueryByEmailForInactiveUser() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        Account.Id activatableAccountId = accountOperations.newAccount().inactive().preferredEmail("foo@activatable.com").create();
        accountIndexedCounter.assertReindexOf(activatableAccountId, 1);
    }
    gApi.changes().query("owner:foo@activatable.com").get();
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.entities.Account) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)205 Test (org.junit.Test)200 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)194 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)90 AccountIndexedCounter (com.google.gerrit.acceptance.AccountIndexedCounter)47 RestResponse (com.google.gerrit.acceptance.RestResponse)39 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)38 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)31 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)23 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)22 TestAccount (com.google.gerrit.acceptance.TestAccount)19 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)19 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)16 RequestCancelledException (com.google.gerrit.server.cancellation.RequestCancelledException)15 Config (org.eclipse.jgit.lib.Config)14 BranchInput (com.google.gerrit.extensions.api.projects.BranchInput)12 CreateProjectArgs (com.google.gerrit.server.project.CreateProjectArgs)11 ProjectCreationValidationListener (com.google.gerrit.server.validators.ProjectCreationValidationListener)11 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 ImmutableList (com.google.common.collect.ImmutableList)10