Search in sources :

Example 1 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class EmailIT method emailApi.

@Test
public void emailApi() throws Exception {
    String email = "foo@example.com";
    assertThat(getEmails()).doesNotContain(email);
    // Create email
    EmailInput emailInput = new EmailInput();
    emailInput.email = email;
    emailInput.noConfirmation = true;
    gApi.accounts().self().createEmail(emailInput);
    assertThat(getEmails()).contains(email);
    assertThat(gApi.accounts().self().get().email).isNotEqualTo(email);
    // Get email
    requestScopeOperations.resetCurrentApiUser();
    EmailApi emailApi = gApi.accounts().self().email(email);
    EmailInfo emailInfo = emailApi.get();
    assertThat(emailInfo.email).isEqualTo(email);
    assertThat(emailInfo.preferred).isNull();
    assertThat(emailInfo.pendingConfirmation).isNull();
    // Set as preferred email
    emailApi.setPreferred();
    assertThat(gApi.accounts().self().get().email).isEqualTo(email);
    // Get email again (now it's the preferred email)
    requestScopeOperations.resetCurrentApiUser();
    emailApi = gApi.accounts().self().email(email);
    emailInfo = emailApi.get();
    assertThat(emailInfo.email).isEqualTo(email);
    assertThat(emailInfo.preferred).isTrue();
    assertThat(emailInfo.pendingConfirmation).isNull();
    // Delete email
    emailApi.delete();
    assertThat(getEmails()).doesNotContain(email);
    // Now the email is no longer found
    requestScopeOperations.resetCurrentApiUser();
    assertThrows(ResourceNotFoundException.class, () -> gApi.accounts().self().email(email).get());
}
Also used : EmailApi(com.google.gerrit.extensions.api.accounts.EmailApi) EmailInfo(com.google.gerrit.extensions.common.EmailInfo) IdString(com.google.gerrit.extensions.restapi.IdString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 2 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class EmailIT method createEmail.

private void createEmail(String email) throws Exception {
    EmailInput input = new EmailInput();
    input.noConfirmation = true;
    RestResponse r = adminRestSession.put("/accounts/self/emails/" + email, input);
    r.assertCreated();
}
Also used : RestResponse(com.google.gerrit.acceptance.RestResponse) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput)

Example 3 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class AccountIT method addEmailSendsConfirmationEmail.

@Test
@GerritConfig(name = "auth.registerEmailPrivateKey", value = "HsOc6l_2lhS9G7sE_RsnS7Z6GJjdRDX14co=")
public void addEmailSendsConfirmationEmail() throws Exception {
    String email = "new.email@example.com";
    EmailInput input = newEmailInput(email, false);
    gApi.accounts().self().addEmail(input);
    assertThat(sender.getMessages()).hasSize(1);
    Message m = sender.getMessages().get(0);
    assertThat(m.rcpt()).containsExactly(Address.create(email));
}
Also used : Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Message(com.google.gerrit.testing.FakeEmailSender.Message) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 4 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput in project gerrit by GerritCodeReview.

the class AccountIT method cannotAddEmailAddressUsedByAnotherAccount.

@Test
public void cannotAddEmailAddressUsedByAnotherAccount() throws Exception {
    String email = "new.email@example.com";
    EmailInput input = newEmailInput(email);
    gApi.accounts().self().addEmail(input);
    ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(user.username()).addEmail(input));
    assertThat(thrown).hasMessageThat().contains("Identity 'mailto:" + email + "' in use by another account");
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) 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 5 with EmailInput

use of com.google.gerrit.extensions.api.accounts.EmailInput 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)

Aggregations

EmailInput (com.google.gerrit.extensions.api.accounts.EmailInput)24 Test (org.junit.Test)19 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)18 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)16 TestAccount (com.google.gerrit.acceptance.TestAccount)9 AccountIndexedCounter (com.google.gerrit.acceptance.AccountIndexedCounter)7 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)7 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)4 AccountDetailInfo (com.google.gerrit.extensions.common.AccountDetailInfo)4 EmailInfo (com.google.gerrit.extensions.common.EmailInfo)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)2 Sandboxed (com.google.gerrit.acceptance.Sandboxed)2 EmailException (com.google.gerrit.exceptions.EmailException)2 StopStrategies (com.github.rholder.retry.StopStrategies)1 FluentIterable (com.google.common.collect.FluentIterable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1