Support deletion of SSH keys in extension API

Use the new method to add a test assertion for SSH key deletion.

Change-Id: If7ddc88fd8387f1e85d78972310d7e1dbacb429a
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-05-02 20:29:23 +02:00
parent efec99df31
commit 3cf19f686a
3 changed files with 42 additions and 0 deletions

View File

@@ -340,6 +340,7 @@ public class AccountIT extends AbstractDaemonTest {
// 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);
String inital = AccountCreator.publicKey(admin.sshKey, admin.email);
assertThat(key.sshPublicKey).isEqualTo(inital);
@@ -350,11 +351,27 @@ public class AccountIT extends AbstractDaemonTest {
gApi.accounts().self().addSshKey(newKey);
info = gApi.accounts().self().listSshKeys();
assertThat(info).hasSize(2);
assertSequenceNumbers(info);
// Add an existing key again
gApi.accounts().self().addSshKey(inital);
info = gApi.accounts().self().listSshKeys();
assertThat(info).hasSize(3);
assertSequenceNumbers(info);
// Delete second key
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);
}
private void assertSequenceNumbers(List<SshKeyInfo> sshKeys) {
int seq = 1;
for (SshKeyInfo key : sshKeys) {
assertThat(key.seq).isEqualTo(seq++);
}
}
private PGPPublicKey getOnlyKeyFromStore(TestKey key) throws Exception {

View File

@@ -49,6 +49,7 @@ public interface AccountApi {
List<SshKeyInfo> listSshKeys() throws RestApiException;
SshKeyInfo addSshKey(String key) throws RestApiException;
void deleteSshKey(int seq) throws RestApiException;
Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException;
Map<String, GpgKeyInfo> putGpgKeys(List<String> add, List<String> remove)
@@ -128,6 +129,11 @@ public interface AccountApi {
throw new NotImplementedException();
}
@Override
public void deleteSshKey(int seq) throws RestApiException {
throw new NotImplementedException();
}
@Override
public Map<String, GpgKeyInfo> putGpgKeys(List<String> add,
List<String> remove) throws RestApiException {

View File

@@ -33,6 +33,7 @@ import com.google.gerrit.server.account.AccountLoader;
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.DeleteSshKey;
import com.google.gerrit.server.account.GetAvatar;
import com.google.gerrit.server.account.GetDiffPreferences;
import com.google.gerrit.server.account.GetEditPreferences;
@@ -41,6 +42,7 @@ import com.google.gerrit.server.account.GetSshKeys;
import com.google.gerrit.server.account.SetDiffPreferences;
import com.google.gerrit.server.account.SetEditPreferences;
import com.google.gerrit.server.account.SetPreferences;
import com.google.gerrit.server.account.SshKeys;
import com.google.gerrit.server.account.StarredChanges;
import com.google.gerrit.server.change.ChangeResource;
import com.google.gerrit.server.change.ChangesCollection;
@@ -75,6 +77,8 @@ public class AccountApiImpl implements AccountApi {
private final GpgApiAdapter gpgApiAdapter;
private final GetSshKeys getSshKeys;
private final AddSshKey addSshKey;
private final DeleteSshKey deleteSshKey;
private final SshKeys sshKeys;
@Inject
AccountApiImpl(AccountLoader.Factory ailf,
@@ -92,6 +96,8 @@ public class AccountApiImpl implements AccountApi {
GpgApiAdapter gpgApiAdapter,
GetSshKeys getSshKeys,
AddSshKey addSshKey,
DeleteSshKey deleteSshKey,
SshKeys sshKeys,
@Assisted AccountResource account) {
this.account = account;
this.accountLoaderFactory = ailf;
@@ -108,6 +114,8 @@ public class AccountApiImpl implements AccountApi {
this.createEmailFactory = createEmailFactory;
this.getSshKeys = getSshKeys;
this.addSshKey = addSshKey;
this.deleteSshKey = deleteSshKey;
this.sshKeys = sshKeys;
this.gpgApiAdapter = gpgApiAdapter;
}
@@ -241,6 +249,17 @@ public class AccountApiImpl implements AccountApi {
}
}
@Override
public void deleteSshKey(int seq) throws RestApiException {
try {
AccountResource.SshKey sshKeyRes =
sshKeys.parse(account, IdString.fromDecoded(Integer.toString(seq)));
deleteSshKey.apply(sshKeyRes, null);
} catch (OrmException e) {
throw new RestApiException("Cannot delete SSH key", e);
}
}
@Override
public Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException {
try {