Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test cases for IdaAuditPlugin #1118

Open
wants to merge 3 commits into
base: release-1.2.0.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check and exception added? there can be audits which will not involve user.

throw new IllegalArgumentException("Username cannot be empty or null in the audit");
}
audit(username, action, status, audit);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<AuditResponse> mockresponseWrapper = new ResponseWrapper<>();
ResponseEntity<ResponseWrapper> responseEntity = ResponseEntity.ok(mockresponseWrapper);
ParameterizedTypeReference<ResponseWrapper> responseType =
new ParameterizedTypeReference<ResponseWrapper>() {
};
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<AuditResponse> mockresponseWrapper = new ResponseWrapper<>();
ResponseEntity<ResponseWrapper> responseEntity = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockresponseWrapper);
ParameterizedTypeReference<ResponseWrapper> responseType =
new ParameterizedTypeReference<ResponseWrapper>() {
};
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<AuditResponse> mockresponseWrapper = new ResponseWrapper<>();
ResponseEntity<ResponseWrapper> responseEntity = ResponseEntity.status(HttpStatus.FORBIDDEN).body(mockresponseWrapper);
ParameterizedTypeReference<ResponseWrapper> responseType =
new ParameterizedTypeReference<ResponseWrapper>() {
};
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();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down