Search in sources :

Example 81 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class UserRefreshTokenServiceImpl method createRefreshToken.

@Override
public String createRefreshToken(User owner, String userAgent) {
    UserRefreshToken urt = new UserRefreshToken();
    Timestamp createDate = Timestamp.valueOf(LocalDateTime.now());
    urt.setCreateDate(createDate);
    urt.setOwner(owner);
    urt.setUserAgent(userAgent);
    urt = userRefreshTokenRepo.save(urt);
    return urt.getId().toString();
}
Also used : UserRefreshToken(com.ncedu.fooddelivery.api.v1.entities.UserRefreshToken) Timestamp(java.sql.Timestamp)

Example 82 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method deleteImgNotAdminNotOwner.

@Test
public void deleteImgNotAdminNotOwner() throws IOException {
    // prepare
    Long ownerId = 1L;
    User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
    Long notOwnerId = 2L;
    User leonardNotOwner = UserUtils.moderatorLeonardHofstadter(notOwnerId);
    String imgName = "test.jpeg";
    Path pathTestPng = UPLOAD_PATH.resolve(imgName);
    // file name, where we locate our test.png
    final String fileUuid = "55420882-1e23-4559-b84e-03c3f4d597af";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File fileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, imgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    doNothing().when(fileRepoMock).delete(fileEntity);
    Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
        fileService.delete(fileEntity, leonardNotOwner);
    });
    String perfectMessage = new CustomAccessDeniedException().getMessage();
    String resultMessage = exception.getMessage();
    assertEquals(perfectMessage, resultMessage);
    verify(fileRepoMock, never()).delete(fileEntity);
}
Also used : Path(java.nio.file.Path) User(com.ncedu.fooddelivery.api.v1.entities.User) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 83 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method saveJpegLargeWidthClient.

@Test
public void saveJpegLargeWidthClient() throws IOException {
    // prepare
    Long userId = 1L;
    User rajeshClient = UserUtils.clientRajeshKoothrappali(userId);
    MultipartFile file = getImgWithType("testLargeWidth", "jpeg");
    Long fileSize = file.getSize();
    log.info("FILE SIZE before uploading: " + fileSize + " FILE NAME: " + file.getName());
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    FileLinkDTO fileLinkDTO = fileService.save(file, rajeshClient);
    verify(fileRepoMock, times(1)).save(any(File.class));
    Path uploadedFile = getUploadedFilePath(fileLinkDTO.getFileUuid());
    assertTrue(Files.exists(uploadedFile));
    Long uploadedFileSize = Files.size(uploadedFile);
    log.info("FILE SIZE after uploading: " + uploadedFileSize + " FILE UUID: " + fileLinkDTO.getFileUuid());
    // uploaded size less than income because convert from jpeg to jpeg with small resolution
    assertTrue(fileSize > uploadedFileSize);
    BufferedImage bufferedImage = ImageIO.read(uploadedFile.toFile());
    assertEquals(CLIENT_IMAGE_WIDTH, bufferedImage.getWidth());
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 84 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method getFileList.

@Test
public void getFileList() {
    Long ownerId = 1L;
    User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
    File file1 = new File(UUID.randomUUID(), FileType.JPEG, "test", 0L, Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    File file2 = new File(UUID.randomUUID(), FileType.JPEG, "test2", 0L, Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    List<File> fileList = new ArrayList<>();
    fileList.add(file1);
    fileList.add(file2);
    Pageable pageable = PageRequest.of(0, 2);
    Page page = new PageImpl(fileList, pageable, fileList.size());
    when(fileRepoMock.findAll(pageable)).thenReturn(page);
    List<FileInfoDTO> resultDTOs = fileService.getAllFiles(pageable);
    List<FileInfoDTO> perfectDTOs = new ArrayList<>();
    for (File file : fileList) {
        perfectDTOs.add(createFileDTO(file));
    }
    assertEquals(perfectDTOs.size(), resultDTOs.size());
    assertEquals(perfectDTOs, resultDTOs);
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInfoDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 85 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method replaceJpgNotOwnerNotAdmin.

@Test
public void replaceJpgNotOwnerNotAdmin() throws IOException {
    // prepare
    Long userId = 1L;
    User pennyClient = UserUtils.clientPennyTeller(userId);
    Long notOwnerId = 2L;
    User rajeshNotOwner = UserUtils.clientRajeshKoothrappali(notOwnerId);
    String oldImgName = "test.jpeg";
    Path pathTestJpeg = UPLOAD_PATH.resolve(oldImgName);
    final String fileUuid = "e897e931-12ce-4b3f-afbc-42f6c165dfc8";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestJpeg, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File oldFileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, oldImgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), pennyClient);
    MultipartFile newFile = getImgWithType("testLargeWidth", "png");
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
        fileService.replace(newFile, oldFileEntity, rajeshNotOwner);
    });
    String perfectMessage = new CustomAccessDeniedException().getMessage();
    String resultMessage = exception.getMessage();
    assertEquals(perfectMessage, resultMessage);
    verify(fileRepoMock, never()).save(any(File.class));
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)58 Test (org.junit.jupiter.api.Test)55 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)55 File (com.ncedu.fooddelivery.api.v1.entities.File)21 Path (java.nio.file.Path)19 MultipartFile (org.springframework.web.multipart.MultipartFile)19 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)18 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)18 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)15 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)14 BufferedImage (java.awt.image.BufferedImage)10 UserInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.UserInfoDTO)9 Role (com.ncedu.fooddelivery.api.v1.entities.Role)7 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)7 IOException (java.io.IOException)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)6 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)6 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)6 CommitAfter (org.apache.tapestry5.jpa.annotations.CommitAfter)6