From 2c21c40fbddfd2a9b1a831572badf543fb28ba62 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 5 Mar 2024 15:21:21 +0100 Subject: [PATCH 01/19] Created CreateShelter.feature file --- src/test/resources/features/CreateShelter.feature | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/test/resources/features/CreateShelter.feature diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature new file mode 100644 index 00000000..0e90717d --- /dev/null +++ b/src/test/resources/features/CreateShelter.feature @@ -0,0 +1,4 @@ +Feature: Join Shelter + In order to use the app + As a user + I want to register myself and get an account \ No newline at end of file From 0efd547ad08880a65a104a44a73479cee31235d1 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 5 Mar 2024 15:40:11 +0100 Subject: [PATCH 02/19] Created CreateShelterStepDefs --- .../udl/eps/softarch/demo/steps/CreateShelterStepDefs.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java new file mode 100644 index 00000000..c127c903 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -0,0 +1,4 @@ +package cat.udl.eps.softarch.demo.steps; + +public class CreateShelterStepDefs { +} From ebd512e503e48615b2fe26ae7f2609ede23d390c Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 5 Mar 2024 16:03:26 +0100 Subject: [PATCH 03/19] First scenario implemented --- .../demo/steps/CreateShelterStepDefs.java | 36 +++++++++++++++++++ .../resources/features/CreateShelter.feature | 15 +++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index c127c903..677741d5 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -1,4 +1,40 @@ package cat.udl.eps.softarch.demo.steps; +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +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.User; +import cat.udl.eps.softarch.demo.repository.UserRepository; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.When; +import org.json.JSONObject; +import org.junit.Assert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; public class CreateShelterStepDefs { + + @Given("^There is no shelter registered with the name \"([^\"]*)\" and location \"([^\"]*)\"$") + public void thereIsNoShelterRegisteredWithTheNameAndLocation(String name, String location) { + Assert.assertFalse("Shelter with name \"" + + name + "\" and location \"" + location + "\" shouldn't exist", + shelterRepository.existsByNameAndLocation(name, location)); + } + + @And("I am a shelter manager") + public void iAmAShelterManager() { + + } + + @When("I attempt to create a new shelter with name {string} and location {string}") + public void iAttemptToCreateANewShelterWithNameAndLocation(String arg0, String arg1) { + + } + + @And("The response contains a success message confirming the shelter was created") + public void theResponseContainsASuccessMessageConfirmingTheShelterWasCreated() { + } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 0e90717d..22301785 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -1,4 +1,11 @@ -Feature: Join Shelter - In order to use the app - As a user - I want to register myself and get an account \ No newline at end of file +Feature: Create Shelter + In order to provide temporary housing for animals + As a shelter manager + I want to create shelters and manage their information + + Scenario: Create a new shelter + Given There is no shelter registered with the name "shelter" and location "city" + And I am a shelter manager + When I attempt to create a new shelter with name "shelter" and location "city" + Then The response code is 201 + And The response contains a success message confirming the shelter was created \ No newline at end of file From acce524b960a8e66b4cbe0009df3bd0971349ea9 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 5 Mar 2024 16:18:00 +0100 Subject: [PATCH 04/19] Changes to Create Shelter Step Defs --- .../softarch/demo/steps/CreateShelterStepDefs.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index 677741d5..fadf7017 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -6,17 +6,20 @@ 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.ShelterVolunteer; import cat.udl.eps.softarch.demo.domain.User; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.When; +import jakarta.validation.constraints.AssertTrue; import org.json.JSONObject; import org.junit.Assert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; public class CreateShelterStepDefs { - + private ShelterRepository shelterRepository; @Given("^There is no shelter registered with the name \"([^\"]*)\" and location \"([^\"]*)\"$") public void thereIsNoShelterRegisteredWithTheNameAndLocation(String name, String location) { Assert.assertFalse("Shelter with name \"" @@ -24,9 +27,10 @@ public void thereIsNoShelterRegisteredWithTheNameAndLocation(String name, String shelterRepository.existsByNameAndLocation(name, location)); } - @And("I am a shelter manager") - public void iAmAShelterManager() { - + @And("I am a shelter volunteer") + public void iAmAShelterVolunteer(User user) { + Assert.assertTrue("User should be a shelter volunteer", + user instanceof ShelterVolunteer); } @When("I attempt to create a new shelter with name {string} and location {string}") From 5eca35afa916967b5186bb121425dabf7402ee59 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 5 Mar 2024 16:19:12 +0100 Subject: [PATCH 05/19] Changes to CreateShelter.feature --- src/test/resources/features/CreateShelter.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 22301785..b6c42f43 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -5,7 +5,7 @@ Feature: Create Shelter Scenario: Create a new shelter Given There is no shelter registered with the name "shelter" and location "city" - And I am a shelter manager + And I am a shelter volunteer When I attempt to create a new shelter with name "shelter" and location "city" Then The response code is 201 And The response contains a success message confirming the shelter was created \ No newline at end of file From ef3a4a479e166ebf7ed28b72ed18b9deff3e80fd Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 5 Mar 2024 16:50:52 +0100 Subject: [PATCH 06/19] Changes to ShelterRepository, Completed test scenario1 --- .../demo/repository/ShelterRepository.java | 3 +- .../demo/steps/CreateShelterStepDefs.java | 53 +++++++++++++++---- .../resources/features/CreateShelter.feature | 4 +- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java index 556b18b0..5d65018a 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/ShelterRepository.java @@ -12,6 +12,5 @@ @RepositoryRestResource public interface ShelterRepository extends CrudRepository, PagingAndSortingRepository { - List findByLocatedAt(@Param("location") Location locatedAt); - + List findByLocatedAtAndName(@Param("location") Location locatedAt, @Param("name") String name); } \ No newline at end of file diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index fadf7017..e8355bf3 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -6,6 +6,8 @@ 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.Location; +import cat.udl.eps.softarch.demo.domain.Shelter; import cat.udl.eps.softarch.demo.domain.ShelterVolunteer; import cat.udl.eps.softarch.demo.domain.User; import cat.udl.eps.softarch.demo.repository.ShelterRepository; @@ -18,13 +20,17 @@ import org.junit.Assert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; + public class CreateShelterStepDefs { + + private StepDefs stepDefs; private ShelterRepository shelterRepository; @Given("^There is no shelter registered with the name \"([^\"]*)\" and location \"([^\"]*)\"$") - public void thereIsNoShelterRegisteredWithTheNameAndLocation(String name, String location) { - Assert.assertFalse("Shelter with name \"" - + name + "\" and location \"" + location + "\" shouldn't exist", - shelterRepository.existsByNameAndLocation(name, location)); + public void thereIsNoShelterRegisteredWithTheNameAndLocation(String name, Location location) {//Location aqui ha de ser Strind o Tipus location? tu fas post de nomes l'id de location o de tot el objecte? + Assert.assertTrue("Shelter with name \"" + + name + "\" and location \"" + location + "\" shouldn't exist", shelterRepository.findByLocatedAtAndName(location, name).isEmpty()); } @And("I am a shelter volunteer") @@ -33,12 +39,41 @@ public void iAmAShelterVolunteer(User user) { user instanceof ShelterVolunteer); } - @When("I attempt to create a new shelter with name {string} and location {string}") - public void iAttemptToCreateANewShelterWithNameAndLocation(String arg0, String arg1) { - + @When("I create a new shelter with name {string} and location {string}") + public void iAttemptToCreateANewShelterWithNameAndLocation(String name, Location location) { + Shelter shelter = new Shelter(); + shelter.setName(name); + shelter.setLocatedAt(location); + + try { + stepDefs.result = stepDefs.mockMvc.perform( + post("/shelters") + .contentType(MediaType.APPLICATION_JSON) + .content(new JSONObject( stepDefs.mapper.writeValueAsString(shelter) + ).put("name", name).toString()) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } catch (Exception e) { + throw new RuntimeException(e); + } } - @And("The response contains a success message confirming the shelter was created") - public void theResponseContainsASuccessMessageConfirmingTheShelterWasCreated() { + @And("It has been created a user with username \"shelter\" and location \"city\", the password is not returned") + public void itHasBeenCreatedShelterWithLocation(String name, Location location) throws Throwable{ + stepDefs.result = stepDefs.mockMvc.perform( + post("/shelters") + .contentType(MediaType.APPLICATION_JSON) + .content(new JSONObject() + .put("name", name) + .put("location", location) + .toString())) + .andDo(print()) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.name", is(name))) + .andExpect(jsonPath("$.location", is(location))) + .andExpect(jsonPath("$.id").exists()) + .andExpect(jsonPath("$.password").doesNotExist()); } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index b6c42f43..d26f5409 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -6,6 +6,6 @@ Feature: Create Shelter Scenario: Create a new shelter Given There is no shelter registered with the name "shelter" and location "city" And I am a shelter volunteer - When I attempt to create a new shelter with name "shelter" and location "city" + When I create a new shelter with name "shelter" and location "city" Then The response code is 201 - And The response contains a success message confirming the shelter was created \ No newline at end of file + And It has been created a user with username "shelter" and location "city", the password is not returned \ No newline at end of file From 61ba33ceca3188fff3b7f537d4a3a3d132f0f724 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Wed, 6 Mar 2024 17:43:44 +0100 Subject: [PATCH 07/19] KickUserFromShelter feature file created --- src/test/resources/features/KickUserFromShelter.feature | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/test/resources/features/KickUserFromShelter.feature diff --git a/src/test/resources/features/KickUserFromShelter.feature b/src/test/resources/features/KickUserFromShelter.feature new file mode 100644 index 00000000..e69de29b From 5e5e289051aa196490665444557c16f4f60bfcf6 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Wed, 6 Mar 2024 17:45:44 +0100 Subject: [PATCH 08/19] Deleted other feature file from branch --- src/test/resources/features/KickUserFromShelter.feature | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/test/resources/features/KickUserFromShelter.feature diff --git a/src/test/resources/features/KickUserFromShelter.feature b/src/test/resources/features/KickUserFromShelter.feature deleted file mode 100644 index e69de29b..00000000 From 543264af6766caab05dd7ca1387505f7bfe03cd0 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Wed, 6 Mar 2024 17:48:07 +0100 Subject: [PATCH 09/19] Changes to CreateShelter feature and steps file --- .../demo/steps/CreateShelterStepDefs.java | 21 +++++++++----- .../resources/features/CreateShelter.feature | 28 +++++++++++++++++-- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index e8355bf3..e5e4aa75 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -6,10 +6,7 @@ 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.Location; -import cat.udl.eps.softarch.demo.domain.Shelter; -import cat.udl.eps.softarch.demo.domain.ShelterVolunteer; -import cat.udl.eps.softarch.demo.domain.User; +import cat.udl.eps.softarch.demo.domain.*; import cat.udl.eps.softarch.demo.repository.ShelterRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; import io.cucumber.java.en.And; @@ -33,10 +30,10 @@ public void thereIsNoShelterRegisteredWithTheNameAndLocation(String name, Locati + name + "\" and location \"" + location + "\" shouldn't exist", shelterRepository.findByLocatedAtAndName(location, name).isEmpty()); } - @And("I am a shelter volunteer") - public void iAmAShelterVolunteer(User user) { + @And("I am a admin") + public void iAmAdmin(User user) { Assert.assertTrue("User should be a shelter volunteer", - user instanceof ShelterVolunteer); + user instanceof Admin); } @When("I create a new shelter with name {string} and location {string}") @@ -76,4 +73,14 @@ public void itHasBeenCreatedShelterWithLocation(String name, Location location) .andExpect(jsonPath("$.id").exists()) .andExpect(jsonPath("$.password").doesNotExist()); } + + @And("I am a shelter volunteer") + public void iAmAShelterVolunteer(User user) { + Assert.assertTrue("User should be a shelter volunteer", + user instanceof ShelterVolunteer); + } + + @And("The response is {string}") + public void theResponseIs(String arg0) { + } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index d26f5409..8a57421d 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -3,9 +3,33 @@ Feature: Create Shelter As a shelter manager I want to create shelters and manage their information - Scenario: Create a new shelter + Scenario: Create a new shelter as Admin + Given There is no shelter registered with the name "shelter" and location "city" + And I am a admin + When I create a new shelter with name "shelter" and location "city" + Then The response code is 201 + And It has been created a user with username "shelter" and location "city", the password is not returned + + Scenario: Create a new shelter as Shelter Volunteer Given There is no shelter registered with the name "shelter" and location "city" And I am a shelter volunteer When I create a new shelter with name "shelter" and location "city" + Then The response code is 403 + And The response is "You are not authorized to create a shelter" + + Scenario: Create a new shelter with the same name and location + Given There is a shelter registered with the name "shelter" and location "city" + And I am a admin + When I create a new shelter with name "shelter" and location "city" + Then The response code is 409 + And The response is "A shelter with the same name and location already exists" + + Scenario: Create a new shelter with the same name and different location + Given There is a shelter registered with the name "shelter" and location is different from "other city" + And I am a admin + When I create a new shelter with name "shelter" and location "other city" Then The response code is 201 - And It has been created a user with username "shelter" and location "city", the password is not returned \ No newline at end of file + And It has been created a user with username "shelter" and location "other city", the password is not returned + + Scenario: Create a new shelter with the same location and different name + Given \ No newline at end of file From 17bf93a46e8b630cabc78639c15123357a8190fb Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Wed, 6 Mar 2024 18:49:33 +0100 Subject: [PATCH 10/19] Changes to CrudShelter.feature file --- src/test/resources/features/CreateShelter.feature | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 8a57421d..bfa8de13 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -1,7 +1,18 @@ Feature: Create Shelter In order to provide temporary housing for animals - As a shelter manager - I want to create shelters and manage their information + As a Admin + I want to create shelters + + Background: + Given There is a registered user with username "user" and password "password" and email "user@sample.app" + Given There is a registered admin with name "admin" and password "password" and email "admin@sample.app" + Given I login as "user" with password "password" + + Scenario: Create a route without being logged in + Given I'm not logged in + When I create a shelter with a name "testShelter", location "testLocation" + Then The response code is 401 + And The error message is "Unauthorized" Scenario: Create a new shelter as Admin Given There is no shelter registered with the name "shelter" and location "city" From 6ded836052e48bde529432a1e90aee08aba96c72 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Thu, 7 Mar 2024 15:58:24 +0100 Subject: [PATCH 11/19] Changes to CrudShelter.feature file2 --- .../demo/steps/CreateShelterStepDefs.java | 55 ++++--------------- .../resources/features/CreateShelter.feature | 22 +------- 2 files changed, 15 insertions(+), 62 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index e5e4aa75..652c8618 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -24,10 +24,10 @@ public class CreateShelterStepDefs { private StepDefs stepDefs; private ShelterRepository shelterRepository; - @Given("^There is no shelter registered with the name \"([^\"]*)\" and location \"([^\"]*)\"$") - public void thereIsNoShelterRegisteredWithTheNameAndLocation(String name, Location location) {//Location aqui ha de ser Strind o Tipus location? tu fas post de nomes l'id de location o de tot el objecte? + @Given("^There is no shelter registered with the name \"([^\"]*)\" ") + public void thereIsNoShelterRegisteredWithName(String name) {//Location aqui ha de ser Strind o Tipus location? tu fas post de nomes l'id de location o de tot el objecte? Assert.assertTrue("Shelter with name \"" - + name + "\" and location \"" + location + "\" shouldn't exist", shelterRepository.findByLocatedAtAndName(location, name).isEmpty()); + + name + "\" and location \" shouldn't exist", shelterRepository.findByLocatedAtAndName(name).isEmpty()); } @And("I am a admin") @@ -36,51 +36,20 @@ public void iAmAdmin(User user) { user instanceof Admin); } - @When("I create a new shelter with name {string} and location {string}") - public void iAttemptToCreateANewShelterWithNameAndLocation(String name, Location location) { - Shelter shelter = new Shelter(); - shelter.setName(name); - shelter.setLocatedAt(location); - - try { - stepDefs.result = stepDefs.mockMvc.perform( - post("/shelters") - .contentType(MediaType.APPLICATION_JSON) - .content(new JSONObject( stepDefs.mapper.writeValueAsString(shelter) - ).put("name", name).toString()) - .characterEncoding(StandardCharsets.UTF_8) - .accept(MediaType.APPLICATION_JSON) - .with(AuthenticationStepDefs.authenticate())) - .andDo(print()); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @And("It has been created a user with username \"shelter\" and location \"city\", the password is not returned") - public void itHasBeenCreatedShelterWithLocation(String name, Location location) throws Throwable{ - stepDefs.result = stepDefs.mockMvc.perform( - post("/shelters") - .contentType(MediaType.APPLICATION_JSON) - .content(new JSONObject() - .put("name", name) - .put("location", location) - .toString())) - .andDo(print()) - .andExpect(status().isCreated()) - .andExpect(jsonPath("$.name", is(name))) - .andExpect(jsonPath("$.location", is(location))) - .andExpect(jsonPath("$.id").exists()) - .andExpect(jsonPath("$.password").doesNotExist()); - } - + @And("I am a shelter volunteer") public void iAmAShelterVolunteer(User user) { Assert.assertTrue("User should be a shelter volunteer", user instanceof ShelterVolunteer); } - @And("The response is {string}") - public void theResponseIs(String arg0) { + @Given("There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"") + public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String arg0, String arg1, String arg2) { + + } + + @When("I create a shelter with a name \"([^\"]*)\"") + public void iCreateAShelterWithAName(String arg0) throws Throwable { + } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index bfa8de13..45d13f97 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -6,11 +6,11 @@ Feature: Create Shelter Background: Given There is a registered user with username "user" and password "password" and email "user@sample.app" Given There is a registered admin with name "admin" and password "password" and email "admin@sample.app" - Given I login as "user" with password "password" - Scenario: Create a route without being logged in + + Scenario: Create a shelter without being logged in Given I'm not logged in - When I create a shelter with a name "testShelter", location "testLocation" + When I create a shelter with a name "testShelter"" Then The response code is 401 And The error message is "Unauthorized" @@ -28,19 +28,3 @@ Feature: Create Shelter Then The response code is 403 And The response is "You are not authorized to create a shelter" - Scenario: Create a new shelter with the same name and location - Given There is a shelter registered with the name "shelter" and location "city" - And I am a admin - When I create a new shelter with name "shelter" and location "city" - Then The response code is 409 - And The response is "A shelter with the same name and location already exists" - - Scenario: Create a new shelter with the same name and different location - Given There is a shelter registered with the name "shelter" and location is different from "other city" - And I am a admin - When I create a new shelter with name "shelter" and location "other city" - Then The response code is 201 - And It has been created a user with username "shelter" and location "other city", the password is not returned - - Scenario: Create a new shelter with the same location and different name - Given \ No newline at end of file From f3ca085036fb5f77a94a8ce2f9ad427038b74705 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Sat, 16 Mar 2024 12:32:04 +0100 Subject: [PATCH 12/19] Basic tests corrected --- .../demo/steps/CreateShelterStepDefs.java | 51 +++++++++++++------ .../resources/features/CreateShelter.feature | 18 +++---- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index 652c8618..007d8a0c 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -21,35 +21,54 @@ import java.nio.charset.StandardCharsets; public class CreateShelterStepDefs { + @Autowired private StepDefs stepDefs; + @Autowired private ShelterRepository shelterRepository; - @Given("^There is no shelter registered with the name \"([^\"]*)\" ") - public void thereIsNoShelterRegisteredWithName(String name) {//Location aqui ha de ser Strind o Tipus location? tu fas post de nomes l'id de location o de tot el objecte? - Assert.assertTrue("Shelter with name \"" - + name + "\" and location \" shouldn't exist", shelterRepository.findByLocatedAtAndName(name).isEmpty()); - } - @And("I am a admin") - public void iAmAdmin(User user) { - Assert.assertTrue("User should be a shelter volunteer", - user instanceof Admin); - } + @Autowired + private UserRepository userRepository; - - @And("I am a shelter volunteer") + @And("^I am a admin with name \"([^\"]*)\"$") + public void iAmAdmin(String name) { + //Admin admin = userRepository.findByName(name).get(0); + } + @And("^I am a shelter volunteer with name \"([^\"]*)\"$") public void iAmAShelterVolunteer(User user) { Assert.assertTrue("User should be a shelter volunteer", user instanceof ShelterVolunteer); } - - @Given("There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"") + @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String arg0, String arg1, String arg2) { } - @When("I create a shelter with a name \"([^\"]*)\"") - public void iCreateAShelterWithAName(String arg0) throws Throwable { + @When("^I create a shelter with a name \"([^\"]*)\" email \"([^\"]*)\" and mobile \"([^\"]*)\"$") + public void iCreateAShelterWithAName(String name, String email, String mobile) throws Throwable { + Shelter newshelter = new Shelter(); + newshelter.setName(name); + newshelter.setEmail(email); + newshelter.setMobile(mobile); + //Following code is doing a post request to /shelters storing the response in stepDefs.result to test REST functionalty + stepDefs.result = stepDefs.mockMvc.perform( + post("/shelters") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(newshelter)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } + @And("^There is (\\d+) Shelter created$") + public void thereIsShelterCreated(int sheltersCreatedNum) { + Assert.assertEquals("Shelters created", sheltersCreatedNum, shelterRepository.count()); + } + + @Given("^There is no shelter registered with the name \"([^\"]*)\"$") + public void thereIsNoShelterRegisteredWithTheName(String name) { + Assert.assertTrue("Shelter with name \"" + + name + "\" shouldn't exist", shelterRepository.findByName(name).isEmpty()); } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 45d13f97..23db9a42 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -10,21 +10,21 @@ Feature: Create Shelter Scenario: Create a shelter without being logged in Given I'm not logged in - When I create a shelter with a name "testShelter"" + When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" Then The response code is 401 And The error message is "Unauthorized" + And There is 0 Shelter created Scenario: Create a new shelter as Admin - Given There is no shelter registered with the name "shelter" and location "city" - And I am a admin - When I create a new shelter with name "shelter" and location "city" + Given There is no shelter registered with the name "shelter" + And I am a admin with name "adminName" + When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" Then The response code is 201 - And It has been created a user with username "shelter" and location "city", the password is not returned Scenario: Create a new shelter as Shelter Volunteer - Given There is no shelter registered with the name "shelter" and location "city" - And I am a shelter volunteer - When I create a new shelter with name "shelter" and location "city" + Given There is no shelter registered with the name "shelter" + And I am a shelter volunteer with name "shelterVolunteerName" + When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" Then The response code is 403 - And The response is "You are not authorized to create a shelter" + And The error message is "You are not authorized to create a shelter" From 620c7421ec46621ea22c5c9551107a4e781d57e2 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Sat, 16 Mar 2024 12:59:09 +0100 Subject: [PATCH 13/19] Added new scenarios --- .../demo/steps/CreateShelterStepDefs.java | 19 ++++++--- .../resources/features/CreateShelter.feature | 40 +++++++++++++++---- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index 007d8a0c..f057836c 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -32,16 +32,23 @@ public class CreateShelterStepDefs { @And("^I am a admin with name \"([^\"]*)\"$") public void iAmAdmin(String name) { - //Admin admin = userRepository.findByName(name).get(0); + } @And("^I am a shelter volunteer with name \"([^\"]*)\"$") - public void iAmAShelterVolunteer(User user) { - Assert.assertTrue("User should be a shelter volunteer", - user instanceof ShelterVolunteer); + public void iAmAShelterVolunteer(String name) { + } - @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") - public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String arg0, String arg1, String arg2) { + @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") + public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) { + if (!userRepository.existsById(username)) { + Admin user = new Admin(); + user.setEmail(email); + user.setId(username); + user.setPassword(password); + user.encodePassword(); + userRepository.save(user); + } } @When("^I create a shelter with a name \"([^\"]*)\" email \"([^\"]*)\" and mobile \"([^\"]*)\"$") diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 23db9a42..e1919c2e 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -15,16 +15,40 @@ Feature: Create Shelter And The error message is "Unauthorized" And There is 0 Shelter created - Scenario: Create a new shelter as Admin - Given There is no shelter registered with the name "shelter" - And I am a admin with name "adminName" + Scenario: Create shelter as Admin + Given I login as "admin" with password "password" When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" Then The response code is 201 - Scenario: Create a new shelter as Shelter Volunteer - Given There is no shelter registered with the name "shelter" - And I am a shelter volunteer with name "shelterVolunteerName" + Scenario: Create shelter as Client + Given I login as "client" with password "password" When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" - Then The response code is 403 - And The error message is "You are not authorized to create a shelter" + Then The response code is 401 + And The error message is "Unauthorized" + And There is 0 Shelter created + + Scenario: Create shelter with missing name + Given I login as "admin" with password "password" + When I create a shelter with a name "" email "testemail" and mobile "123456789" + Then The response code is 400 + And There is 0 Shelter created + + Scenario: Create shelter with missing email + Given I login as "admin" with password "password" + When I create a shelter with a name "testShelter" email "" and mobile "123456789" + Then The response code is 400 + And There is 0 Shelter created + + Scenario: Create shelter with missing mobile + Given I login as "admin" with password "password" + When I create a shelter with a name "testShelter" email "testemail" and mobile "" + Then The response code is 400 + And There is 0 Shelter created + Scenario: Create a shelter that already exists: + Given I login as "admin" with password "password" + When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" + Then The response code is 400 + When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" + Then The response code is 400 + And There is 0 Shelter created \ No newline at end of file From d28e5a3e017769cb02835c8d84e68a49698823a5 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Sat, 16 Mar 2024 12:59:57 +0100 Subject: [PATCH 14/19] Removed empty funcitons in step def --- .../eps/softarch/demo/steps/CreateShelterStepDefs.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index f057836c..7e938fa2 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -30,15 +30,6 @@ public class CreateShelterStepDefs { @Autowired private UserRepository userRepository; - @And("^I am a admin with name \"([^\"]*)\"$") - public void iAmAdmin(String name) { - - } - @And("^I am a shelter volunteer with name \"([^\"]*)\"$") - public void iAmAShelterVolunteer(String name) { - - } - @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) { if (!userRepository.existsById(username)) { From fad2ff336e41792a40d85b19b5f9766da1b26e96 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Wed, 20 Mar 2024 18:00:58 +0100 Subject: [PATCH 15/19] Created Shelter event handler and added new scenarios --- .../demo/handler/ShelterEventHandler.java | 44 +++++++++++++++++++ .../demo/steps/CreateShelterStepDefs.java | 15 +++++-- .../resources/features/CreateShelter.feature | 26 ++++++----- 3 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java new file mode 100644 index 00000000..c626b4f4 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java @@ -0,0 +1,44 @@ +package cat.udl.eps.softarch.demo.handler; + +import cat.udl.eps.softarch.demo.domain.Shelter; +import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import org.springframework.data.rest.core.annotation.HandleBeforeCreate; +import org.springframework.data.rest.core.annotation.RepositoryEventHandler; +import org.springframework.stereotype.Component; + +@Component +@RepositoryEventHandler +public class ShelterEventHandler { + final ShelterRepository shelterRepository; + + public ShelterEventHandler(ShelterRepository shelterRepository) { + this.shelterRepository = shelterRepository; + } + + @HandleBeforeCreate + public void handleShelterPreCreate(Shelter shelter) { + if (shelter.getRating() == null) { + shelter.setRating(0); + } + if (shelter.getCreatedAt() == null) { + throw new IllegalArgumentException("Shelter must have a creation date"); + } + if (shelter.getUpdatedAt() == null) { + throw new IllegalArgumentException("Shelter must have an update date"); + } + if (checkIfValidEmail(shelter)) { + shelterRepository.save(shelter); + } + + } + private boolean checkIfValidEmail(Shelter shelter){ + if (shelter.getEmail() == null) { + throw new IllegalArgumentException("Shelter must have an email"); + }else if (!shelter.getEmail().contains("@")) { + throw new IllegalArgumentException("Shelter email must contain @"); + }else if (!shelter.getEmail().contains(".")) { + throw new IllegalArgumentException("Shelter email must contain ."); + } + return true; + } +} diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index 7e938fa2..7eda4fd1 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -7,6 +7,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import cat.udl.eps.softarch.demo.domain.*; +import cat.udl.eps.softarch.demo.repository.AdminRepository; import cat.udl.eps.softarch.demo.repository.ShelterRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; import io.cucumber.java.en.And; @@ -19,6 +20,7 @@ import org.springframework.http.MediaType; import java.nio.charset.StandardCharsets; +import java.time.ZonedDateTime; public class CreateShelterStepDefs { @Autowired @@ -30,6 +32,9 @@ public class CreateShelterStepDefs { @Autowired private UserRepository userRepository; + @Autowired + private AdminRepository adminRepository; + @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) { if (!userRepository.existsById(username)) { @@ -38,25 +43,27 @@ public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, user.setId(username); user.setPassword(password); user.encodePassword(); - userRepository.save(user); + adminRepository.save(user); } } - @When("^I create a shelter with a name \"([^\"]*)\" email \"([^\"]*)\" and mobile \"([^\"]*)\"$") - public void iCreateAShelterWithAName(String name, String email, String mobile) throws Throwable { + @When("^I create a shelter with a name \"([^\"]*)\", email \"([^\"]*)\" and phone \"([^\"]*)\" and location \"([^\"]*)\"$") + public void iCreateAShelterWithAName(String name, String email, String mobile, String location) throws Throwable {; Shelter newshelter = new Shelter(); newshelter.setName(name); newshelter.setEmail(email); newshelter.setMobile(mobile); + newshelter.setCreatedAt(ZonedDateTime.now()); + newshelter.setUpdatedAt(ZonedDateTime.now()); //Following code is doing a post request to /shelters storing the response in stepDefs.result to test REST functionalty stepDefs.result = stepDefs.mockMvc.perform( post("/shelters") .contentType(MediaType.APPLICATION_JSON) .content(stepDefs.mapper.writeValueAsString(newshelter)) .characterEncoding(StandardCharsets.UTF_8) - .accept(MediaType.APPLICATION_JSON) .with(AuthenticationStepDefs.authenticate())) .andDo(print()); + } @And("^There is (\\d+) Shelter created$") diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index e1919c2e..26b01f36 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -10,45 +10,51 @@ Feature: Create Shelter Scenario: Create a shelter without being logged in Given I'm not logged in - When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" + When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 401 And The error message is "Unauthorized" And There is 0 Shelter created Scenario: Create shelter as Admin Given I login as "admin" with password "password" - When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" + When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 201 Scenario: Create shelter as Client Given I login as "client" with password "password" - When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" + When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 401 And The error message is "Unauthorized" And There is 0 Shelter created Scenario: Create shelter with missing name Given I login as "admin" with password "password" - When I create a shelter with a name "" email "testemail" and mobile "123456789" + When I create a shelter with a name "", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 400 And There is 0 Shelter created Scenario: Create shelter with missing email Given I login as "admin" with password "password" - When I create a shelter with a name "testShelter" email "" and mobile "123456789" + When I create a shelter with a name "name", email "" and phone "123123123" and location "location" Then The response code is 400 And There is 0 Shelter created Scenario: Create shelter with missing mobile Given I login as "admin" with password "password" - When I create a shelter with a name "testShelter" email "testemail" and mobile "" + When I create a shelter with a name "name", email "shelter@sample.app" and phone "" and location "location" Then The response code is 400 And There is 0 Shelter created Scenario: Create a shelter that already exists: Given I login as "admin" with password "password" - When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" - Then The response code is 400 - When I create a shelter with a name "testShelter" email "testemail" and mobile "123456789" - Then The response code is 400 + When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" + Then The response code is 201 + When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" + Then The response code is 409 + And There is 1 Shelter created + + Scenario: Create a shelter with invalid email + Given I login as "admin" with password "password" + When I create a shelter with a name "name", email "shelter@sample" and phone "123123123" and location "location" + Then The response code is 500 And There is 0 Shelter created \ No newline at end of file From d25590d29dd4c27210feb0473bec9909c59cd646 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Wed, 20 Mar 2024 19:15:41 +0100 Subject: [PATCH 16/19] Solved pull request erros --- .../demo/handler/ShelterEventHandler.java | 17 +---------------- .../resources/features/CreateShelter.feature | 6 ------ 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java index c626b4f4..78b2e537 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java @@ -17,28 +17,13 @@ public ShelterEventHandler(ShelterRepository shelterRepository) { @HandleBeforeCreate public void handleShelterPreCreate(Shelter shelter) { - if (shelter.getRating() == null) { - shelter.setRating(0); - } if (shelter.getCreatedAt() == null) { throw new IllegalArgumentException("Shelter must have a creation date"); } if (shelter.getUpdatedAt() == null) { throw new IllegalArgumentException("Shelter must have an update date"); } - if (checkIfValidEmail(shelter)) { - shelterRepository.save(shelter); - } - } - private boolean checkIfValidEmail(Shelter shelter){ - if (shelter.getEmail() == null) { - throw new IllegalArgumentException("Shelter must have an email"); - }else if (!shelter.getEmail().contains("@")) { - throw new IllegalArgumentException("Shelter email must contain @"); - }else if (!shelter.getEmail().contains(".")) { - throw new IllegalArgumentException("Shelter email must contain ."); - } - return true; + } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 26b01f36..5db8e08b 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -52,9 +52,3 @@ Feature: Create Shelter When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 409 And There is 1 Shelter created - - Scenario: Create a shelter with invalid email - Given I login as "admin" with password "password" - When I create a shelter with a name "name", email "shelter@sample" and phone "123123123" and location "location" - Then The response code is 500 - And There is 0 Shelter created \ No newline at end of file From b94a6f8a0425d21ac19c133e51fd41cc7c0824fd Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Thu, 21 Mar 2024 18:32:22 +0100 Subject: [PATCH 17/19] admin registered posted to mvc --- .../softarch/demo/steps/CreateShelterStepDefs.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index 7eda4fd1..25d0f810 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -36,14 +36,21 @@ public class CreateShelterStepDefs { private AdminRepository adminRepository; @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") - public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) { + public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) throws Throwable { if (!userRepository.existsById(username)) { Admin user = new Admin(); user.setEmail(email); user.setId(username); user.setPassword(password); user.encodePassword(); - adminRepository.save(user); + userRepository.save(user); + stepDefs.result = stepDefs.mockMvc.perform( + post("/admins") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(user)) + .characterEncoding(StandardCharsets.UTF_8) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); } } From 051ccbc1c826798ac702b69b2edf4bf40ae9fc21 Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 26 Mar 2024 17:01:19 +0100 Subject: [PATCH 18/19] Solved ShelterHandler values inicialization --- .../demo/handler/ShelterEventHandler.java | 16 ++++++----- .../demo/steps/CreateShelterStepDefs.java | 28 +++++++++++++------ .../resources/features/CreateShelter.feature | 8 ++++++ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java index 78b2e537..93176c97 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.java @@ -2,10 +2,13 @@ import cat.udl.eps.softarch.demo.domain.Shelter; import cat.udl.eps.softarch.demo.repository.ShelterRepository; +import org.springframework.data.rest.core.annotation.HandleAfterSave; import org.springframework.data.rest.core.annotation.HandleBeforeCreate; import org.springframework.data.rest.core.annotation.RepositoryEventHandler; import org.springframework.stereotype.Component; +import java.time.ZonedDateTime; + @Component @RepositoryEventHandler public class ShelterEventHandler { @@ -17,13 +20,12 @@ public ShelterEventHandler(ShelterRepository shelterRepository) { @HandleBeforeCreate public void handleShelterPreCreate(Shelter shelter) { - if (shelter.getCreatedAt() == null) { - throw new IllegalArgumentException("Shelter must have a creation date"); - } - if (shelter.getUpdatedAt() == null) { - throw new IllegalArgumentException("Shelter must have an update date"); - } - + shelter.setCreatedAt(ZonedDateTime.now()); + shelter.setUpdatedAt(ZonedDateTime.now()); } + @HandleAfterSave + public void handleShelterPostSave(Shelter shelter) { + shelter.setUpdatedAt(ZonedDateTime.now()); + } } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index 25d0f810..f9b3e4f3 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -9,6 +9,7 @@ import cat.udl.eps.softarch.demo.domain.*; 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; @@ -32,25 +33,22 @@ public class CreateShelterStepDefs { @Autowired private UserRepository userRepository; + @Autowired + private ShelterVolunteerRepository ShelterVolunteerRepository; + @Autowired private AdminRepository adminRepository; @Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) throws Throwable { - if (!userRepository.existsById(username)) { + if (!adminRepository.existsById(username)) { Admin user = new Admin(); user.setEmail(email); user.setId(username); user.setPassword(password); user.encodePassword(); - userRepository.save(user); - stepDefs.result = stepDefs.mockMvc.perform( - post("/admins") - .contentType(MediaType.APPLICATION_JSON) - .content(stepDefs.mapper.writeValueAsString(user)) - .characterEncoding(StandardCharsets.UTF_8) - .with(AuthenticationStepDefs.authenticate())) - .andDo(print()); + adminRepository.save(user); + } } @@ -83,4 +81,16 @@ public void thereIsNoShelterRegisteredWithTheName(String name) { Assert.assertTrue("Shelter with name \"" + name + "\" shouldn't exist", shelterRepository.findByName(name).isEmpty()); } + + @Given("^There is a registered volunteer with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") + public void thereIsARegisteredVolunteerWithNameAndPasswordAndEmail(String name, String password, String email) { + if(!ShelterVolunteerRepository.existsById(name)) { + ShelterVolunteer user = new ShelterVolunteer(); + user.setEmail(email); + user.setId(name); + user.setPassword(password); + user.encodePassword(); + ShelterVolunteerRepository.save(user); + } + } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 5db8e08b..482130a9 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -6,6 +6,7 @@ Feature: Create Shelter Background: Given There is a registered user with username "user" and password "password" and email "user@sample.app" Given There is a registered admin with name "admin" and password "password" and email "admin@sample.app" + Given There is a registered volunteer with name "volunteer" and password "password" and email "volunteer@sample.app" Scenario: Create a shelter without being logged in @@ -20,6 +21,13 @@ Feature: Create Shelter When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 201 + #Scenario: Create shelter as Volunteer + # Given I login as "volunteer" with password "password" + # When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" + # Then The response code is 401 + # And The error message is "Unauthorized" + # And There is 0 Shelter created + Scenario: Create shelter as Client Given I login as "client" with password "password" When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" From c138612b608cf3a4c4fbeab24d3e2c83af2015be Mon Sep 17 00:00:00 2001 From: Sebastian Jitaru Date: Tue, 26 Mar 2024 18:04:25 +0100 Subject: [PATCH 19/19] WebSecurityConfig changed so only admin can post to /shelters/** --- .../eps/softarch/demo/config/WebSecurityConfig.java | 6 ++++-- .../softarch/demo/steps/CreateShelterStepDefs.java | 12 ++++++------ src/test/resources/features/CreateShelter.feature | 12 ++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) 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..775d4702 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 @@ -30,11 +30,13 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce .requestMatchers(HttpMethod.GET, "/identity").authenticated() .requestMatchers(HttpMethod.POST, "/users").anonymous() .requestMatchers(HttpMethod.POST, "/users/*").denyAll() - .requestMatchers(HttpMethod.POST, "/**/*").authenticated() .requestMatchers(HttpMethod.PUT, "/**/*").authenticated() .requestMatchers(HttpMethod.PATCH, "/**/*").authenticated() .requestMatchers(HttpMethod.DELETE, "/**/*").authenticated() - .anyRequest().permitAll()) + .requestMatchers("/shelters/**").hasAuthority("ROLE_ADMIN") + .requestMatchers(HttpMethod.POST, "/**/*").authenticated() + + .anyRequest().permitAll()) .csrf((csrf) -> csrf.disable()) .sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .cors((cors) -> cors.configurationSource(corsConfigurationSource())) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java index f9b3e4f3..d69e67a8 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.java @@ -85,12 +85,12 @@ public void thereIsNoShelterRegisteredWithTheName(String name) { @Given("^There is a registered volunteer with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") public void thereIsARegisteredVolunteerWithNameAndPasswordAndEmail(String name, String password, String email) { if(!ShelterVolunteerRepository.existsById(name)) { - ShelterVolunteer user = new ShelterVolunteer(); - user.setEmail(email); - user.setId(name); - user.setPassword(password); - user.encodePassword(); - ShelterVolunteerRepository.save(user); + ShelterVolunteer volunteer = new ShelterVolunteer(); + volunteer.setEmail(email); + volunteer.setId(name); + volunteer.setPassword(password); + volunteer.encodePassword(); + ShelterVolunteerRepository.save(volunteer); } } } diff --git a/src/test/resources/features/CreateShelter.feature b/src/test/resources/features/CreateShelter.feature index 482130a9..a92be1f3 100644 --- a/src/test/resources/features/CreateShelter.feature +++ b/src/test/resources/features/CreateShelter.feature @@ -21,12 +21,12 @@ Feature: Create Shelter When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" Then The response code is 201 - #Scenario: Create shelter as Volunteer - # Given I login as "volunteer" with password "password" - # When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" - # Then The response code is 401 - # And The error message is "Unauthorized" - # And There is 0 Shelter created + Scenario: Create shelter as Volunteer + Given I login as "volunteer" with password "password" + When I create a shelter with a name "name", email "shelter@sample.app" and phone "123123123" and location "location" + Then The response code is 403 + And The error message is "Forbidden" + And There is 0 Shelter created Scenario: Create shelter as Client Given I login as "client" with password "password"