use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.
the class AccountIT method assertKeys.
private void assertKeys(Iterable<TestKey> expectedKeys) throws Exception {
// Check via API.
FluentIterable<TestKey> expected = FluentIterable.from(expectedKeys);
Map<String, GpgKeyInfo> keyMap = gApi.accounts().self().listGpgKeys();
assertWithMessage("keys returned by listGpgKeys()").that(keyMap.keySet()).containsExactlyElementsIn(expected.transform(TestKey::getKeyIdString));
for (TestKey key : expected) {
assertKeyEquals(key, gApi.accounts().self().gpgKey(key.getKeyIdString()).get());
assertKeyEquals(key, gApi.accounts().self().gpgKey(Fingerprint.toString(key.getPublicKey().getFingerprint())).get());
assertKeyMapContains(key, keyMap);
}
// Check raw external IDs.
Account.Id currAccountId = atrScope.get().getUser().getAccountId();
Iterable<String> expectedFps = expected.transform(k -> BaseEncoding.base16().encode(k.getPublicKey().getFingerprint()));
Iterable<String> actualFps = externalIds.byAccount(currAccountId, SCHEME_GPGKEY).stream().map(e -> e.key().id()).collect(toSet());
assertWithMessage("external IDs in database").that(actualFps).containsExactlyElementsIn(expectedFps);
// Check raw stored keys.
for (TestKey key : expected) {
getOnlyKeyFromStore(key);
}
}
use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.
the class AccountIT method addOtherUsersGpgKey_Conflict.
@Test
public void addOtherUsersGpgKey_Conflict() throws Exception {
AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
// Both users have a matching external ID for this key.
addExternalIdEmail(admin, "test5@example.com");
accountIndexedCounter.clear();
accountsUpdateProvider.get().update("Add External ID", user.id(), u -> u.addExternalId(externalIdFactory.create("foo", "myId", user.id())));
accountIndexedCounter.assertReindexOf(user);
TestKey key = validKeyWithSecondUserId();
addGpgKey(key.getPublicKeyArmored());
requestScopeOperations.setApiUser(user.id());
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> addGpgKey(user, key.getPublicKeyArmored()));
assertThat(thrown).hasMessageThat().contains("GPG key already associated with another account");
}
}
use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.
the class AccountIT method adminCannotAddGpgKeyToOtherAccount.
@Test
public void adminCannotAddGpgKeyToOtherAccount() throws Exception {
TestKey key = validKeyWithoutExpiration();
addExternalIdEmail(user, "test1@example.com");
sender.clear();
requestScopeOperations.setApiUser(admin.id());
assertThrows(ResourceNotFoundException.class, () -> addGpgKey(user, key.getPublicKeyArmored()));
}
use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.
the class AccountIT method reAddExistingGpgKey.
@Test
public void reAddExistingGpgKey() throws Exception {
addExternalIdEmail(admin, "test5@example.com");
TestKey key = validKeyWithSecondUserId();
String id = key.getKeyIdString();
PGPPublicKey pk = key.getPublicKey();
sender.clear();
GpgKeyInfo info = addGpgKey(armor(pk)).get(id);
assertThat(info.userIds).hasSize(2);
assertIteratorSize(2, getOnlyKeyFromStore(key).getUserIDs());
assertThat(sender.getMessages()).hasSize(1);
assertThat(sender.getMessages().get(0).body()).contains("new GPG keys have been added");
pk = PGPPublicKey.removeCertification(pk, "foo:myId");
sender.clear();
info = addGpgKeyNoReindex(armor(pk)).get(id);
assertThat(info.userIds).hasSize(1);
assertIteratorSize(1, getOnlyKeyFromStore(key).getUserIDs());
// 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 GPG keys have been added");
}
use of com.google.gerrit.gpg.testing.TestKey in project gerrit by GerritCodeReview.
the class AccountIT method deleteGpgKey.
@Test
public void deleteGpgKey() throws Exception {
AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
TestKey key = validKeyWithoutExpiration();
String id = key.getKeyIdString();
addExternalIdEmail(admin, "test1@example.com");
addGpgKey(key.getPublicKeyArmored());
assertKeys(key);
accountIndexedCounter.clear();
sender.clear();
gApi.accounts().self().gpgKey(id).delete();
accountIndexedCounter.assertReindexOf(admin);
assertKeys();
assertThat(sender.getMessages()).hasSize(1);
assertThat(sender.getMessages().get(0).body()).contains("GPG keys have been deleted");
ResourceNotFoundException thrown = assertThrows(ResourceNotFoundException.class, () -> gApi.accounts().self().gpgKey(id).get());
assertThat(thrown).hasMessageThat().contains(id);
}
}
Aggregations