Skip to content

Commit

Permalink
MOSIP-37601 : Deactivate FTM Chip details (#1007)
Browse files Browse the repository at this point in the history
* MOSIP-37601 : Deactivate FTM Chip details

Signed-off-by: sudeep <[email protected]>

* MOSIP-37601 : Deactivate FTM Chip details

Signed-off-by: sudeep <[email protected]>

---------

Signed-off-by: sudeep <[email protected]>
  • Loading branch information
Sudeep7353 authored Nov 29, 2024
1 parent caf40db commit 793b43b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,13 @@ public ResponseWrapperV2<FtmDetailResponseDto> deactivateFtm(String ftmId) {
validateFtmChipDetail(ftmChipDetail);

FTPChipDetail ftm = ftmChipDetail.get();
Partner partnerDetails = getAssociatedPartner(partnerList, ftm, userId);
checkIfPartnerIsNotActive(partnerDetails);

boolean isAdmin = partnerHelper.isPartnerAdmin(authUserDetails().getAuthorities().toString());
if(!isAdmin){
Partner partnerDetails = getAssociatedPartner(partnerList, ftm, userId);
checkIfPartnerIsNotActive(partnerDetails);
}

if (!ftm.getApprovalStatus().equals(APPROVED)) {
LOGGER.error("Unable to deactivate FTM with id {}", ftm.getFtpChipDetailId());
throw new PartnerServiceException(ErrorCode.FTM_NOT_APPROVED.getErrorCode(),
Expand Down Expand Up @@ -610,8 +615,12 @@ public ResponseWrapperV2<OriginalCertDownloadResponseDto> getOriginalFtmCertific
validateFtmChipDetail(ftmChipDetail);

FTPChipDetail ftm = ftmChipDetail.get();
Partner partnerDetails = getAssociatedPartner(partnerList, ftm, userId);
checkIfPartnerIsNotActive(partnerDetails);

boolean isAdmin = partnerHelper.isPartnerAdmin(authUserDetails().getAuthorities().toString());
if(!isAdmin){
Partner partnerDetails = getAssociatedPartner(partnerList, ftm, userId);
checkIfPartnerIsNotActive(partnerDetails);
}

if (!(ftm.getApprovalStatus().equals(PENDING_APPROVAL) || ftm.getApprovalStatus().equals(APPROVED))) {
LOGGER.error("Unable to download original FTM certificate with id {}", ftm.getFtpChipDetailId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,17 @@ public String updateAPIKeyStatus(String partnerId, String policyId, APIkeyStatus
throw new PartnerManagerServiceException(ErrorCode.PARTNER_POLICY_LABEL_NOT_EXISTS.getErrorCode(),
ErrorCode.PARTNER_POLICY_LABEL_NOT_EXISTS.getErrorMessage());
}
// check if Partner is Active or not.
if (policyByLabel.getPartner() != null && !policyByLabel.getPartner().getIsActive()) {
LOGGER.error("Partner is not Active, hence status of API key cannot be updated, for partner: " + partnerId);
auditUtil.setAuditRequestDto(PartnerManageEnum.ACTIVATE_DEACTIVATE_API_PARTNERS_FAILED, partnerId,
"partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorMessage());
//check if logged in user is admin
boolean isAdmin = partnerHelper.isPartnerAdmin(authUserDetails().getAuthorities().toString());
if (!isAdmin){
// check if Partner is Active or not.
if (policyByLabel.getPartner() != null && !policyByLabel.getPartner().getIsActive()) {
LOGGER.error("Partner is not Active, hence status of API key cannot be updated, for partner: " + partnerId);
auditUtil.setAuditRequestDto(PartnerManageEnum.ACTIVATE_DEACTIVATE_API_PARTNERS_FAILED, partnerId,
"partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorMessage());
}
}
// check if API key has been already deactivated
if (!policyByLabel.getIsActive() && request.getStatus().equalsIgnoreCase(PartnerConstants.DEACTIVE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;

import io.mosip.kernel.openid.bridge.model.AuthUserDetails;
import io.mosip.kernel.openid.bridge.model.MosipUserDto;
Expand Down Expand Up @@ -43,6 +40,8 @@
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithUserDetails;
Expand Down Expand Up @@ -802,12 +801,25 @@ public void getTheRequestForPartnerAPIKeyToPolicyMappingsForGivenRequestIdTest_S
}

@Test
public void updateAPIKeyStatusTest01() {
public void updateAPIKeyStatusTest01() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
APIkeyStatusUpdateRequestDto statusDto = new APIkeyStatusUpdateRequestDto();
statusDto.setLabel("456");
statusDto.setStatus("De-Activate");
Mockito.when(partnerPolicyRepository.findByPartnerIdPolicyIdAndLabel(Mockito.any(), Mockito.any(),
Mockito.any())).thenReturn(getPartnerPolicy());

io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto();
AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123");
Collection<GrantedAuthority> newAuthorities = List.of(
new SimpleGrantedAuthority("ROLE_USER")
);
Method addAuthoritiesMethod = AuthUserDetails.class.getDeclaredMethod("addAuthorities", Collection.class, String.class);
addAuthoritiesMethod.setAccessible(true);
addAuthoritiesMethod.invoke(authUserDetails, newAuthorities, null);
SecurityContextHolder.setContext(securityContext);
when(authentication.getPrincipal()).thenReturn(authUserDetails);
when(securityContext.getAuthentication()).thenReturn(authentication);
Mockito.when(partnerHelper.isPartnerAdmin(authentication.getAuthorities().toString())).thenReturn(false);
try {
partnerManagementImpl.updateAPIKeyStatus("1234", "456",statusDto);
}catch (PartnerManagerServiceException e) {
Expand All @@ -816,13 +828,27 @@ public void updateAPIKeyStatusTest01() {
}

@Test
public void updateAPIKeyStatusTest02() {
public void updateAPIKeyStatusTest02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
APIkeyStatusUpdateRequestDto statusDto = new APIkeyStatusUpdateRequestDto();
statusDto.setLabel("456");
statusDto.setStatus("De-Active");
Mockito.when(partnerPolicyRepository.findByPartnerIdPolicyIdAndLabel(Mockito.any(), Mockito.any(),
Mockito.any())).thenReturn(getPartnerPolicy());
Mockito.when(authPolicyRepository.findById(Mockito.any())).thenReturn(Optional.of(getAuthPolicies().get(0)));

io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto();
AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123");
Collection<GrantedAuthority> newAuthorities = List.of(
new SimpleGrantedAuthority("ROLE_USER")
);
Method addAuthoritiesMethod = AuthUserDetails.class.getDeclaredMethod("addAuthorities", Collection.class, String.class);
addAuthoritiesMethod.setAccessible(true);
addAuthoritiesMethod.invoke(authUserDetails, newAuthorities, null);
SecurityContextHolder.setContext(securityContext);
when(authentication.getPrincipal()).thenReturn(authUserDetails);
when(securityContext.getAuthentication()).thenReturn(authentication);
Mockito.when(partnerHelper.isPartnerAdmin(authentication.getAuthorities().toString())).thenReturn(false);

partnerManagementImpl.updateAPIKeyStatus("1234", "456", statusDto);
}

Expand All @@ -842,13 +868,27 @@ public void updateAPIKeyStatusTest03() {
}

@Test
public void updateAPIKeyStatusTest04() {
public void updateAPIKeyStatusTest04() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
APIkeyStatusUpdateRequestDto statusDto = new APIkeyStatusUpdateRequestDto();
statusDto.setLabel("456");
statusDto.setStatus("Active");
Mockito.when(partnerPolicyRepository.findByPartnerIdPolicyIdAndLabel(Mockito.any(), Mockito.any(),
Mockito.any())).thenReturn(getPartnerPolicy());
Mockito.when(authPolicyRepository.findById(Mockito.any())).thenReturn(Optional.of(getAuthPolicies().get(0)));

io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto();
AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123");
Collection<GrantedAuthority> newAuthorities = List.of(
new SimpleGrantedAuthority("ROLE_USER")
);
Method addAuthoritiesMethod = AuthUserDetails.class.getDeclaredMethod("addAuthorities", Collection.class, String.class);
addAuthoritiesMethod.setAccessible(true);
addAuthoritiesMethod.invoke(authUserDetails, newAuthorities, null);
SecurityContextHolder.setContext(securityContext);
when(authentication.getPrincipal()).thenReturn(authUserDetails);
when(securityContext.getAuthentication()).thenReturn(authentication);
Mockito.when(partnerHelper.isPartnerAdmin(authentication.getAuthorities().toString())).thenReturn(false);

partnerManagementImpl.updateAPIKeyStatus("1234", "456", statusDto);
}

Expand Down

0 comments on commit 793b43b

Please sign in to comment.