Search in sources :

Example 86 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class UniversalGroupBackendTest method otherMemberships.

@Test
public void otherMemberships() {
    final AccountGroup.UUID handled = AccountGroup.uuid("handled");
    final AccountGroup.UUID notHandled = AccountGroup.uuid("not handled");
    final IdentifiedUser member = mock(IdentifiedUser.class);
    final IdentifiedUser notMember = mock(IdentifiedUser.class);
    GroupBackend backend = mock(GroupBackend.class);
    when(backend.handles(eq(handled))).thenReturn(true);
    when(backend.handles(not(eq(handled)))).thenReturn(false);
    when(backend.membershipsOf(any(IdentifiedUser.class))).thenAnswer(new Answer<GroupMembership>() {

        @Override
        public GroupMembership answer(InvocationOnMock invocation) {
            GroupMembership membership = mock(GroupMembership.class);
            when(membership.contains(eq(handled))).thenReturn(invocation.getArguments()[0] == member);
            when(membership.contains(eq(notHandled))).thenReturn(false);
            return membership;
        }
    });
    backends = new DynamicSet<>();
    backends.add("gerrit", backend);
    backend = new UniversalGroupBackend(new PluginSetContext<>(backends, PluginMetrics.DISABLED_INSTANCE));
    GroupMembership checker = backend.membershipsOf(member);
    assertFalse(checker.contains(REGISTERED_USERS));
    assertFalse(checker.contains(OTHER_UUID));
    assertTrue(checker.contains(handled));
    assertFalse(checker.contains(notHandled));
    checker = backend.membershipsOf(notMember);
    assertFalse(checker.contains(handled));
    assertFalse(checker.contains(notHandled));
}
Also used : SystemGroupBackend(com.google.gerrit.server.group.SystemGroupBackend) AccountGroup(com.google.gerrit.entities.AccountGroup) PluginSetContext(com.google.gerrit.server.plugincontext.PluginSetContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) UUID(com.google.gerrit.entities.AccountGroup.UUID) Test(org.junit.Test)

Example 87 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class CommitRewriterTest method fixRealUserFooterIdent.

@Test
public void fixRealUserFooterIdent() throws Exception {
    Change c = newChange();
    String realUserIdentToFix = getAccountIdentToFix(otherUser.getAccount());
    ObjectId invalidUpdateCommit = writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, "Comment on behalf of user", "Real-user: " + realUserIdentToFix), getAuthorIdent(changeOwner.getAccount()));
    IdentifiedUser impersonatedChangeOwner = this.userFactory.runAs(null, changeOwner.getAccountId(), requireNonNull(otherUser).getRealUser());
    ChangeUpdate impersonatedChangeMessageUpdate = newUpdate(c, impersonatedChangeOwner);
    impersonatedChangeMessageUpdate.setChangeMessage("Other comment on behalf of");
    impersonatedChangeMessageUpdate.commit();
    Ref metaRefBeforeRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
    ImmutableList<RevCommit> commitsBeforeRewrite = logMetaRef(repo, metaRefBeforeRewrite);
    int invalidCommitIndex = commitsBeforeRewrite.indexOf(invalidUpdateCommit);
    ChangeNotes notesBeforeRewrite = newNotes(c);
    RunOptions options = new RunOptions();
    options.dryRun = false;
    BackfillResult result = rewriter.backfillProject(project, repo, options);
    assertThat(result.fixedRefDiff.keySet()).containsExactly(RefNames.changeMetaRef(c.getId()));
    ChangeNotes notesAfterRewrite = newNotes(c);
    assertThat(changeMessages(notesBeforeRewrite)).containsExactly("Comment on behalf of user", "Other comment on behalf of");
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getAuthor()).isEqualTo(changeOwner.getAccountId());
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getRealAuthor()).isEqualTo(otherUser.getAccountId());
    assertThat(changeMessages(notesAfterRewrite)).containsExactly("Comment on behalf of user", "Other comment on behalf of");
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getAuthor()).isEqualTo(changeOwner.getAccountId());
    assertThat(notesBeforeRewrite.getChangeMessages().get(0).getRealAuthor()).isEqualTo(otherUser.getAccountId());
    Ref metaRefAfterRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
    assertThat(metaRefAfterRewrite.getObjectId()).isNotEqualTo(metaRefBeforeRewrite.getObjectId());
    ImmutableList<RevCommit> commitsAfterRewrite = logMetaRef(repo, metaRefAfterRewrite);
    assertValidCommits(commitsBeforeRewrite, commitsAfterRewrite, ImmutableList.of(invalidCommitIndex));
    assertFixedCommits(ImmutableList.of(invalidUpdateCommit), result, c.getId());
    List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
    assertThat(commitHistoryDiff).containsExactly("@@ -9 +9 @@\n" + "-Real-user: Other Account <2@gerrit>\n" + "+Real-user: Gerrit User 2 <2@gerrit>\n");
    BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
    assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
    assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Also used : BackfillResult(com.google.gerrit.server.notedb.CommitRewriter.BackfillResult) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Ref(org.eclipse.jgit.lib.Ref) RunOptions(com.google.gerrit.server.notedb.CommitRewriter.RunOptions) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 88 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class GerritPublicKeyCheckerTest method checkWithValidKeyButWrongExpectedUserInChecker.

@Test
public void checkWithValidKeyButWrongExpectedUserInChecker() throws Exception {
    // A---Bx
    // \
    // \---C---D
    // \
    // \---Ex
    // 
    // The server ultimately trusts B and D.
    // D and E trust C to be a valid introducer of depth 2.
    IdentifiedUser userB = addUser("userB");
    TestKey keyA = add(keyA(), user);
    TestKey keyB = add(keyB(), userB);
    add(keyC(), addUser("userC"));
    add(keyD(), addUser("userD"));
    add(keyE(), addUser("userE"));
    // Checker for A, checking B.
    PublicKeyChecker checkerA = checkerFactory.create(user, store);
    assertProblems(checkerA.check(keyB.getPublicKey()), Status.BAD, "Key is expired", "Key must contain a valid certification for one of the following" + " identities:\n" + "  gerrit:user\n" + "  mailto:testa@example.com\n" + "  testa@example.com\n" + "  username:user");
    // Checker for B, checking A.
    PublicKeyChecker checkerB = checkerFactory.create(userB, store);
    assertProblems(checkerB.check(keyA.getPublicKey()), Status.BAD, "Key must contain a valid certification for one of the following" + " identities:\n" + "  gerrit:userB\n" + "  mailto:testb@example.com\n" + "  testb@example.com\n" + "  username:userB");
}
Also used : TestKey(com.google.gerrit.gpg.testing.TestKey) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Test(org.junit.Test)

Example 89 with IdentifiedUser

use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.

the class AbstractQueryChangesTest method insert.

protected Change insert(TestRepository<Repo> repo, ChangeInserter ins, @Nullable Account.Id owner, Instant createdOn) throws Exception {
    Project.NameKey project = Project.nameKey(repo.getRepository().getDescription().getRepositoryName());
    Account.Id ownerId = owner != null ? owner : userId;
    IdentifiedUser user = userFactory.create(ownerId);
    try (BatchUpdate bu = updateFactory.create(project, user, createdOn)) {
        bu.insertChange(ins);
        bu.execute();
        return ins.getChange();
    }
}
Also used : Project(com.google.gerrit.entities.Project) Account(com.google.gerrit.entities.Account) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Aggregations

IdentifiedUser (com.google.gerrit.server.IdentifiedUser)89 AuthException (com.google.gerrit.extensions.restapi.AuthException)27 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)19 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)15 CurrentUser (com.google.gerrit.server.CurrentUser)13 IOException (java.io.IOException)13 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)13 Project (com.google.gerrit.entities.Project)12 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)12 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)12 Inject (com.google.inject.Inject)12 Singleton (com.google.inject.Singleton)12 ArrayList (java.util.ArrayList)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 Provider (com.google.inject.Provider)11 Change (com.google.gerrit.entities.Change)10 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)10 Repository (org.eclipse.jgit.lib.Repository)10 Account (com.google.gerrit.entities.Account)9 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)9