diff --git a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java index 1e2ce7be147..5e9c1d45ce2 100644 --- a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java +++ b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java @@ -54,6 +54,9 @@ public void logAudit(Action action, ActionStatus status, AuditDTO audit, Throwab @Override public void logAudit(String username, Action action, ActionStatus status, AuditDTO audit, Throwable t) { + if (t!=null && StringUtils.isEmpty(username)) { + throw new IllegalArgumentException("Username cannot be empty or null in the audit"); + } audit(username, action, status, audit); } diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java new file mode 100644 index 00000000000..0aa3dceced2 --- /dev/null +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java @@ -0,0 +1,187 @@ +package io.mosip.authentication.esignet.integration.service; + +import io.mosip.esignet.api.dto.AuditDTO; +import io.mosip.esignet.api.util.Action; +import io.mosip.esignet.api.util.ActionStatus; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.mosip.authentication.esignet.integration.dto.AuditResponse; +import io.mosip.authentication.esignet.integration.helper.AuthTransactionHelper; +import io.mosip.kernel.core.http.ResponseWrapper; +import org.mockito.*; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; + +import static org.mockito.ArgumentMatchers.*; + + + +@RunWith(MockitoJUnitRunner.class) +public class IdaAuditPluginImplTest { + + @InjectMocks + private IdaAuditPluginImpl idaAuditPlugin; + + @Mock + private AuthTransactionHelper authTransactionHelper; + + @Mock + private ObjectMapper objectMapper; + + @Mock + private RestTemplate restTemplate; + + @Test + public void logAudit_WithValidDetails_ThenPass() { + Action action = Action.AUTHENTICATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + try { + idaAuditPlugin.logAudit(action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithThrowable_ThenPass() { + Action action = Action.GENERATE_TOKEN; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + try { + idaAuditPlugin.logAudit(action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithUsername_WithValidDetails_ThenPass() { + String username = "username"; + Action action = Action.OIDC_CLIENT_UPDATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + try { + idaAuditPlugin.logAudit(username, action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithEmptyUsername_ThenFail() { + String username = ""; + Action action = Action.OIDC_CLIENT_UPDATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + Assert.assertThrows(IllegalArgumentException.class,() -> idaAuditPlugin.logAudit(username, action, status, auditDTO, throwable)); + } + + @Test + public void logAudit_WithUsername_WithThrowable() throws Exception { + String username = "username"; + Action action = Action.GENERATE_TOKEN; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithValidStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.ok(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithUnauthorizedStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + + } + + @Test + public void logAudit_WithForbiddenStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.FORBIDDEN).body(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + + } + +} \ No newline at end of file diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java index b37730feed7..25681f8e861 100644 --- a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java @@ -205,7 +205,7 @@ public void getVerifiableCredentialWithLinkedDataProof_withInValidIndividualId_t } @Test - public void getVerifiableCredentialWithLinkedDataProof_withInVlidResponse_thenFail() throws Exception { + public void getVerifiableCredentialWithLinkedDataProof_withInValidResponse_thenFail() throws Exception { ReflectionTestUtils.setField(idaVCIssuancePlugin,"vciExchangeUrl","http://example.com"); ReflectionTestUtils.setField(idaVCIssuancePlugin,"storeIndividualId",true);