Skip to content

Commit

Permalink
Development: Remove unused endpoint to get single complaint (#7852)
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-christl authored Jan 8, 2024
1 parent 9f5fd99 commit 7d294a3
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 184 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.tum.in.www1.artemis.web.rest;

import java.security.Principal;
import java.util.Objects;
import java.util.Optional;

import org.slf4j.Logger;
Expand All @@ -11,18 +9,11 @@
import org.springframework.web.bind.annotation.*;

import de.tum.in.www1.artemis.domain.*;
import de.tum.in.www1.artemis.domain.participation.Participant;
import de.tum.in.www1.artemis.domain.participation.StudentParticipation;
import de.tum.in.www1.artemis.repository.ComplaintRepository;
import de.tum.in.www1.artemis.repository.ComplaintResponseRepository;
import de.tum.in.www1.artemis.repository.UserRepository;
import de.tum.in.www1.artemis.security.annotations.EnforceAtLeastStudent;
import de.tum.in.www1.artemis.security.annotations.EnforceAtLeastTutor;
import de.tum.in.www1.artemis.service.AuthorizationCheckService;
import de.tum.in.www1.artemis.service.ComplaintResponseService;
import de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException;
import de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException;
import tech.jhipster.web.util.ResponseUtil;

/**
* REST controller for managing complaints.
Expand All @@ -35,22 +26,15 @@ public class ComplaintResponseResource {

public static final String ENTITY_NAME = "complaintResponse";

private final ComplaintResponseRepository complaintResponseRepository;

private final ComplaintRepository complaintRepository;

private final ComplaintResponseService complaintResponseService;

private final AuthorizationCheckService authorizationCheckService;

private final UserRepository userRepository;

public ComplaintResponseResource(ComplaintResponseRepository complaintResponseRepository, ComplaintResponseService complaintResponseService,
AuthorizationCheckService authorizationCheckService, UserRepository userRepository, ComplaintRepository complaintRepository) {
this.complaintResponseRepository = complaintResponseRepository;
public ComplaintResponseResource(ComplaintResponseService complaintResponseService, UserRepository userRepository, ComplaintRepository complaintRepository) {
this.complaintResponseService = complaintResponseService;
this.complaintRepository = complaintRepository;
this.authorizationCheckService = authorizationCheckService;
this.userRepository = userRepository;
}

Expand Down Expand Up @@ -121,69 +105,6 @@ public ResponseEntity<ComplaintResponse> resolveComplaint(@RequestBody Complaint
return ResponseEntity.ok().body(updatedComplaintResponse);
}

/**
* Get /complaint-responses/complaint/:id get a complaint response associated with the complaint "id"
*
* @param complaintId the id of the complaint for which we want to find a linked response
* @param principal the user who called the method
* @return the ResponseEntity with status 200 (OK) and with body the complaint response, or with status 404 (Not Found)
*/
// TODO: change URL to /complaint-responses?complaintId={complaintId}
@GetMapping("/complaint-responses/complaint/{complaintId}")
@EnforceAtLeastStudent
public ResponseEntity<ComplaintResponse> getComplaintResponseByComplaintId(@PathVariable long complaintId, Principal principal) {
log.debug("REST request to get ComplaintResponse associated to complaint : {}", complaintId);
Optional<ComplaintResponse> complaintResponse = complaintResponseRepository.findByComplaint_Id(complaintId);
return handleComplaintResponse(complaintId, principal, complaintResponse);
}

private ResponseEntity<ComplaintResponse> handleComplaintResponse(long complaintId, Principal principal, Optional<ComplaintResponse> optionalComplaintResponse) {
if (optionalComplaintResponse.isEmpty()) {
throw new EntityNotFoundException("ComplaintResponse with " + complaintId + " was not found!");
}
var user = userRepository.getUserWithGroupsAndAuthorities();
var complaintResponse = optionalComplaintResponse.get();
// All tutors and higher can see this, and also the students who first open the complaint
Participant originalAuthor = complaintResponse.getComplaint().getParticipant();
StudentParticipation studentParticipation = (StudentParticipation) complaintResponse.getComplaint().getResult().getParticipation();
Exercise exercise = studentParticipation.getExercise();
var atLeastTA = authorizationCheckService.isAtLeastTeachingAssistantForExercise(exercise, user);
if (!atLeastTA && !isOriginalAuthor(principal, originalAuthor)) {
throw new AccessForbiddenException("Insufficient permission for this complaint response");
}

if (!authorizationCheckService.isAtLeastInstructorForExercise(exercise, user)) {
complaintResponse.getComplaint().setParticipant(null);
}

if (!atLeastTA) {
complaintResponse.setReviewer(null);
}

if (isOriginalAuthor(principal, originalAuthor)) {
// hide complaint completely if the user is the student who created the complaint
complaintResponse.setComplaint(null);
}
else {
// hide unnecessary information
complaintResponse.getComplaint().getResult().setParticipation(null);
complaintResponse.getComplaint().getResult().setSubmission(null);
}
return ResponseUtil.wrapOrNotFound(optionalComplaintResponse);
}

private boolean isOriginalAuthor(Principal principal, Participant originalAuthor) {
if (originalAuthor instanceof User) {
return Objects.equals(((User) originalAuthor).getLogin(), principal.getName());
}
else if (originalAuthor instanceof Team) {
return ((Team) originalAuthor).hasStudentWithLogin(principal.getName());
}
else {
throw new Error("Unknown Participant type");
}
}

private Complaint getComplaintFromDatabaseAndCheckAccessRights(long complaintId) {
Optional<Complaint> complaintFromDatabaseOptional = complaintRepository.findByIdWithEagerAssessor(complaintId);
if (complaintFromDatabaseOptional.isEmpty()) {
Expand Down
6 changes: 0 additions & 6 deletions src/main/webapp/app/complaints/complaint-response.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ export class ComplaintResponseService {
.pipe(map((res: EntityResponseType) => this.convertComplaintResponseEntityResponseDatesFromServer(res)));
}

findByComplaintId(complaintId: number): Observable<EntityResponseType> {
return this.http
.get<ComplaintResponse>(`${this.resourceUrl}/complaint/${complaintId}`, { observe: 'response' })
.pipe(map((res: EntityResponseType) => this.convertComplaintResponseEntityResponseDatesFromServer(res)));
}

public convertComplaintResponseDatesFromClient(complaintResponse: ComplaintResponse): ComplaintResponse {
return Object.assign({}, complaintResponse, {
submittedTime: convertDateFromClient(complaintResponse.submittedTime),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,80 +580,6 @@ void getComplaintsForAssessmentDashboard_testRun_emptyComplaints() throws Except
assertThat(complaints).hasSize(0);
}

@Test
@WithMockUser(username = TEST_PREFIX + "student1")
void getComplaintResponseByComplaintId_reviewerHiddenForStudent() throws Exception {
complaint.setParticipant(userUtilService.getUserByLogin(TEST_PREFIX + "student1"));
complaintRepo.save(complaint);

ComplaintResponse complaintResponse = new ComplaintResponse().complaint(complaint.accepted(false)).responseText("rejected")
.reviewer(userUtilService.getUserByLogin(TEST_PREFIX + "tutor1"));
complaintResponseRepo.save(complaintResponse);

ComplaintResponse receivedComplaintResponse = request.get("/api/complaint-responses/complaint/" + complaint.getId(), HttpStatus.OK, ComplaintResponse.class);

assertThat(receivedComplaintResponse.getReviewer()).as("reviewer is not set").isNull();
assertThat(receivedComplaintResponse.getComplaint()).as("complaint is not set").isNull();
}

@Test
@WithMockUser(username = TEST_PREFIX + "tutor1")
void getComplaintResponseByComplaintId_sensitiveDataHiddenForTutor() throws Exception {
complaint.setParticipant(userUtilService.getUserByLogin(TEST_PREFIX + "student1"));

complaint = complaintRepo.save(complaint);
ComplaintResponse complaintResponse = new ComplaintResponse();
complaintResponse.setComplaint(complaint);
complaintResponse.getComplaint().setAccepted(false);
complaintResponse.setResponseText("rejected");
complaintResponse = complaintResponseRepo.save(complaintResponse);
complaintResponse.setReviewer(userUtilService.getUserByLogin(TEST_PREFIX + "tutor1"));

complaintResponseRepo.save(complaintResponse);

ComplaintResponse receivedComplaintResponse = request.get("/api/complaint-responses/complaint/" + complaint.getId(), HttpStatus.OK, ComplaintResponse.class);

Complaint receivedComplaint = receivedComplaintResponse.getComplaint();
assertThat(receivedComplaint.getParticipant()).as("student is not set").isNull();
assertThat(receivedComplaint.getResult().getParticipation()).as("participation is not set").isNull();
assertThat(receivedComplaint.getResult().getSubmission()).as("submission is not set").isNull();
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1")
void getComplaintResponseByComplaintId_sensitiveDataHiddenForInstructor() throws Exception {
complaint.setParticipant(userUtilService.getUserByLogin(TEST_PREFIX + "student1"));
complaint = complaintRepo.save(complaint);
ComplaintResponse complaintResponse = new ComplaintResponse();
complaintResponse.setComplaint(complaint);
complaintResponse.getComplaint().setAccepted(false);
complaintResponse.setResponseText("rejected");
complaintResponse = complaintResponseRepo.save(complaintResponse);
complaintResponse.setReviewer(userUtilService.getUserByLogin(TEST_PREFIX + "instructor1"));

complaintResponseRepo.save(complaintResponse);

ComplaintResponse receivedComplaintResponse = request.get("/api/complaint-responses/complaint/" + complaint.getId(), HttpStatus.OK, ComplaintResponse.class);

Complaint receivedComplaint = receivedComplaintResponse.getComplaint();
assertThat(receivedComplaint.getParticipant()).as("student is set").isNotNull();
assertThat(receivedComplaint.getResult().getParticipation()).as("participation is not set").isNull();
assertThat(receivedComplaint.getResult().getSubmission()).as("submission is not set").isNull();
}

@Test
@WithMockUser(username = TEST_PREFIX + "student2")
void getComplaintResponseByComplaintId_studentNotOriginalAuthor_forbidden() throws Exception {
complaint.setParticipant(userUtilService.getUserByLogin(TEST_PREFIX + "student1"));
complaintRepo.save(complaint);

ComplaintResponse complaintResponse = new ComplaintResponse().complaint(complaint.accepted(false)).responseText("rejected")
.reviewer(userUtilService.getUserByLogin(TEST_PREFIX + "tutor1"));
complaintResponseRepo.save(complaintResponse);

request.get("/api/complaint-responses/complaint/" + complaint.getId(), HttpStatus.FORBIDDEN, ComplaintResponse.class);
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void getSubmittedComplaints_byComplaintType() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,6 @@ void getComplaintByResultId_studentAndNotPartOfTeam_forbidden() throws Exception
request.get("/api/complaints/submissions/" + modelingSubmission.getId(), HttpStatus.FORBIDDEN, Complaint.class);
}

@Test
@WithMockUser(username = TEST_PREFIX + "student1")
void getComplaintResponseByComplaintId_studentNotPartOfTeam_forbidden() throws Exception {
complaint.setParticipant(team);
complaintRepo.save(complaint);

ComplaintResponse complaintResponse = new ComplaintResponse().complaint(complaint.accepted(false)).responseText("rejected")
.reviewer(userUtilService.getUserByLogin(TEST_PREFIX + "tutor1"));
complaintResponseRepo.save(complaintResponse);

request.get("/api/complaint-responses/complaint/" + complaint.getId(), HttpStatus.FORBIDDEN, ComplaintResponse.class);
}

private void saveModelingSubmissionAndAssessment() throws Exception {
modelingSubmission = ParticipationFactory.generateModelingSubmission(FileUtils.loadFileFromResources("test-data/model-submission/model.54727.json"), true);
modelingSubmission = modelingExerciseUtilService.addModelingTeamSubmission(modelingExercise, modelingSubmission, team);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,4 @@ describe('ComplaintResponseService', () => {
req.flush(returnedFromService);
expect(expectedComplaintResponse.body).toEqual(defaultComplaintResponse);
});

it('should call findByComplaintId', async () => {
const returnedFromService = { ...defaultComplaintResponse };
complaintResponseService
.findByComplaintId(1)
.pipe(take(1))
.subscribe((resp) => (expectedComplaintResponse = resp));
const req = httpTestingController.expectOne({ method: 'GET' });
req.flush(returnedFromService);
expect(expectedComplaintResponse.body).toEqual(defaultComplaintResponse);
});
});

0 comments on commit 7d294a3

Please sign in to comment.