Skip to content

Commit

Permalink
Development: Enable deactivated LocalVC integration tests (#9942)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonEntholzer authored Dec 12, 2024
1 parent cc6c76d commit dd557bd
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.TestSecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.test.util.AssertionErrors;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
Expand Down Expand Up @@ -760,6 +762,15 @@ public void getWithForwardedUrl(String path, HttpStatus expectedStatus, String e
restoreSecurityContext();
}

public void getWithFileContents(String path, HttpStatus expectedStatus, String expectedFileContents) throws Exception {
performMvcRequest(MockMvcRequestBuilders.get(new URI(path))).andExpect(status().is(expectedStatus.value())).andExpect(matchFileContents(expectedFileContents)).andReturn();
restoreSecurityContext();
}

public static ResultMatcher matchFileContents(String expectedFilesAsString) {
return (result) -> AssertionErrors.assertEquals("File contents", expectedFilesAsString, result.getResponse().getContentAsString());
}

public String getRedirectTarget(String path, HttpStatus expectedStatus) throws Exception {
MvcResult res = performMvcRequest(MockMvcRequestBuilders.get(new URI(path))).andExpect(status().is(expectedStatus.value())).andReturn();
return res.getResponse().getRedirectedUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static de.tum.cit.aet.artemis.programming.web.ProgrammingExerciseResourceErrorKeys.INVALID_TEMPLATE_BUILD_PLAN_ID;
import static de.tum.cit.aet.artemis.programming.web.ProgrammingExerciseResourceErrorKeys.INVALID_TEMPLATE_REPOSITORY_URL;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
Expand All @@ -34,8 +35,10 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -99,12 +102,14 @@
import de.tum.cit.aet.artemis.programming.domain.ProgrammingExerciseStudentParticipation;
import de.tum.cit.aet.artemis.programming.domain.ProgrammingExerciseTestCase;
import de.tum.cit.aet.artemis.programming.domain.ProgrammingLanguage;
import de.tum.cit.aet.artemis.programming.domain.ProgrammingSubmission;
import de.tum.cit.aet.artemis.programming.domain.ProjectType;
import de.tum.cit.aet.artemis.programming.domain.RepositoryType;
import de.tum.cit.aet.artemis.programming.domain.VcsRepositoryUri;
import de.tum.cit.aet.artemis.programming.dto.ProgrammingExerciseResetOptionsDTO;
import de.tum.cit.aet.artemis.programming.dto.ProgrammingExerciseTestCaseDTO;
import de.tum.cit.aet.artemis.programming.dto.ProgrammingExerciseTestCaseStateDTO;
import de.tum.cit.aet.artemis.programming.hestia.util.HestiaUtilTestService;
import de.tum.cit.aet.artemis.programming.repository.AuxiliaryRepositoryRepository;
import de.tum.cit.aet.artemis.programming.service.GitService;
import de.tum.cit.aet.artemis.programming.service.UriService;
Expand Down Expand Up @@ -192,6 +197,9 @@ public class ProgrammingExerciseIntegrationTestService {
@Autowired
private TextExerciseUtilService textExerciseUtilService;

@Autowired
protected HestiaUtilTestService hestiaUtilTestService;

@Autowired
private ProgrammingExerciseTestRepository programmingExerciseTestRepository;

Expand Down Expand Up @@ -2278,6 +2286,98 @@ void testReEvaluateAndUpdateProgrammingExercise_isNotSameGivenExerciseIdInReques
request.put("/api/programming-exercises/" + programmingExercise.getId() + "/re-evaluate", programmingExerciseToBeConflicted, HttpStatus.CONFLICT);
}

void test_redirectGetSolutionRepositoryFilesWithoutContent() throws Exception {
test_redirectGetSolutionRepositoryFilesWithoutContent((exercise, files) -> {
LocalRepository localRepository = new LocalRepository("main");
try {
hestiaUtilTestService.setupSolution(files, exercise, localRepository);
}
catch (Exception e) {
fail("Setup solution threw unexpected exception: " + e.getMessage());
}
return localRepository;
});
}

private void test_redirectGetSolutionRepositoryFilesWithoutContent(BiFunction<ProgrammingExercise, Map<String, String>, LocalRepository> setupRepositoryMock) throws Exception {
setupRepositoryMock.apply(programmingExercise, Map.ofEntries(Map.entry("A.java", "abc"), Map.entry("B.java", "cde"), Map.entry("C.java", "efg")));

var savedExercise = programmingExerciseRepository.findByIdWithTemplateAndSolutionParticipationElseThrow(programmingExercise.getId());

// We expect an URL which is the endpoint, with which the file contents can be retrieved
request.getWithForwardedUrl("/api/programming-exercises/" + programmingExercise.getId() + "/file-names", HttpStatus.OK,
"/api/repository/" + savedExercise.getSolutionParticipation().getId() + "/file-names");
}

void test_redirectGetTemplateRepositoryFilesWithContent() throws Exception {
test_redirectGetTemplateRepositoryFilesWithContent((exercise, files) -> {
LocalRepository localRepository = new LocalRepository("main");
try {
hestiaUtilTestService.setupTemplate(files, exercise, localRepository);
}
catch (Exception e) {
fail("Setup template threw unexpected exception: " + e.getMessage());
}
return localRepository;
});
}

private void test_redirectGetTemplateRepositoryFilesWithContent(BiFunction<ProgrammingExercise, Map<String, String>, LocalRepository> setupRepositoryMock) throws Exception {
setupRepositoryMock.apply(programmingExercise, Map.ofEntries(Map.entry("A.java", "abc"), Map.entry("B.java", "cde"), Map.entry("C.java", "efg")));

var savedExercise = programmingExerciseRepository.findByIdWithTemplateAndSolutionParticipationElseThrow(programmingExercise.getId());

request.getWithForwardedUrl("/api/programming-exercises/" + programmingExercise.getId() + "/template-files-content", HttpStatus.OK,
"/api/repository/" + savedExercise.getTemplateParticipation().getId() + "/files-content");
}

void testRedirectGetParticipationRepositoryFilesWithContentAtCommit(String testPrefix) throws Exception {
testRedirectGetParticipationRepositoryFilesWithContentAtCommit((exercise, files) -> {
LocalRepository localRepository = new LocalRepository("main");
var studentLogin = testPrefix + "student1";
try {
localRepository.configureRepos("testLocalRepo", "testOriginRepo");
return hestiaUtilTestService.setupSubmission(files, exercise, localRepository, studentLogin);
}
catch (Exception e) {
fail("Test setup failed");
}
return null;
});
}

private void testRedirectGetParticipationRepositoryFilesWithContentAtCommit(BiFunction<ProgrammingExercise, Map<String, String>, ProgrammingSubmission> setupRepositoryMock)
throws Exception {
var submission = setupRepositoryMock.apply(programmingExercise, Map.ofEntries(Map.entry("A.java", "abc"), Map.entry("B.java", "cde"), Map.entry("C.java", "efg")));
String filesWithContentsAsJson = "{\n" + " \"C.java\" : \"efg\",\n" + " \"B.java\" : \"cde\",\n" + " \"A.java\" : \"abc\"\n" + "}";

request.getWithFileContents("/api/programming-exercise-participations/" + participation1.getId() + "/files-content/" + submission.getCommitHash(), HttpStatus.OK,
filesWithContentsAsJson);
}

void testRedirectGetParticipationRepositoryFilesWithContentAtCommitForbidden(String testPrefix) throws Exception {
testRedirectGetParticipationRepositoryFilesWithContentAtCommitForbidden((exercise, files) -> {
LocalRepository localRepository = new LocalRepository("main");

var studentLogin = testPrefix + "student1";
try {
localRepository.configureRepos("testLocalRepo", "testOriginRepo");
return hestiaUtilTestService.setupSubmission(files, exercise, localRepository, studentLogin);
}
catch (Exception e) {
fail("Test setup failed");
}
return null;
});
}

private void testRedirectGetParticipationRepositoryFilesWithContentAtCommitForbidden(
BiFunction<ProgrammingExercise, Map<String, String>, ProgrammingSubmission> setupRepositoryMock) throws Exception {
var submission = setupRepositoryMock.apply(programmingExercise, Map.ofEntries(Map.entry("A.java", "abc"), Map.entry("B.java", "cde"), Map.entry("C.java", "efg")));

request.get("/api/programming-exercise-participations/" + participation1.getId() + "/files-content/" + submission.getCommitHash(), HttpStatus.FORBIDDEN, Map.class);
}

private long getMaxProgrammingExerciseId() {
return programmingExerciseRepository.findAll(PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "id"))).stream().mapToLong(ProgrammingExercise::getId).max().orElse(1L);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,30 +174,28 @@ void testReEvaluateAndUpdateProgrammingExercise_notFound() throws Exception {
void testReEvaluateAndUpdateProgrammingExercise_isNotSameGivenExerciseIdInRequestBody_conflict() throws Exception {
programmingExerciseIntegrationTestService.testReEvaluateAndUpdateProgrammingExercise_isNotSameGivenExerciseIdInRequestBody_conflict();
}
//
// @Test
// @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
// void test_redirectGetSolutionRepositoryFilesWithoutContent() throws Exception {
// programmingExerciseIntegrationTestService.test_redirectGetSolutionRepositoryFilesWithoutContent();
// }
//
// @Test
// @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
// void test_redirectGetTemplateRepositoryFilesWithContent() throws Exception {
// programmingExerciseIntegrationTestService.test_redirectGetTemplateRepositoryFilesWithContent();
// }
//
// @Test
// @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
// void testRedirectGetParticipationRepositoryFilesWithContentAtCommit() throws Exception {
// programmingExerciseIntegrationTestService.testRedirectGetParticipationRepositoryFilesWithContentAtCommit();
// }
//
// @Test
// @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
// void testRedirectGetParticipationRepositoryFilesWithContentAtCommitForbidden() throws Exception {
// programmingExerciseIntegrationTestService.testRedirectGetParticipationRepositoryFilesWithContentAtCommitForbidden();
// }

// TODO add all other tests

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void test_redirectGetSolutionRepositoryFilesWithoutContent() throws Exception {
programmingExerciseIntegrationTestService.test_redirectGetSolutionRepositoryFilesWithoutContent();
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void test_redirectGetTemplateRepositoryFilesWithContent() throws Exception {
programmingExerciseIntegrationTestService.test_redirectGetTemplateRepositoryFilesWithContent();
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testGetParticipationFilesWithContentAtCommitShouldRedirect() throws Exception {
programmingExerciseIntegrationTestService.testRedirectGetParticipationRepositoryFilesWithContentAtCommit(TEST_PREFIX);
}

@Test
@WithMockUser(username = TEST_PREFIX + "editor1", roles = "EDITOR")
void testGetParticipationFilesWithContentAtCommitEditorForbidden() throws Exception {
programmingExerciseIntegrationTestService.testRedirectGetParticipationRepositoryFilesWithContentAtCommitForbidden(TEST_PREFIX);
}
}

0 comments on commit dd557bd

Please sign in to comment.