Search in sources :

Example 16 with OrcidUnauthorizedException

use of org.orcid.core.exception.OrcidUnauthorizedException in project ORCID-Source by ORCID.

the class SalesForceManagerImplTest method testCheckContactUpdatePermissions.

@Test
public void testCheckContactUpdatePermissions() {
    List<Contact> existingContacts = new ArrayList<>();
    existingContacts.add(createContactWithRole("contact1", "account1", "contact1@test.com", "0000-0000-0000-0001", "role1", ContactRoleType.MAIN_CONTACT));
    existingContacts.add(createContactWithRole("contact2", "account1", "contact2@test.com", "0000-0000-0000-0002", "role2", ContactRoleType.AGREEMENT_SIGNATORY));
    existingContacts.add(createContactWithRole("contact3", "account1", "contact3@test.com", "0000-0000-0000-0003", "role3", ContactRoleType.INVOICE_CONTACT));
    Contact votingContact = createContactWithRole("contact4", "account1", "contact4@test.com", "0000-0000-0000-0004", "role4", ContactRoleType.OTHER_CONTACT);
    votingContact.getRole().setVotingContact(true);
    existingContacts.add(votingContact);
    List<Contact> updatedContacts = new ArrayList<>();
    Contact updatedContact1 = createContactWithRole("contact1", "account1", "contact1@test.com", "0000-0000-0000-0001", "role1", ContactRoleType.MAIN_CONTACT);
    updatedContacts.add(updatedContact1);
    updatedContacts.add(createContactWithRole("contact2", "account1", "contact2@test.com", "0000-0000-0000-0002", "role2", ContactRoleType.AGREEMENT_SIGNATORY));
    updatedContacts.add(createContactWithRole("contact3", "account1", "contact3@test.com", "0000-0000-0000-0003", "role3", ContactRoleType.INVOICE_CONTACT));
    Contact updatedVotingContact = createContactWithRole("contact4", "account1", "contact4@test.com", "0000-0000-0000-0004", "role4", ContactRoleType.OTHER_CONTACT);
    updatedVotingContact.getRole().setVotingContact(true);
    updatedContacts.add(updatedVotingContact);
    when(sourceManager.retrieveRealUserOrcid()).thenReturn("0000-0000-0000-0001");
    salesForceManager.checkContactUpdatePermissions(existingContacts, updatedContacts);
    updatedContact1.getRole().setRoleType(ContactRoleType.OTHER_CONTACT);
    when(sourceManager.retrieveRealUserOrcid()).thenReturn("0000-0000-0000-0001");
    try {
        salesForceManager.checkContactUpdatePermissions(existingContacts, updatedContacts);
    } catch (OrcidUnauthorizedException e) {
        fail("Should be able to change main contact role when am main contact");
    }
    when(sourceManager.retrieveRealUserOrcid()).thenReturn("0000-0000-0000-0003");
    boolean preventedChange = false;
    try {
        salesForceManager.checkContactUpdatePermissions(existingContacts, updatedContacts);
    } catch (OrcidUnauthorizedException e) {
        preventedChange = true;
    }
    if (!preventedChange) {
        fail("Should not be able to change main contact role when am not main/signatory contact");
    }
    // Set role back to what it was, but change voting contact.
    updatedContact1.getRole().setRoleType(ContactRoleType.MAIN_CONTACT);
    updatedContact1.getRole().setVotingContact(true);
    updatedVotingContact.getRole().setVotingContact(false);
    when(sourceManager.retrieveRealUserOrcid()).thenReturn("0000-0000-0000-0001");
    try {
        salesForceManager.checkContactUpdatePermissions(existingContacts, updatedContacts);
    } catch (OrcidUnauthorizedException e) {
        fail("Should be able to change voting contact when am main contact");
    }
    when(sourceManager.retrieveRealUserOrcid()).thenReturn("0000-0000-0000-0003");
    preventedChange = false;
    try {
        salesForceManager.checkContactUpdatePermissions(existingContacts, updatedContacts);
    } catch (OrcidUnauthorizedException e) {
        preventedChange = true;
    }
    if (!preventedChange) {
        fail("Should not be able to change voting contact when am not main/signatory contact");
    }
}
Also used : OrcidUnauthorizedException(org.orcid.core.exception.OrcidUnauthorizedException) ArrayList(java.util.ArrayList) Contact(org.orcid.core.salesforce.model.Contact) Test(org.junit.Test)

Aggregations

OrcidUnauthorizedException (org.orcid.core.exception.OrcidUnauthorizedException)16 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)4 Authentication (org.springframework.security.core.Authentication)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 AccessControlException (java.security.AccessControlException)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 NoResultException (javax.persistence.NoResultException)2 Response (javax.ws.rs.core.Response)2 OrcidString (org.orcid.core.cache.OrcidString)2 DeactivatedException (org.orcid.core.exception.DeactivatedException)2 OrcidAccessControlException (org.orcid.core.exception.OrcidAccessControlException)2 OrcidDeprecatedException (org.orcid.core.exception.OrcidDeprecatedException)2 OrcidNotClaimedException (org.orcid.core.exception.OrcidNotClaimedException)2 OrcidVisibilityException (org.orcid.core.exception.OrcidVisibilityException)2 WrongSourceException (org.orcid.core.exception.WrongSourceException)2 Contact (org.orcid.core.salesforce.model.Contact)2 MemberDetails (org.orcid.core.salesforce.model.MemberDetails)2 LockedException (org.orcid.core.security.aop.LockedException)2 BulkElement (org.orcid.jaxb.model.record.bulk.BulkElement)2