mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 07:45:49 +00:00
Arreglos para user, cambio de urls y organizacion de jsp , y test de
controladores de speed y food Offers
This commit is contained in:
parent
bd58ac2abb
commit
205c9d9a71
18 changed files with 319 additions and 50 deletions
5
pom.xml
5
pom.xml
|
@ -69,6 +69,11 @@
|
|||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-taglibs</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
|
|
|
@ -44,12 +44,12 @@ 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("/clients/new").permitAll()
|
||||
.antMatchers("/offers/**").hasAnyAuthority("admin")
|
||||
.antMatchers("/offers/**/new").hasAnyAuthority("admin","client")
|
||||
.antMatchers("/offers/**/activate").hasAnyAuthority("admin","client")
|
||||
.antMatchers("/offers/**").permitAll()
|
||||
|
||||
.and().formLogin()
|
||||
.loginPage("/login").permitAll()
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
@Controller
|
||||
public class FoodOfferController {
|
||||
|
||||
private static final String VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM = "foodOffers/createOrUpdateFoodOfferForm";
|
||||
private static final String VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM = "offers/food/createOrUpdateFoodOfferForm";
|
||||
|
||||
private final FoodOfferService foodOfferService;
|
||||
private final ClientService clientService;
|
||||
|
@ -31,19 +31,14 @@ public class FoodOfferController {
|
|||
this.clientService = clientService;
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@GetMapping("/foodOffers/new")
|
||||
@GetMapping("/offers/food/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
FoodOffer foodOffer = new FoodOffer();
|
||||
model.put("foodOffer", foodOffer);
|
||||
return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/foodOffers/new")
|
||||
@PostMapping("/offers/food/new")
|
||||
public String processCreationForm(@Valid FoodOffer foodOffer, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
|
@ -53,11 +48,11 @@ public class FoodOfferController {
|
|||
foodOffer.setClient(client);
|
||||
foodOffer.setType(StatusOffer.hidden);
|
||||
this.foodOfferService.saveFoodOffer(foodOffer);
|
||||
return "redirect:/foodOffers/" + foodOffer.getId();
|
||||
return "redirect:/offers/food/" + foodOffer.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping(value = "/foodOffers/{foodOfferId}/activate")
|
||||
@GetMapping(value = "/offers/food/{foodOfferId}/activate")
|
||||
public String activateFoodOffer(@PathVariable("foodOfferId") final int foodOfferId, ModelMap modelMap) {
|
||||
FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId);
|
||||
Client client = this.clientService.getCurrentClient();
|
||||
|
@ -68,7 +63,7 @@ public class FoodOfferController {
|
|||
} else {
|
||||
modelMap.addAttribute("message", "You don't have access to this food offer");
|
||||
}
|
||||
return "redirect:/foodOffers/";
|
||||
return "redirect:/offers/food/" + foodOffer.getId();
|
||||
}
|
||||
@GetMapping("/offers/food/{foodOfferId}")
|
||||
public String processShowForm(@PathVariable("foodOfferId") int foodOfferId, Map<String, Object> model) {
|
||||
|
@ -77,7 +72,7 @@ public class FoodOfferController {
|
|||
|
||||
model.put("foodOffer", foodOffer);
|
||||
|
||||
return "foodOffers/foodOffersShow";
|
||||
return "offers/food/foodOffersShow";
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
@Controller
|
||||
public class SpeedOfferController {
|
||||
|
||||
private static final String VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM = "speedOffers/createOrUpdateSpeedOfferForm";
|
||||
private static final String VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM = "offers/speed/createOrUpdateSpeedOfferForm";
|
||||
|
||||
private final SpeedOfferService speedOfferService;
|
||||
private final ClientService clientService;
|
||||
|
@ -47,19 +47,14 @@ public class SpeedOfferController {
|
|||
this.clientService = clientService;
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
@GetMapping("/speedOffers/new")
|
||||
@GetMapping("/offers/speed/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
SpeedOffer speedOffer = new SpeedOffer();
|
||||
model.put("speedOffer", speedOffer);
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/speedOffers/new")
|
||||
@PostMapping("/offers/speed/new")
|
||||
public String processCreationForm(@Valid SpeedOffer speedOffer, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
|
@ -69,11 +64,11 @@ public class SpeedOfferController {
|
|||
speedOffer.setClient(client);
|
||||
speedOffer.setType(StatusOffer.hidden);
|
||||
this.speedOfferService.saveSpeedOffer(speedOffer);
|
||||
return "redirect:/speedOffers/" + speedOffer.getId();
|
||||
return "redirect:/offers/speed/" + speedOffer.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping(value = "/speedOffers/{speedOfferId}/activate")
|
||||
@GetMapping(value = "/offers/speed/{speedOfferId}/activate")
|
||||
public String activateSpeedOffer(@PathVariable("speedOfferId") final int speedOfferId, ModelMap modelMap) {
|
||||
SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId);
|
||||
Client client = this.clientService.getCurrentClient();
|
||||
|
@ -84,7 +79,7 @@ public class SpeedOfferController {
|
|||
} else {
|
||||
modelMap.addAttribute("message", "You don't have access to this speed offer");
|
||||
}
|
||||
return "redirect:/speedOffers/";
|
||||
return "redirect:/offers/speed/" + speedOffer.getId();
|
||||
}
|
||||
|
||||
@GetMapping("/offers/speed/{speedOfferId}")
|
||||
|
@ -92,6 +87,6 @@ public class SpeedOfferController {
|
|||
|
||||
SpeedOffer speedOffer=this.speedOfferService.findSpeedOfferById(speedOfferId);
|
||||
model.put("speedOffer", speedOffer);
|
||||
return "speedOffers/speedOffersShow";
|
||||
return "offers/speed/speedOffersShow";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,17 +9,17 @@ INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '
|
|||
INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435');
|
||||
INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487');
|
||||
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('user','admin','admin', TRUE );
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','admin','admin', TRUE );
|
||||
INSERT INTO authorities VALUES ('admin','admin');
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('user','manoli','manoli', TRUE );
|
||||
INSERT INTO authorities VALUES ('manoli','cliente');
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('user','david','david', TRUE );
|
||||
INSERT INTO authorities VALUES ('david','cliente');
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('user','paco','paco', TRUE );
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','manoli','manoli', TRUE );
|
||||
INSERT INTO authorities VALUES ('manoli','client');
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','david','david', TRUE );
|
||||
INSERT INTO authorities VALUES ('david','client');
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','paco','paco', TRUE );
|
||||
INSERT INTO authorities VALUES ('paco','usuario');
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('user','lolo','lolo', TRUE );
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','lolo','lolo', TRUE );
|
||||
INSERT INTO authorities VALUES ('lolo','usuario');
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('user','pepe','pepe', TRUE );
|
||||
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','pepe','pepe', TRUE );
|
||||
INSERT INTO authorities VALUES ('pepe','usuario');
|
||||
|
||||
INSERT INTO usuarios VALUES (1, 'admin', 'admin', 'admin', 'C/admin', '000000000', 'admin@gmail.com','admin');
|
||||
|
@ -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%');
|
||||
|
|
|
@ -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