From 5d533ed896b3a6bba981c577c417bedf8515567d Mon Sep 17 00:00:00 2001 From: Malena Date: Thu, 15 Jan 2026 12:29:12 +0100 Subject: [PATCH 1/8] added Securityconfigurations for the custiomer and admin --- .../udl/eps/softarch/demo/config/SecurityConfiguration.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java b/src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java new file mode 100644 index 0000000..5e5fa24 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java @@ -0,0 +1,4 @@ +package cat.udl.eps.softarch.demo.config; + +public class SecurityConfiguration { +} From 0540516b9376727ddb303942a1f8b539dd67be32 Mon Sep 17 00:00:00 2001 From: Malena Date: Thu, 15 Jan 2026 18:03:30 +0100 Subject: [PATCH 2/8] id to the gmail --- .../demo/config/SecurityConfiguration.java | 4 - .../demo/config/WebSecurityConfig.java | 2 +- .../eps/softarch/demo/domain/Customer.java | 7 ++ .../demo/handler/CustomerEventHandler.java | 4 + .../demo/steps/UpdateLoyaltyStepDefs.java | 94 +++++++++++++++++++ .../resources/features/UpdateLoyalty.feature | 27 ++++++ 6 files changed, 133 insertions(+), 5 deletions(-) delete mode 100644 src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java create mode 100644 src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java create mode 100644 src/test/resources/features/UpdateLoyalty.feature diff --git a/src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java b/src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java deleted file mode 100644 index 5e5fa24..0000000 --- a/src/main/java/cat/udl/eps/softarch/demo/config/SecurityConfiguration.java +++ /dev/null @@ -1,4 +0,0 @@ -package cat.udl.eps.softarch.demo.config; - -public class SecurityConfiguration { -} 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 183d6dd..3ab3574 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 @@ -31,7 +31,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce .requestMatchers(HttpMethod.GET, "/businesses", "/businesses/**").permitAll() .requestMatchers(HttpMethod.GET, "/products", "/products/**").permitAll() // <-- NUEVO: Ver productos es público .requestMatchers(HttpMethod.POST, "/users").anonymous() - .requestMatchers(HttpMethod.POST, "/customers").anonymous() + .requestMatchers(HttpMethod.POST, "/customers").permitAll() // Endpoints Bloqueados Específicamente .requestMatchers(HttpMethod.POST, "/users/*").denyAll() diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java index 0328a83..9ae5c7d 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java @@ -25,6 +25,13 @@ public class Customer extends User { @NotEmpty private String phoneNumber; + // Asegurar que el ID sea el email + @Override + public void setEmail(String email) { + super.setEmail(email); + this.setId(email); + } + @Override @JsonValue(value = false) @JsonProperty(access = JsonProperty.Access.READ_ONLY) diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/CustomerEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/CustomerEventHandler.java index e339427..0d35ada 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/CustomerEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/CustomerEventHandler.java @@ -33,6 +33,10 @@ public CustomerEventHandler(CustomerRepository customerRepository) { @HandleBeforeCreate public void handleCustomerPreCreate(Customer customer) { logger.info("Before creating: {}", customer.toString()); + // Codificar password ANTES de crear + if (customer.getPassword() != null && !customer.getPassword().isEmpty()) { + customer.encodePassword(); + } } @HandleBeforeSave diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java new file mode 100644 index 0000000..b3d08ae --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java @@ -0,0 +1,94 @@ +package cat.udl.eps.softarch.demo.steps; + +import cat.udl.eps.softarch.demo.domain.Business; +import cat.udl.eps.softarch.demo.domain.Customer; +import cat.udl.eps.softarch.demo.domain.Loyalty; +import cat.udl.eps.softarch.demo.repository.BusinessRepository; +import cat.udl.eps.softarch.demo.repository.CustomerRepository; +import cat.udl.eps.softarch.demo.repository.LoyaltyRepository; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.When; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + +public class UpdateLoyaltyStepDefs { + + @Autowired + private StepDefs stepDefs; + + @Autowired + private LoyaltyRepository loyaltyRepository; + + @Autowired + private CustomerRepository customerRepository; + + @Autowired + private BusinessRepository businessRepository; + + private Long loyaltyId; + + @When("I update the loyalty for customer {string} and business {Long} to have {int} points") + public void iUpdateTheLoyaltyForCustomerAndBusinessToHavePoints( + String customerUsername, String businessId, Integer newPoints) throws Exception { + + Customer customer = customerRepository.findById(customerUsername).orElseThrow(); + Business business = businessRepository.findById(businessId).orElseThrow(); + + List loyalties = loyaltyRepository.findByCustomerAndBusiness(customer, business); + assertTrue(!loyalties.isEmpty(), "Loyalty should exist before updating"); + Loyalty loyalty = loyalties.get(0); + loyaltyId = loyalty.getId(); + + JSONObject loyaltyUpdate = new JSONObject(); + loyaltyUpdate.put("accumulatedPoints", newPoints); + + stepDefs.result = stepDefs.mockMvc.perform( + patch("/loyalties/{id}", loyaltyId) + .contentType(MediaType.APPLICATION_JSON) + .content(loyaltyUpdate.toString()) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } + + @When("I update the loyalty with id {long} to have {int} points") + public void iUpdateTheLoyaltyWithIdToHavePoints(Long id, Integer newPoints) throws Exception { + JSONObject loyaltyUpdate = new JSONObject(); + loyaltyUpdate.put("accumulatedPoints", newPoints); + + stepDefs.result = stepDefs.mockMvc.perform( + patch("/loyalties/{id}", id) + .contentType(MediaType.APPLICATION_JSON) + .content(loyaltyUpdate.toString()) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } + + @And("The loyalty has {int} accumulated points") + public void theLoyaltyHasAccumulatedPoints(Integer expectedPoints) throws Exception { + // Verificamos que el último resultado contenga los puntos esperados + stepDefs.result.andExpect(jsonPath("$.accumulatedPoints", is(expectedPoints))); + } + + @Given("There is no loyalty with id {long}") + public void thereIsNoLoyaltyWithId(Long id) { + assertFalse(loyaltyRepository.existsById(id), + "Loyalty with id \"" + id + "\" shouldn't exist"); + } +} diff --git a/src/test/resources/features/UpdateLoyalty.feature b/src/test/resources/features/UpdateLoyalty.feature new file mode 100644 index 0000000..843bdb9 --- /dev/null +++ b/src/test/resources/features/UpdateLoyalty.feature @@ -0,0 +1,27 @@ +Feature: Update Loyalty + In order to manage loyalty program progress + As a business + I want to update existing loyalty records + + Background: + Given There is a registered customer with username "customer1" and password "password" and email "customer1@example.com" + Given There is a registered business with id 1 and name "Test Business" and address "123 Main St" + And There is a loyalty for customer "customer1" and business 1 with 50 points + + + And I login as "customer1" with password "password" + And There is a loyalty for customer "customer1" and business 1 with 50 points + + Scenario: Update loyalty points successfully + When I update the loyalty for customer "customer1" and business 1 to have 120 points + Then The response code is 200 + And The loyalty has 120 accumulated points + + Scenario: Update loyalty points to a negative value fails + When I update the loyalty for customer "customer1" and business 1 to have -30 points + Then The response code is 400 + + Scenario: Update non-existent loyalty fails + Given There is no loyalty with id 999 + When I update the loyalty with id 999 to have 200 points + Then The response code is 404 From 1e818ca5e94a7ff5be04f77a3415c4c7c7586f0a Mon Sep 17 00:00:00 2001 From: Malena Date: Fri, 16 Jan 2026 11:58:38 +0100 Subject: [PATCH 3/8] Added Customer Controler --- .../demo/controller/CustomerController.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java b/src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java new file mode 100644 index 0000000..890e132 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java @@ -0,0 +1,25 @@ +package cat.udl.eps.softarch.demo.controller; + +import cat.udl.eps.softarch.demo.domain.Customer; +import cat.udl.eps.softarch.demo.repository.CustomerRepository; +import org.springframework.web.bind.annotation.*; + +import java.util.Set; + +@RestController +@RequestMapping("/customers") +public class CustomerController { + + private final CustomerRepository customerRepository; + + public CustomerController(CustomerRepository customerRepository) { + this.customerRepository = customerRepository; + } + + @PostMapping + public Customer register(@RequestBody Customer customer) { + customer.encodePassword(); // 🔐 CLAVE + customer.setRoles(Set.of("CUSTOMER")); // 🔑 CLAVE + return customerRepository.save(customer); + } +} From 6f384de91c99569264c9ed56ed4d54b38bcf82b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Sat, 17 Jan 2026 23:49:58 +0100 Subject: [PATCH 4/8] fix: remove necessary CustomerController There is already the CustomerRepository RepositoryRestResource --- .../demo/controller/CustomerController.java | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java b/src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java deleted file mode 100644 index 890e132..0000000 --- a/src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java +++ /dev/null @@ -1,25 +0,0 @@ -package cat.udl.eps.softarch.demo.controller; - -import cat.udl.eps.softarch.demo.domain.Customer; -import cat.udl.eps.softarch.demo.repository.CustomerRepository; -import org.springframework.web.bind.annotation.*; - -import java.util.Set; - -@RestController -@RequestMapping("/customers") -public class CustomerController { - - private final CustomerRepository customerRepository; - - public CustomerController(CustomerRepository customerRepository) { - this.customerRepository = customerRepository; - } - - @PostMapping - public Customer register(@RequestBody Customer customer) { - customer.encodePassword(); // 🔐 CLAVE - customer.setRoles(Set.of("CUSTOMER")); // 🔑 CLAVE - return customerRepository.save(customer); - } -} From 752580fd61d13d6cbd3364242b52983aa1bfebbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Sat, 17 Jan 2026 23:50:50 +0100 Subject: [PATCH 5/8] fix: use Id from extended user --- .../java/cat/udl/eps/softarch/demo/domain/Customer.java | 8 +------- .../java/cat/udl/eps/softarch/demo/domain/Loyalty.java | 3 +++ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java index 9ae5c7d..fa8ccbc 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Customer.java @@ -25,12 +25,6 @@ public class Customer extends User { @NotEmpty private String phoneNumber; - // Asegurar que el ID sea el email - @Override - public void setEmail(String email) { - super.setEmail(email); - this.setId(email); - } @Override @JsonValue(value = false) @@ -47,4 +41,4 @@ public Collection getAuthorities() { //TODO // @OneToOne // private Loyalty loyalty; -} \ No newline at end of file +} diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Loyalty.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Loyalty.java index f41eec0..194d6b4 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/domain/Loyalty.java +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Loyalty.java @@ -1,5 +1,6 @@ package cat.udl.eps.softarch.demo.domain; +import com.fasterxml.jackson.annotation.JsonIdentityReference; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.PositiveOrZero; @@ -25,10 +26,12 @@ public class Loyalty extends UriEntity { @NotNull @ManyToOne + @JsonIdentityReference(alwaysAsId = true) private Customer customer; @NotNull @ManyToOne + @JsonIdentityReference(alwaysAsId = true) private Business business; @NotNull From 34beada54aa88a40dcef1f85ab1f1263fcffb3d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Sat, 17 Jan 2026 23:52:24 +0100 Subject: [PATCH 6/8] refactor: serialise Loyalty from class instance --- .../softarch/demo/steps/CreateLoyaltyStepDefs.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateLoyaltyStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateLoyaltyStepDefs.java index 37c2cb5..90b3b61 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/CreateLoyaltyStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/CreateLoyaltyStepDefs.java @@ -92,19 +92,19 @@ public void thereIsALoyaltyForCustomerAndBusinessWithPoints( @When("I create a loyalty for customer {string} and business {string} with {int} points") public void iCreateALoyaltyForCustomerAndBusinessWithPoints( String customerUsername, String businessId, Integer points) throws Exception { - Customer customer = customerRepository.findById(customerUsername).orElseThrow(); + Customer customer = customerRepository.findByName(customerUsername).getFirst(); Business business = businessRepository.findById(businessId).orElseThrow(); - JSONObject loyalty = new JSONObject(); - loyalty.put("startDate", ZonedDateTime.now().toString()); - loyalty.put("accumulatedPoints", points); - loyalty.put("customer", "/customers/" + customerUsername); - loyalty.put("business", "/businesses/" + businessId); + Loyalty loyalty = new Loyalty(); + loyalty.setAccumulatedPoints(points); + loyalty.setCustomer(customer); + loyalty.setBusiness(business); + loyalty.setStartDate(ZonedDateTime.now()); stepDefs.result = stepDefs.mockMvc.perform( post("/loyalties") .contentType(MediaType.APPLICATION_JSON) - .content(loyalty.toString()) + .content(stepDefs.mapper.writeValueAsString(loyalty)) .characterEncoding(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON) .with(AuthenticationStepDefs.authenticate())) From 08329babf76be968ac3075292d7e0dcd3ac11d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Sat, 17 Jan 2026 23:52:56 +0100 Subject: [PATCH 7/8] fix: update loyalty tests --- .../demo/steps/UpdateLoyaltyStepDefs.java | 22 +++++-------------- .../resources/features/UpdateLoyalty.feature | 11 ++++------ 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java index b3d08ae..56042fc 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java @@ -40,16 +40,16 @@ public class UpdateLoyaltyStepDefs { private Long loyaltyId; - @When("I update the loyalty for customer {string} and business {Long} to have {int} points") + @When("I update the loyalty for customer {string} and business {string} to have {int} points") public void iUpdateTheLoyaltyForCustomerAndBusinessToHavePoints( - String customerUsername, String businessId, Integer newPoints) throws Exception { + String customerUsername, String businessUsername, Integer newPoints) throws Exception { Customer customer = customerRepository.findById(customerUsername).orElseThrow(); - Business business = businessRepository.findById(businessId).orElseThrow(); + Business business = businessRepository.findById(businessUsername).orElseThrow(); List loyalties = loyaltyRepository.findByCustomerAndBusiness(customer, business); - assertTrue(!loyalties.isEmpty(), "Loyalty should exist before updating"); - Loyalty loyalty = loyalties.get(0); + assertFalse(loyalties.isEmpty(), "Loyalty should exist before updating"); + Loyalty loyalty = loyalties.getFirst(); loyaltyId = loyalty.getId(); JSONObject loyaltyUpdate = new JSONObject(); @@ -79,16 +79,4 @@ public void iUpdateTheLoyaltyWithIdToHavePoints(Long id, Integer newPoints) thro .with(AuthenticationStepDefs.authenticate())) .andDo(print()); } - - @And("The loyalty has {int} accumulated points") - public void theLoyaltyHasAccumulatedPoints(Integer expectedPoints) throws Exception { - // Verificamos que el último resultado contenga los puntos esperados - stepDefs.result.andExpect(jsonPath("$.accumulatedPoints", is(expectedPoints))); - } - - @Given("There is no loyalty with id {long}") - public void thereIsNoLoyaltyWithId(Long id) { - assertFalse(loyaltyRepository.existsById(id), - "Loyalty with id \"" + id + "\" shouldn't exist"); - } } diff --git a/src/test/resources/features/UpdateLoyalty.feature b/src/test/resources/features/UpdateLoyalty.feature index 843bdb9..b831faa 100644 --- a/src/test/resources/features/UpdateLoyalty.feature +++ b/src/test/resources/features/UpdateLoyalty.feature @@ -5,20 +5,17 @@ Feature: Update Loyalty Background: Given There is a registered customer with username "customer1" and password "password" and email "customer1@example.com" - Given There is a registered business with id 1 and name "Test Business" and address "123 Main St" - And There is a loyalty for customer "customer1" and business 1 with 50 points - - + And There is a registered business with id "testbusiness" and name "Test Business" and address "123 Main St" + And There is a loyalty for customer "customer1" and business "testbusiness" with 50 points And I login as "customer1" with password "password" - And There is a loyalty for customer "customer1" and business 1 with 50 points Scenario: Update loyalty points successfully - When I update the loyalty for customer "customer1" and business 1 to have 120 points + When I update the loyalty for customer "customer1" and business "testbusiness" to have 120 points Then The response code is 200 And The loyalty has 120 accumulated points Scenario: Update loyalty points to a negative value fails - When I update the loyalty for customer "customer1" and business 1 to have -30 points + When I update the loyalty for customer "customer1" and business "testbusiness" to have -30 points Then The response code is 400 Scenario: Update non-existent loyalty fails From 75dcf7c008356be277eb259b42842cda84c4ef04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Garc=C3=ADa?= Date: Sat, 17 Jan 2026 23:58:20 +0100 Subject: [PATCH 8/8] fix: error code for repeated customer username is 409 --- src/test/resources/features/RegisterCustomer.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/features/RegisterCustomer.feature b/src/test/resources/features/RegisterCustomer.feature index 76612cd..0533420 100644 --- a/src/test/resources/features/RegisterCustomer.feature +++ b/src/test/resources/features/RegisterCustomer.feature @@ -14,7 +14,7 @@ Feature: Register Customer Given There is a registered customer with username "customer1" and password "password" and email "customer1@example.com" and phoneNumber "123456789" And I'm not logged in When I register a new customer with username "customer1" and password "newpass" and email "customer2@example.com" and phoneNumber "987654321" - Then The response code is 400 + Then The response code is 409 Scenario: Register customer without username Given I'm not logged in