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());
}
}
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);
}
}
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);
}
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);
}
}
Aggregations