Merge pull request #40 from cheapy-ispp/011-pruebas-unitarias-publicar-ofertas-abel

011 pruebas unitarias publicar ofertas
This commit is contained in:
abemorcardc 2021-03-28 17:50:23 +02:00 committed by GitHub
commit f7f9e000f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 630 additions and 91 deletions

View file

@ -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>

View file

@ -36,21 +36,19 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.antMatchers(HttpMethod.GET, "/", "/oups").permitAll()
.antMatchers("/users/new").permitAll()
.antMatchers("/offers/nu/**").hasAnyAuthority("admin","cliente")
.antMatchers("/offers/time/**").hasAnyAuthority("admin","cliente")
.antMatchers("/login/**").anonymous()
.antMatchers("/logout").permitAll()
.antMatchers("/usuarios/new").permitAll()
.antMatchers("/admin/**").hasAnyAuthority("admin")
.antMatchers("/offers/speed/**").hasAnyAuthority("admin", "cliente")
.antMatchers("/offers/food/**").hasAnyAuthority("admin", "cliente")
.antMatchers("/owners/**").hasAnyAuthority("owner", "admin")
.antMatchers("/clients/new").permitAll()
.antMatchers("/offers").permitAll()
.antMatchers("/offers/**/new").hasAnyAuthority("admin", "client")
.antMatchers("/offers/**/activate").hasAnyAuthority("admin","client")
.antMatchers("/clients/new").permitAll()
.antMatchers("/offers/**").permitAll()
.and().formLogin()

View file

@ -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;

View file

@ -3,7 +3,9 @@ package org.springframework.cheapy.web;
import java.time.format.DateTimeFormatter;
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;
@ -19,7 +21,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;
@ -29,25 +31,15 @@ public class FoodOfferController {
this.clientService = clientService;
}
/*private boolean checkIdentity(final int foodOfferId) {
boolean res = false;
Client client = this.clientService.getCurrentClient();
FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId);
Client clientOffer = foodOffer.getClient();
if (client.equals(clientOffer)) {
res = true;
}
return res;
}*/
@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;
@ -59,8 +51,8 @@ public class FoodOfferController {
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();
@ -71,7 +63,8 @@ public class FoodOfferController {
} else {
modelMap.addAttribute("message", "You don't have access to this food offer");
}
return "redirect:/foodOffers/";
return "redirect:/offers/food/"+foodOfferId;
}
@GetMapping("/offers/food/{foodOfferId}")
@ -84,7 +77,7 @@ public class FoodOfferController {
model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
return "foodOffers/foodOffersShow";
return "offers/food/foodOffersShow";
}

View file

@ -22,7 +22,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;
@ -31,27 +31,16 @@ public class NuOfferController {
this.nuOfferService = nuOfferService;
this.clientService = clientService;
}
/*private boolean checkIdentity(final int nuOfferId) {
boolean res = false;
Client client = this.clientService.getCurrentClient();
NuOffer nuOffer = this.nuOfferService.findNuOfferById(nuOfferId);
Client clientOffer = nuOffer.getClient();
if (client.equals(clientOffer)) {
res = true;
}
return res;
}*/
@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;
@ -63,11 +52,11 @@ public class NuOfferController {
nuOffer.setClient(client);
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);
@ -75,12 +64,11 @@ public class NuOfferController {
nuOffer.setStatus(StatusOffer.active);
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:/nuOffers/"+ nuOffer.getId();
}
@ -88,8 +76,9 @@ public class NuOfferController {
public String processShowForm(@PathVariable("nuOfferId") int nuOfferId, Map<String, Object> model) {
NuOffer nuOffer = this.nuOfferService.findNuOfferById(nuOfferId);
model.put("nuOffer", nuOffer);
model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
return "nuOffers/nuOffersShow";
return "offers/nu/nuOffersShow";
}

View file

@ -20,7 +20,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;
@ -29,26 +29,16 @@ public class SpeedOfferController {
this.speedOfferService = speedOfferService;
this.clientService = clientService;
}
/*private boolean checkIdentity(final int speedOfferId) {
boolean res = false;
Client client = this.clientService.getCurrentClient();
SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId);
Client clientOffer = speedOffer.getClient();
if (client.equals(clientOffer)) {
res = true;
}
return res;
}*/
@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;
@ -57,11 +47,12 @@ public class SpeedOfferController {
speedOffer.setClient(client);
speedOffer.setStatus(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();
@ -72,7 +63,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}")
@ -80,8 +71,9 @@ public class SpeedOfferController {
SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId);
model.put("speedOffer", speedOffer);
model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
return "speedOffers/speedOffersShow";
return "offers/speed/speedOffersShow";
}
@GetMapping(value = "/offers/speed/{speedOfferId}/edit")

View file

@ -21,8 +21,8 @@ import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class TimeOfferController {
private static final String VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM = "timeOffers/createOrUpdateTimeOfferForm";
private static final String VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM = "offers/time/createOrUpdateTimeOfferForm";
private final TimeOfferService timeOfferService;
private final ClientService clientService;
@ -30,27 +30,15 @@ public class TimeOfferController {
this.timeOfferService = timeOfferService;
this.clientService = clientService;
}
/*private boolean checkIdentity(final int timeOfferId) {
boolean res = false;
Client client = this.clientService.getCurrentClient();
TimeOffer timeOffer = this.timeOfferService.findTimeOfferById(timeOfferId);
Client clientOffer = timeOffer.getClient();
if (client.equals(clientOffer)) {
res = true;
}
return res;
}
*/
@GetMapping("/timeOffers/new")
@GetMapping("/offers/time/new")
public String initCreationForm(Map<String, Object> model) {
TimeOffer timeOffer = new TimeOffer();
model.put("timeOffer", timeOffer);
return VIEWS_TIME_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_TIME_OFFER_CREATE_OR_UPDATE_FORM;
@ -62,11 +50,11 @@ public class TimeOfferController {
timeOffer.setClient(client);
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);
@ -75,11 +63,12 @@ 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:/timeOffers/" + timeOffer.getId();
}
@ -92,7 +81,7 @@ public class TimeOfferController {
model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
return "timeOffers/timeOffersShow";
return "offers/time/timeOffersShow";
}

View file

@ -12,9 +12,11 @@ INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Wa
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 authorities VALUES ('manoli','client');
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','david','david', TRUE );
INSERT INTO authorities VALUES ('david','cliente');
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 );

View file

@ -20,7 +20,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,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"));
}
}

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,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"));
}
}

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"));
}
}