Search in sources :

Example 21 with AccountIndexedCounter

use of com.google.gerrit.acceptance.AccountIndexedCounter in project gerrit by GerritCodeReview.

the class AccountIT method fetchUserBranch.

@Test
public void fetchUserBranch() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        requestScopeOperations.setApiUser(user.id());
        TestRepository<InMemoryRepository> allUsersRepo = cloneProject(allUsers, user);
        String userRefName = RefNames.refsUsers(user.id());
        // remove default READ permissions
        try (ProjectConfigUpdate u = updateProject(allUsers)) {
            u.getConfig().upsertAccessSection(RefNames.REFS_USERS + "${" + RefPattern.USERID_SHARDED + "}", as -> {
                as.remove(Permission.builder(Permission.READ));
            });
            u.save();
        }
        // deny READ permission that is inherited from All-Projects
        projectOperations.project(allUsers).forUpdate().add(deny(Permission.READ).ref(RefNames.REFS + "*").group(ANONYMOUS_USERS)).update();
        // fetching user branch without READ permission fails
        assertThrows(TransportException.class, () -> fetch(allUsersRepo, userRefName + ":userRef"));
        // allow each user to read its own user branch
        projectOperations.project(allUsers).forUpdate().add(allow(Permission.READ).ref(RefNames.REFS_USERS + "${" + RefPattern.USERID_SHARDED + "}").group(REGISTERED_USERS)).update();
        // fetch user branch using refs/users/YY/XXXXXXX
        fetch(allUsersRepo, userRefName + ":userRef");
        Ref userRef = allUsersRepo.getRepository().exactRef("userRef");
        assertThat(userRef).isNotNull();
        // fetch user branch using refs/users/self
        fetch(allUsersRepo, RefNames.REFS_USERS_SELF + ":userSelfRef");
        Ref userSelfRef = allUsersRepo.getRepository().getRefDatabase().exactRef("userSelfRef");
        assertThat(userSelfRef).isNotNull();
        assertThat(userSelfRef.getObjectId()).isEqualTo(userRef.getObjectId());
        accountIndexedCounter.assertNoReindex();
        // fetching user branch of another user fails
        String otherUserRefName = RefNames.refsUsers(admin.id());
        TransportException thrown = assertThrows(TransportException.class, () -> fetch(allUsersRepo, otherUserRefName + ":otherUserRef"));
        assertThat(thrown).hasMessageThat().contains("Remote does not have " + otherUserRefName + " available for fetch.");
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) GitUtil.deleteRef(com.google.gerrit.acceptance.GitUtil.deleteRef) Ref(org.eclipse.jgit.lib.Ref) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) TransportException(org.eclipse.jgit.api.errors.TransportException) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 22 with AccountIndexedCounter

use of com.google.gerrit.acceptance.AccountIndexedCounter in project gerrit by GerritCodeReview.

the class AccountIT method create.

@Test
public void create() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        AccountInput input = new AccountInput();
        input.username = "foo";
        input.name = "Foo";
        input.email = "foo@example.com";
        AccountInfo accountInfo = gApi.accounts().create(input).get();
        assertThat(accountInfo._accountId).isNotNull();
        assertThat(accountInfo.username).isEqualTo(input.username);
        assertThat(accountInfo.name).isEqualTo(input.name);
        assertThat(accountInfo.email).isEqualTo(input.email);
        assertThat(accountInfo.status).isNull();
        assertThat(accountInfo.tags).isNull();
        Account.Id accountId = Account.id(accountInfo._accountId);
        accountIndexedCounter.assertReindexOf(accountId, 1);
        assertThat(externalIds.byAccount(accountId)).containsExactly(externalIdFactory.createUsername(input.username, accountId, null), externalIdFactory.createEmail(accountId, input.email));
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.entities.Account) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AccountInput(com.google.gerrit.extensions.api.accounts.AccountInput) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 23 with AccountIndexedCounter

use of com.google.gerrit.acceptance.AccountIndexedCounter in project gerrit by GerritCodeReview.

the class AccountIT method addEmailAndSetPreferred.

@Test
public void addEmailAndSetPreferred() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        String email = "foo.bar@example.com";
        EmailInput input = new EmailInput();
        input.email = email;
        input.noConfirmation = true;
        input.preferred = true;
        gApi.accounts().self().addEmail(input);
        // Account is reindexed twice; once on adding the new email,
        // and then again on setting the email preferred.
        accountIndexedCounter.assertReindexOf(admin, 2);
        String preferred = gApi.accounts().self().get().email;
        assertThat(preferred).isEqualTo(email);
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 24 with AccountIndexedCounter

use of com.google.gerrit.acceptance.AccountIndexedCounter in project gerrit by GerritCodeReview.

the class AccountIT method deleteEmailOfOtherUser.

@Test
public void deleteEmailOfOtherUser() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        String email = "foo.bar@example.com";
        EmailInput input = new EmailInput();
        input.email = email;
        input.noConfirmation = true;
        gApi.accounts().id(user.id().get()).addEmail(input);
        accountIndexedCounter.assertReindexOf(user);
        requestScopeOperations.setApiUser(user.id());
        assertThat(getEmails()).contains(email);
        // admin can delete email of user
        requestScopeOperations.setApiUser(admin.id());
        gApi.accounts().id(user.id().get()).deleteEmail(email);
        accountIndexedCounter.assertReindexOf(user);
        requestScopeOperations.setApiUser(user.id());
        assertThat(getEmails()).doesNotContain(email);
        // user cannot delete email of admin
        AuthException thrown = assertThrows(AuthException.class, () -> gApi.accounts().id(admin.id().get()).deleteEmail(admin.email()));
        assertThat(thrown).hasMessageThat().contains("modify account not permitted");
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AuthException(com.google.gerrit.extensions.restapi.AuthException) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) EmailInput(com.google.gerrit.extensions.api.accounts.EmailInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 25 with AccountIndexedCounter

use of com.google.gerrit.acceptance.AccountIndexedCounter in project gerrit by GerritCodeReview.

the class AccountIT method getByIntId.

@Test
public void getByIntId() throws Exception {
    AccountIndexedCounter accountIndexedCounter = new AccountIndexedCounter();
    try (Registration registration = extensionRegistry.newRegistration().add(accountIndexedCounter)) {
        AccountInfo info = gApi.accounts().id("admin").get();
        AccountInfo infoByIntId = gApi.accounts().id(info._accountId).get();
        assertThat(info.name).isEqualTo(infoByIntId.name);
        accountIndexedCounter.assertNoReindex();
    }
}
Also used : AccountIndexedCounter(com.google.gerrit.acceptance.AccountIndexedCounter) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

AccountIndexedCounter (com.google.gerrit.acceptance.AccountIndexedCounter)47 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)47 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)44 Test (org.junit.Test)44 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)18 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)18 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)16 TestAccount (com.google.gerrit.acceptance.TestAccount)13 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)13 Config (org.eclipse.jgit.lib.Config)13 EmailInput (com.google.gerrit.extensions.api.accounts.EmailInput)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 TestKey (com.google.gerrit.gpg.testing.TestKey)5 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)4 Message (com.google.gerrit.testing.FakeEmailSender.Message)4 UseSsh (com.google.gerrit.acceptance.UseSsh)3 Account (com.google.gerrit.entities.Account)3 GpgKeyInfo (com.google.gerrit.extensions.common.GpgKeyInfo)3 SshKeyInfo (com.google.gerrit.extensions.common.SshKeyInfo)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3