Skip to content

Commit

Permalink
refactor: Refactor LoginCheck and existing unit test for License Serv…
Browse files Browse the repository at this point in the history
…ice (#523)

* refator: simplify func call

Signed-off-by: Mykola Rudyk <[email protected]>

* test: add extra lines for coverage

Signed-off-by: Mykola Rudyk <[email protected]>

* test: update GitHubControllerTests and increase coverage

Signed-off-by: Mykola Rudyk <[email protected]>

---------

Signed-off-by: Mykola Rudyk <[email protected]>
  • Loading branch information
m-rudyk authored May 31, 2024
1 parent 8a6f674 commit 03b112d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/lpvs/controller/LPVSWebController.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.web.util.HtmlUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -235,7 +236,8 @@ public ResponseEntity<HistoryEntity> newHistoryPageByUser(
String[] pullNumberTemp = pr.getPullRequestUrl().split("/");
LocalDateTime localDateTime =
pr.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
String formattingDateTime = lpvsLoginCheckService.dateTimeFormatting(localDateTime);
String formattingDateTime =
localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

// Validate and sanitize user inputs to prevent XSS attacks
sanitizeUserInputs(pr);
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/lpvs/service/LPVSLoginCheckService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
Expand Down Expand Up @@ -131,14 +129,4 @@ public HistoryPageEntity pathCheck(

return new HistoryPageEntity(prPage, count);
}

/**
* Format LocalDateTime object as a string.
*
* @param localDateTime The LocalDateTime object to format.
* @return String representation of the formatted LocalDateTime.
*/
public String dateTimeFormatting(LocalDateTime localDateTime) {
return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
48 changes: 46 additions & 2 deletions src/test/java/com/lpvs/controller/GitHubControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class GitHubControllerTest {

@SystemStub private EnvironmentVariables environmentVars;

private static final String SIGNATURE = "X-Hub-Signature-256";
private static final String SIGNATURE =
"sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17";
private static final String SUCCESS = "Success";
private static final String ERROR = "Error";

Expand All @@ -61,6 +62,15 @@ public class GitHubControllerTest {
"",
exitHandler);

GitHubController gitHubControllerWrongSecret =
new GitHubController(
mocked_instance_queueServ,
mocked_instance_ghServ,
mocked_ghConnServ,
mocked_queueRepo,
"LPVS",
exitHandler);

@Test
public void noSignatureTest() {
ResponseEntity<LPVSResponseWrapper> actual;
Expand All @@ -74,6 +84,19 @@ public void noSignatureTest() {
assertEquals(expected.toString().substring(0, 56), actual.toString().substring(0, 56));
}

@Test
public void wrongGithubSecretTest() {
ResponseEntity<LPVSResponseWrapper> actual;
try {
actual = gitHubControllerWrongSecret.gitHubWebhooks(SIGNATURE, "test");
} catch (Exception e) {
actual = null;
}
ResponseEntity<LPVSResponseWrapper> expected =
new ResponseEntity<>(new LPVSResponseWrapper(ERROR), HttpStatus.FORBIDDEN);
assertEquals(expected.toString().substring(0, 56), actual.toString().substring(0, 56));
}

@Test
public void noPayloadTest() {
ResponseEntity<LPVSResponseWrapper> actual;
Expand Down Expand Up @@ -179,6 +202,27 @@ public void wrongSecretTest() {
}
}

@Test
public void noSecretSetTest() {

environmentVars.set("", "LPVS");

try {
gitHubController.initializeGitHubController();
fail("Expected Exception was not thrown");
} catch (NullPointerException e) {
// Test passes if a NullPointerException is caught during access to null pointer
// If we remove ExitHandler any time, this behaviour should be changed
log.info(
"GitHubControllerTest::noSecretSetTest passed with NullPointerException: " + e);
} catch (Exception e) {
// Test fails if any other exception is caught
log.error(
"GitHubControllerTest::noSecretSetTest failed with unexpected exception: " + e);
fail("Unexpected exception thrown: " + e);
}
}

@Test
public void testGitHubSingleScan_Success() throws Exception {
environmentVars.set("LPVS_GITHUB_SECRET", "LPVS");
Expand Down Expand Up @@ -217,7 +261,7 @@ public void testGitHubSingleScan_ConnectionError() throws Exception {
when(mocked_instance_ghServ.getInternalQueueByPullRequest(anyString()))
.thenThrow(new RuntimeException("Connection error"));
ResponseEntity<LPVSResponseWrapper> responseEntity =
gitHubController.gitHubSingleScan("org", "repo", 1);
gitHubControllerWrongSecret.gitHubSingleScan("org", "repo", 1);

assertEquals(HttpStatus.FORBIDDEN, responseEntity.getStatusCode());
}
Expand Down
18 changes: 13 additions & 5 deletions src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public void testFindConflicts() {
class TestInit {
final LPVSLicenseService licenseService = new LPVSLicenseService(null, exitHandler);

final LPVSLicenseService licenseServiceWithSource =
new LPVSLicenseService("db", exitHandler);

final LPVSLicenseRepository lpvsLicenseRepository =
Mockito.mock(LPVSLicenseRepository.class);

Expand Down Expand Up @@ -221,17 +224,17 @@ public void testInitDB() {
Field lpvsLicenseRepositoryField =
LPVSLicenseService.class.getDeclaredField("lpvsLicenseRepository");
lpvsLicenseRepositoryField.setAccessible(true);
lpvsLicenseRepositoryField.set(licenseService, lpvsLicenseRepository);
lpvsLicenseRepositoryField.set(licenseServiceWithSource, lpvsLicenseRepository);

Field lpvsLicenseConflictRepositoryField =
LPVSLicenseService.class.getDeclaredField("lpvsLicenseConflictRepository");
lpvsLicenseConflictRepositoryField.setAccessible(true);
lpvsLicenseConflictRepositoryField.set(
licenseService, lpvsLicenseConflictRepository);
licenseServiceWithSource, lpvsLicenseConflictRepository);

Method init_method = licenseService.getClass().getDeclaredMethod("init");
Method init_method = licenseServiceWithSource.getClass().getDeclaredMethod("init");
init_method.setAccessible(true);
init_method.invoke(licenseService);
init_method.invoke(licenseServiceWithSource);
} catch (NoSuchMethodException
| IllegalAccessException
| InvocationTargetException
Expand Down Expand Up @@ -314,13 +317,16 @@ class TestFindLicenseBySPDXFindLicenseByName {
LPVSLicense lpvs_license_2;
final String license_name_2 = "Apache-2.0 License";
final String spdx_id_2 = "Apache-2.0";
final String license_name_aleternative_2 = "Apache License 2.0";

@BeforeEach
void setUp() throws NoSuchFieldException, IllegalAccessException {
licenseService = new LPVSLicenseService(null, exitHandler);

lpvs_license_1 = new LPVSLicense(1L, license_name_1, spdx_id_1, null, null, null);
lpvs_license_2 = new LPVSLicense(2L, license_name_2, spdx_id_2, null, null, null);
lpvs_license_2 =
new LPVSLicense(
2L, license_name_2, spdx_id_2, null, license_name_aleternative_2, null);

licenseService.addLicenseToList(lpvs_license_1);
licenseService.addLicenseToList(lpvs_license_2);
Expand All @@ -334,6 +340,8 @@ public void testFindLicenseBySPDXFindLicenseByName() {

assertEquals(lpvs_license_1, licenseService.findLicenseByName(license_name_1));
assertEquals(lpvs_license_2, licenseService.findLicenseByName(license_name_2));
assertEquals(
lpvs_license_2, licenseService.findLicenseByName(license_name_aleternative_2));
assertNull(licenseService.findLicenseByName("Apache-1.1 License"));
}
}
Expand Down

0 comments on commit 03b112d

Please sign in to comment.