From 45b70ad53707e2dabf36fc3e130f8b85d5f61f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Wed, 3 Apr 2024 18:30:49 +0200 Subject: [PATCH] Check if user is deleted --- .../softarch/demo/domain/ShelterVolunteer.java | 3 +++ .../handler/ShelterVolunteerEventHandler.java | 4 ++-- .../features/KickUserFromShelter.feature | 17 ++++++----------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterVolunteer.java b/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterVolunteer.java index 93263839..5e3d7ee5 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterVolunteer.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/ShelterVolunteer.java @@ -10,6 +10,8 @@ import jakarta.persistence.ManyToOne; import lombok.Data; import lombok.EqualsAndHashCode; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; @@ -18,6 +20,7 @@ @EqualsAndHashCode(callSuper = true) @Data @Entity +@OnDelete(action = OnDeleteAction.CASCADE) public class ShelterVolunteer extends User { diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java index 83b42212..4e9845c6 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java @@ -29,7 +29,7 @@ public ShelterVolunteerEventHandler(ShelterVolunteerRepository shelterVolunteerR } @HandleBeforeDelete - public void handleShelterVolunteerBeforeCreate(ShelterVolunteer volunteer) { + public void handleShelterVolunteerBeforeDelete(ShelterVolunteer volunteer) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String username = authentication.getName(); ShelterVolunteer requestVolunteer = shelterVolunteerRepository.findById(username).orElse(null); @@ -41,7 +41,7 @@ public void handleShelterVolunteerBeforeCreate(ShelterVolunteer volunteer) { // Do something with the username, like associating it with the created user } @HandleAfterDelete - public void handleShelterVolunteerPostCreate(ShelterVolunteer volunteer) { + public void handleShelterVolunteerPostDelete(ShelterVolunteer volunteer) { logger.info("After deleting: {}", volunteer.toString()); shelterVolunteerRepository.delete(volunteer); diff --git a/src/test/resources/features/KickUserFromShelter.feature b/src/test/resources/features/KickUserFromShelter.feature index 6d64f48f..eb7a72de 100644 --- a/src/test/resources/features/KickUserFromShelter.feature +++ b/src/test/resources/features/KickUserFromShelter.feature @@ -1,7 +1,7 @@ Feature: Kick ShelterVolunteer from Shelter In order to kick a shelter volunteer - As a Admin - I want to be able to kick a volunteer from the shelter + As a Volunteer + I want to be able to kick a volunteer from my shelter but not able to kick volunteers from other shelters Background: Given There is a registered user with username "user" and password "pass" and email "user@user.com" @@ -13,29 +13,24 @@ Feature: Kick ShelterVolunteer from Shelter Scenario: Kick volunteer from shelter Given I can login with username "volunteer" and password "pass" - And The response code is 200 - And There is a shelter volunteer with username "volunteer1" and password "pass" in the shelter "shelter" When I kick user "volunteer1" from shelter "shelter" Then The response code is 200 And I cannot login with username "volunteer1" and password "pass" - And The response code is 200 + Scenario: Kick volunteer from shelter as user And I can login with username "user" and password "pass" - Then The response code is 200 - And There is a shelter volunteer with username "volunteer" and password "pass" in the shelter "shelter" - When I kick user "volunteer" from shelter "shelter" + When I kick user "volunteer1" from shelter "shelter" Then The response code is 403 + And I can login with username "volunteer1" and password "pass" Scenario: Kick volunteer from shelter as volunteer in another shelter Given I can login with username "volunteer" and password "pass" - Then The response code is 200 - And There is a shelter volunteer with username "volunteer" and password "pass" in the shelter "shelter" When I kick user "volunteer2" from shelter "shelter" Then The response code is 412 And I can login with username "volunteer2" and password "pass" - And The response code is 401 \ No newline at end of file +