diff --git a/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java b/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java index 083de609..e9e0686c 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java +++ b/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java @@ -33,6 +33,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce .requestMatchers(HttpMethod.POST, "/**/*").authenticated() .requestMatchers(HttpMethod.PUT, "/**/*").authenticated() .requestMatchers(HttpMethod.PATCH, "/**/*").authenticated() + .requestMatchers(HttpMethod.DELETE, "/shelters/*").hasRole("ADMIN") .requestMatchers(HttpMethod.DELETE, "/**/*").authenticated() .anyRequest().permitAll()) .csrf((csrf) -> csrf.disable()) diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Admin.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Admin.java index cddd8027..f6e586ee 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Admin.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Admin.java @@ -1,5 +1,7 @@ package cat.udl.eps.softarch.demo.domain; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import lombok.Data; @@ -15,8 +17,10 @@ public class Admin extends User{ @Override + @JsonValue(value = false) + @JsonProperty(access = JsonProperty.Access.READ_ONLY) @ElementCollection - public Collection getAuthorities(){ + public Collection getAuthorities() { return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"); } } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterStepDefs.java new file mode 100644 index 00000000..4e823dbf --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterStepDefs.java @@ -0,0 +1,49 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.When; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.util.List; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +public class DeleteShelterStepDefs { + + @Autowired + private StepDefs stepDefs; + + @Autowired + private ShelterRepository shelterRepository; + + + @Given("There is a created shelter with name {string}, email {string} and phone {string}") + public void thereIsACreatedShelterWithNameEmailAndPhone(String name, String email, String phone) { + if (shelterRepository.findByName(name).isEmpty()) { + Shelter shelter = new Shelter(); + shelter.setName(name); + shelter.setEmail(email); + shelter.setMobile(phone); + shelter.setLocatedAt(null); + + shelterRepository.save(shelter); + } + } + + @When("I try to delete Shelter with name {string}") + public void iTryToDeleteShelterWithName(String name) throws Exception { + List shelterList = shelterRepository.findByName(name); + + stepDefs.result = stepDefs.mockMvc.perform( + delete("/shelters/{id}", shelterList.isEmpty() ? "0" : shelterList.get(0).getId()) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } + + +} diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterStepDefs.java index e505cb67..99cb6744 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/RegisterStepDefs.java @@ -7,7 +7,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import cat.udl.eps.softarch.demo.domain.Admin; +import cat.udl.eps.softarch.demo.domain.ShelterVolunteer; import cat.udl.eps.softarch.demo.domain.User; +import cat.udl.eps.softarch.demo.repository.AdminRepository; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; @@ -27,6 +32,12 @@ public class RegisterStepDefs { @Autowired private UserRepository userRepository; + @Autowired + private AdminRepository adminRepository; + + @Autowired + private ShelterVolunteerRepository shelterVolunteerRepository; + @Given("^There is no registered user with username \"([^\"]*)\"$") public void thereIsNoRegisteredUserWithUsername(String user) { Assert.assertFalse("User \"" @@ -46,6 +57,31 @@ public void thereIsARegisteredUserWithUsernameAndPasswordAndEmail(String usernam } } + + @Given("^There is a registered admin with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") + public void thereIsARegisteredAdminWithUsernameAndPasswordAndEmail(String username, String password, String email) { + if (!adminRepository.existsById(username)) { + Admin user = new Admin(); + user.setEmail(email); + user.setId(username); + user.setPassword(password); + user.encodePassword(); + userRepository.save(user); + } + } + + @Given("^There is a registered shelter volunteer with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") + public void thereIsARegisteredShelterVolunteerWithUsernameAndPasswordAndEmail(String username, String password, String email) { + if (!shelterVolunteerRepository.existsById(username)) { + ShelterVolunteer user = new ShelterVolunteer(); + user.setEmail(email); + user.setId(username); + user.setPassword(password); + user.encodePassword(); + userRepository.save(user); + } + } + @And("^I can login with username \"([^\"]*)\" and password \"([^\"]*)\"$") public void iCanLoginWithUsernameAndPassword(String username, String password) throws Throwable { AuthenticationStepDefs.currentUsername = username; diff --git a/src/test/resources/features/DeleteShelter.feature b/src/test/resources/features/DeleteShelter.feature new file mode 100644 index 00000000..a2ca0b00 --- /dev/null +++ b/src/test/resources/features/DeleteShelter.feature @@ -0,0 +1,47 @@ +Feature: Delete Shelter + In order to use the app + only admins must be able to delete Shelters + + Background: + Given There is a created shelter with name "name", email "shelter@sample.com" and phone "123123123" + + Scenario: Delete a Shelter not logged in + Given I'm not logged in + When I try to delete Shelter with name "name" + Then The response code is 401 + + Scenario: Delete a Shelter as user + Given There is a registered user with username "user" and password "password" and email "user@sample.app" + Given I login as "user" with password "password" + When I try to delete Shelter with name "name" + Then The response code is 403 + + Scenario: Delete a Shelter as ShelterVolunteer + Given There is a registered shelter volunteer with username "ShelterVolunteer" and password "password" and email "shelterv@sample.app" + Given I login as "ShelterVolunteer" with password "password" + When I try to delete Shelter with name "name" + Then The response code is 403 + + Scenario: Delete a Shelter as admin + Given There is a registered admin with username "admin" and password "password" and email "admin@sample.app" + Given I login as "admin" with password "password" + When I try to delete Shelter with name "name" + Then The response code is 200 + + Scenario: Delete a Shelter that does not exist as user + Given There is a registered user with username "user" and password "password" and email "user@sample.app" + Given I login as "user" with password "password" + When I try to delete Shelter with name "name" + Then The response code is 403 + + Scenario: Delete a Shelter that does not exist as ShelterVolunteer + Given There is a registered shelter volunteer with username "ShelterVolunteer" and password "password" and email "shelterv@sample.app" + Given I login as "ShelterVolunteer" with password "password" + When I try to delete Shelter with name "name" + Then The response code is 403 + + Scenario: Delete a Shelter that does not exist as admin + Given There is a registered admin with username "admin" and password "password" and email "admin@sample.app" + Given I login as "admin" with password "password" + When I try to delete Shelter with name "unregistered" + Then The response code is 404