Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
package org.entando.entando.aps.system.services.userpreferences;

import java.util.Optional;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.entando.entando.ent.exception.EntException;

@Slf4j
@Setter
public class UserPreferencesManager implements IUserPreferencesManager {

private UserPreferencesDAO userPreferencesDAO;
Expand Down Expand Up @@ -57,9 +61,5 @@ public void updateUserGravatarPreference(String username, boolean enabled) throw
public void deleteUserPreferences(String username) throws EntException {
userPreferencesDAO.deleteUserPreferences(username);
}

public void setUserPreferencesDAO(UserPreferencesDAO userPreferencesDAO) {
this.userPreferencesDAO = userPreferencesDAO;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void deleteAvatar(UserDetails userDetails, BindingResult bindingResult) {
}
}

@Override
public void deleteAvatar(String username) throws EntException {
this.deletePrevUserAvatarFromFileSystemIfPresent(username);
}

//------------------------ Utility methods ------------------------------------//

private FileBrowserFileRequest addProfileImageToFileSystem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import com.agiletec.aps.system.services.user.UserDetails;
import org.entando.entando.aps.system.services.userprofile.model.AvatarDto;
import org.entando.entando.ent.exception.EntException;
import org.entando.entando.web.userprofile.model.ProfileAvatarRequest;
import org.springframework.validation.BindingResult;

public interface IAvatarService {

AvatarDto getAvatarData(UserDetails userDetails);


String updateAvatar(ProfileAvatarRequest request, UserDetails userDetails,
BindingResult bindingResult);

void deleteAvatar(UserDetails userDetails, BindingResult bindingResult);

void deleteAvatar(String username) throws EntException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
import com.agiletec.aps.system.services.user.AbstractUser;
import com.agiletec.aps.system.services.user.UserDetails;
import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager;
import org.entando.entando.ent.exception.EntException;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Implementation of ProfileManager Aspect. This class join a user with his
Expand All @@ -28,11 +31,21 @@
* @author E.Santoboni
*/
@Aspect
public class UserProfileManagerAspect {
public class UserManagementAspect {

private static final EntLogger logger = EntLogFactory.getSanitizedLogger(UserProfileManagerAspect.class);
private static final EntLogger logger = EntLogFactory.getSanitizedLogger(UserManagementAspect.class);

private IUserProfileManager userProfileManager;
private final IUserProfileManager userProfileManager;
private final IAvatarService avatarService;
private final IUserPreferencesManager userPreferencesManager;

@Autowired
public UserManagementAspect(IUserProfileManager userProfileManager,
IAvatarService avatarService, IUserPreferencesManager userPreferencesManager) {
this.userProfileManager = userProfileManager;
this.userPreferencesManager = userPreferencesManager;
this.avatarService = avatarService;
}

@AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.getUser(..))", returning = "user")
public void injectProfile(Object user) {
Expand Down Expand Up @@ -80,7 +93,7 @@ public void updateProfile(Object user) {
}

@AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.removeUser(..)) && args(key)")
public void deleteProfile(Object key) {
public void deleteUserData(Object key) {
String username = null;
if (key instanceof String) {
username = key.toString();
Expand All @@ -91,18 +104,24 @@ public void deleteProfile(Object key) {
if (username != null) {
try {
this.getUserProfileManager().deleteProfile(username);
} catch (Throwable t) {
logger.error("Error deleting profile. user: {}", username, t);
} catch (EntException t) {
logger.error("Error deleting user profile. user: {}", username, t);
}
try {
this.avatarService.deleteAvatar(username);
} catch (EntException t) {
logger.error("Error deleting user avatar. user: {}", username, t);
}
try {
this.userPreferencesManager.deleteUserPreferences(username);
} catch (EntException t) {
logger.error("Error deleting user preverences. user: {}", username, t);
}
}
}

protected IUserProfileManager getUserProfileManager() {
return userProfileManager;
}

public void setUserProfileManager(IUserProfileManager userProfileManager) {
this.userProfileManager = userProfileManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
</property>
</bean>

<bean id="UserProfileManagerAspect" class="org.entando.entando.aps.system.services.userprofile.UserProfileManagerAspect">
<property name="userProfileManager" ref="UserProfileManager" />
</bean>
<bean id="UserManagementAspect" class="org.entando.entando.aps.system.services.userprofile.UserManagementAspect" />

<!-- API -->

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Copyright 2018-Present Entando Inc. (http://www.entando.com) All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package org.entando.entando.aps.system.services.userprofile;

import static org.mockito.Mockito.when;

import com.agiletec.aps.system.SystemConstants;
import com.agiletec.aps.system.common.entity.model.attribute.MonoTextAttribute;
import com.agiletec.aps.system.common.entity.parse.attribute.MonoTextAttributeHandler;
import com.agiletec.aps.system.services.user.User;
import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager;
import org.entando.entando.aps.system.services.userprofile.model.IUserProfile;
import org.entando.entando.aps.system.services.userprofile.model.UserProfile;
import org.entando.entando.ent.exception.EntException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class UserManagementAspectTest {

@InjectMocks
private UserManagementAspect userManagementAspect;

@Mock
private UserProfileManager userProfileManager;
@Mock
private IAvatarService avatarService;
@Mock
private IUserPreferencesManager userPreferencesManager;

@Test
void testInjectProfile() throws EntException {
IUserProfile returned = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
when(userProfileManager.getProfile(Mockito.anyString())).thenReturn(returned);

User user = new User();
user.setUsername("test");
Assertions.assertNull(user.getProfile());

userManagementAspect.injectProfile(user);
Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test");
IUserProfile profile = (IUserProfile) user.getProfile();
Assertions.assertNotNull(profile);
Assertions.assertEquals("test", profile.getUsername());
}

@Test
void testInjectProfileWithError() throws EntException {
when(userProfileManager.getProfile(Mockito.anyString())).thenThrow(EntException.class);

User user = new User();
user.setUsername("test");
Assertions.assertNull(user.getProfile());

userManagementAspect.injectProfile(user);
Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test");
Assertions.assertNull(user.getProfile());
}

@Test
void testInjectProfileWithNullUser() throws EntException {
userManagementAspect.injectProfile(null);
Mockito.verifyNoInteractions(userProfileManager);
}

@Test
void testMissingInjectProfile() throws EntException {
IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
User user = new User();
user.setUsername("test");
user.setProfile(profile);

userManagementAspect.injectProfile(user);
Mockito.verify(userProfileManager, Mockito.times(0)).getProfile("test");
Assertions.assertNotNull(user.getProfile());
Assertions.assertSame(profile, user.getProfile());
}

@Test
void testAddProfile() throws EntException {
IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
User user = new User();
user.setUsername("test");
user.setProfile(profile);
userManagementAspect.addProfile(user);
Mockito.verify(userProfileManager, Mockito.times(1)).addProfile("test", profile);
}

@Test
void testAddProfileWithNullUser() throws EntException {
userManagementAspect.addProfile(null);
Mockito.verifyNoInteractions(userProfileManager);
}

@Test
void testInvokeAddProfileWithUserWithNullProfile() throws EntException {
User user = new User();
user.setUsername("test");
userManagementAspect.addProfile(user);
Mockito.verifyNoInteractions(userProfileManager);
}

@Test
void testInvokeAddProfileWithError() {
try {
Mockito.doThrow(EntException.class).when(userProfileManager).addProfile(Mockito.anyString(), Mockito.any());
User user = new User();
user.setUsername("test");
user.setProfile(Mockito.mock(IUserProfile.class));
userManagementAspect.addProfile(user);
Mockito.verify(userProfileManager, Mockito.times(1)).addProfile(Mockito.anyString(), Mockito.any(IUserProfile.class));
} catch (Exception e) {
Assertions.fail();
}
}

@Test
void testUpdateProfile() throws EntException {
IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
User user = new User();
user.setUsername("test");
user.setProfile(profile);
userManagementAspect.updateProfile(user);
Mockito.verify(userProfileManager, Mockito.times(1)).updateProfile("test", profile);
}

@Test
void testUpdateProfileWithNullUSer() throws EntException {
userManagementAspect.updateProfile(null);
Mockito.verifyNoInteractions(userProfileManager);
}

@Test
void testInvokeUpdateProfileWithUserWithNullProfile() throws EntException {
User user = new User();
user.setUsername("test");
userManagementAspect.updateProfile(user);
Mockito.verifyNoInteractions(userProfileManager);
}

@Test
void testInvokeUpdateProfileWithError() {
try {
Mockito.doThrow(EntException.class).when(userProfileManager).updateProfile(Mockito.anyString(), Mockito.any());
User user = new User();
user.setUsername("test");
user.setProfile(Mockito.mock(IUserProfile.class));
userManagementAspect.updateProfile(user);
Mockito.verify(userProfileManager, Mockito.times(1)).updateProfile(Mockito.anyString(), Mockito.any(IUserProfile.class));
} catch (Exception e) {
Assertions.fail();
}
}

@Test
void testDeleteUserDataFromUser() throws EntException {
User user = new User();
user.setUsername("test");
userManagementAspect.deleteUserData(user);
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("test");
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("test");
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("test");
}

@Test
void testDeleteUserDataFromUsername() throws EntException {
userManagementAspect.deleteUserData("test");
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("test");
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("test");
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("test");
}

@Test
void testDeleteUserDataWithNullUsername() throws EntException {
userManagementAspect.deleteUserData(null);
Mockito.verifyNoInteractions(userProfileManager);
Mockito.verifyNoInteractions(userPreferencesManager);
Mockito.verifyNoInteractions(avatarService);
}

@Test
void testDeleteUserDataWithErrorOnUserProfileManager() {
try {
Mockito.doThrow(EntException.class).when(userProfileManager).deleteProfile(Mockito.anyString());
userManagementAspect.deleteUserData("username_error1");
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error1");
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error1");
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error1");
} catch (Exception e) {
Assertions.fail();
}
}

@Test
void testDeleteUserDataWithErrorOnUserAvatarService() {
try {
Mockito.doThrow(EntException.class).when(avatarService).deleteAvatar(Mockito.anyString());
userManagementAspect.deleteUserData("username_error2");
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error2");
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error2");
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error2");
} catch (Exception e) {
Assertions.fail();
}
}

@Test
void testDeleteUserDataWithErrorOnUserPreference() {
try {
Mockito.doThrow(EntException.class).when(userPreferencesManager).deleteUserPreferences(Mockito.anyString());
userManagementAspect.deleteUserData("username_error3");
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error3");
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error3");
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error3");
} catch (Exception e) {
Assertions.fail();
}
}

private IUserProfile createFakeProfile(String username, String defaultProfileTypeCode) {
UserProfile userProfile = new UserProfile();
userProfile.setId(username);
MonoTextAttribute monoTextAttribute = new MonoTextAttribute();
monoTextAttribute.setName("Name");
monoTextAttribute.setHandler(new MonoTextAttributeHandler());
userProfile.addAttribute(monoTextAttribute);
userProfile.setTypeCode(defaultProfileTypeCode);
return userProfile;
}

}
Loading