mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 07:45:49 +00:00
Merge pull request #36 from cheapy-ispp/011-pruebas-unitarias-publicar-ofertas-tibo
Arreglos para user, cambio de urls y organizacion de jsp , y test de controladores de speed y food Offers
This commit is contained in:
commit
f92e4f171c
10 changed files with 294 additions and 21 deletions
6
pom.xml
6
pom.xml
|
@ -70,9 +70,9 @@
|
|||
<artifactId>spring-security-taglibs</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -44,8 +44,6 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||
|
||||
.antMatchers("/usuarios/new").permitAll()
|
||||
.antMatchers("/admin/**").hasAnyAuthority("admin")
|
||||
.antMatchers("/speedOffers/**").hasAnyAuthority("admin", "client")
|
||||
.antMatchers("/foodOffers/**").hasAnyAuthority("admin", "client")
|
||||
.antMatchers("/owners/**").hasAnyAuthority("owner", "admin")
|
||||
|
||||
.antMatchers("/offers/**/new").hasAnyAuthority("admin", "client")
|
||||
|
|
|
@ -29,10 +29,7 @@ import org.springframework.format.annotation.DateTimeFormat;
|
|||
|
||||
@MappedSuperclass
|
||||
public class Offer extends BaseEntity {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
//Clase padre
|
||||
|
@ -46,7 +43,6 @@ public class Offer extends BaseEntity {
|
|||
@Future
|
||||
private LocalDateTime end;
|
||||
|
||||
|
||||
private String code;
|
||||
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
|
|
|
@ -10,9 +10,6 @@ import javax.validation.constraints.NotNull;
|
|||
@Table(name = "speed_offers")
|
||||
public class SpeedOffer extends Offer {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotNull
|
||||
|
|
|
@ -7,7 +7,6 @@ import javax.validation.constraints.NotBlank;
|
|||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
//@MappedSuperclass
|
||||
public class User{
|
||||
|
||||
@Id
|
||||
|
@ -18,10 +17,6 @@ public class User{
|
|||
|
||||
boolean enabled;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
|
|
|
@ -31,7 +31,6 @@ public class FoodOfferController {
|
|||
this.clientService = clientService;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/offers/food/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
FoodOffer foodOffer = new FoodOffer();
|
||||
|
@ -65,6 +64,7 @@ public class FoodOfferController {
|
|||
modelMap.addAttribute("message", "You don't have access to this food offer");
|
||||
}
|
||||
return "redirect:/offers/food/"+foodOfferId;
|
||||
|
||||
}
|
||||
@GetMapping("/offers/food/{foodOfferId}")
|
||||
public String processShowForm(@PathVariable("foodOfferId") int foodOfferId, Map<String, Object> model) {
|
||||
|
|
|
@ -27,8 +27,8 @@ INSERT INTO usuarios VALUES (2, 'Paco', 'Naranjo', '21154416G', 'C/Esperanza', '
|
|||
INSERT INTO usuarios VALUES (3, 'Lolo', 'Lopez', '25486596L', 'C/Macarena', '690670547' ,'Lolo@gmail.com','lolo');
|
||||
INSERT INTO usuarios VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690670547', 'Pepe@gmail.com','pepe');
|
||||
|
||||
INSERT INTO clients VALUES (1,'manoli@gmail.com','C/Betis','10:00','22:00','608726190', 'description 1', 'code1', 'ESPAÑOLA','manoli');
|
||||
INSERT INTO clients VALUES (2,'david@gmail.com','C/Sevilla','09:30','22:00','608726190', 'description 2', 'code2', 'americana','david');
|
||||
INSERT INTO clients VALUES (1,'manoli@gmail.com','C/Betis','10:00','22:00','608726190', 'description 1', 'code1', 'ESPAÑOLA','manoli');
|
||||
INSERT INTO clients VALUES (2,'david@gmail.com','C/Sevilla','09:30','22:00','608726190', 'description 2', 'code2', 'americana','david');
|
||||
|
||||
INSERT INTO food_offers(start, end, code, type, client_id, food, discount, units) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FO-1', 'active', null, 'macarrones', '15%', 10);
|
||||
INSERT INTO speed_offers(start, end, code, type, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'SP-1', 'active', null, 5, '15%', 10, '10%', 15, '5%');
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
<petclinic:inputField label="Hora de final" name="finish"/>
|
||||
<petclinic:inputField label="Descuento" name="discount"/>
|
||||
|
||||
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
|
|
|
@ -0,0 +1,137 @@
|
|||
package org.springframework.cheapy.web;
|
||||
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||
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.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.BDDMockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cheapy.configuration.SecurityConfiguration;
|
||||
import org.springframework.cheapy.model.Client;
|
||||
import org.springframework.cheapy.model.FoodOffer;
|
||||
import org.springframework.cheapy.model.User;
|
||||
import org.springframework.cheapy.service.ClientService;
|
||||
import org.springframework.cheapy.service.FoodOfferService;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
|
||||
|
||||
@WebMvcTest(value = FoodOfferController.class,
|
||||
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class),
|
||||
excludeAutoConfiguration = SecurityConfiguration.class)
|
||||
class FoodOfferControllerTest {
|
||||
|
||||
private static final int TEST_CLIENT_ID = 1;
|
||||
private static final int TEST_FOODOFFER_ID = 1;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private FoodOfferService foodOfferService;
|
||||
|
||||
@MockBean
|
||||
private ClientService clientService;
|
||||
|
||||
private FoodOffer fo1;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
User user1 = new User();
|
||||
user1.setUsername("user1");
|
||||
user1.setPassword("user1");
|
||||
Client client1 = new Client();
|
||||
client1.setId(TEST_CLIENT_ID);
|
||||
client1.setEmail("client1");
|
||||
client1.setAddress("client1");
|
||||
client1.setInit("01:00");
|
||||
client1.setFinish("01:01");
|
||||
client1.setTelephone("123456789");
|
||||
client1.setDescription("client1");
|
||||
client1.setCode("client1");
|
||||
client1.setFood("client1");
|
||||
client1.setUsername(user1);
|
||||
BDDMockito.given(this.clientService.getCurrentClient()).willReturn(client1);
|
||||
|
||||
FoodOffer fo1test = new FoodOffer();
|
||||
fo1test.setId(TEST_FOODOFFER_ID);
|
||||
fo1test.setStart(LocalDateTime.of(2021, 12, 23, 12, 30));
|
||||
fo1test.setEnd(LocalDateTime.of(2022, 12, 23, 12, 30));
|
||||
fo1test.setFood("fo1test");
|
||||
fo1test.setDiscount("fo1test");
|
||||
fo1test.setUnits(1);
|
||||
fo1test.setClient(client1);
|
||||
this.fo1 = fo1test;
|
||||
BDDMockito.given(this.foodOfferService.findFoodOfferById(TEST_FOODOFFER_ID)).willReturn(this.fo1);
|
||||
|
||||
}
|
||||
|
||||
@WithMockUser(value = "spring", authorities = "client")
|
||||
@Test
|
||||
void testInitCreationForm() throws Exception {
|
||||
mockMvc.perform(get("/offers/food/new"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(model().attributeExists("foodOffer"))
|
||||
.andExpect(view().name("offers/food/createOrUpdateFoodOfferForm"));
|
||||
}
|
||||
|
||||
@WithMockUser(value = "spring", authorities = "client")
|
||||
@Test
|
||||
void testProcessCreationFormSuccess() throws Exception {
|
||||
mockMvc.perform(post("/offers/food/new")
|
||||
.with(csrf())
|
||||
.param("start", "23/12/2021 12:30")
|
||||
.param("end", "23/12/2022 12:30")
|
||||
.param("food", "food")
|
||||
.param("discount", "10%")
|
||||
.param("units", "1"))
|
||||
.andExpect(status().is3xxRedirection());
|
||||
}
|
||||
|
||||
@WithMockUser(value = "spring", authorities = "client")
|
||||
@Test
|
||||
void testProcessCreationFormHasErrors() throws Exception {
|
||||
mockMvc.perform(post("/offers/food/new")
|
||||
.with(csrf())
|
||||
.param("start", "lsqdufhlqhf")
|
||||
.param("end", "")
|
||||
.param("food", "")
|
||||
.param("discount", "")
|
||||
.param("units", "qsdfy"))
|
||||
.andExpect(model().attributeHasErrors("foodOffer"))
|
||||
.andExpect(model().attributeHasFieldErrors("foodOffer", "start"))
|
||||
.andExpect(model().attributeHasFieldErrors("foodOffer", "end"))
|
||||
.andExpect(model().attributeHasFieldErrors("foodOffer", "food"))
|
||||
.andExpect(model().attributeHasFieldErrors("foodOffer", "discount"))
|
||||
.andExpect(model().attributeHasFieldErrors("foodOffer", "units"))
|
||||
.andExpect(view().name("offers/food/createOrUpdateFoodOfferForm"));
|
||||
}
|
||||
|
||||
@WithMockUser(value = "user1", authorities = "client")
|
||||
@Test
|
||||
void testActivateSuccess() throws Exception {
|
||||
mockMvc.perform(get("/offers/food/{foodOfferId}/activate", TEST_FOODOFFER_ID))
|
||||
.andExpect(status().is3xxRedirection())
|
||||
.andExpect(view().name("redirect:/offers/food/"+TEST_FOODOFFER_ID));
|
||||
}
|
||||
|
||||
@WithMockUser(value = "user1", authorities = "client")
|
||||
@Test
|
||||
void testActivateHasErrors() throws Exception {
|
||||
mockMvc.perform(get("/offers/food/{foodOfferId}/activate", TEST_FOODOFFER_ID+1))
|
||||
.andExpect(view().name("exception"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
package org.springframework.cheapy.web;
|
||||
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||
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.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.BDDMockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cheapy.configuration.SecurityConfiguration;
|
||||
import org.springframework.cheapy.model.Client;
|
||||
import org.springframework.cheapy.model.SpeedOffer;
|
||||
import org.springframework.cheapy.model.User;
|
||||
import org.springframework.cheapy.service.ClientService;
|
||||
import org.springframework.cheapy.service.SpeedOfferService;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
|
||||
|
||||
@WebMvcTest(value = SpeedOfferController.class,
|
||||
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class),
|
||||
excludeAutoConfiguration = SecurityConfiguration.class)
|
||||
class SpeedOfferControllerTest {
|
||||
|
||||
private static final int TEST_CLIENT_ID = 1;
|
||||
private static final int TEST_SPEEDOFFER_ID = 1;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private SpeedOfferService speedOfferService;
|
||||
|
||||
@MockBean
|
||||
private ClientService clientService;
|
||||
|
||||
private SpeedOffer sp1;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
User user1 = new User();
|
||||
user1.setUsername("user1");
|
||||
user1.setPassword("user1");
|
||||
Client client1 = new Client();
|
||||
client1.setId(TEST_CLIENT_ID);
|
||||
client1.setEmail("client1");
|
||||
client1.setAddress("client1");
|
||||
client1.setInit("01:00");
|
||||
client1.setFinish("01:01");
|
||||
client1.setTelephone("123456789");
|
||||
client1.setDescription("client1");
|
||||
client1.setCode("client1");
|
||||
client1.setFood("client1");
|
||||
client1.setUsername(user1);
|
||||
BDDMockito.given(this.clientService.getCurrentClient()).willReturn(client1);
|
||||
|
||||
SpeedOffer sp1test = new SpeedOffer();
|
||||
sp1test.setId(TEST_SPEEDOFFER_ID);
|
||||
sp1test.setStart(LocalDateTime.of(2021, 12, 23, 12, 30));
|
||||
sp1test.setEnd(LocalDateTime.of(2022, 12, 23, 12, 30));
|
||||
sp1test.setGold(5);
|
||||
sp1test.setDiscountGold("15%");
|
||||
sp1test.setSilver(10);
|
||||
sp1test.setDiscountGold("10%");
|
||||
sp1test.setGold(15);
|
||||
sp1test.setDiscountGold("5%");
|
||||
sp1test.setClient(client1);
|
||||
this.sp1 = sp1test;
|
||||
BDDMockito.given(this.speedOfferService.findSpeedOfferById(TEST_SPEEDOFFER_ID)).willReturn(this.sp1);
|
||||
|
||||
}
|
||||
|
||||
@WithMockUser(value = "spring", authorities = "client")
|
||||
@Test
|
||||
void testInitCreationForm() throws Exception {
|
||||
mockMvc.perform(get("/offers/speed/new"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(model().attributeExists("speedOffer"))
|
||||
.andExpect(view().name("offers/speed/createOrUpdateSpeedOfferForm"));
|
||||
}
|
||||
|
||||
@WithMockUser(value = "spring", authorities = "client")
|
||||
@Test
|
||||
void testProcessCreationFormSuccess() throws Exception {
|
||||
mockMvc.perform(post("/offers/speed/new")
|
||||
.with(csrf())
|
||||
.param("start", "23/12/2021 12:30")
|
||||
.param("end", "23/12/2022 12:30")
|
||||
.param("gold", "5")
|
||||
.param("discountGold", "15%")
|
||||
.param("silver", "10")
|
||||
.param("discountSilver", "10%")
|
||||
.param("bronze", "15")
|
||||
.param("discountBronze", "5%"))
|
||||
.andExpect(status().is3xxRedirection());
|
||||
}
|
||||
|
||||
@WithMockUser(value = "spring", authorities = "client")
|
||||
@Test
|
||||
void testProcessCreationFormHasErrors() throws Exception {
|
||||
mockMvc.perform(post("/offers/speed/new")
|
||||
.with(csrf())
|
||||
.param("start", "lsqdufhlqhf")
|
||||
.param("end", "")
|
||||
.param("gold", "gold")
|
||||
.param("discountGold", "")
|
||||
.param("silver", "")
|
||||
.param("discountSilver", "")
|
||||
.param("bronze", "")
|
||||
.param("discountBronze", ""))
|
||||
.andExpect(model().attributeHasErrors("speedOffer"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "start"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "end"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "gold"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "discountGold"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "silver"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "discountSilver"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "bronze"))
|
||||
.andExpect(model().attributeHasFieldErrors("speedOffer", "discountBronze"))
|
||||
.andExpect(view().name("offers/speed/createOrUpdateSpeedOfferForm"));
|
||||
}
|
||||
|
||||
@WithMockUser(value = "user1", authorities = "client")
|
||||
@Test
|
||||
void testActivateSuccess() throws Exception {
|
||||
mockMvc.perform(get("/offers/speed/{speedOfferId}/activate", TEST_SPEEDOFFER_ID))
|
||||
.andExpect(status().is3xxRedirection())
|
||||
.andExpect(view().name("redirect:/offers/speed/"+TEST_SPEEDOFFER_ID));
|
||||
}
|
||||
|
||||
@WithMockUser(value = "user1", authorities = "client")
|
||||
@Test
|
||||
void testActivateHasErrors() throws Exception {
|
||||
mockMvc.perform(get("/offers/speed/{speedOfferId}/activate", TEST_SPEEDOFFER_ID+1))
|
||||
.andExpect(view().name("exception"));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue