diff --git a/doc/api.yml b/doc/api.yml index fe1fe411b9..93c89fc124 100644 --- a/doc/api.yml +++ b/doc/api.yml @@ -2285,6 +2285,132 @@ paths: $ref: '#/components/responses/429' '500': $ref: '#/components/responses/500' + /gate_keepers: + get: + tags: + - Users + summary: Get all gate keepers + operationId: getGateKeepers + parameters: + - name: page + in: query + schema: + $ref: '#/components/schemas/Page' + - name: page_size + in: query + schema: + $ref: '#/components/schemas/PageSize' + - name: status + required: false + in: query + description: Filter gate keepers by status value return all by default + schema: + $ref: '#/components/schemas/EnableStatus' + - name: sex + required: false + in: query + description: Filter gate keepers by sex value return all by default + schema: + $ref: '#/components/schemas/Sex' + - name: first_name + required: false + in: query + description: Filter gate keepers by first name, case is ignored + schema: + type: string + - name: last_name + required: false + in: query + description: Filter gate keepers by last name, case is ignored + schema: + type: string + - name: ref + required: false + in: query + description: Filter gate keeper by ref, case is ignored + schema: + type: string + responses: + '200': + description: List of gate keeper, ordered by ref. + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/GateKeeper' + '400': + $ref: '#/components/responses/400' + '403': + $ref: '#/components/responses/403' + '404': + $ref: '#/components/responses/404' + '429': + $ref: '#/components/responses/429' + '500': + $ref: '#/components/responses/500' + put: + tags: + - Users + summary: Create or update gate keepers + operationId: crupdateGateKeepers + requestBody: + description: gate keepers + required: true + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/CrupdateGateKeeper' + responses: + '200': + description: List of gate keepers, ordered by ref. + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/GateKeeper' + '400': + $ref: '#/components/responses/400' + '403': + $ref: '#/components/responses/403' + '404': + $ref: '#/components/responses/404' + '429': + $ref: '#/components/responses/429' + '500': + $ref: '#/components/responses/500' + /gate_keepers/{id}: + get: + tags: + - Users + summary: Get gate keeper by id + operationId: getGateKeeperById + parameters: + - name: id + required: true + in: path + schema: + type: string + responses: + '200': + description: The Gate Keeper + content: + application/json: + schema: + $ref: '#/components/schemas/GateKeeper' + '400': + $ref: '#/components/responses/400' + '403': + $ref: '#/components/responses/403' + '404': + $ref: '#/components/responses/404' + '429': + $ref: '#/components/responses/429' + '500': + $ref: '#/components/responses/500' /staff_members/raw/xlsx: get: @@ -5554,6 +5680,7 @@ components: - ADMIN - STAFF_MEMBER - ORGANIZER + - GATE_KEEPER bearer: type: string UserIdentifier: @@ -5709,6 +5836,9 @@ components: CrupdateOrganizer: allOf: - $ref: '#/components/schemas/CrupdateUser' + CrupdateGateKeeper: + allOf: + - $ref: '#/components/schemas/CrupdateUser' CrupdateWorkStudyStudent: properties: id: @@ -5801,6 +5931,12 @@ components: properties: profile_picture: type: string + GateKeeper: + allOf: + - $ref: '#/components/schemas/CrupdateUser' + properties: + profile_picture: + type: string GroupIdentifier: type: object properties: diff --git a/src/main/java/school/hei/haapi/endpoint/rest/controller/GateKeeperController.java b/src/main/java/school/hei/haapi/endpoint/rest/controller/GateKeeperController.java new file mode 100644 index 0000000000..b370e03492 --- /dev/null +++ b/src/main/java/school/hei/haapi/endpoint/rest/controller/GateKeeperController.java @@ -0,0 +1,71 @@ +package school.hei.haapi.endpoint.rest.controller; + +import static java.util.stream.Collectors.toUnmodifiableList; +import static school.hei.haapi.model.User.Role.GATE_KEEPER; + +import java.util.List; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import school.hei.haapi.endpoint.rest.mapper.SexEnumMapper; +import school.hei.haapi.endpoint.rest.mapper.StatusEnumMapper; +import school.hei.haapi.endpoint.rest.mapper.UserMapper; +import school.hei.haapi.endpoint.rest.model.CrupdateGateKeeper; +import school.hei.haapi.endpoint.rest.model.EnableStatus; +import school.hei.haapi.endpoint.rest.model.GateKeeper; +import school.hei.haapi.endpoint.rest.model.Sex; +import school.hei.haapi.endpoint.rest.validator.CoordinatesValidator; +import school.hei.haapi.model.BoundedPageSize; +import school.hei.haapi.model.PageFromOne; +import school.hei.haapi.model.User; +import school.hei.haapi.service.UserService; + +@RestController +@AllArgsConstructor +public class GateKeeperController { + private final UserService userService; + private final StatusEnumMapper statusEnumMapper; + private final SexEnumMapper sexEnumMapper; + private final UserMapper userMapper; + private final CoordinatesValidator validator; + + @GetMapping("/gate_keepers") + public List getGateKeepers( + @RequestParam PageFromOne page, + @RequestParam("page_size") BoundedPageSize pageSize, + @RequestParam(name = "status", required = false) EnableStatus status, + @RequestParam(name = "sex", required = false) Sex sex, + @RequestParam(value = "first_name", required = false, defaultValue = "") String firstName, + @RequestParam(value = "last_name", required = false, defaultValue = "") String lastName, + @RequestParam(value = "ref", required = false, defaultValue = "") String ref) { + User.Status domainStatus = statusEnumMapper.toDomainStatus(status); + User.Sex domainSexEnum = sexEnumMapper.toDomainSexEnum(sex); + return userService + .getByCriteria( + GATE_KEEPER, firstName, lastName, ref, page, pageSize, domainStatus, domainSexEnum) + .stream() + .map(userMapper::toRestGateKeeper) + .collect(toUnmodifiableList()); + } + + @GetMapping("/gate_keepers/{id}") + public GateKeeper getGateKeeperById(@PathVariable String id) { + return userMapper.toRestGateKeeper(userService.findById(id)); + } + + @PutMapping("/gate_keepers") + public List crupdateGateKeepers( + @RequestBody List crupdateGateKeeper) { + crupdateGateKeeper.forEach(gateKeeper -> validator.accept(gateKeeper.getCoordinates())); + return userService + .saveAll( + crupdateGateKeeper.stream().map(userMapper::toDomain).collect(toUnmodifiableList())) + .stream() + .map(userMapper::toRestGateKeeper) + .collect(toUnmodifiableList()); + } +} diff --git a/src/main/java/school/hei/haapi/endpoint/rest/mapper/GradeMapper.java b/src/main/java/school/hei/haapi/endpoint/rest/mapper/GradeMapper.java index 6ca6dd86e4..f53c7e9800 100644 --- a/src/main/java/school/hei/haapi/endpoint/rest/mapper/GradeMapper.java +++ b/src/main/java/school/hei/haapi/endpoint/rest/mapper/GradeMapper.java @@ -49,8 +49,7 @@ public GetStudentGrade toRestStudentExamGrade(User student, Exam exam) { exam.getGrades().stream() .filter(grade -> grade.getStudent().getId().equals(student.getId())) .findFirst(); - school.hei.haapi.model.Grade grade = optionalGrade.get(); - var getStudentGrade = new GetStudentGrade().grade(toRest(grade)); + var getStudentGrade = new GetStudentGrade().grade(optionalGrade.map(this::toRest).orElse(null)); getStudentGrade.setStudent(userMapper.toRestStudent(student)); return getStudentGrade; } diff --git a/src/main/java/school/hei/haapi/endpoint/rest/mapper/UserMapper.java b/src/main/java/school/hei/haapi/endpoint/rest/mapper/UserMapper.java index 6fdb021b03..b4d50184fa 100644 --- a/src/main/java/school/hei/haapi/endpoint/rest/mapper/UserMapper.java +++ b/src/main/java/school/hei/haapi/endpoint/rest/mapper/UserMapper.java @@ -1,6 +1,7 @@ package school.hei.haapi.endpoint.rest.mapper; import static school.hei.haapi.endpoint.rest.mapper.FileInfoMapper.ONE_DAY_DURATION_AS_LONG; +import static school.hei.haapi.model.User.Role.GATE_KEEPER; import static school.hei.haapi.model.User.Role.ORGANIZER; import java.util.HashMap; @@ -260,6 +261,34 @@ public Organizer toRestOrganizer(User user) { return organizer; } + public GateKeeper toRestGateKeeper(User user) { + GateKeeper gateKeeper = new GateKeeper(); + String profilePictureKey = user.getProfilePictureKey(); + String url = + profilePictureKey != null + ? fileService.getPresignedUrl(profilePictureKey, ONE_DAY_DURATION_AS_LONG) + : null; + + gateKeeper.setId(user.getId()); + gateKeeper.setFirstName(user.getFirstName()); + gateKeeper.setLastName(user.getLastName()); + gateKeeper.setEmail(user.getEmail()); + gateKeeper.setRef(user.getRef()); + gateKeeper.setStatus(statusEnumMapper.toRestStatus(user.getStatus())); + gateKeeper.setPhone(user.getPhone()); + gateKeeper.setEntranceDatetime(user.getEntranceDatetime()); + gateKeeper.setBirthDate(user.getBirthDate()); + gateKeeper.setSex(sexEnumMapper.toRestSexEnum(user.getSex())); + gateKeeper.setAddress(user.getAddress()); + gateKeeper.setBirthPlace(user.getBirthPlace()); + gateKeeper.setNic(user.getNic()); + gateKeeper.setProfilePicture(url); + gateKeeper.setCoordinates( + new Coordinates().longitude(user.getLongitude()).latitude(user.getLatitude())); + gateKeeper.setHighSchoolOrigin(user.getHighSchoolOrigin()); + return gateKeeper; + } + public User toDomain(CrupdateManager manager) { return User.builder() .role(User.Role.MANAGER) @@ -384,6 +413,28 @@ public User toDomain(CrupdateMonitor monitor) { .build(); } + public User toDomain(CrupdateGateKeeper gateKeeper) { + return User.builder() + .role(GATE_KEEPER) + .id(gateKeeper.getId()) + .firstName(gateKeeper.getFirstName()) + .lastName(gateKeeper.getLastName()) + .email(gateKeeper.getEmail()) + .ref(gateKeeper.getRef()) + .status(statusEnumMapper.toDomainStatus(gateKeeper.getStatus())) + .phone(gateKeeper.getPhone()) + .entranceDatetime(gateKeeper.getEntranceDatetime()) + .birthDate(gateKeeper.getBirthDate()) + .sex(sexEnumMapper.toDomainSexEnum(gateKeeper.getSex())) + .address(gateKeeper.getAddress()) + .nic(gateKeeper.getNic()) + .birthPlace(gateKeeper.getBirthPlace()) + .longitude(gateKeeper.getCoordinates().getLongitude()) + .latitude(gateKeeper.getCoordinates().getLatitude()) + .highSchoolOrigin(gateKeeper.getHighSchoolOrigin()) + .build(); + } + public User toDomain(CrupdateOrganizer organizer) { return User.builder() .role(ORGANIZER) diff --git a/src/main/java/school/hei/haapi/endpoint/rest/security/SecurityConf.java b/src/main/java/school/hei/haapi/endpoint/rest/security/SecurityConf.java index 5249e60578..3fc377acab 100644 --- a/src/main/java/school/hei/haapi/endpoint/rest/security/SecurityConf.java +++ b/src/main/java/school/hei/haapi/endpoint/rest/security/SecurityConf.java @@ -231,6 +231,9 @@ req, res, null, forbiddenWithRemoteInfo(req)))) antMatcher(GET, "/organizers/*"), antMatcher(PUT, "/organizers"), antMatcher(POST, "/organizers/*/picture/raw"), + antMatcher(GET, "/gate_keepers"), + antMatcher(GET, "/gate_keepers/*"), + antMatcher(PUT, "/gate_keepers"), antMatcher(PUT, STUDENT_COURSE), nonAccessibleBySuspendedUserPath)), AnonymousAuthenticationFilter.class) @@ -727,6 +730,17 @@ req, res, null, forbiddenWithRemoteInfo(req)))) new SelfMatcher(POST, "/organizers/*/picture/raw", "organizers")) .hasRole(ORGANIZER.getRole()) // + // Gate keeper resources + // + .requestMatchers(GET, "/gate_keepers") + .hasAnyRole(ADMIN.getRole(), MANAGER.getRole()) + .requestMatchers(new SelfMatcher(GET, "/gate_keepers/*", "gate_keepers")) + .hasAnyRole(GATE_KEEPER.getRole()) + .requestMatchers(GET, "/gate_keepers/*") + .hasAnyRole(ADMIN.getRole(), MANAGER.getRole()) + .requestMatchers(PUT, "/gate_keepers") + .hasAnyRole(ADMIN.getRole(), MANAGER.getRole()) + // // Letter resources // .requestMatchers(GET, "/students/letters") diff --git a/src/main/java/school/hei/haapi/endpoint/rest/security/model/Role.java b/src/main/java/school/hei/haapi/endpoint/rest/security/model/Role.java index 0db5fa4d10..07d76c9ca5 100644 --- a/src/main/java/school/hei/haapi/endpoint/rest/security/model/Role.java +++ b/src/main/java/school/hei/haapi/endpoint/rest/security/model/Role.java @@ -9,7 +9,8 @@ public enum Role implements GrantedAuthority { TEACHER, STAFF_MEMBER, MANAGER, - ORGANIZER; + ORGANIZER, + GATE_KEEPER; public String getRole() { return name(); diff --git a/src/main/java/school/hei/haapi/model/User.java b/src/main/java/school/hei/haapi/model/User.java index 87c2f13b19..b81905c7b5 100644 --- a/src/main/java/school/hei/haapi/model/User.java +++ b/src/main/java/school/hei/haapi/model/User.java @@ -220,6 +220,7 @@ public enum Role { STUDENT, TEACHER, MANAGER, - ORGANIZER; + ORGANIZER, + GATE_KEEPER; } } diff --git a/src/main/java/school/hei/haapi/service/ownCloud/OwnCloudService.java b/src/main/java/school/hei/haapi/service/ownCloud/OwnCloudService.java index 3a57d67ab5..371836a218 100644 --- a/src/main/java/school/hei/haapi/service/ownCloud/OwnCloudService.java +++ b/src/main/java/school/hei/haapi/service/ownCloud/OwnCloudService.java @@ -28,7 +28,7 @@ public OcsData createShareLink(String path) throws JsonProcessingException { Integer permission = switch (currentUser.getRole()) { case ADMIN, MANAGER -> 15; - case STUDENT, TEACHER, MONITOR, STAFF_MEMBER, ORGANIZER -> 1; + case STUDENT, TEACHER, MONITOR, STAFF_MEMBER, ORGANIZER, GATE_KEEPER -> 1; }; HttpHeaders headers = new HttpHeaders(); diff --git a/src/main/resources/db/migration/V43_60__Update_user_role.sql b/src/main/resources/db/migration/V43_60__Update_user_role.sql index b70f59eacf..904d5d0f45 100644 --- a/src/main/resources/db/migration/V43_60__Update_user_role.sql +++ b/src/main/resources/db/migration/V43_60__Update_user_role.sql @@ -1 +1,2 @@ -alter type "role" add value 'ORGANIZER'; \ No newline at end of file +alter type "role" add value 'ORGANIZER'; +alter type "role" add value 'GATE_KEEPER'; \ No newline at end of file diff --git a/src/test/java/school/hei/haapi/integration/GateKeeperIT.java b/src/test/java/school/hei/haapi/integration/GateKeeperIT.java new file mode 100644 index 0000000000..81772f317b --- /dev/null +++ b/src/test/java/school/hei/haapi/integration/GateKeeperIT.java @@ -0,0 +1,108 @@ +package school.hei.haapi.integration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static school.hei.haapi.integration.conf.TestUtils.GATE_KEEPER1_ID; +import static school.hei.haapi.integration.conf.TestUtils.GATE_KEEPER1_TOKEN; +import static school.hei.haapi.integration.conf.TestUtils.GATE_KEEPER2_TOKEN; +import static school.hei.haapi.integration.conf.TestUtils.MANAGER1_TOKEN; +import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_ID; +import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_ID; +import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; +import static school.hei.haapi.integration.conf.TestUtils.crupdateGateKeeper1; +import static school.hei.haapi.integration.conf.TestUtils.gateKeeper1; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; +import static school.hei.haapi.integration.conf.TestUtils.uploadProfilePicture; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.io.InputStream; +import java.net.http.HttpResponse; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.testcontainers.junit.jupiter.Testcontainers; +import school.hei.haapi.endpoint.rest.api.UsersApi; +import school.hei.haapi.endpoint.rest.client.ApiClient; +import school.hei.haapi.endpoint.rest.client.ApiException; +import school.hei.haapi.endpoint.rest.model.GateKeeper; +import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.TestUtils; +import software.amazon.awssdk.services.eventbridge.EventBridgeClient; + +@Testcontainers +@AutoConfigureMockMvc +public class GateKeeperIT extends FacadeITMockedThirdParties { + @Autowired ObjectMapper objectMapper; + @MockBean private EventBridgeClient eventBridgeClientMock; + + private ApiClient anApiClient(String token) { + return TestUtils.anApiClient(token, localPort); + } + + @BeforeEach + public void setUp() { + setUpCognito(cognitoComponentMock); + setUpEventBridge(eventBridgeClientMock); + } + + @Test + @Disabled("Not implemented: gate keeper can modify picture") + void gatekeeper_update_own_profile_picture() throws IOException, InterruptedException { + HttpResponse response = + uploadProfilePicture(localPort, GATE_KEEPER1_TOKEN, GATE_KEEPER1_ID, "gatekeeper"); + assertEquals(200, response.statusCode()); + + GateKeeper gateKeeper = objectMapper.readValue(response.body(), GateKeeper.class); + assertEquals(gateKeeper1().getRef(), gateKeeper.getRef()); + } + + @Test + void read_student_ko() { + ApiClient student1Client = anApiClient(GATE_KEEPER2_TOKEN); + UsersApi api = new UsersApi(student1Client); + + assertThrowsForbiddenException(() -> api.getStudentById(STUDENT1_ID)); + assertThrowsForbiddenException( + () -> api.getStudents(1, 20, null, null, null, null, null, null, null, null, null)); + } + + @Test + void read_teacher_ko() { + ApiClient teacher1Client = anApiClient(GATE_KEEPER1_TOKEN); + UsersApi api = new UsersApi(teacher1Client); + + assertThrowsForbiddenException(() -> api.getTeacherById(TEACHER1_ID)); + assertThrowsForbiddenException(() -> api.getTeachers(1, 20, null, null, null, null, null)); + } + + @Test + void gate_keeper_access_to_its_own_account_ok() throws ApiException { + UsersApi api = new UsersApi(anApiClient(GATE_KEEPER1_TOKEN)); + GateKeeper gateKeeperById1 = api.getGateKeeperById(GATE_KEEPER1_ID); + assertEquals(gateKeeper1(), gateKeeperById1); + } + + @Test + void manager_manipulate_gate_keeper_ok() throws ApiException { + UsersApi api = new UsersApi(anApiClient(MANAGER1_TOKEN)); + String modifiedFirstName = "test"; + List gateKeepers = + api.crupdateGateKeepers(List.of(crupdateGateKeeper1().firstName(modifiedFirstName))); + assertEquals(modifiedFirstName, gateKeepers.getFirst().getFirstName()); + + // Can turn the gateKeeper first name to normal stat + List originalGateKeepers = api.crupdateGateKeepers(List.of(crupdateGateKeeper1())); + assertEquals(gateKeeper1().getFirstName(), originalGateKeepers.getFirst().getFirstName()); + } + + @Test + void gate_keeper_access_to_other_account_ko() { + UsersApi api = new UsersApi(anApiClient(GATE_KEEPER2_TOKEN)); + assertThrowsForbiddenException(() -> api.getGateKeeperById(GATE_KEEPER1_ID)); + } +} diff --git a/src/test/java/school/hei/haapi/integration/GradeIT.java b/src/test/java/school/hei/haapi/integration/GradeIT.java index b007aff209..f271db1727 100644 --- a/src/test/java/school/hei/haapi/integration/GradeIT.java +++ b/src/test/java/school/hei/haapi/integration/GradeIT.java @@ -1,5 +1,6 @@ package school.hei.haapi.integration; +import static java.util.stream.Collectors.toUnmodifiableList; import static org.hibernate.validator.internal.util.Contracts.assertNotNull; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -69,13 +70,14 @@ void manager_read_ok() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); TeachingApi api = new TeachingApi(manager1Client); - List actualAwardedCourseExamGrades = - api.getStudentGrades(STUDENT1_ID, 1, 10); + List actualAwardedCourseExamGradesId = + api.getStudentGrades(STUDENT1_ID, 1, 10).stream() + .map(AwardedCourseExam::getId) + .collect(toUnmodifiableList()); - assertTrue(actualAwardedCourseExamGrades.contains(awardedCourseExam1())); - - assertTrue(actualAwardedCourseExamGrades.contains(awardedCourseExam2())); - assertTrue(actualAwardedCourseExamGrades.contains(awardedCourseExam4())); + assertTrue(actualAwardedCourseExamGradesId.contains(awardedCourseExam1().getId())); + assertTrue(actualAwardedCourseExamGradesId.contains(awardedCourseExam2().getId())); + assertTrue(actualAwardedCourseExamGradesId.contains(awardedCourseExam4().getId())); } @Test @@ -83,11 +85,14 @@ void teacher_read_ok() throws ApiException { ApiClient teacher1Client = anApiClient(TEACHER1_TOKEN); TeachingApi api = new TeachingApi(teacher1Client); - List actual = api.getStudentGrades(STUDENT1_ID, 1, 10); + List actual = + api.getStudentGrades(STUDENT1_ID, 1, 10).stream() + .map(AwardedCourseExam::getId) + .collect(toUnmodifiableList()); - assertTrue(actual.contains(awardedCourseExam1())); - assertTrue(actual.contains(awardedCourseExam2())); - assertTrue(actual.contains(awardedCourseExam4())); + assertTrue(actual.contains(awardedCourseExam1().getId())); + assertTrue(actual.contains(awardedCourseExam2().getId())); + assertTrue(actual.contains(awardedCourseExam4().getId())); } @Test @@ -95,11 +100,14 @@ void student_read_his_grade_ok() throws ApiException { ApiClient student1Client = anApiClient(STUDENT1_TOKEN); TeachingApi api = new TeachingApi(student1Client); - List actual = api.getStudentGrades(STUDENT1_ID, 1, 10); + List actual = + api.getStudentGrades(STUDENT1_ID, 1, 10).stream() + .map(AwardedCourseExam::getId) + .collect(toUnmodifiableList()); - assertTrue(actual.contains(awardedCourseExam1())); - assertTrue(actual.contains(awardedCourseExam2())); - assertTrue(actual.contains(awardedCourseExam4())); + assertTrue(actual.contains(awardedCourseExam1().getId())); + assertTrue(actual.contains(awardedCourseExam2().getId())); + assertTrue(actual.contains(awardedCourseExam4().getId())); } @Test diff --git a/src/test/java/school/hei/haapi/integration/UserFileIT.java b/src/test/java/school/hei/haapi/integration/UserFileIT.java index ff4001c0df..acac3db89e 100644 --- a/src/test/java/school/hei/haapi/integration/UserFileIT.java +++ b/src/test/java/school/hei/haapi/integration/UserFileIT.java @@ -12,6 +12,7 @@ import static school.hei.haapi.integration.StudentIT.student1; import static school.hei.haapi.integration.conf.TestUtils.FEE1_ID; import static school.hei.haapi.integration.conf.TestUtils.FEE4_ID; +import static school.hei.haapi.integration.conf.TestUtils.GATE_KEEPER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.MANAGER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.MONITOR1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.ORGANIZER1_TOKEN; @@ -313,6 +314,13 @@ void organizer_load_other_files_ko() { assertThrowsForbiddenException(() -> api.getUserFiles(STUDENT1_ID, 1, 15, null)); } + @Test + void gatekeeper_load_other_files_ko() { + ApiClient organizerClient = anApiClient(GATE_KEEPER1_TOKEN); + FilesApi api = new FilesApi(organizerClient); + assertThrowsForbiddenException(() -> api.getUserFiles(TEACHER1_ID, 1, 15, null)); + } + @Test @Disabled("TODO: maybe student get disabled somewhere") void student_read_own_files_ok() throws ApiException { diff --git a/src/test/java/school/hei/haapi/integration/conf/TestUtils.java b/src/test/java/school/hei/haapi/integration/conf/TestUtils.java index 7010d58947..05433ad79a 100644 --- a/src/test/java/school/hei/haapi/integration/conf/TestUtils.java +++ b/src/test/java/school/hei/haapi/integration/conf/TestUtils.java @@ -77,6 +77,7 @@ import school.hei.haapi.endpoint.rest.model.CreateFee; import school.hei.haapi.endpoint.rest.model.CrupdateExam; import school.hei.haapi.endpoint.rest.model.CrupdateFeeTemplate; +import school.hei.haapi.endpoint.rest.model.CrupdateGateKeeper; import school.hei.haapi.endpoint.rest.model.CrupdateGrade; import school.hei.haapi.endpoint.rest.model.CrupdateMonitor; import school.hei.haapi.endpoint.rest.model.CrupdatePromotion; @@ -89,6 +90,7 @@ import school.hei.haapi.endpoint.rest.model.ExamInfo; import school.hei.haapi.endpoint.rest.model.Fee; import school.hei.haapi.endpoint.rest.model.FeeTemplate; +import school.hei.haapi.endpoint.rest.model.GateKeeper; import school.hei.haapi.endpoint.rest.model.GetStudentGrade; import school.hei.haapi.endpoint.rest.model.Grade; import school.hei.haapi.endpoint.rest.model.Group; @@ -211,6 +213,11 @@ public class TestUtils { public static final String ORGANIZER1_TOKEN = "organizer1_token"; public static final String ORGANIZER2_TOKEN = "organizer2_token"; + public static final String GATE_KEEPER1_ID = "gate_keeper1_id"; + public static final String GATE_KEEPER2_ID = "gate_keeper2_id"; + public static final String GATE_KEEPER1_TOKEN = "gate_keeper1_token"; + public static final String GATE_KEEPER2_TOKEN = "gate_keeper2_token"; + public static ApiClient anApiClient(String token, int serverPort) { ApiClient client = new ApiClient(); client.setScheme("http"); @@ -254,6 +261,10 @@ public static void setUpCognito(CognitoComponent cognitoComponent) { .thenReturn("test+organizer+2@hei.school"); when(cognitoComponent.getEmailByIdToken(SUSPENDED_TOKEN)) .thenReturn("test+suspended@hei.school"); + when(cognitoComponent.getEmailByIdToken(GATE_KEEPER1_TOKEN)) + .thenReturn("test+gatekeeper@hei.school"); + when(cognitoComponent.getEmailByIdToken(GATE_KEEPER2_TOKEN)) + .thenReturn("test+gatekeeper+2@hei.school"); } public static void setUpS3Service(FileService fileService, Student user) { @@ -1655,6 +1666,59 @@ public static Letter letter3() { .description("CV"); } + public static GateKeeper gateKeeper1() { + return new GateKeeper() + .id(GATE_KEEPER1_ID) + .firstName("GateKeeper 1") + .lastName("John") + .email("test+gatekeeper@hei.school") + .ref("GKP22001") + .status(ENABLED) + .sex(M) + .birthDate(LocalDate.parse("1980-10-10")) + .entranceDatetime(Instant.parse("2022-09-08T08:25:29.00Z")) + .phone("0322400028") + .address("Adr 9") + .nic("") + .birthPlace("") + .coordinates(new Coordinates().longitude(55.555).latitude(-55.555)); + } + + public static CrupdateGateKeeper crupdateGateKeeper1() { + return new CrupdateGateKeeper() + .id(gateKeeper1().getId()) + .ref(gateKeeper1().getRef()) + .nic(gateKeeper1().getNic()) + .address(gateKeeper1().getAddress()) + .email(gateKeeper1().getEmail()) + .birthDate(gateKeeper1().getBirthDate()) + .birthPlace(gateKeeper1().getBirthPlace()) + .coordinates(gateKeeper1().getCoordinates()) + .entranceDatetime(gateKeeper1().getEntranceDatetime()) + .firstName(gateKeeper1().getFirstName()) + .highSchoolOrigin(gateKeeper1().getHighSchoolOrigin()) + .lastName(gateKeeper1().getLastName()) + .phone(gateKeeper1().getPhone()) + .sex(gateKeeper1().getSex()) + .status(gateKeeper1().getStatus()); + } + + public static GateKeeper gateKeeper2() { + return new GateKeeper() + .id(GATE_KEEPER2_ID) + .firstName("GateKeeper 2") + .lastName("One") + .email("test+gatekeeper+2@hei.school") + .ref("GKP22002") + .status(ENABLED) + .sex(F) + .birthDate(LocalDate.parse("1890-01-01")) + .entranceDatetime(Instant.parse("2022-09-08T08:25:29.00Z")) + .phone("0322411113") + .address("Adr 21") + .coordinates(new Coordinates().longitude(55.555).latitude(-55.555)); + } + public static UpdatePromotionSGroup addGroupToPromotion3() { return new UpdatePromotionSGroup().type(ADD).groupIds(List.of(group5().getId())); } diff --git a/src/test/resources/db/testdata/V99_2__Test_create_users.sql b/src/test/resources/db/testdata/V99_2__Test_create_users.sql index 21bdb68f7c..fc050fba94 100644 --- a/src/test/resources/db/testdata/V99_2__Test_create_users.sql +++ b/src/test/resources/db/testdata/V99_2__Test_create_users.sql @@ -52,4 +52,10 @@ values ('student1_id', 'Ryan', 'Andria', 'test+ryan@hei.school', 'STD21001', 'EN '2022-09-08T08:25:29.00Z', '0322400028', 'Adr 10', 'ORGANIZER', '', '', 55.555, -55.555, null), ('organizer2_id', 'Organizer 2', 'Doe', 'test+organizer+2@hei.school', 'ORG22002', 'ENABLED', 'F', '1890-01-01', - '2022-09-08T08:25:29.00Z', '0322411113', 'Adr 12', 'ORGANIZER', '', '', 55.555, -55.555, null); + '2022-09-08T08:25:29.00Z', '0322411113', 'Adr 12', 'ORGANIZER', '', '', 55.555, -55.555, null), + ('gate_keeper1_id', 'GateKeeper 1', 'John', 'test+gatekeeper@hei.school', 'GKP22001', 'ENABLED', 'M', + '1980-10-10', + '2022-09-08T08:25:29.00Z', '0322400028', 'Adr 9', 'GATE_KEEPER', '', '', 55.555, -55.555, null), + ('gate_keeper2_id', 'GateKeeper 2', 'One', 'test+gatekeeper+2@hei.school', 'GKP22002', 'ENABLED', 'F', + '1890-01-01', + '2022-09-08T08:25:29.00Z', '0322411113', 'Adr 21', 'GATE_KEEPER', '', '', 55.555, -55.555, null);