AccountApi: Add methods to get emails and delete email

Also add a test for email deletion and add asserts for the addEmail()
test.

Bug: Issue 5385
Change-Id: I3e192638fdaac58d0cdaa535430bb968d7d32abb
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2017-02-08 13:38:15 +01:00
parent bf94427742
commit e1f9dd1120
8 changed files with 114 additions and 24 deletions

View File

@ -29,6 +29,7 @@ import static com.google.gerrit.server.StarredChangesUtil.IGNORE_LABEL;
import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toSet;
import static org.junit.Assert.fail;
import com.google.common.collect.FluentIterable;
@ -54,6 +55,7 @@ import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.gpg.Fingerprint;
import com.google.gerrit.gpg.PublicKeyStore;
import com.google.gerrit.gpg.server.GpgKeys;
@ -80,6 +82,7 @@ import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
@ -374,24 +377,19 @@ public class AccountIT extends AbstractDaemonTest {
// Not in the list of TLDs but added to override in OutgoingEmailValidator
"new.email@example.local");
Set<String> currentEmails = getEmails();
for (String email : emails) {
assertThat(currentEmails).doesNotContain(email);
EmailInput input = new EmailInput();
input.email = email;
input.noConfirmation = true;
gApi.accounts().self().addEmail(input);
}
}
@Test
public void putStatus() throws Exception {
List<String> statuses = ImmutableList.of("OOO", "Busy");
AccountInfo info;
for (String status : statuses) {
gApi.accounts().self().setStatus(status);
admin.status = status;
info = gApi.accounts().self().get();
assertUser(info, admin);
}
// enforce a new request context so that emails that are cached in
// IdentifiedUser are reloaded
setApiUser(admin);
assertThat(getEmails()).containsAllIn(emails);
}
@Test
@ -422,6 +420,39 @@ public class AccountIT extends AbstractDaemonTest {
}
}
@Test
public void deleteEmail() throws Exception {
String email = "foo.bar@example.com";
EmailInput input = new EmailInput();
input.email = email;
input.noConfirmation = true;
gApi.accounts().self().addEmail(input);
// enforce a new request context so that emails that are cached in
// IdentifiedUser are reloaded
setApiUser(admin);
assertThat(getEmails()).contains(email);
gApi.accounts().self().deleteEmail(input.email);
// enforce a new request context so that emails that are cached in
// IdentifiedUser are reloaded
setApiUser(admin);
assertThat(getEmails()).doesNotContain(email);
}
@Test
public void putStatus() throws Exception {
List<String> statuses = ImmutableList.of("OOO", "Busy");
AccountInfo info;
for (String status : statuses) {
gApi.accounts().self().setStatus(status);
admin.status = status;
info = gApi.accounts().self().get();
assertUser(info, admin);
}
}
@Test
public void fetchUserBranch() throws Exception {
// change something in the user preferences to ensure that the user branch
@ -851,4 +882,8 @@ public class AccountIT extends AbstractDaemonTest {
assertThat(info.username).isEqualTo(account.username);
assertThat(info.status).isEqualTo(account.status);
}
private Set<String> getEmails() throws RestApiException {
return gApi.accounts().self().getEmails().stream().map(e -> e.email).collect(toSet());
}
}

View File

@ -23,6 +23,7 @@ import com.google.gerrit.extensions.common.AccountExternalIdInfo;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.common.AgreementInfo;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.common.GpgKeyInfo;
import com.google.gerrit.extensions.common.SshKeyInfo;
import com.google.gerrit.extensions.restapi.NotImplementedException;
@ -68,8 +69,12 @@ public interface AccountApi {
List<ChangeInfo> getStarredChanges() throws RestApiException;
List<EmailInfo> getEmails() throws RestApiException;
void addEmail(EmailInput input) throws RestApiException;
void deleteEmail(String email) throws RestApiException;
void setStatus(String status) throws RestApiException;
List<SshKeyInfo> listSshKeys() throws RestApiException;
@ -189,11 +194,21 @@ public interface AccountApi {
throw new NotImplementedException();
}
@Override
public List<EmailInfo> getEmails() {
throw new NotImplementedException();
}
@Override
public void addEmail(EmailInput input) {
throw new NotImplementedException();
}
@Override
public void deleteEmail(String email) {
throw new NotImplementedException();
}
@Override
public void setStatus(String status) {
throw new NotImplementedException();

View File

@ -0,0 +1,25 @@
// Copyright (C) 2017 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.extensions.common;
public class EmailInfo {
public String email;
public Boolean preferred;
public Boolean pendingConfirmation;
public void preferred(String e) {
this.preferred = e != null && e.equals(email) ? true : null;
}
}

View File

@ -19,6 +19,7 @@ import static com.google.gerrit.extensions.client.AuthType.DEVELOPMENT_BECOME_AN
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.extensions.api.accounts.EmailInput;
import com.google.gerrit.extensions.client.AccountFieldName;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
@ -28,7 +29,6 @@ import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.GetEmails.EmailInfo;
import com.google.gerrit.server.config.AuthConfig;
import com.google.gerrit.server.mail.send.OutgoingEmailValidator;
import com.google.gerrit.server.mail.send.RegisterNewEmailSender;

View File

@ -14,8 +14,8 @@
package com.google.gerrit.server.account;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.account.GetEmails.EmailInfo;
import com.google.inject.Inject;
import com.google.inject.Singleton;

View File

@ -14,6 +14,7 @@
package com.google.gerrit.server.account;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.inject.Singleton;
import java.util.ArrayList;
@ -45,14 +46,4 @@ public class GetEmails implements RestReadView<AccountResource> {
});
return emails;
}
public static class EmailInfo {
public String email;
public Boolean preferred;
public Boolean pendingConfirmation;
void preferred(String e) {
this.preferred = e != null && e.equals(email) ? true : null;
}
}
}

View File

@ -31,6 +31,7 @@ import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.common.AgreementInfo;
import com.google.gerrit.extensions.common.AgreementInput;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.common.GpgKeyInfo;
import com.google.gerrit.extensions.common.SshKeyInfo;
import com.google.gerrit.extensions.restapi.IdString;
@ -43,6 +44,7 @@ import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AddSshKey;
import com.google.gerrit.server.account.CreateEmail;
import com.google.gerrit.server.account.DeleteActive;
import com.google.gerrit.server.account.DeleteEmail;
import com.google.gerrit.server.account.DeleteExternalIds;
import com.google.gerrit.server.account.DeleteSshKey;
import com.google.gerrit.server.account.DeleteWatchedProjects;
@ -51,6 +53,7 @@ import com.google.gerrit.server.account.GetAgreements;
import com.google.gerrit.server.account.GetAvatar;
import com.google.gerrit.server.account.GetDiffPreferences;
import com.google.gerrit.server.account.GetEditPreferences;
import com.google.gerrit.server.account.GetEmails;
import com.google.gerrit.server.account.GetExternalIds;
import com.google.gerrit.server.account.GetPreferences;
import com.google.gerrit.server.account.GetSshKeys;
@ -100,7 +103,9 @@ public class AccountApiImpl implements AccountApi {
private final Stars stars;
private final Stars.Get starsGet;
private final Stars.Post starsPost;
private final GetEmails getEmails;
private final CreateEmail.Factory createEmailFactory;
private final DeleteEmail deleteEmail;
private final GpgApiAdapter gpgApiAdapter;
private final GetSshKeys getSshKeys;
private final AddSshKey addSshKey;
@ -135,7 +140,9 @@ public class AccountApiImpl implements AccountApi {
Stars stars,
Stars.Get starsGet,
Stars.Post starsPost,
GetEmails getEmails,
CreateEmail.Factory createEmailFactory,
DeleteEmail deleteEmail,
GpgApiAdapter gpgApiAdapter,
GetSshKeys getSshKeys,
AddSshKey addSshKey,
@ -169,7 +176,9 @@ public class AccountApiImpl implements AccountApi {
this.stars = stars;
this.starsGet = starsGet;
this.starsPost = starsPost;
this.getEmails = getEmails;
this.createEmailFactory = createEmailFactory;
this.deleteEmail = deleteEmail;
this.getSshKeys = getSshKeys;
this.addSshKey = addSshKey;
this.deleteSshKey = deleteSshKey;
@ -353,6 +362,11 @@ public class AccountApiImpl implements AccountApi {
}
}
@Override
public List<EmailInfo> getEmails() {
return getEmails.apply(account);
}
@Override
public void addEmail(EmailInput input) throws RestApiException {
AccountResource.Email rsrc = new AccountResource.Email(account.getUser(), input.email);
@ -363,6 +377,16 @@ public class AccountApiImpl implements AccountApi {
}
}
@Override
public void deleteEmail(String email) throws RestApiException {
AccountResource.Email rsrc = new AccountResource.Email(account.getUser(), email);
try {
deleteEmail.apply(rsrc, null);
} catch (OrmException | IOException e) {
throw new RestApiException("Cannot delete email", e);
}
}
@Override
public void setStatus(String status) throws RestApiException {
PutStatus.Input in = new PutStatus.Input(status);

View File

@ -22,6 +22,7 @@ import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.api.accounts.EmailInput;
import com.google.gerrit.extensions.common.EmailInfo;
import com.google.gerrit.extensions.common.SshKeyInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
@ -36,7 +37,6 @@ import com.google.gerrit.server.account.DeleteActive;
import com.google.gerrit.server.account.DeleteEmail;
import com.google.gerrit.server.account.DeleteSshKey;
import com.google.gerrit.server.account.GetEmails;
import com.google.gerrit.server.account.GetEmails.EmailInfo;
import com.google.gerrit.server.account.GetSshKeys;
import com.google.gerrit.server.account.PutActive;
import com.google.gerrit.server.account.PutHttpPassword;