FINERACT-2246: New command processing - document management
This commit is contained in:
committed by
Aleksandar Vidakovic
parent
8a61e30a45
commit
2ff0c62f40
@@ -67,4 +67,5 @@ dependencies {
|
||||
|
||||
testImplementation 'org.wiremock:wiremock-standalone'
|
||||
testImplementation 'com.google.guava:guava'
|
||||
testImplementation 'com.github.romankh3:image-comparison'
|
||||
}
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.fineract.integrationtests.bulkimport.importhandler;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public final class LocalContentStorageUtil {
|
||||
|
||||
private LocalContentStorageUtil() {}
|
||||
|
||||
public static String path(String path) {
|
||||
var currentPath = Path.of("").toAbsolutePath();
|
||||
|
||||
if (Files.exists(Path.of(path))) {
|
||||
return path;
|
||||
}
|
||||
|
||||
if (Files.exists(Path.of("/", path))) {
|
||||
return "/" + path;
|
||||
}
|
||||
|
||||
final var ghaPath = Path.of("/home/runner/.fineract/DefaultDemoTenant").resolve(path);
|
||||
|
||||
// github actions
|
||||
if (Files.exists(ghaPath)) {
|
||||
return ghaPath.toString();
|
||||
}
|
||||
|
||||
// local dev run
|
||||
final var devRunPath = Path.of(System.getProperty("user.home")).toAbsolutePath().resolve(".fineract/DefaultDemoTenant")
|
||||
.resolve(path);
|
||||
|
||||
if (Files.exists(devRunPath)) {
|
||||
return devRunPath.toString();
|
||||
}
|
||||
|
||||
// local docker volumes
|
||||
final var dockerPath = Path.of("").toAbsolutePath().getParent().resolve("build/fineract/tmp/DefaultDemoTenant").resolve(path);
|
||||
|
||||
if (Files.exists(dockerPath)) {
|
||||
return dockerPath.toString();
|
||||
}
|
||||
|
||||
throw new RuntimeException("Cannot find local fineract path: " + path + " (" + currentPath + ")");
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedMethod")
|
||||
public static void waitFor(String path) throws InterruptedException {
|
||||
Path file = Paths.get(path);
|
||||
long maxWaitMillis = 10000;
|
||||
long start = System.currentTimeMillis();
|
||||
boolean exists = false;
|
||||
|
||||
while ((System.currentTimeMillis() - start) < maxWaitMillis) {
|
||||
if (Files.exists(file)) {
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Thread.sleep(500);
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
log.info("File found!");
|
||||
} else {
|
||||
log.warn("Timed out waiting for file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -37,6 +37,7 @@ import java.util.Locale;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.ClientEntityConstants;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.TemplatePopulateImportConstants;
|
||||
import org.apache.fineract.infrastructure.bulkimport.data.GlobalEntityType;
|
||||
import org.apache.fineract.integrationtests.bulkimport.importhandler.LocalContentStorageUtil;
|
||||
import org.apache.fineract.integrationtests.common.ClientHelper;
|
||||
import org.apache.fineract.integrationtests.common.OfficeHelper;
|
||||
import org.apache.fineract.integrationtests.common.Utils;
|
||||
@@ -144,7 +145,7 @@ public class ClientEntityImportHandlerTest {
|
||||
Thread.sleep(10000);
|
||||
|
||||
// check status column of output excel
|
||||
String location = clientHelper.getOutputTemplateLocation(importDocumentId);
|
||||
String location = LocalContentStorageUtil.path(clientHelper.getOutputTemplateLocation(importDocumentId));
|
||||
FileInputStream fileInputStream = new FileInputStream(location);
|
||||
Workbook outputWorkbook = new HSSFWorkbook(fileInputStream);
|
||||
Sheet outputClientEntitySheet = outputWorkbook.getSheet(TemplatePopulateImportConstants.CLIENT_ENTITY_SHEET_NAME);
|
||||
|
||||
+2
-1
@@ -45,6 +45,7 @@ import org.apache.fineract.client.models.PaymentTypeRequest;
|
||||
import org.apache.fineract.client.models.PostPaymentTypesResponse;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.LoanConstants;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.TemplatePopulateImportConstants;
|
||||
import org.apache.fineract.integrationtests.bulkimport.importhandler.LocalContentStorageUtil;
|
||||
import org.apache.fineract.integrationtests.common.CollateralManagementHelper;
|
||||
import org.apache.fineract.integrationtests.common.GroupHelper;
|
||||
import org.apache.fineract.integrationtests.common.OfficeDomain;
|
||||
@@ -246,7 +247,7 @@ public class LoanImportHandlerTest {
|
||||
Thread.sleep(10000);
|
||||
|
||||
// check status column of output excel
|
||||
String location = loanTransactionHelper.getOutputTemplateLocation(importDocumentId);
|
||||
String location = LocalContentStorageUtil.path(loanTransactionHelper.getOutputTemplateLocation(importDocumentId));
|
||||
FileInputStream fileInputStream = new FileInputStream(location);
|
||||
Workbook outputworkbook = new HSSFWorkbook(fileInputStream);
|
||||
Sheet outputLoanSheet = outputworkbook.getSheet(TemplatePopulateImportConstants.LOANS_SHEET_NAME);
|
||||
|
||||
+6
-7
@@ -31,8 +31,10 @@ import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.OfficeConstants;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.TemplatePopulateImportConstants;
|
||||
import org.apache.fineract.integrationtests.bulkimport.importhandler.LocalContentStorageUtil;
|
||||
import org.apache.fineract.integrationtests.common.OfficeHelper;
|
||||
import org.apache.fineract.integrationtests.common.Utils;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
@@ -42,13 +44,10 @@ import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Slf4j
|
||||
public class OfficeImportHandlerTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(OfficeImportHandlerTest.class);
|
||||
|
||||
private ResponseSpecification responseSpec;
|
||||
private RequestSpecification requestSpec;
|
||||
|
||||
@@ -96,14 +95,14 @@ public class OfficeImportHandlerTest {
|
||||
Thread.sleep(10000);
|
||||
|
||||
// check status column of output excel
|
||||
String location = officeHelper.getOutputTemplateLocation(importDocumentId);
|
||||
String location = LocalContentStorageUtil.path(officeHelper.getOutputTemplateLocation(importDocumentId));
|
||||
FileInputStream fileInputStream = new FileInputStream(location);
|
||||
Workbook outputWorkbook = new HSSFWorkbook(fileInputStream);
|
||||
Sheet officeSheet = outputWorkbook.getSheet(TemplatePopulateImportConstants.OFFICE_SHEET_NAME);
|
||||
Row row = officeSheet.getRow(1);
|
||||
|
||||
LOG.info("Output location: {}", location);
|
||||
LOG.info("Failure reason column: {}", row.getCell(OfficeConstants.STATUS_COL).getStringCellValue());
|
||||
log.info("Output location: {}", location);
|
||||
log.info("Failure reason column: {}", row.getCell(OfficeConstants.STATUS_COL).getStringCellValue());
|
||||
|
||||
Assertions.assertEquals("Imported", row.getCell(OfficeConstants.STATUS_COL).getStringCellValue());
|
||||
outputWorkbook.close();
|
||||
|
||||
+6
-5
@@ -40,6 +40,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.SavingsConstants;
|
||||
import org.apache.fineract.infrastructure.bulkimport.constants.TemplatePopulateImportConstants;
|
||||
import org.apache.fineract.integrationtests.bulkimport.importhandler.LocalContentStorageUtil;
|
||||
import org.apache.fineract.integrationtests.common.GroupHelper;
|
||||
import org.apache.fineract.integrationtests.common.OfficeDomain;
|
||||
import org.apache.fineract.integrationtests.common.OfficeHelper;
|
||||
@@ -185,17 +186,17 @@ public class SavingsImportHandlerTest {
|
||||
Thread.sleep(1000);
|
||||
|
||||
// check status column of output excel
|
||||
String location = savingsAccountHelper.getOutputTemplateLocation(importDocumentId);
|
||||
String location = LocalContentStorageUtil.path(savingsAccountHelper.getOutputTemplateLocation(importDocumentId));
|
||||
FileInputStream fileInputStream = new FileInputStream(location);
|
||||
Workbook Outputworkbook = new HSSFWorkbook(fileInputStream);
|
||||
Sheet OutputSavingsSheet = Outputworkbook.getSheet(TemplatePopulateImportConstants.SAVINGS_ACCOUNTS_SHEET_NAME);
|
||||
Row row = OutputSavingsSheet.getRow(1);
|
||||
Workbook wb = new HSSFWorkbook(fileInputStream);
|
||||
Sheet sheet = wb.getSheet(TemplatePopulateImportConstants.SAVINGS_ACCOUNTS_SHEET_NAME);
|
||||
Row row = sheet.getRow(1);
|
||||
|
||||
LOG.info("Output location: {}", location);
|
||||
LOG.info("Failure reason column: {}", row.getCell(SavingsConstants.STATUS_COL).getStringCellValue());
|
||||
|
||||
Assertions.assertEquals("Imported", row.getCell(SavingsConstants.STATUS_COL).getStringCellValue());
|
||||
Outputworkbook.close();
|
||||
wb.close();
|
||||
}
|
||||
|
||||
private void safeNumericValueSetter(Row targetRow, int targetColId, Sheet sourceSheet, int rowId, int colId) {
|
||||
|
||||
+7
-3
@@ -38,7 +38,7 @@ import retrofit2.Response;
|
||||
*
|
||||
* @author Michael Vorburger.ch
|
||||
*/
|
||||
public class DocumentTest extends IntegrationTest {
|
||||
class DocumentTest extends IntegrationTest {
|
||||
|
||||
final File testFile = new File(getClass().getResource("/michael.vorburger-crepes.jpg").getFile());
|
||||
|
||||
@@ -87,7 +87,11 @@ public class DocumentTest extends IntegrationTest {
|
||||
try (ResponseBody body = r.body()) {
|
||||
assertThat(body.contentType()).isEqualTo(MediaType.get("image/jpeg"));
|
||||
assertThat(body.bytes().length).isEqualTo(testFile.length());
|
||||
assertThat(body.contentLength()).isEqualTo(testFile.length());
|
||||
// NOTE: now that everything is properly streamed and NOT loaded into memory the framework (Jersey) uses
|
||||
// chunked encoding to serve dynamic aka large content; this is more efficient and outweighs the presenće of
|
||||
// this information beforehand; the user can always count bytes after the download of the content; just to
|
||||
// say: this here is a feature and intentional
|
||||
// assertThat(body.contentLength()).isEqualTo(testFile.length());
|
||||
}
|
||||
assertThat(Parts.fileName(r)).hasValue(testFile.getName());
|
||||
}
|
||||
@@ -113,7 +117,7 @@ public class DocumentTest extends IntegrationTest {
|
||||
ok(fineractClient().documents.deleteDocument("clients", clientId, documentId));
|
||||
CallFailedRuntimeException exception = assertThrows(CallFailedRuntimeException.class,
|
||||
() -> ok(fineractClient().documents.getDocument("clients", clientId, documentId)));
|
||||
assertEquals(404, exception.getResponse().code());
|
||||
assertEquals(403, exception.getResponse().code());
|
||||
}
|
||||
|
||||
@Order(9999)
|
||||
|
||||
+2
-4
@@ -30,7 +30,6 @@ import java.nio.file.Files;
|
||||
import org.apache.fineract.client.feign.FeignException;
|
||||
import org.apache.fineract.client.feign.FineractMultipartEncoder;
|
||||
import org.apache.fineract.client.models.DocumentData;
|
||||
import org.apache.fineract.client.models.PostEntityTypeEntityIdDocumentsResponse;
|
||||
import org.apache.fineract.integrationtests.client.feign.helpers.FeignClientHelper;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
@@ -63,8 +62,7 @@ public class FeignDocumentTest extends FeignIntegrationTest {
|
||||
FineractMultipartEncoder.MultipartData multipartData = new FineractMultipartEncoder.MultipartData()
|
||||
.addFile("file", testFile.getName(), fileData, "image/jpeg").addText("name", name).addText("description", description);
|
||||
|
||||
PostEntityTypeEntityIdDocumentsResponse response = ok(
|
||||
() -> fineractClient().documentsFixed().createDocument("clients", clientId, multipartData));
|
||||
var response = ok(() -> fineractClient().documentsFixed().createDocument("clients", clientId, multipartData));
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getResourceId()).isNotNull();
|
||||
@@ -135,7 +133,7 @@ public class FeignDocumentTest extends FeignIntegrationTest {
|
||||
|
||||
FeignException exception = assertThrows(FeignException.class,
|
||||
() -> fineractClient().documentsFixed().getDocument("clients", clientId, documentId));
|
||||
assertEquals(404, exception.status());
|
||||
assertEquals(403, exception.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+87
-39
@@ -18,21 +18,27 @@
|
||||
*/
|
||||
package org.apache.fineract.integrationtests.client;
|
||||
|
||||
import static com.github.romankh3.image.comparison.model.ImageComparisonState.MATCH;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.github.romankh3.image.comparison.ImageComparison;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.fineract.client.services.ImagesApi;
|
||||
import org.apache.fineract.client.util.Parts;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Headers;
|
||||
|
||||
@@ -42,9 +48,12 @@ import retrofit2.http.Headers;
|
||||
* @author Michael Vorburger.ch
|
||||
*/
|
||||
@Slf4j
|
||||
public class ImageTest extends IntegrationTest {
|
||||
class ImageTest extends IntegrationTest {
|
||||
|
||||
final MultipartBody.Part testPart = createPart("michael.vorburger-crepes.jpg", "michael.vorburger-crepes.jpg", "image/jpeg");
|
||||
static final String TEST_RESOURCE = "michael.vorburger-crepes.jpg";
|
||||
static final int TEST_IMAGE_DIFF_PERCENTAGE = 2;
|
||||
|
||||
final MultipartBody.Part testPart = createPart(TEST_RESOURCE, TEST_RESOURCE, "image/jpeg");
|
||||
|
||||
Long clientId = new ClientTest().getClientId();
|
||||
Long staffId = new StaffTest().getStaffId();
|
||||
@@ -59,65 +68,66 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(2)
|
||||
void getOriginalSize() throws IOException {
|
||||
ResponseBody r = ok(fineractClient().images.get("staff", staffId, 3505, 1972, null));
|
||||
var r = ok(fineractClient().images.get("staff", staffId, 3505, 1972, null));
|
||||
assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain"));
|
||||
String encodedImage = r.string();
|
||||
var encodedImage = r.string();
|
||||
assertThat(encodedImage).startsWith("data:image/jpeg;base64,");
|
||||
assertThat(encodedImage).hasSize(2846549);
|
||||
assertThat(r.contentLength()).isEqualTo(-1);
|
||||
assertImage(encodedImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void getSmallerSize() throws IOException {
|
||||
ResponseBody r = ok(fineractClient().images.get("staff", staffId, 128, 128, null));
|
||||
assertThat(r.string()).hasSize(6591);
|
||||
var r = ok(fineractClient().images.get("staff", staffId, 128, 128, null));
|
||||
assertThat(r.string()).hasSize(7251);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void getBiggerSize() throws IOException {
|
||||
ResponseBody r = ok(fineractClient().images.get("staff", staffId, 9000, 6000, null));
|
||||
assertThat(r.string()).hasSize(2846549);
|
||||
var r = ok(fineractClient().images.get("staff", staffId, 9000, 6000, null));
|
||||
assertImage(r.string());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void getInlineOctetOutput() throws IOException {
|
||||
// 3505x1972 is the exact original size of testFile
|
||||
Response<ResponseBody> r = okR(fineractClient().images.get("staff", staffId, 3505, 1972, "inline_octet"));
|
||||
try (ResponseBody body = r.body()) {
|
||||
var r = okR(fineractClient().images.get("staff", staffId, 3505, 1972, "inline_octet"));
|
||||
try (var body = r.body()) {
|
||||
assertThat(body.contentType()).isEqualTo(MediaType.get("image/jpeg"));
|
||||
assertThat(body.bytes().length).isEqualTo(testPart.body().contentLength());
|
||||
assertThat(body.contentLength()).isEqualTo(testPart.body().contentLength());
|
||||
assertImage(body);
|
||||
}
|
||||
|
||||
var staff = ok(fineractClient().staff.retrieveOne8(staffId));
|
||||
String expectedFileName = staff.getDisplayName() + "JPEG"; // without dot!
|
||||
assertThat(Parts.fileName(r)).hasValue(expectedFileName);
|
||||
assertThat(Parts.fileName(r)).hasValue(staff.getDisplayName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void getOctetOutput() throws IOException {
|
||||
ResponseBody r = ok(fineractClient().images.get("staff", staffId, 3505, 1972, "octet"));
|
||||
var r = ok(fineractClient().images.get("staff", staffId, 3505, 1972, "octet"));
|
||||
assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg"));
|
||||
assertThat(r.bytes().length).isEqualTo(testPart.body().contentLength());
|
||||
assertThat(r.contentLength()).isEqualTo(testPart.body().contentLength());
|
||||
// NOTE: content length is not a reliable criteria; the server removes metadata (see it as a security feature)
|
||||
// which makes the file immediately only half the size, but pixel wise the images are still the same
|
||||
assertImage(r);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void getAnotherOutput() throws IOException {
|
||||
ResponseBody r = ok(fineractClient().images.get("staff", staffId, 3505, 1972, "abcd"));
|
||||
var r = ok(fineractClient().images.get("staff", staffId, 3505, 1972, "abcd"));
|
||||
assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain"));
|
||||
assertThat(r.string()).startsWith("data:image/jpeg;base64,");
|
||||
var content = r.string();
|
||||
assertThat(content).startsWith("data:image/jpeg;base64,");
|
||||
assertImage(content);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void getText() throws IOException {
|
||||
ResponseBody r = ok(fineractClient().createService(ImagesApiWithHeadersForTest.class).getText("staff", staffId, 3505, 1972, null));
|
||||
var r = ok(fineractClient().createService(ImagesApiWithHeadersForTest.class).getText("staff", staffId, 3505, 1972, null));
|
||||
assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain"));
|
||||
assertThat(r.string()).startsWith("data:image/jpeg;base64,");
|
||||
}
|
||||
@@ -125,9 +135,9 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(9)
|
||||
void getBytes() throws IOException {
|
||||
ResponseBody r = ok(fineractClient().createService(ImagesApiWithHeadersForTest.class).getBytes("staff", staffId, 3505, 1972, null));
|
||||
var r = ok(fineractClient().createService(ImagesApiWithHeadersForTest.class).getBytes("staff", staffId, 3505, 1972, null));
|
||||
assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg"));
|
||||
assertThat(r.bytes().length).isEqualTo(testPart.body().contentLength());
|
||||
assertImage(r.bytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -146,12 +156,12 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(100)
|
||||
void pathTraversalJsp() {
|
||||
final MultipartBody.Part part = createPart("image-text-wrong-content.jsp",
|
||||
"../../../../../../../../../../tmp/image-text-wrong-content.jsp", "image/gif");
|
||||
final var part = createPart("image-text-wrong-content.jsp", "../../../../../../../../../../tmp/image-text-wrong-content.jsp",
|
||||
"image/gif");
|
||||
|
||||
assertThat(part).isNotNull();
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
var exception = assertThrows(Exception.class, () -> {
|
||||
ok(fineractClient().images.create("clients", clientId, part));
|
||||
});
|
||||
|
||||
@@ -163,11 +173,11 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(101)
|
||||
void gifWithPngExtension() {
|
||||
final MultipartBody.Part part = createPart("image-gif-wrong-extension.png", "image-gif-wrong-extension.png", "image/png");
|
||||
final var part = createPart("image-gif-wrong-extension.png", "image-gif-wrong-extension.png", "image/png");
|
||||
|
||||
assertThat(part).isNotNull();
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
var exception = assertThrows(Exception.class, () -> {
|
||||
ok(fineractClient().images.create("clients", clientId, part));
|
||||
});
|
||||
|
||||
@@ -179,11 +189,11 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(102)
|
||||
void gifImage() {
|
||||
final MultipartBody.Part part = createPart("image-gif-correct-extension.gif", "image-gif-correct-extension.gif", "image/png");
|
||||
final var part = createPart("image-gif-correct-extension.gif", "image-gif-correct-extension.gif", "image/png");
|
||||
|
||||
assertThat(part).isNotNull();
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
var exception = assertThrows(Exception.class, () -> {
|
||||
ok(fineractClient().images.create("clients", clientId, part));
|
||||
});
|
||||
|
||||
@@ -195,12 +205,12 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(103)
|
||||
void pathTraversalJpg() {
|
||||
final MultipartBody.Part part = createPart("michael.vorburger-crepes.jpg",
|
||||
"../../../../../../../../../../tmp/michael.vorburger-crepes.jpg", "image/jpeg");
|
||||
final var part = createPart("michael.vorburger-crepes.jpg", "../../../../../../../../../../tmp/michael.vorburger-crepes.jpg",
|
||||
"image/jpeg");
|
||||
|
||||
assertThat(part).isNotNull();
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
var exception = assertThrows(Exception.class, () -> {
|
||||
ok(fineractClient().images.create("clients", clientId, part));
|
||||
});
|
||||
|
||||
@@ -212,11 +222,12 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(104)
|
||||
void pathTraversalWithAbsolutePathJpg() {
|
||||
final MultipartBody.Part part = createPart("michael.vorburger-crepes.jpg", "../17/michael.vorburger-crepes.jpg", "image/jpeg");
|
||||
create();
|
||||
final var part = createPart("michael.vorburger-crepes.jpg", "../17/michael.vorburger-crepes.jpg", "image/jpeg");
|
||||
|
||||
assertThat(part).isNotNull();
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
var exception = assertThrows(Exception.class, () -> {
|
||||
ok(fineractClient().images.create("clients", clientId, part));
|
||||
});
|
||||
|
||||
@@ -228,11 +239,11 @@ public class ImageTest extends IntegrationTest {
|
||||
@Test
|
||||
@Order(105)
|
||||
void pathTraversalWithAbsolutePathJpg2() {
|
||||
final MultipartBody.Part part = createPart("michael.vorburger-crepes.jpg", "..//17//michael.vorburger-crepes.jpg", "image/jpeg");
|
||||
final var part = createPart("michael.vorburger-crepes.jpg", "..//17//michael.vorburger-crepes.jpg", "image/jpeg");
|
||||
|
||||
assertThat(part).isNotNull();
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () -> {
|
||||
var exception = assertThrows(Exception.class, () -> {
|
||||
ok(fineractClient().images.create("clients", clientId, part));
|
||||
});
|
||||
|
||||
@@ -244,7 +255,7 @@ public class ImageTest extends IntegrationTest {
|
||||
private MultipartBody.Part createPart(String fileResource, String fileName, String mediaType) {
|
||||
try {
|
||||
byte[] data = IOUtils.toByteArray(ImageTest.class.getClassLoader().getResourceAsStream(fileResource));
|
||||
RequestBody rb = RequestBody.create(data, MediaType.get(mediaType));
|
||||
var rb = RequestBody.create(data, MediaType.get(mediaType));
|
||||
return MultipartBody.Part.createFormData("file", fileName, rb);
|
||||
} catch (Exception e) {
|
||||
log.error("Error creating file part.", e);
|
||||
@@ -253,6 +264,43 @@ public class ImageTest extends IntegrationTest {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void assertImage(String content) {
|
||||
assertImage(content, TEST_IMAGE_DIFF_PERCENTAGE);
|
||||
}
|
||||
|
||||
private void assertImage(String content, double diffPercent) {
|
||||
if (content.contains(",")) {
|
||||
content = content.substring(content.indexOf(",") + 1);
|
||||
}
|
||||
assertImage(new Base64().decode(content), diffPercent);
|
||||
}
|
||||
|
||||
private void assertImage(ResponseBody r) throws IOException {
|
||||
assertImage(r.bytes(), TEST_IMAGE_DIFF_PERCENTAGE);
|
||||
}
|
||||
|
||||
private void assertImage(byte[] data) {
|
||||
assertImage(data, TEST_IMAGE_DIFF_PERCENTAGE);
|
||||
}
|
||||
|
||||
private void assertImage(byte[] data, double diffPercent) {
|
||||
try (var resource = ImageTest.class.getClassLoader().getResourceAsStream(TEST_RESOURCE)) {
|
||||
requireNonNull(resource);
|
||||
|
||||
var expectedImage = ImageIO.read(resource);
|
||||
var actualImage = ImageIO.read(new ByteArrayInputStream(data));
|
||||
|
||||
var result = new ImageComparison(expectedImage, actualImage).setAllowingPercentOfDifferentPixels(diffPercent).compareImages();
|
||||
// result.writeResultTo(new File("build/diff.png"));
|
||||
|
||||
log.info("Image diff percentage: {}", result.getDifferencePercent());
|
||||
|
||||
assertEquals(MATCH, result.getImageComparisonState(), "The images should be identical");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
interface ImagesApiWithHeadersForTest extends ImagesApi {
|
||||
|
||||
@Headers("Accept: text/plain")
|
||||
|
||||
+2
-3
@@ -494,9 +494,8 @@ public final class Utils {
|
||||
@Deprecated(forRemoval = true)
|
||||
public static String performServerOutputTemplateLocationGet(final RequestSpecification requestSpec,
|
||||
final ResponseSpecification responseSpec, final String getURL, final String importDocumentId) {
|
||||
final String templateLocation = given().spec(requestSpec).queryParam("importDocumentId", importDocumentId).expect()
|
||||
.spec(responseSpec).log().ifError().when().get(getURL).andReturn().asString();
|
||||
return templateLocation.substring(1, templateLocation.length() - 1);
|
||||
return given().spec(requestSpec).queryParam("importDocumentId", importDocumentId).expect().spec(responseSpec).log().ifError().when()
|
||||
.get(getURL).andReturn().asString();
|
||||
}
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
|
||||
Reference in New Issue
Block a user