From 66be0baca6e9805f66763d1ca5b11168dc97b193 Mon Sep 17 00:00:00 2001 From: Pierre Tellier Date: Wed, 9 Apr 2025 17:39:43 +0200 Subject: [PATCH] feat: created a better account creation flow --- .../WebSecurityCustomConfiguration.java | 2 - .../myinpulse/controller/AdminApi.java | 16 +++ .../myinpulse/controller/UnauthApi.java | 51 +++++++++ .../myinpulse/model/Administrator.java | 2 +- .../enseirb/myinpulse/model/Entrepreneur.java | 26 ++++- .../java/enseirb/myinpulse/model/User.java | 31 +++--- .../myinpulse/repository/UserRepository.java | 1 + .../myinpulse/service/AdminApiService.java | 44 +++++++- .../service/EntrepreneurApiService.java | 22 +++- .../myinpulse/service/KeycloakApi.java | 58 +++++----- .../service/database/EntrepreneurService.java | 13 +++ .../service/database/UserService.java | 4 + .../myinpulse/AdminApiServiceTest.java | 92 ++++++++++++++-- .../src/views/testComponent.vue | 103 ++++++++++++++---- 14 files changed, 377 insertions(+), 88 deletions(-) create mode 100644 MyINPulse-back/src/main/java/enseirb/myinpulse/controller/UnauthApi.java diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/config/WebSecurityCustomConfiguration.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/config/WebSecurityCustomConfiguration.java index 81f1dd8..e83eaa7 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/config/WebSecurityCustomConfiguration.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/config/WebSecurityCustomConfiguration.java @@ -61,8 +61,6 @@ public class WebSecurityCustomConfiguration { .requestMatchers("/admin/**", "/shared/**") .access(hasRole("REALM_MyINPulse-admin")) .requestMatchers("/unauth/**") - .permitAll() - .anyRequest() .authenticated()) .oauth2ResourceServer( oauth2 -> diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/AdminApi.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/AdminApi.java index d3f432a..87ba33e 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/AdminApi.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/AdminApi.java @@ -99,4 +99,20 @@ public class AdminApi { public void deleteProject(@PathVariable long projectId) { adminApiService.deleteProject(projectId); } + + @GetMapping("/admin/setadmin/{userId}") + public void setAdmin(@PathVariable long userId, @AuthenticationPrincipal Jwt principal) { + this.adminApiService.setAdmin(userId, principal.getTokenValue()); + } + + @GetMapping("/admin/validate_user_account/{userId}") + public void validateEntrepreneurAcc( + @PathVariable long userId, @AuthenticationPrincipal Jwt principal) { + this.adminApiService.validateEntrepreneurAccount(userId, principal.getTokenValue()); + } + + @GetMapping("/admin/get_pending_accounts") + public Iterable validateEntrepreneurAcc() { + return this.adminApiService.getPendingUsers(); + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/UnauthApi.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/UnauthApi.java new file mode 100644 index 0000000..ea23c58 --- /dev/null +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/controller/UnauthApi.java @@ -0,0 +1,51 @@ +package enseirb.myinpulse.controller; + +import enseirb.myinpulse.model.Entrepreneur; +import enseirb.myinpulse.service.EntrepreneurApiService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.web.bind.annotation.*; + +@SpringBootApplication +@RestController +public class UnauthApi { + + private final EntrepreneurApiService entrepreneurApiService; + + @Autowired + UnauthApi(EntrepreneurApiService entrepreneurApiService) { + this.entrepreneurApiService = entrepreneurApiService; + } + + @GetMapping("/unauth/create_account") + public void createAccount(@AuthenticationPrincipal Jwt principal) { + boolean sneeStatus; + if (principal.getClaimAsString("sneeStatus") != null) { + sneeStatus = principal.getClaimAsString("sneeStatus").equals("true"); + } else { + sneeStatus = false; + } + String userSurname = principal.getClaimAsString("userSurname"); + String username = principal.getClaimAsString("preferred_username"); + String primaryMail = principal.getClaimAsString("email"); + String secondaryMail = principal.getClaimAsString("secondaryMail"); + String phoneNumber = principal.getClaimAsString("phoneNumber"); + String school = principal.getClaimAsString("school"); + String course = principal.getClaimAsString("course"); + Entrepreneur e = + new Entrepreneur( + userSurname, + username, + primaryMail, + secondaryMail, + phoneNumber, + school, + course, + sneeStatus, + true); + entrepreneurApiService.createAccount(e); + } +} diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java index d6eecda..c7d6cae 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java @@ -37,7 +37,7 @@ public class Administrator extends User { String primaryMail, String secondaryMail, String phoneNumber) { - super(null, userSurname, username, primaryMail, secondaryMail, phoneNumber); + super(userSurname, username, primaryMail, secondaryMail, phoneNumber, false); } public List getListProject() { diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java index 35b6e71..dbe04f0 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java @@ -44,15 +44,30 @@ public class Entrepreneur extends User { String phoneNumber, String school, String course, - boolean sneeStatus) { - super(userSurname, username, primaryMail, secondaryMail, phoneNumber); + boolean sneeStatus, + boolean pending) { + super(userSurname, username, primaryMail, secondaryMail, phoneNumber, pending); + this.school = school; + this.course = course; + this.sneeStatus = sneeStatus; + } + + public Entrepreneur( + String userSurname, + String username, + String primaryMail, + String secondaryMail, + String phoneNumber, + String school, + String course, + boolean sneeStatus) { + super(userSurname, username, primaryMail, secondaryMail, phoneNumber, false); this.school = school; this.course = course; this.sneeStatus = sneeStatus; } public Entrepreneur( - Long idUser, String userSurname, String userName, String primaryMail, @@ -63,8 +78,9 @@ public class Entrepreneur extends User { boolean sneeStatus, Project projectParticipation, Project projectProposed, - MakeAppointment makeAppointment) { - super(idUser, userSurname, userName, primaryMail, secondaryMail, phoneNumber); + MakeAppointment makeAppointment, + boolean pending) { + super(userSurname, userName, primaryMail, secondaryMail, phoneNumber, pending); this.school = school; this.course = course; this.sneeStatus = sneeStatus; diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/User.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/User.java index 37a551d..0f4c338 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/User.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/User.java @@ -26,36 +26,23 @@ public class User { @Column(length = 20) private String phoneNumber; + @Column private boolean pending; + public User() {} - // TODO: this should be removed as we shouldn't be able to chose the ID. Leaving it for - // compatibility purposes, as soon as it's not used anymore, delete it - public User( - Long idUser, - String userSurname, - String userName, - String primaryMail, - String secondaryMail, - String phoneNumber) { - this.idUser = idUser; - this.userSurname = userSurname; - this.userName = userName; - this.primaryMail = primaryMail; - this.secondaryMail = secondaryMail; - this.phoneNumber = phoneNumber; - } - public User( String userSurname, String userName, String primaryMail, String secondaryMail, - String phoneNumber) { + String phoneNumber, + boolean pending) { this.userSurname = userSurname; this.userName = userName; this.primaryMail = primaryMail; this.secondaryMail = secondaryMail; this.phoneNumber = phoneNumber; + this.pending = pending; } public Long getIdUser() { @@ -105,4 +92,12 @@ public class User { public void setPhoneNumber(String phoneNumber) { phoneNumber = phoneNumber; } + + public boolean isPending() { + return pending; + } + + public void setPending(boolean pending) { + this.pending = pending; + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/repository/UserRepository.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/repository/UserRepository.java index 687248c..96f5eaf 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/repository/UserRepository.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/repository/UserRepository.java @@ -11,6 +11,7 @@ import java.util.Optional; public interface UserRepository extends JpaRepository { Optional findByPrimaryMail(String primaryMail); + Iterable findAllByPendingEquals(boolean pending); /* @Query("SELECT u from User u") User findAllUser(); */ diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java index bd05a2c..4aade61 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java @@ -24,6 +24,7 @@ public class AdminApiService { private final ProjectService projectService; private final UserService userService; private final AdministratorService administratorService; + private final EntrepreneurService entrepreneurService; private final UtilsService utilsService; private final AppointmentService appointmentService; private final ReportService reportService; @@ -35,6 +36,7 @@ public class AdminApiService { UserService userService, AdministratorService administratorService, UtilsService utilsService, + EntrepreneurService entrepreneurService, AppointmentService appointmentService, ReportService reportService, SectionCellService sectionCellService) { @@ -45,6 +47,7 @@ public class AdminApiService { this.appointmentService = appointmentService; this.reportService = reportService; this.sectionCellService = sectionCellService; + this.entrepreneurService = entrepreneurService; } // TODO: check if tests are sufficient - peer verification required @@ -75,6 +78,12 @@ public class AdminApiService { } if (user instanceof Entrepreneur) { Project project = ((Entrepreneur) user).getProjectParticipation(); + if (project == null) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, + "The user has no project, thus no appointments. No users should have no project"); + } + project.getListSectionCell() .forEach( sectionCell -> { @@ -104,7 +113,7 @@ public class AdminApiService { } // TODO: check if tests are sufficient - peer verification required - public void addNewProject(Project project) { + public Project addNewProject(Project project) { project.setIdProject(null); // We remove the ID from the request to be sure that it will be auto generated try { @@ -136,6 +145,7 @@ public class AdminApiService { sectionCell -> { sectionCell.setProjectSectionCell(newProject); }); + return newProject; } public void createAppointmentReport(long appointmentId, Report report, String mail) { @@ -164,4 +174,36 @@ public class AdminApiService { public void deleteProject(long projectId) { this.projectService.deleteProjectById(projectId); } + + public void setAdmin(long userId, String token) { + Entrepreneur e = this.entrepreneurService.getEntrepreneurById(userId); + Administrator a = + new Administrator( + e.getUserSurname(), + e.getUserName(), + e.getPrimaryMail(), + e.getSecondaryMail(), + e.getPhoneNumber()); + this.entrepreneurService.deleteEntrepreneur(e); + this.administratorService.addAdministrator(a); + try { + KeycloakApi.setRoleToUser(a.getUserName(), "MyINPulse-admin", token); + } catch (Exception err) { + logger.error(err); + } + } + + public void validateEntrepreneurAccount(long userId, String token) { + Entrepreneur e = this.entrepreneurService.getEntrepreneurById(userId); + try { + KeycloakApi.setRoleToUser(e.getUserName(), "MyINPulse-entrepreneur", token); + } catch (Exception err) { + logger.error(err); + } + this.entrepreneurService.validateEntrepreneurById(userId); + } + + public Iterable getPendingUsers() { + return this.userService.getPendingAccounts(); + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java index 3bb8a8b..fe0dc71 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java @@ -2,10 +2,13 @@ package enseirb.myinpulse.service; import static enseirb.myinpulse.model.ProjectDecisionValue.PENDING; +import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.SectionCell; +import enseirb.myinpulse.service.database.EntrepreneurService; import enseirb.myinpulse.service.database.ProjectService; import enseirb.myinpulse.service.database.SectionCellService; +import enseirb.myinpulse.service.database.UserService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -22,15 +25,21 @@ public class EntrepreneurApiService { private final SectionCellService sectionCellService; private final ProjectService projectService; private final UtilsService utilsService; + private final UserService userService; + private final EntrepreneurService entrepreneurService; @Autowired EntrepreneurApiService( SectionCellService sectionCellService, ProjectService projectService, - UtilsService utilsService) { + UtilsService utilsService, + UserService userService, + EntrepreneurService entrepreneurService) { this.sectionCellService = sectionCellService; this.projectService = projectService; this.utilsService = utilsService; + this.userService = userService; + this.entrepreneurService = entrepreneurService; } public void editSectionCell(Long sectionCellId, String content, String mail) { @@ -128,4 +137,15 @@ public class EntrepreneurApiService { project.setProjectStatus(PENDING); projectService.addNewProject(project); } + + public void createAccount(Entrepreneur e) { + try { + userService.getUserByEmail(e.getPrimaryMail()); + logger.error("The user {} already exists in the system", e.getPrimaryMail()); + } catch (ResponseStatusException err) { + this.entrepreneurService.addEntrepreneur(e); + return; + } + throw new ResponseStatusException(HttpStatus.CONFLICT, "User already exists in the system"); + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/KeycloakApi.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/KeycloakApi.java index fb97c70..8942c31 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/KeycloakApi.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/KeycloakApi.java @@ -6,12 +6,17 @@ import enseirb.myinpulse.exception.UserNotFoundException; import enseirb.myinpulse.model.RoleRepresentation; import enseirb.myinpulse.model.UserRepresentation; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.web.client.RestClient; +import java.util.List; + import javax.management.relation.RoleNotFoundException; public class KeycloakApi { + protected static final Logger logger = LogManager.getLogger(); static final String keycloakUrl; static final String realmName; @@ -29,44 +34,48 @@ public class KeycloakApi { realmName = System.getenv("VITE_KEYCLOAK_REALM"); } + static String toBearer(String b) { + return "Bearer " + b; + } + /** * Uses Keycloak API to retrieve a role representation of a role by its name * * @param roleName name of the role - * @param bearer authorization header used by the client to authenticate to keycloak + * @param token authorization header used by the client to authenticate to keycloak */ - public static RoleRepresentation getRoleRepresentationByName(String roleName, String bearer) + public static RoleRepresentation getRoleRepresentationByName(String roleName, String token) throws RoleNotFoundException { - RoleRepresentation[] response = + RoleRepresentation response = RestClient.builder() .baseUrl(keycloakUrl) - .defaultHeader("Authorization", bearer) + .defaultHeader("Authorization", toBearer(token)) .build() .get() .uri("/admin/realms/{realmName}/roles/{roleName}", realmName, roleName) .retrieve() - .body(RoleRepresentation[].class); - - if (response == null || response.length == 0) { - throw new RoleNotFoundException("Role not found"); - } - return response[0]; + .body(RoleRepresentation.class); + /* + {"id":"7a845f2e-c832-4465-8cd8-894d72bc13f1","name":"MyINPulse-entrepreneur","description":"Role for entrepreneur","composite":false,"clientRole":false,"containerId":"0d6f691b-e328-471a-b89e-c30bd7e5b6b0","attributes":{}} + */ + // TODO: check what happens when role does not exist + return response; } /** * Use keycloak API to to retreive a userID via his name or email. * * @param username username or mail of the user - * @param bearer bearer of the user, allowing access to database + * @param token bearer of the user, allowing access to database * @return the userid, as a String * @throws UserNotFoundException */ - public static String getUserIdByName(String username, String bearer) + public static String getUserIdByName(String username, String token) throws UserNotFoundException { UserRepresentation[] response = RestClient.builder() .baseUrl(keycloakUrl) - .defaultHeader("Authorization", bearer) + .defaultHeader("Authorization", toBearer(token)) .build() .get() .uri( @@ -91,27 +100,26 @@ public class KeycloakApi { * * @param username * @param roleName - * @param bearer + * @param token * @throws RoleNotFoundException * @throws UserNotFoundException */ - public static void setRoleToUser(String username, String roleName, String bearer) + public static void setRoleToUser(String username, String roleName, String token) throws RoleNotFoundException, UserNotFoundException { - RoleRepresentation roleRepresentation = getRoleRepresentationByName(roleName, bearer); - String userId = getUserIdByName(username, bearer); - + RoleRepresentation roleRepresentation = getRoleRepresentationByName(roleName, token); + String userId = getUserIdByName(username, token); + List rolesToAdd = List.of(roleRepresentation); + logger.debug("Adding role {} to user {}", roleRepresentation.id, userId); RestClient.builder() .baseUrl(keycloakUrl) - .defaultHeader("Authorization", bearer) + .defaultHeader("Authorization", toBearer(token)) .build() .post() - .uri( - "/admin/realms/${realmName}/users/${userId}/role-mappings/realm", - realmName, - userId) - .body(roleRepresentation) + .uri("/admin/realms/" + realmName + "/users/" + userId + "/role-mappings/realm") + .body(rolesToAdd) .contentType(APPLICATION_JSON) - .retrieve(); + .retrieve() + .toBodilessEntity(); } /** diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java index 11cab5c..f2dc225 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/EntrepreneurService.java @@ -122,4 +122,17 @@ public class EntrepreneurService { public Iterable GetEntrepreneurByProject(Project project) { return this.entrepreneurRepository.getEntrepreneurByProjectParticipation(project); } + + public void deleteEntrepreneur(Entrepreneur e) { + this.entrepreneurRepository.delete(e); + } + + public void validateEntrepreneurById(Long id) { + Optional e = this.entrepreneurRepository.findById(id); + if (e.isEmpty()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Entrepreneur n'existe pas"); + } + e.get().setPending(false); + this.entrepreneurRepository.save(e.get()); + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/UserService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/UserService.java index 567c519..2c2dc25 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/UserService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/UserService.java @@ -117,4 +117,8 @@ public class UserService { } return this.userRepository.save(user.get()); } + + public Iterable getPendingAccounts() { + return this.userRepository.findAllByPendingEquals(true); + } } diff --git a/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java b/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java index 8fd0bd6..b287b97 100644 --- a/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java +++ b/MyINPulse-back/src/test/java/enseirb/myinpulse/AdminApiServiceTest.java @@ -4,12 +4,10 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.*; import static org.junit.jupiter.api.Assertions.*; -import enseirb.myinpulse.model.Administrator; -import enseirb.myinpulse.model.Entrepreneur; -import enseirb.myinpulse.model.Project; -import enseirb.myinpulse.model.ProjectDecision; +import enseirb.myinpulse.model.*; import enseirb.myinpulse.service.AdminApiService; import enseirb.myinpulse.service.database.AdministratorService; +import enseirb.myinpulse.service.database.AppointmentService; import enseirb.myinpulse.service.database.EntrepreneurService; import enseirb.myinpulse.service.database.ProjectService; @@ -21,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; import java.time.LocalDate; +import java.time.LocalTime; import java.util.ArrayList; import java.util.List; @@ -37,7 +36,8 @@ public class AdminApiServiceTest { static void setup( @Autowired AdministratorService administratorService, @Autowired ProjectService projectService, - @Autowired EntrepreneurService entrepreneurService) { + @Autowired EntrepreneurService entrepreneurService, + @Autowired AppointmentService appoitmentService) { administratorService.addAdministrator( new Administrator( "admin", @@ -54,6 +54,7 @@ public class AdminApiServiceTest { "testAdmin@example.com", "")); administratorid = administrator.getIdUser(); + entrepreneur = new Entrepreneur( "JeSuisUnEntrepreneurDeCompet", @@ -65,14 +66,33 @@ public class AdminApiServiceTest { "info ofc", false); entrepreneurService.addEntrepreneur(entrepreneur); - projectService.addNewProject( - new Project( - "sampleProjectAdminApiService", + + Entrepreneur entrepreneur2 = + new Entrepreneur( + "GDProjets", "", "Entrepreneur2@inpulse.com", "", "", "", "info ofc", true); + entrepreneurService.addEntrepreneur(entrepreneur2); + + Project p = + projectService.addNewProject( + new Project( + "sampleProjectAdminApiService", + null, + LocalDate.now(), + ACTIVE, + administratorService.getAdministratorByPrimaryMain( + "testAdminFull@example.com"))); + + entrepreneurService.updateEntrepreneurProjectParticipation(entrepreneur2.getIdUser(), p); + + Appointment a = + new Appointment( null, LocalDate.now(), - ACTIVE, - administratorService.getAdministratorByPrimaryMain( - "testAdminFull@example.com"))); + LocalTime.now(), + LocalTime.now(), + "Salle TD 15", + "Discussion importante"); + appoitmentService.addNewAppointment(a); } private List IterableToList(Iterable iterable) { @@ -221,4 +241,54 @@ public class AdminApiServiceTest { this.adminApiService.addNewProject(p1); assertThrows(ResponseStatusException.class, () -> this.adminApiService.addNewProject(p2)); } + + // We could do a delete active project, but it's not really useful. + @Test + void deletePendingProject() { + int oldsize = IterableToList(this.adminApiService.getPendingProjects()).size(); + Project p1 = + new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null); + Project p2 = this.adminApiService.addNewProject(p1); + + assertEquals(oldsize + 1, IterableToList(this.adminApiService.getPendingProjects()).size()); + this.adminApiService.deleteProject(p2.getIdProject()); + + assertEquals(oldsize, IterableToList(this.adminApiService.getPendingProjects()).size()); + for (int i = 0; i < oldsize; i++) { + assertNotEquals( + p1.getIdProject(), + IterableToList(this.adminApiService.getPendingProjects()) + .get(i) + .getIdProject()); + } + } + + @Test + void getUpcommingAppointmentUnkwnownUser() { + assertThrows( + ResponseStatusException.class, + () -> { + Iterable a = + this.adminApiService.getUpcomingAppointments( + "entrepreneur-inexistent@mail.fr"); + }); + } + + @Test + void getUpcommingAppointmentNoProject() { + assertThrows( + ResponseStatusException.class, + () -> { + Iterable a = + this.adminApiService.getUpcomingAppointments( + "Entrepreneur@inpulse.com"); + }); + } + + @Test + void getUpcommingAppointmentEmpty() { + Iterable a = + this.adminApiService.getUpcomingAppointments("Entrepreneur2@inpulse.com"); + assertEquals(0, IterableToList(a).size()); + } } diff --git a/front/MyINPulse-front/src/views/testComponent.vue b/front/MyINPulse-front/src/views/testComponent.vue index 1b1fd8a..ec3039a 100644 --- a/front/MyINPulse-front/src/views/testComponent.vue +++ b/front/MyINPulse-front/src/views/testComponent.vue @@ -4,6 +4,7 @@ import { callApi } from "@/services/api.ts"; import { ref } from "vue"; const CustomRequest = ref(""); +const USERID = ref("");