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:
@@ -340,6 +340,7 @@ public class AccountIT extends AbstractDaemonTest {
|
|||||||
// The test account should initially have exactly one ssh key
|
// The test account should initially have exactly one ssh key
|
||||||
List<SshKeyInfo> info = gApi.accounts().self().listSshKeys();
|
List<SshKeyInfo> info = gApi.accounts().self().listSshKeys();
|
||||||
assertThat(info).hasSize(1);
|
assertThat(info).hasSize(1);
|
||||||
|
assertSequenceNumbers(info);
|
||||||
SshKeyInfo key = info.get(0);
|
SshKeyInfo key = info.get(0);
|
||||||
String inital = AccountCreator.publicKey(admin.sshKey, admin.email);
|
String inital = AccountCreator.publicKey(admin.sshKey, admin.email);
|
||||||
assertThat(key.sshPublicKey).isEqualTo(inital);
|
assertThat(key.sshPublicKey).isEqualTo(inital);
|
||||||
@@ -350,11 +351,27 @@ public class AccountIT extends AbstractDaemonTest {
|
|||||||
gApi.accounts().self().addSshKey(newKey);
|
gApi.accounts().self().addSshKey(newKey);
|
||||||
info = gApi.accounts().self().listSshKeys();
|
info = gApi.accounts().self().listSshKeys();
|
||||||
assertThat(info).hasSize(2);
|
assertThat(info).hasSize(2);
|
||||||
|
assertSequenceNumbers(info);
|
||||||
|
|
||||||
// Add an existing key again
|
// Add an existing key again
|
||||||
gApi.accounts().self().addSshKey(inital);
|
gApi.accounts().self().addSshKey(inital);
|
||||||
info = gApi.accounts().self().listSshKeys();
|
info = gApi.accounts().self().listSshKeys();
|
||||||
assertThat(info).hasSize(3);
|
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 {
|
private PGPPublicKey getOnlyKeyFromStore(TestKey key) throws Exception {
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ public interface AccountApi {
|
|||||||
|
|
||||||
List<SshKeyInfo> listSshKeys() throws RestApiException;
|
List<SshKeyInfo> listSshKeys() throws RestApiException;
|
||||||
SshKeyInfo addSshKey(String key) throws RestApiException;
|
SshKeyInfo addSshKey(String key) throws RestApiException;
|
||||||
|
void deleteSshKey(int seq) throws RestApiException;
|
||||||
|
|
||||||
Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException;
|
Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException;
|
||||||
Map<String, GpgKeyInfo> putGpgKeys(List<String> add, List<String> remove)
|
Map<String, GpgKeyInfo> putGpgKeys(List<String> add, List<String> remove)
|
||||||
@@ -128,6 +129,11 @@ public interface AccountApi {
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteSshKey(int seq) throws RestApiException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, GpgKeyInfo> putGpgKeys(List<String> add,
|
public Map<String, GpgKeyInfo> putGpgKeys(List<String> add,
|
||||||
List<String> remove) throws RestApiException {
|
List<String> remove) throws RestApiException {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import com.google.gerrit.server.account.AccountLoader;
|
|||||||
import com.google.gerrit.server.account.AccountResource;
|
import com.google.gerrit.server.account.AccountResource;
|
||||||
import com.google.gerrit.server.account.AddSshKey;
|
import com.google.gerrit.server.account.AddSshKey;
|
||||||
import com.google.gerrit.server.account.CreateEmail;
|
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.GetAvatar;
|
||||||
import com.google.gerrit.server.account.GetDiffPreferences;
|
import com.google.gerrit.server.account.GetDiffPreferences;
|
||||||
import com.google.gerrit.server.account.GetEditPreferences;
|
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.SetDiffPreferences;
|
||||||
import com.google.gerrit.server.account.SetEditPreferences;
|
import com.google.gerrit.server.account.SetEditPreferences;
|
||||||
import com.google.gerrit.server.account.SetPreferences;
|
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.account.StarredChanges;
|
||||||
import com.google.gerrit.server.change.ChangeResource;
|
import com.google.gerrit.server.change.ChangeResource;
|
||||||
import com.google.gerrit.server.change.ChangesCollection;
|
import com.google.gerrit.server.change.ChangesCollection;
|
||||||
@@ -75,6 +77,8 @@ public class AccountApiImpl implements AccountApi {
|
|||||||
private final GpgApiAdapter gpgApiAdapter;
|
private final GpgApiAdapter gpgApiAdapter;
|
||||||
private final GetSshKeys getSshKeys;
|
private final GetSshKeys getSshKeys;
|
||||||
private final AddSshKey addSshKey;
|
private final AddSshKey addSshKey;
|
||||||
|
private final DeleteSshKey deleteSshKey;
|
||||||
|
private final SshKeys sshKeys;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AccountApiImpl(AccountLoader.Factory ailf,
|
AccountApiImpl(AccountLoader.Factory ailf,
|
||||||
@@ -92,6 +96,8 @@ public class AccountApiImpl implements AccountApi {
|
|||||||
GpgApiAdapter gpgApiAdapter,
|
GpgApiAdapter gpgApiAdapter,
|
||||||
GetSshKeys getSshKeys,
|
GetSshKeys getSshKeys,
|
||||||
AddSshKey addSshKey,
|
AddSshKey addSshKey,
|
||||||
|
DeleteSshKey deleteSshKey,
|
||||||
|
SshKeys sshKeys,
|
||||||
@Assisted AccountResource account) {
|
@Assisted AccountResource account) {
|
||||||
this.account = account;
|
this.account = account;
|
||||||
this.accountLoaderFactory = ailf;
|
this.accountLoaderFactory = ailf;
|
||||||
@@ -108,6 +114,8 @@ public class AccountApiImpl implements AccountApi {
|
|||||||
this.createEmailFactory = createEmailFactory;
|
this.createEmailFactory = createEmailFactory;
|
||||||
this.getSshKeys = getSshKeys;
|
this.getSshKeys = getSshKeys;
|
||||||
this.addSshKey = addSshKey;
|
this.addSshKey = addSshKey;
|
||||||
|
this.deleteSshKey = deleteSshKey;
|
||||||
|
this.sshKeys = sshKeys;
|
||||||
this.gpgApiAdapter = gpgApiAdapter;
|
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
|
@Override
|
||||||
public Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException {
|
public Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user