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 2cfc3a7..d54a3d9 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 @@ -34,6 +34,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce .requestMatchers(HttpMethod.POST, "/apartments/*").hasAnyRole("OWNER") .requestMatchers(HttpMethod.POST, "/advertisementStatuses").hasAnyRole("OWNER") .requestMatchers(HttpMethod.DELETE, "/advertisementStatuses").hasAnyRole("OWNER") + .requestMatchers(HttpMethod.PUT, "/advertisementStatuses/*").hasAnyRole("OWNER") .requestMatchers(HttpMethod.POST, "/users").anonymous() .requestMatchers(HttpMethod.POST, "/users/*").denyAll() .requestMatchers(HttpMethod.POST, "/rooms").authenticated() diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteAdvertisementStatusStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteAdvertisementStatusStepDefs.java index 4410655..d10228f 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteAdvertisementStatusStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteAdvertisementStatusStepDefs.java @@ -44,12 +44,6 @@ public class DeleteAdvertisementStatusStepDefs { private ObjectMapper objectMapper; - @And("^The advertisement status with name \"([^\"]*)\" no longer exists$") - public void theApartmentWithNameNoLongerExists(String name) { - List apartments = advertisementStatusRepository.findByStatus(name); - assertTrue("Apartment with name \"" + name + "\" should no longer exist", apartments.isEmpty()); - } - @When("I delete the advertisement status with status {string}") public void iDeleteTheAdvertisementStatusWithStatus(String status) throws Exception { AdvertisementStatus adStatus = advertisementStatusRepository.findByStatus(status).stream().findFirst().orElse(null); @@ -57,7 +51,7 @@ public void iDeleteTheAdvertisementStatusWithStatus(String status) throws Except if (adStatus == null) { stepDefs.result = stepDefs.mockMvc.perform( - delete("/advertisements/{id}", 9999).accept(MediaType.APPLICATION_JSON) + delete("/advertisementStatuses/{id}", 9999).accept(MediaType.APPLICATION_JSON) .characterEncoding(StandardCharsets.UTF_8) .with(AuthenticationStepDefs.authenticate())) .andDo(print()); diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/ModifyAdvertisementStatusStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/ModifyAdvertisementStatusStepDefs.java new file mode 100644 index 0000000..f55bb4e --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/ModifyAdvertisementStatusStepDefs.java @@ -0,0 +1,84 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.AdvertisementStatus; +import cat.udl.eps.softarch.demo.repository.AdvertisementStatusRepository; +import io.cucumber.java.en.And; +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.nio.charset.StandardCharsets; + +import static org.junit.Assert.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +public class ModifyAdvertisementStatusStepDefs { + @Autowired + StepDefs stepDefs; + + @Autowired + AdvertisementStatusRepository adStatusRepository; + + private AdvertisementStatus adStatus; + + private String previousStatus; + + private long adStatusId; + + @Given("^There is an advertisementStatus already created with status \"([^\"]*)\"$") + public void there_is_an_advertisement_status_already_created_with_description(String status) { + this.adStatus = new AdvertisementStatus(); + this.adStatus.setStatus(status); + this.adStatusRepository.save(this.adStatus); + } + + @When("^I modify an advertisementStatus with status \"([^\"]*)\" and change it to \"([^\"]*)\"$") + public void modify_advertisement_status(String status, String newStatus) throws Throwable { + AdvertisementStatus adStatus = adStatusRepository.findByStatus(status).stream().findFirst().orElse(null); + assertNotNull("Apartment with name \"" + status + "\" should exist", adStatus); + this.previousStatus = this.adStatus.getStatus(); + this.adStatusId = adStatus.getId(); + if (newStatus.equals("null")) { + adStatus.setStatus(null); + } else { + adStatus.setStatus(newStatus); + } + + this.stepDefs.result = this.stepDefs.mockMvc.perform(put("/advertisementStatuses/" + this.adStatusId) + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(adStatus)) // Enviar el objeto actualizado + .characterEncoding(StandardCharsets.UTF_8) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + + } + + @And("The advertisementStatus has not been modified to \"([^\"]*)\"$") + public void the_advertisement_status_has_not_been_modified(String descToCheck) throws Throwable { + + AdvertisementStatus adStatusToCheck = this.adStatusRepository.findById(this.adStatusId).get(); + + assertNotEquals(adStatusToCheck.getStatus(), descToCheck); + assertEquals(adStatusToCheck.getStatus(), previousStatus); + } + + @And("The advertisementStatus has been modified to \"([^\"]*)\"$") + public void the_advertisement_status_has_been_modified(String status) { + + AdvertisementStatus adStatusToCheck = this.adStatusRepository.findById(this.adStatusId).get(); + + assertEquals(status, adStatusToCheck.getStatus()); + } + + @And("^The advertisementStatus has been modified with the description \"([^\"]*)\"$") + public void the_advertisement_status_has_been_modified_with_description(String newDescription) throws Throwable { + + AdvertisementStatus adStatusToCheck = this.adStatusRepository.findById(this.adStatusId).get(); + + assertEquals(adStatusToCheck.getStatus(), newDescription); + assertNotEquals(adStatusToCheck.getStatus(), previousStatus); + } +} diff --git a/src/test/resources/features/ModifyAdvertisementStatus.feature b/src/test/resources/features/ModifyAdvertisementStatus.feature new file mode 100644 index 0000000..724a685 --- /dev/null +++ b/src/test/resources/features/ModifyAdvertisementStatus.feature @@ -0,0 +1,40 @@ +Feature: Modify an Advertisement Status + In order to make changes on a property + As an owner + I want to modify a property + + Background: + Given There is a registered user with username "user" and password "password" and email "user@user.app" + Given There is a registered owner with username "owner" and password "password" and email "owner@example.com" + + Scenario: Modify an Advertisement Status when not logged in + Given I'm not logged in + And There is an advertisementStatus already created with status "Available" + When I modify an advertisementStatus with status "Available" and change it to "Occupied" + Then The response code is 401 + And The error message is "Unauthorized" + And The advertisementStatus has not been modified to "Occupied" + + Scenario: Modify a Advertisement Status when logged in as a user + Given I login as "user" with password "password" + And There is an advertisementStatus already created with status "Available" + When I modify an advertisementStatus with status "Available" and change it to "Occupied" + Then The response code is 403 + And The error message is "Forbidden" + And The advertisementStatus has not been modified to "Occupied" + + Scenario: Modify a Advertisement Status when logged in as a owner + Given I login as "owner" with password "password" + And There is an advertisementStatus already created with status "Available" + When I modify an advertisementStatus with status "Available" and change it to "Occupied" + Then The response code is 204 + And The advertisementStatus has been modified to "Occupied" + + Scenario: Modify a Advertisement Status when logged in as a owner and status null + Given I login as "owner" with password "password" + And There is an advertisementStatus already created with status "Available" + When I modify an advertisementStatus with status "Available" and change it to "null" + Then The response code is 400 + And The advertisementStatus has not been modified to "null" + +