Search in sources :

Example 1 with AccountActivationListener

use of com.google.gerrit.extensions.events.AccountActivationListener in project gerrit by GerritCodeReview.

the class AccountIT method validateAccountActivation.

@Test
public void validateAccountActivation() throws Exception {
    Account.Id activatableAccountId = accountOperations.newAccount().inactive().preferredEmail("foo@activatable.com").create();
    Account.Id deactivatableAccountId = accountOperations.newAccount().preferredEmail("foo@deactivatable.com").create();
    AccountActivationValidationListener validationListener = new AccountActivationValidationListener() {

        @Override
        public void validateActivation(AccountState account) throws ValidationException {
            String preferredEmail = account.account().preferredEmail();
            if (preferredEmail == null || !preferredEmail.endsWith("@activatable.com")) {
                throw new ValidationException("not allowed to active account");
            }
        }

        @Override
        public void validateDeactivation(AccountState account) throws ValidationException {
            String preferredEmail = account.account().preferredEmail();
            if (preferredEmail == null || !preferredEmail.endsWith("@deactivatable.com")) {
                throw new ValidationException("not allowed to deactive account");
            }
        }
    };
    AccountActivationListener listener = mock(AccountActivationListener.class);
    try (Registration registration = extensionRegistry.newRegistration().add(validationListener).add(listener)) {
        /* Test account that can be activated, but not deactivated */
        // Deactivate account that is already inactive
        ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(activatableAccountId.get()).setActive(false));
        assertThat(thrown).hasMessageThat().isEqualTo("account not active");
        assertThat(accountOperations.account(activatableAccountId).get().active()).isFalse();
        verifyNoInteractions(listener);
        // Activate account that can be activated
        gApi.accounts().id(activatableAccountId.get()).setActive(true);
        assertThat(accountOperations.account(activatableAccountId).get().active()).isTrue();
        verify(listener).onAccountActivated(activatableAccountId.get());
        verifyNoMoreInteractions(listener);
        // Activate account that is already active
        gApi.accounts().id(activatableAccountId.get()).setActive(true);
        assertThat(accountOperations.account(activatableAccountId).get().active()).isTrue();
        verifyNoMoreInteractions(listener);
        // Try deactivating account that cannot be deactivated
        thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(activatableAccountId.get()).setActive(false));
        assertThat(thrown).hasMessageThat().isEqualTo("not allowed to deactive account");
        assertThat(accountOperations.account(activatableAccountId).get().active()).isTrue();
        verifyNoMoreInteractions(listener);
        /* Test account that can be deactivated, but not activated */
        // Activate account that is already inactive
        gApi.accounts().id(deactivatableAccountId.get()).setActive(true);
        assertThat(accountOperations.account(deactivatableAccountId).get().active()).isTrue();
        verifyNoMoreInteractions(listener);
        // Deactivate account that can be deactivated
        gApi.accounts().id(deactivatableAccountId.get()).setActive(false);
        assertThat(accountOperations.account(deactivatableAccountId).get().active()).isFalse();
        verify(listener).onAccountDeactivated(deactivatableAccountId.get());
        verifyNoMoreInteractions(listener);
        // Deactivate account that is already inactive
        thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(deactivatableAccountId.get()).setActive(false));
        assertThat(thrown).hasMessageThat().isEqualTo("account not active");
        assertThat(accountOperations.account(deactivatableAccountId).get().active()).isFalse();
        verifyNoMoreInteractions(listener);
        // Try activating account that cannot be activated
        thrown = assertThrows(ResourceConflictException.class, () -> gApi.accounts().id(deactivatableAccountId.get()).setActive(true));
        assertThat(thrown).hasMessageThat().isEqualTo("not allowed to active account");
        assertThat(accountOperations.account(deactivatableAccountId).get().active()).isFalse();
        verifyNoMoreInteractions(listener);
    }
}
Also used : TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.entities.Account) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ValidationException(com.google.gerrit.server.validators.ValidationException) AccountActivationListener(com.google.gerrit.extensions.events.AccountActivationListener) AccountActivationValidationListener(com.google.gerrit.server.validators.AccountActivationValidationListener) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) AccountState(com.google.gerrit.server.account.AccountState) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)1 TestAccount (com.google.gerrit.acceptance.TestAccount)1 Account (com.google.gerrit.entities.Account)1 AccountActivationListener (com.google.gerrit.extensions.events.AccountActivationListener)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)1 AccountState (com.google.gerrit.server.account.AccountState)1 AccountActivationValidationListener (com.google.gerrit.server.validators.AccountActivationValidationListener)1 ValidationException (com.google.gerrit.server.validators.ValidationException)1 Test (org.junit.Test)1