Search in sources :

Example 46 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountManagerIT method cannotLinkEmailThatIsAlreadyUsed.

@Test
public void cannotLinkEmailThatIsAlreadyUsed() throws Exception {
    String email = "foo@example.com";
    // Create an account with an SCHEME_EXTERNAL external ID that occupies the email.
    String username = "foo";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key externalExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_EXTERNAL, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.addExternalId(externalIdFactory.createWithEmail(externalExtIdKey, accountId, email)));
    // Create another account with a SCHEME_GERRIT external ID and no email
    String username2 = "foo";
    Account.Id accountId2 = Account.id(seq.nextAccountId());
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username2);
    accountsUpdate.insert("Create Test Account", accountId2, u -> u.addExternalId(externalIdFactory.create(gerritExtIdKey, accountId2)));
    // Try to link the email to the second account (via a new MAILTO external ID) and expect that
    // this fails because the email is already assigned to the first account.
    AuthRequest who = authRequestFactory.createForEmail(email);
    AccountException thrown = assertThrows(AccountException.class, () -> accountManager.link(accountId2, who));
    assertThat(thrown).hasMessageThat().contains("Email 'foo@example.com' in use by another account");
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) AccountException(com.google.gerrit.server.account.AccountException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 47 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountManagerIT method authenticateWithUsernameAndUpdateEmail.

@Test
public void authenticateWithUsernameAndUpdateEmail() throws Exception {
    String username = "foo";
    String email = "foo@example.com";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.setPreferredEmail(email).addExternalId(externalIdFactory.createWithEmail(gerritExtIdKey, accountId, email)));
    AuthRequest who = authRequestFactory.createForUser(username);
    String newEmail = "bar@example.com";
    who.setEmailAddress(newEmail);
    AuthResult authResult = accountManager.authenticate(who);
    assertAuthResultForExistingAccount(authResult, accountId, gerritExtIdKey);
    Optional<ExternalId> gerritExtId = externalIds.get(gerritExtIdKey);
    assertThat(gerritExtId).isPresent();
    assertThat(gerritExtId.get().email()).isEqualTo(newEmail);
    Optional<AccountState> accountState = accounts.get(accountId);
    assertThat(accountState).isPresent();
    assertThat(accountState.get().account().preferredEmail()).isEqualTo(newEmail);
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthResult(com.google.gerrit.server.account.AuthResult) AccountState(com.google.gerrit.server.account.AccountState) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 48 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountManagerIT method cannotAuthenticateWithInactiveAccount.

@Test
public void cannotAuthenticateWithInactiveAccount() throws Exception {
    String username = "foo";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.setActive(false).addExternalId(externalIdFactory.create(gerritExtIdKey, accountId)));
    AuthRequest who = authRequestFactory.createForUser(username);
    AccountException thrown = assertThrows(AccountException.class, () -> accountManager.authenticate(who));
    assertThat(thrown).hasMessageThat().contains("Authentication error, account inactive");
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) AccountException(com.google.gerrit.server.account.AccountException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 49 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountManagerIT method authenticateWithUsernameAndUpdateDisplayName.

private void authenticateWithUsernameAndUpdateDisplayName(AccountManager am) throws Exception {
    String username = "foo";
    String email = "foo@example.com";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.setFullName("Initial Name").setPreferredEmail(email).addExternalId(externalIdFactory.createWithEmail(gerritExtIdKey, accountId, email)));
    AuthRequest who = authRequestFactory.createForUser(username);
    String newName = "Updated Name";
    who.setDisplayName(newName);
    AuthResult authResult = am.authenticate(who);
    assertAuthResultForExistingAccount(authResult, accountId, gerritExtIdKey);
    Optional<AccountState> accountState = accounts.get(accountId);
    assertThat(accountState).isPresent();
    assertThat(accountState.get().account().fullName()).isEqualTo(newName);
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthResult(com.google.gerrit.server.account.AuthResult) AccountState(com.google.gerrit.server.account.AccountState)

Example 50 with Account

use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.

the class AccountManagerIT method authenticateWithUsername.

@Test
public void authenticateWithUsername() throws Exception {
    String username = "foo";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.addExternalId(externalIdFactory.create(gerritExtIdKey, accountId)));
    AuthRequest who = authRequestFactory.createForUser(username);
    AuthResult authResult = accountManager.authenticate(who);
    assertAuthResultForExistingAccount(authResult, accountId, gerritExtIdKey);
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthResult(com.google.gerrit.server.account.AuthResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

Account (com.google.gerrit.entities.Account)124 Test (org.junit.Test)59 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)37 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)35 AccountState (com.google.gerrit.server.account.AccountState)35 IOException (java.io.IOException)31 Repository (org.eclipse.jgit.lib.Repository)31 Change (com.google.gerrit.entities.Change)28 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)26 Inject (com.google.inject.Inject)25 PersonIdent (org.eclipse.jgit.lib.PersonIdent)25 List (java.util.List)24 ArrayList (java.util.ArrayList)23 HashSet (java.util.HashSet)23 Set (java.util.Set)22 RefNames (com.google.gerrit.entities.RefNames)21 AuthRequest (com.google.gerrit.server.account.AuthRequest)21 Map (java.util.Map)21 ObjectId (org.eclipse.jgit.lib.ObjectId)21 ImmutableList (com.google.common.collect.ImmutableList)20