Search in sources :

Example 6 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class ProfileServiceIT method testUpdateAttributes.

@Test
@DirtiesContext
public void testUpdateAttributes() throws Exception {
    // Update a bunch attributes
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, false, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
    Map<String, Object> attributes = new HashMap<>();
    try {
        Map<String, Object> subscriptions = new HashMap<>();
        subscriptions.put("frequency", JDOE_SUBSCRIPTIONS_FREQUENCY);
        subscriptions.put("autoWatch", JDOE_SUBSCRIPTIONS_AUTO_WATCH);
        subscriptions.put("targets", JDOE_SUBSCRIPTIONS_TARGETS);
        attributes.put("subscriptions", subscriptions);
        profile = profileService.updateAttributes(profile.getId().toString(), attributes);
        attributes = profile.getAttributes();
        assertNotNull(attributes);
        assertEquals(1, attributes.size());
        subscriptions = (Map<String, Object>) attributes.get("subscriptions");
        assertNotNull(subscriptions);
        assertEquals(3, subscriptions.size());
        assertEquals(JDOE_SUBSCRIPTIONS_FREQUENCY, subscriptions.get("frequency"));
        assertEquals(JDOE_SUBSCRIPTIONS_AUTO_WATCH, subscriptions.get("autoWatch"));
        assertEquals(JDOE_SUBSCRIPTIONS_TARGETS, subscriptions.get("targets"));
        accessTokenIdResolver.setAccessTokenId(RANDOM_APP_ACCESS_TOKEN_ID);
        // Unallowed updates should be rejected
        try {
            profileService.updateAttributes(profile.getId().toString(), attributes);
            fail("Exception " + ProfileRestServiceException.class.getName() + " expected");
        } catch (ProfileRestServiceException e) {
            assertEquals(HttpStatus.FORBIDDEN, e.getStatus());
            assertEquals(ErrorCode.ACTION_DENIED, e.getErrorCode());
        }
    } finally {
        profileService.deleteProfile(profile.getId().toString());
    }
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 7 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class AuthenticationManagerImpl method authenticateUser.

@Override
public Authentication authenticateUser(Profile profile, boolean remembered) throws AuthenticationException {
    try {
        Ticket ticket = authenticationService.createTicket(profile.getId().toString());
        String ticketId = ticket.getId();
        DefaultAuthentication auth = new DefaultAuthentication(ticketId, profile, remembered);
        authenticationCache.putAuthentication(auth);
        logger.debug("Authentication successful for user '{}' (ticket ID = '{}')", ticket.getProfileId(), ticketId);
        return auth;
    } catch (ProfileRestServiceException e) {
        if (e.getErrorCode() == ErrorCode.DISABLED_PROFILE) {
            throw new DisabledUserException("User is disabled", e);
        } else {
            throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
        }
    } catch (ProfileException e) {
        throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
    }
}
Also used : Ticket(org.craftercms.profile.api.Ticket) ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) AuthenticationSystemException(org.craftercms.security.exception.AuthenticationSystemException) ProfileException(org.craftercms.profile.api.exceptions.ProfileException) DisabledUserException(org.craftercms.security.exception.DisabledUserException)

Example 8 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class AuthenticationManagerImplTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(authenticationService.authenticate(TENANT1, USERNAME1, PASSWORD1)).thenReturn(getTicket1());
    when(authenticationService.authenticate(TENANT2, USERNAME2, PASSWORD2)).thenReturn(getTicket2());
    doThrow(new ProfileRestServiceException(HttpStatus.UNAUTHORIZED, ErrorCode.BAD_CREDENTIALS, "")).when(authenticationService).authenticate(TENANT1, USERNAME2, PASSWORD2);
    doThrow(new ProfileRestServiceException(HttpStatus.FORBIDDEN, ErrorCode.DISABLED_PROFILE, "")).when(authenticationService).authenticate(TENANT1, DISABLED_USERNAME, PASSWORD1);
    when(profileService.getProfile(PROFILE_ID1.toString(), new String[0])).thenReturn(getProfile1());
    when(profileService.getProfile(PROFILE_ID2.toString(), new String[0])).thenReturn(getProfile2());
    when(profileService.getProfileByTicket(TICKET_ID1, new String[0])).thenReturn(getProfile1());
    doThrow(new ProfileRestServiceException(HttpStatus.BAD_REQUEST, ErrorCode.NO_SUCH_TICKET, "")).when(profileService).getProfileByTicket(INVALID_TICKET_ID, new String[0]);
    when(authenticationCache.getAuthentication(TICKET_ID1)).thenReturn(getAuthentication1());
    authenticationManager = new AuthenticationManagerImpl();
    authenticationManager.setAuthenticationService(authenticationService);
    authenticationManager.setProfileService(profileService);
    authenticationManager.setAuthenticationCache(authenticationCache);
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) Before(org.junit.Before)

Example 9 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class AbstractProfileRestClientBase method handleRestServiceException.

protected void handleRestServiceException(RestServiceException e) throws ProfileException {
    if (e.getErrorDetails() instanceof ErrorDetails) {
        ErrorDetails errorDetails = (ErrorDetails) e.getErrorDetails();
        HttpStatus responseStatus = e.getResponseStatus();
        ErrorCode errorCode = errorDetails.getErrorCode();
        String message = errorDetails.getMessage();
        throw new ProfileRestServiceException(responseStatus, errorCode, message);
    } else {
        HttpStatus responseStatus = e.getResponseStatus();
        String message = e.getErrorDetails().toString();
        throw new ProfileRestServiceException(responseStatus, message);
    }
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) HttpStatus(org.springframework.http.HttpStatus) ErrorDetails(org.craftercms.profile.api.exceptions.ErrorDetails) ErrorCode(org.craftercms.profile.api.exceptions.ErrorCode)

Aggregations

ProfileRestServiceException (org.craftercms.profile.exceptions.ProfileRestServiceException)9 Profile (org.craftercms.profile.api.Profile)4 Test (org.junit.Test)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 DirtiesContext (org.springframework.test.annotation.DirtiesContext)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Ticket (org.craftercms.profile.api.Ticket)2 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)2 AuthenticationSystemException (org.craftercms.security.exception.AuthenticationSystemException)2 DisabledUserException (org.craftercms.security.exception.DisabledUserException)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Tenant (org.craftercms.profile.api.Tenant)1 ErrorCode (org.craftercms.profile.api.exceptions.ErrorCode)1 ErrorDetails (org.craftercms.profile.api.exceptions.ErrorDetails)1 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)1 BadCredentialsException (org.craftercms.security.exception.BadCredentialsException)1 Before (org.junit.Before)1 HttpStatus (org.springframework.http.HttpStatus)1