Fix all Tests
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-22 13:40:26 +01:00
parent 917ed77a95
commit 8e4d6bf820
6 changed files with 81 additions and 3 deletions
@@ -14,6 +14,10 @@ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabas
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;
import java.io.IOException;
import static SuMoC.backend.Factories.UserTestFactory.uniqueUser;
import static org.junit.jupiter.api.Assertions.*;
@@ -65,7 +69,7 @@ public class UserServiceTest {
userRepository.save(differentUser);
MinimalUserDTO publicSetting = userService.getUser(differentUser.getId());
assertNotNull(publicSetting);
assertEquals(differentUser.getPublicName(), publicSetting.getDisplayName());
assertEquals(differentUser.getPrivateName(), publicSetting.getDisplayName());
}
@@ -177,5 +181,41 @@ public class UserServiceTest {
assertEquals(0, selfDTO.getFriendships().size());
}
@Test
@WithMockUser(username = "test1@test.de", roles = {"USER"})
public void testGetSetProfilePicture() throws IOException {
// Own profile -> should throw before picture is set
assertThrows(ResponseStatusException.class , () -> userService.getProfilePicture(sender.getId()));
// Set profile picture
MultipartFile file = new org.springframework.mock.web.MockMultipartFile(
"profile.jpg", new byte[]{1,2,3,4,5});
userService.setProfilePicture(sender.getId(), file);
// Get profile picture
byte[] profilePicture = userService.getProfilePicture(sender.getId());
assertNotNull(profilePicture);
assertEquals(file.getBytes().length, profilePicture.length);
assertArrayEquals(file.getBytes(), profilePicture);
// Different user's profile picture -> should throw
assertThrows(ResponseStatusException.class , () -> userService.setProfilePicture(differentUser.getId(), file));
assertThrows(ResponseStatusException.class , () -> userService.getProfilePicture(differentUser.getId()));
// Different user's profile picture with public visibility -> should work
User anotherUser = userRepository.save(uniqueUser(3));
anotherUser.setVisibilityPrivacy(VisibilityPrivacy.PUBLIC);
userRepository.save(anotherUser);
byte[] secondImage = new byte[]{10,20,30,40,50};
anotherUser.getProfileImage().setImageData(secondImage);
byte[] profilePicture2 = userService.getProfilePicture(anotherUser.getId());
assertNotNull(profilePicture2);
assertEquals(secondImage.length, profilePicture2.length);
assertArrayEquals(secondImage, profilePicture2);
}
}