FINERACT-1678-Restrict-COB-catchup-api-for-batch-managers

This commit is contained in:
Ruchi Dhamankar
2023-02-20 11:39:01 +01:00
committed by Arnold Gálovics
parent 89deb6d3b8
commit 6cf37b7749
5 changed files with 243 additions and 7 deletions
@@ -41,12 +41,16 @@ import org.springframework.web.filter.OncePerRequestFilter;
@RequiredArgsConstructor
public class FineractInstanceModeApiFilter extends OncePerRequestFilter {
private static final List<ExceptionListItem> EXCEPTION_LIST = List.of(
item(FineractProperties.FineractModeProperties::isBatchManagerEnabled, pi -> pi.startsWith("/jobs")),
item(p -> true, pi -> pi.startsWith("/instance-mode")),
// Batches with all GET requests need to be allowed in read-only instances, hence this check will be moved
// under the Api Resource.
item(p -> true, pi -> pi.startsWith("/batches")));
private static final List<ExceptionListItem> EXCEPTION_LIST = List
.of(item(FineractProperties.FineractModeProperties::isBatchManagerEnabled, pi -> pi.startsWith("/jobs")),
item(FineractProperties.FineractModeProperties::isBatchManagerEnabled, pi -> pi.startsWith("/loans/catch-up")),
item(FineractProperties.FineractModeProperties::isBatchManagerEnabled,
pi -> pi.startsWith("/loans/is-catch-up-running")),
item(p -> true, pi -> pi.startsWith("/instance-mode")),
// Batches with all GET requests need to be allowed in read-only instances, hence this check will be
// moved
// under the Api Resource.
item(p -> true, pi -> pi.startsWith("/batches")));
private final FineractProperties fineractProperties;
@@ -284,4 +284,88 @@ class FineractInstanceModeApiFilterTest {
verify(filterChain).doFilter(request, response);
}
@Test
void testDoFilterInternal_ShouldLetLoanCOBCatchUpApiThrough_WhenFineractIsInBatchManagerMode() throws ServletException, IOException {
// given
FineractProperties.FineractModeProperties modeProperties = InstanceModeMock.createModeProps(false, false, false, true);
given(fineractProperties.getMode()).willReturn(modeProperties);
given(request.getMethod()).willReturn(HttpMethod.POST.name());
given(request.getPathInfo()).willReturn("/loans/catch-up");
// when
underTest.doFilterInternal(request, response, filterChain);
// then
verify(filterChain).doFilter(request, response);
}
@Test
void testDoFilterInternal_ShouldNotLetLoanCOBCatchUpApiThrough_WhenFineractIsNotInBatchManagerMode()
throws ServletException, IOException {
// given
FineractProperties.FineractModeProperties modeProperties = InstanceModeMock.createModeProps(false, false, true, false);
given(fineractProperties.getMode()).willReturn(modeProperties);
given(request.getMethod()).willReturn(HttpMethod.POST.name());
given(request.getPathInfo()).willReturn("/loans/catch-up");
// when
underTest.doFilterInternal(request, response, filterChain);
// then
verifyNoInteractions(filterChain);
verify(response).setStatus(HttpStatus.SC_METHOD_NOT_ALLOWED);
}
@Test
void testDoFilterInternal_ShouldLetLoanCOBCatchUpStatusApiThrough_WhenFineractIsInBatchManagerMode()
throws ServletException, IOException {
// given
FineractProperties.FineractModeProperties modeProperties = InstanceModeMock.createModeProps(false, false, false, true);
given(fineractProperties.getMode()).willReturn(modeProperties);
given(request.getMethod()).willReturn(HttpMethod.POST.name());
given(request.getPathInfo()).willReturn("/loans/is-catch-up-running");
// when
underTest.doFilterInternal(request, response, filterChain);
// then
verify(filterChain).doFilter(request, response);
}
@Test
void testDoFilterInternal_ShouldNotLetLoanCOBCatchUpStatusApiThrough_WhenFineractIsNotInBatchManagerMode()
throws ServletException, IOException {
// given
FineractProperties.FineractModeProperties modeProperties = InstanceModeMock.createModeProps(false, false, true, false);
given(fineractProperties.getMode()).willReturn(modeProperties);
given(request.getMethod()).willReturn(HttpMethod.POST.name());
given(request.getPathInfo()).willReturn("/loans/is-catch-up-running");
// when
underTest.doFilterInternal(request, response, filterChain);
// then
verifyNoInteractions(filterChain);
verify(response).setStatus(HttpStatus.SC_METHOD_NOT_ALLOWED);
}
@Test
void testDoFilterInternal_ShouldLetOtherLoanCatchUpApisThrough_WhenFineractIsInBatchManagerAndReadModeAndIsGetApi()
throws ServletException, IOException {
// given
FineractProperties.FineractModeProperties modeProperties = InstanceModeMock.createModeProps(true, false, false, true);
given(fineractProperties.getMode()).willReturn(modeProperties);
given(request.getMethod()).willReturn(HttpMethod.GET.name());
given(request.getPathInfo()).willReturn("/loans/oldest-cob-closed");
// when
underTest.doFilterInternal(request, response, filterChain);
// then
verify(filterChain).doFilter(request, response);
}
@Test
void testDoFilterInternal_ShouldLetOtherLoanCatchUpApisThrough_WhenFineractIsInReadModeAndIsGetApi()
throws ServletException, IOException {
// given
FineractProperties.FineractModeProperties modeProperties = InstanceModeMock.createModeProps(true, false, false, false);
given(fineractProperties.getMode()).willReturn(modeProperties);
given(request.getMethod()).willReturn(HttpMethod.GET.name());
given(request.getPathInfo()).willReturn("/loans/oldest-cob-closed");
// when
underTest.doFilterInternal(request, response, filterChain);
// then
verify(filterChain).doFilter(request, response);
}
}