Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.POST, "/users").anonymous()
.requestMatchers(HttpMethod.POST, "/users/*").denyAll()
.requestMatchers(HttpMethod.POST, "/*").authenticated()
.requestMatchers(HttpMethod.POST, "/*/*").authenticated()
.requestMatchers(HttpMethod.PUT, "/*/*").authenticated()
.requestMatchers(HttpMethod.PATCH, "/*/*").authenticated()
.requestMatchers(HttpMethod.DELETE, "/*/*").authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Product extends UriEntity<Long>{
private Long id;

@NotEmpty
@Column(unique = true, nullable = false)
private String name;

@Column(length = 100)
Expand Down
42 changes: 30 additions & 12 deletions src/test/java/cat/udl/eps/softarch/demo/steps/ProductStepDefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import cat.udl.eps.softarch.demo.domain.Product;
import cat.udl.eps.softarch.demo.repository.ProductRepository;
import org.springframework.http.MediaType;

import static org.hamcrest.Matchers.hasSize;
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 io.cucumber.java.Before;
import io.cucumber.java.en.And;
Expand All @@ -20,8 +23,6 @@ public class ProductStepDefs {
@Autowired
private StepDefs stepDefs;

public static String currentUsername;
public static String currentPassword;
public static Product currentProduct;

@Autowired
Expand All @@ -37,23 +38,40 @@ public void iRegisterANewProductWithName(String name) throws Exception {
currentProduct = new Product();
currentProduct.setName(name);

stepDefs.result = stepDefs.mockMvc.perform(
post("/products")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(currentProduct))
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate())
).andDo(print());
// Crear el request builder base
var requestBuilder = post("/products")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(currentProduct))
.accept(MediaType.APPLICATION_JSON);

// Solo añadir autenticación si hay un usuario logueado
if (AuthenticationStepDefs.currentUsername != null) {
requestBuilder = requestBuilder.with(AuthenticationStepDefs.authenticate());
}

stepDefs.result = stepDefs.mockMvc.perform(requestBuilder)
.andDo(print());
}

@And("^The product with name \"([^\"]*)\" is registered$")
public void theProductWithNameIsRegistered(String productName) throws Exception {

if (productRepository.findByName(productName).isEmpty()) {
Product product = new Product();
product.setName(productName);
productRepository.save(product);
}

}

@And("^The product with name \"([^\"]*)\" is not registered$")
public void theProductWithNameIsNotRegistered(String productName) throws Throwable {
public void theProductWithNameIsNotRegistered(String productName) throws Exception {
stepDefs.result = stepDefs.mockMvc.perform(
get("/products")
get("/products/search/findByName")
.param("name", productName)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andExpect(status().isNotFound());
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.products", hasSize(0))); // Solo 1
}
}
40 changes: 20 additions & 20 deletions src/test/resources/features/RegisterProduct.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ Feature: Register Product

Scenario: Register a new product successfully
Given I login as "demo" with password "password"
When I register a new product with name "Coffee"
When I register a new product with name "Orange"
Then The response code is 201


# Scenario: Register a new product that already exists
# Given I login as "demo" with password "password"
# When I register a new product with name "Coffee"
# Then The response code is 409
# And The product with name "Orange" is not registered
#
#
# Scenario: Register a product when not authenticated
# Given I'm not logged in
# When I register a new product with name "Apple"
# Then The response code is 403
# And The product with name "Apple" is not registered
#
#
# Scenario: Register a product with empty name
# Given I login as "demo" with password "password"
# When I register a new product with name ""
# Then The response code is 400
# And The product with name "" is not registered
Scenario: Register a new product that already exists
Given I login as "demo" with password "password"
And The product with name "Orange" is registered
When I register a new product with name "Orange"
Then The response code is 409


Scenario: Register a product when not authenticated
Given I'm not logged in
When I register a new product with name "Apple"
Then The response code is 401
And The product with name "Apple" is not registered


Scenario: Register a product with empty name
Given I login as "demo" with password "password"
When I register a new product with name ""
Then The response code is 400
And The product with name "" is not registered


## TODO check user roles
Expand Down