Skip to content

Commit

Permalink
added route to fetch all test related files
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqua-sc committed May 9, 2024
1 parent c35e8cb commit 59c24ca
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ private ResponseEntity<?> alterTests(
DockerSubmissionTestModel.removeDockerImage(
finalDockerImage1);
});

}
}

Expand Down Expand Up @@ -203,102 +202,19 @@ private ResponseEntity<?> alterTests(
@GetMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests")
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getTests(@PathVariable("projectid") long projectId, Auth auth) {
CheckResult<TestEntity> projectCheck = testUtil.getTestIfAdmin(projectId, auth.getUserEntity());
CheckResult<Pair<TestEntity, Boolean>> projectCheck = testUtil.getTestWithAdminStatus(projectId, auth.getUserEntity());
if (!projectCheck.getStatus().equals(HttpStatus.OK)) {
return ResponseEntity.status(projectCheck.getStatus()).body(projectCheck.getMessage());
}
TestEntity test = projectCheck.getData();
TestEntity test = projectCheck.getData().getFirst();
if (!projectCheck.getData().getSecond()) { // user is not an admin, hide script and image
test.setDockerTestScript(null);
test.setDockerImage(null);
}
TestJson res = entityToJsonConverter.testEntityToTestJson(test, projectId);
return ResponseEntity.ok(res);
}

/**
* Function to get the structure test file of a project
* @param projectId the id of the project to get the structure test file for
* @param auth the authentication object of the requesting user
* @HttpMethod GET
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6133750">apiDog documentation</a>
* @AllowedRoles teacher, student
* @ApiPath /api/projects/{projectid}/tests/structuretest
* @return ResponseEntity with the structure test file
*/
@GetMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests/structuretest")
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getStructureTestFile(@PathVariable("projectid") long projectId, Auth auth) {
return getTestProperty(projectId, auth, TestEntity::getStructureTemplate);
}

/**
* Function to get the docker test template of a project
* @param projectId the id of the project to get the docker test file for
* @param auth the authentication object of the requesting user
* @HttpMethod GET
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6133798">apiDog documentation</a>
* @AllowedRoles teacher, student
* @ApiPath /api/projects/{projectid}/tests/dockertest
* @return ResponseEntity with the docker test file
*/
@GetMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests/dockertesttemplate")
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getDockerTestTemplate(@PathVariable("projectid") long projectId, Auth auth) {
return getTestProperty(projectId, auth, TestEntity::getDockerTestTemplate);
}

/**
* Function to get the docker test script of a project
* @param projectId the id of the project to get the docker test file for
* @param auth the authentication object of the requesting user
* @HttpMethod GET
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6133798">apiDog documentation</a>
* @AllowedRoles teacher, student
* @ApiPath /api/projects/{projectid}/tests/dockertest
* @return ResponseEntity with the docker test file
*/
@GetMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests/dockertestscript")
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getDockerTestScript(@PathVariable("projectid") long projectId, Auth auth) {
return getTestPropertyCheckAdmin(projectId, auth, TestEntity::getDockerTestScript);
}

/**
* Function to get the docker image of a project test
* @param projectId the id of the project to get the docker test file for
* @param auth the authentication object of the requesting user
* @HttpMethod GET
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6133798">apiDog documentation</a>
* @AllowedRoles teacher, student
* @ApiPath /api/projects/{projectid}/tests/dockertest
* @return ResponseEntity with the docker test file
*/
@GetMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests/dockertestimage")
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getDockerTestImage(@PathVariable("projectid") long projectId, Auth auth) {
return getTestPropertyCheckAdmin(projectId, auth, TestEntity::getDockerImage);
}


public ResponseEntity<?> getTestPropertyCheckAdmin(long projectId, Auth auth, Function<TestEntity, String> propertyGetter) {
CheckResult<TestEntity> projectCheck = testUtil.getTestIfAdmin(projectId, auth.getUserEntity());
if (!projectCheck.getStatus().equals(HttpStatus.OK)) {
return ResponseEntity.status(projectCheck.getStatus()).body(projectCheck.getMessage());
}
TestEntity testEntity = projectCheck.getData();
if (testEntity == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No tests found for project with id: " + projectId);
}
return propertyGetter.apply(testEntity) == null ? ResponseEntity.status(HttpStatus.NOT_FOUND).body("No test found") : ResponseEntity.ok(propertyGetter.apply(testEntity));
}

public ResponseEntity<?> getTestProperty(long projectId, Auth auth, Function<TestEntity, String> propertyGetter) {
TestEntity testEntity = testUtil.getTestIfExists(projectId);
if (testEntity == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No tests found for project with id: " + projectId);
}

return propertyGetter.apply(testEntity) == null ? ResponseEntity.status(HttpStatus.NOT_FOUND).body("No test found") : ResponseEntity.ok(propertyGetter.apply(testEntity));
}


/**
* Function to delete the tests of a project
* @param projectId the id of the test to delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
public class TestJson {
private String projectUrl;
private String dockerImage;
private String dockerTestUrl;
private String structureTestUrl;
private String dockerScript;
private String dockerTemplate;
private String structureTest;

public TestJson() {
}

public TestJson(String projectUrl, String dockerImage, String dockerTestUrl, String structureTestUrl) {
public TestJson(String projectUrl, String dockerImage, String dockerScript,
String dockerTemplate, String structureTest) {
this.projectUrl = projectUrl;
this.dockerImage = dockerImage;
this.dockerTestUrl = dockerTestUrl;
this.structureTestUrl = structureTestUrl;
this.dockerScript = dockerScript;
this.dockerTemplate = dockerTemplate;
this.structureTest = structureTest;
}

public String getProjectUrl() {
Expand All @@ -32,19 +35,27 @@ public void setDockerImage(String dockerImage) {
this.dockerImage = dockerImage;
}

public String getDockerTestUrl() {
return dockerTestUrl;
public String getDockerScript() {
return dockerScript;
}

public void setDockerTestUrl(String dockerTestUrl) {
this.dockerTestUrl = dockerTestUrl;
public void setDockerScript(String dockerScript) {
this.dockerScript = dockerScript;
}

public String getStructureTestUrl() {
return structureTestUrl;
public String getStructureTest() {
return structureTest;
}

public void setStructureTestUrl(String structureTestUrl) {
this.structureTestUrl = structureTestUrl;
public void setStructureTest(String structureTest) {
this.structureTest = structureTest;
}

public String getDockerTemplate() {
return dockerTemplate;
}

public void setDockerTemplate(String dockerTemplate) {
this.dockerTemplate = dockerTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@ else if (submission.getDockerTestType().equals(DockerTestType.SIMPLE)) {
public TestJson testEntityToTestJson(TestEntity testEntity, long projectId) {
return new TestJson(
ApiRoutes.PROJECT_BASE_PATH + "/" + projectId,
testEntity.getDockerImage(),
ApiRoutes.PROJECT_BASE_PATH + "/" + projectId + "/tests/dockertest",
ApiRoutes.PROJECT_BASE_PATH + "/" + projectId + "/tests/structuretest"
testEntity.getDockerImage(),
testEntity.getDockerTestScript(),
testEntity.getDockerTestTemplate(),
testEntity.getStructureTemplate()
);
}
}
21 changes: 21 additions & 0 deletions backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,26 @@ public CheckResult<TestEntity> getTestIfAdmin(long projectId, UserEntity user) {
return new CheckResult<>(HttpStatus.OK, "", testEntity);
}

public CheckResult<Pair<TestEntity, Boolean>> getTestWithAdminStatus(long projectId, UserEntity user) {
TestEntity testEntity = getTestIfExists(projectId);
if (testEntity == null) {
return new CheckResult<>(HttpStatus.NOT_FOUND, "No tests found for project with id: " + projectId, null);
}

boolean userPartOfProject = projectUtil.userPartOfProject(projectId, user.getId());
if (!userPartOfProject) {
return new CheckResult<>(HttpStatus.FORBIDDEN, "You are not part of this project", null);
}

boolean admin = false;

CheckResult<Void> isProjectAdmin = projectUtil.isProjectAdmin(projectId, user);
if (isProjectAdmin.getStatus().equals(HttpStatus.OK)) {
admin = true;
} else if (!isProjectAdmin.getStatus().equals(HttpStatus.FORBIDDEN)){
return new CheckResult<>(isProjectAdmin.getStatus(), isProjectAdmin.getMessage(), null);
}

return new CheckResult<>(HttpStatus.OK, "", new Pair<>(testEntity, admin));
}
}
4 changes: 2 additions & 2 deletions backend/app/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ server.port=8080


# TODO: this is just temporary, we will need to think of an actual limit at some point
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.max-file-size=50MB
spring.servlet.multipart.max-request-size=50MB

0 comments on commit 59c24ca

Please sign in to comment.