Merge branch '011-pruebas-unitarias-publicar-ofertas-abel' into 011-pruebas-unitarias-publicar-ofertas-tibo

This commit is contained in:
abemorcardc 2021-03-28 00:54:39 +01:00 committed by GitHub
commit aa13d055f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 317 additions and 38 deletions

View file

@ -46,9 +46,10 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.antMatchers("/admin/**").hasAnyAuthority("admin")
.antMatchers("/owners/**").hasAnyAuthority("owner", "admin")
.antMatchers("/clients/new").permitAll()
.antMatchers("/offers/**/new").hasAnyAuthority("admin","client")
.antMatchers("/offers/**/new").hasAnyAuthority("admin", "client")
.antMatchers("/offers/**/activate").hasAnyAuthority("admin","client")
.antMatchers("/clients/new").permitAll()
.antMatchers("/offers/**").permitAll()
.and().formLogin()

View file

@ -2,7 +2,9 @@
package org.springframework.cheapy.web;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.StatusOffer;
@ -11,9 +13,7 @@ import org.springframework.cheapy.service.FoodOfferService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@ -63,7 +63,8 @@ public class FoodOfferController {
} else {
modelMap.addAttribute("message", "You don't have access to this food offer");
}
return "redirect:/offers/food/" + foodOffer.getId();
return "redirect:/offers/food/"+foodOfferId;
}
@GetMapping("/offers/food/{foodOfferId}")
public String processShowForm(@PathVariable("foodOfferId") int foodOfferId, Map<String, Object> model) {

View file

@ -12,9 +12,7 @@ import org.springframework.cheapy.service.NuOfferService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@ -22,7 +20,7 @@ import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class NuOfferController {
private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "nuOffers/createOrUpdateNuOfferForm";
private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "offers/nu/createOrUpdateNuOfferForm";
private final NuOfferService nuOfferService;
private final ClientService clientService;
@ -35,19 +33,16 @@ public class NuOfferController {
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@GetMapping("/nuOffers/new")
@GetMapping("/offers/nu/new")
public String initCreationForm(Map<String, Object> model) {
NuOffer nuOffer = new NuOffer();
model.put("nuOffer", nuOffer);
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
}
@PostMapping("/nuOffers/new")
@PostMapping("/offers/nu/new")
public String processCreationForm(@Valid NuOffer nuOffer, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
@ -61,10 +56,10 @@ public class NuOfferController {
this.nuOfferService.saveNuOffer(nuOffer);
return "redirect:/nuOffers/" + nuOffer.getId();
return "redirect:/offers/nu/"+nuOffer.getId();
}
}
@GetMapping(value ="/nuOffers/{nuOfferId}/activate")
@GetMapping(value ="/offers/nu/{nuOfferId}/activate")
public String activateNuOffer(@PathVariable("nuOfferId") final int nuOfferId, final ModelMap modelMap) {
Client client = this.clientService.getCurrentClient();
NuOffer nuOffer=this.nuOfferService.findNuOfferById(nuOfferId);
@ -73,11 +68,11 @@ public class NuOfferController {
nuOffer.setCode("NU-"+nuOfferId);
this.nuOfferService.saveNuOffer(nuOffer);
return "redirect:/nuOffers/" + nuOffer.getId();
} else {
modelMap.addAttribute("message", "You don't have access to this number offer");
}
return "redirect:/nuOffers/";
return "redirect:/offers/nu/"+nuOffer.getId();
}
@ -89,7 +84,7 @@ public class NuOfferController {
model.put("nuOffer", nuOffer);
return "nuOffers/nuOffersShow";
return "offers/nu/nuOffersShow";
}

View file

@ -28,9 +28,7 @@ import org.springframework.cheapy.service.SpeedOfferService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

View file

@ -1,19 +1,18 @@
package org.springframework.cheapy.web;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.TimeOfferService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@ -21,7 +20,7 @@ import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class TimeOfferController {
private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "timeOffers/createOrUpdateTimeOfferForm";
private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "offers/time/createOrUpdateTimeOfferForm";
private final TimeOfferService timeOfferService;
private final ClientService clientService;
@ -34,19 +33,15 @@ public class TimeOfferController {
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@GetMapping("/timeOffers/new")
@GetMapping("/offers/time/new")
public String initCreationForm(Map<String, Object> model) {
TimeOffer timeOffer = new TimeOffer();
model.put("timeOffer", timeOffer);
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
}
@PostMapping("/timeOffers/new")
@PostMapping("/offers/time/new")
public String processCreationForm(@Valid TimeOffer timeOffer, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
@ -60,10 +55,10 @@ public class TimeOfferController {
this.timeOfferService.saveTimeOffer(timeOffer);
return "redirect:/TimeOffers/" + timeOffer.getId();
return "redirect:/offers/time/" + timeOffer.getId();
}
}
@GetMapping(value ="/timeOffers/{timeOfferId}/activate")
@GetMapping(value ="/offers/time/{timeOfferId}/activate")
public String activateTimeOffer(@PathVariable("timeOfferId") final int timeOfferId, final ModelMap modelMap) {
Client client = this.clientService.getCurrentClient();
TimeOffer timeOffer=this.timeOfferService.findTimeOfferById(timeOfferId);
@ -72,11 +67,10 @@ public class TimeOfferController {
timeOffer.setCode("TI-"+timeOfferId);
this.timeOfferService.saveTimeOffer(timeOffer);
return "redirect:/timeOffers/" + timeOffer.getId();
} else {
modelMap.addAttribute("message", "You don't have access to this time offer");
}
return "redirect:/timeOffers/";
return "redirect:/offers/time/" + timeOffer.getId();
}
@ -88,7 +82,7 @@ public class TimeOfferController {
model.put("timeOffer", timeOffer);
return "timeOffers/timeOffersShow";
return "offers/time/timeOffersShow";
}

View file

@ -33,5 +33,5 @@ INSERT INTO clients VALUES (2,'david@gmail.com','C/Sevilla','09:30','22:00','608
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%');
INSERT INTO time_offers(start, end, code, type, client_id, init, finish, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'jkhlljk', 'active', null, '12:00:00', '13:00:00', '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', 'jkhlljk', 'active', null,5,'25%',10,'15%',15,'10%' );
INSERT INTO speed_offers(id,start, end, code, type, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES (3,'2021-06-15 12:00:00', '2021-06-16 12:00:00', 'jkhlljk', 'active', 1,5,'25%',10,'15%',15,'10%' );
INSERT INTO nu_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', 'jkhlljk', 'active', null,15,'25%',10,'15%',5,'10%' );

View file

@ -17,7 +17,8 @@
<petclinic:inputField label="Hora de inicio" name="init"/>
<petclinic:inputField label="Hora de final" name="finish"/>
<petclinic:inputField label="Decuento" name="discount"/>
<petclinic:inputField label="Descuento" name="discount"/>
</div>
<div class="form-group">

View file

@ -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.NuOffer;
import org.springframework.cheapy.model.User;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.NuOfferService;
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.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(value = NuOfferController.class,
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class),
excludeAutoConfiguration = SecurityConfiguration.class)
class NuOfferControllerTest {
private static final int TEST_CLIENT_ID = 1;
private static final int TEST_NUOFFER_ID = 1;
@Autowired
private MockMvc mockMvc;
@MockBean
private NuOfferService nuOfferService;
@MockBean
private ClientService clientService;
private NuOffer nu1;
@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);
NuOffer nu1test = new NuOffer();
nu1test.setId(TEST_NUOFFER_ID);
nu1test.setStart(LocalDateTime.of(2021, 12, 23, 12, 30));
nu1test.setEnd(LocalDateTime.of(2022, 12, 23, 12, 30));
nu1test.setGold(5);
nu1test.setDiscountGold("15%");
nu1test.setSilver(10);
nu1test.setDiscountGold("10%");
nu1test.setGold(15);
nu1test.setDiscountGold("5%");
nu1test.setClient(client1);
this.nu1 = nu1test;
BDDMockito.given(this.nuOfferService.findNuOfferById(TEST_NUOFFER_ID)).willReturn(this.nu1);
}
@WithMockUser(value = "spring", authorities = "client")
@Test
void testInitCreationForm() throws Exception {
mockMvc.perform(get("/offers/nu/new"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("nuOffer"))
.andExpect(view().name("offers/nu/createOrUpdateNuOfferForm"));
}
@WithMockUser(value = "spring", authorities = "client")
@Test
void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/offers/nu/new")
.with(SecurityMockMvcRequestPostProcessors.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/nu/new")
.with(csrf())
.param("start", "lsqdufhlqhf")
.param("end", "")
.param("gold", "gold")
.param("discountGold", "")
.param("silver", "")
.param("discountSilver", "")
.param("bronze", "")
.param("discountBronze", ""))
.andExpect(model().attributeHasErrors("nuOffer"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "start"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "end"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "gold"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "discountGold"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "silver"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "discountSilver"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "bronze"))
.andExpect(model().attributeHasFieldErrors("nuOffer", "discountBronze"))
.andExpect(view().name("offers/nu/createOrUpdateNuOfferForm"));
}
@WithMockUser(value = "user1", authorities = "client")
@Test
void testActivateSuccess() throws Exception {
mockMvc.perform(get("/offers/nu/{nuOfferId}/activate", TEST_NUOFFER_ID))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/offers/nu/"+TEST_NUOFFER_ID));
}
@WithMockUser(value = "user1", authorities = "client")
@Test
void testActivateHasErrors() throws Exception {
mockMvc.perform(get("/offers/nu/{nuOfferId}/activate", TEST_NUOFFER_ID+1))
.andExpect(view().name("exception"));
}
}

View file

@ -0,0 +1,140 @@
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 java.time.LocalTime;
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.TimeOffer;
import org.springframework.cheapy.model.User;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.TimeOfferService;
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.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(value = TimeOfferController.class,
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class),
excludeAutoConfiguration = SecurityConfiguration.class)
class TimeOfferControllerTest {
private static final int TEST_CLIENT_ID = 1;
private static final int TEST_TIMEOFFER_ID = 1;
@Autowired
private MockMvc mockMvc;
@MockBean
private TimeOfferService timeOfferService;
@MockBean
private ClientService clientService;
private TimeOffer time1;
@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);
TimeOffer time1test = new TimeOffer();
time1test.setId(TEST_TIMEOFFER_ID);
time1test.setStart(LocalDateTime.of(2021, 12, 23, 12, 30));
time1test.setEnd(LocalDateTime.of(2022, 12, 23, 12, 30));
time1test.setInit(LocalTime.of(12, 00));
time1test.setFinish(LocalTime.of(13, 00));
time1test.setDiscount("10");
time1test.setClient(client1);
this.time1 = time1test;
BDDMockito.given(this.timeOfferService.findTimeOfferById(TEST_TIMEOFFER_ID)).willReturn(this.time1);
}
@WithMockUser(value = "spring", authorities = "client")
@Test
void testInitCreationForm() throws Exception {
mockMvc.perform(get("/offers/time/new"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("timeOffer"))
.andExpect(view().name("offers/time/createOrUpdateTimeOfferForm"));
}
@WithMockUser(value = "spring", authorities = "client")
@Test
void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/offers/time/new")
.with(SecurityMockMvcRequestPostProcessors.csrf())
.param("start", "23/12/2021 12:30")
.param("end", "23/12/2022 12:30")
.param("init", "12:30")
.param("finish", "13:30")
.param("discount", "10"))
.andExpect(status().is3xxRedirection());
}
@WithMockUser(value = "spring", authorities = "client")
@Test
void testProcessCreationFormHasErrors() throws Exception {
mockMvc.perform(post("/offers/time/new")
.with(csrf())
.param("start", "lsqdufhlqhf")
.param("end", "")
.param("init", "gold")
.param("finish", "")
.param("discount", ""))
.andExpect(model().attributeHasErrors("timeOffer"))
.andExpect(model().attributeHasFieldErrors("timeOffer", "start"))
.andExpect(model().attributeHasFieldErrors("timeOffer", "end"))
.andExpect(model().attributeHasFieldErrors("timeOffer", "init"))
.andExpect(model().attributeHasFieldErrors("timeOffer", "finish"))
.andExpect(model().attributeHasFieldErrors("timeOffer", "discount"))
.andExpect(view().name("offers/time/createOrUpdateTimeOfferForm"));
}
@WithMockUser(value = "user1", authorities = "client")
@Test
void testActivateSuccess() throws Exception {
mockMvc.perform(get("/offers/time/{timeOfferId}/activate", TEST_TIMEOFFER_ID))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/offers/time/"+TEST_TIMEOFFER_ID));
}
@WithMockUser(value = "user1", authorities = "client")
@Test
void testActivateHasErrors() throws Exception {
mockMvc.perform(get("/offers/time/{timeOfferId}/activate", TEST_TIMEOFFER_ID+1))
.andExpect(view().name("exception"));
}
}