diff --git a/pom.xml b/pom.xml index 38e0cd40b..c0ab78aad 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,11 @@ org.springframework.security spring-security-taglibs + + org.springframework.security + spring-security-test + test + org.springframework.boot spring-boot-starter-security diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 9435b1b30..7189740f2 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -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() diff --git a/src/main/java/org/springframework/cheapy/model/User.java b/src/main/java/org/springframework/cheapy/model/User.java index bd5b2dd30..e9c232562 100644 --- a/src/main/java/org/springframework/cheapy/model/User.java +++ b/src/main/java/org/springframework/cheapy/model/User.java @@ -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; diff --git a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java index c11296573..b2870d93b 100644 --- a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java @@ -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 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"; } diff --git a/src/main/java/org/springframework/cheapy/web/NuOfferController.java b/src/main/java/org/springframework/cheapy/web/NuOfferController.java index 730eb5635..979f912b3 100644 --- a/src/main/java/org/springframework/cheapy/web/NuOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/NuOfferController.java @@ -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 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 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"; } diff --git a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java index aa9236f82..2162e8558 100644 --- a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java @@ -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 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") diff --git a/src/main/java/org/springframework/cheapy/web/TimeOfferController.java b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java index c6d0f0430..1429535ad 100644 --- a/src/main/java/org/springframework/cheapy/web/TimeOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/TimeOfferController.java @@ -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 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"; } diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index ce81f2d0c..baa9d8384 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -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 ); diff --git a/src/main/webapp/WEB-INF/jsp/foodOffers/createOrUpdateFoodOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/offers/food/createOrUpdateFoodOfferForm.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/foodOffers/createOrUpdateFoodOfferForm.jsp rename to src/main/webapp/WEB-INF/jsp/offers/food/createOrUpdateFoodOfferForm.jsp diff --git a/src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersDisable.jsp b/src/main/webapp/WEB-INF/jsp/offers/food/foodOffersDisable.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersDisable.jsp rename to src/main/webapp/WEB-INF/jsp/offers/food/foodOffersDisable.jsp diff --git a/src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/food/foodOffersShow.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/foodOffers/foodOffersShow.jsp rename to src/main/webapp/WEB-INF/jsp/offers/food/foodOffersShow.jsp diff --git a/src/main/webapp/WEB-INF/jsp/nuOffers/createOrUpdateNuOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/offers/nu/createOrUpdateNuOfferForm.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/nuOffers/createOrUpdateNuOfferForm.jsp rename to src/main/webapp/WEB-INF/jsp/offers/nu/createOrUpdateNuOfferForm.jsp diff --git a/src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersDisable.jsp b/src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersDisable.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersDisable.jsp rename to src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersDisable.jsp diff --git a/src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersShow.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/nuOffers/nuOffersShow.jsp rename to src/main/webapp/WEB-INF/jsp/offers/nu/nuOffersShow.jsp diff --git a/src/main/webapp/WEB-INF/jsp/speedOffers/createOrUpdateSpeedOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/offers/speed/createOrUpdateSpeedOfferForm.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/speedOffers/createOrUpdateSpeedOfferForm.jsp rename to src/main/webapp/WEB-INF/jsp/offers/speed/createOrUpdateSpeedOfferForm.jsp diff --git a/src/main/webapp/WEB-INF/jsp/speedOffers/speedOffersDisable.jsp b/src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersDisable.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/speedOffers/speedOffersDisable.jsp rename to src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersDisable.jsp diff --git a/src/main/webapp/WEB-INF/jsp/speedOffers/speedOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersShow.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/speedOffers/speedOffersShow.jsp rename to src/main/webapp/WEB-INF/jsp/offers/speed/speedOffersShow.jsp diff --git a/src/main/webapp/WEB-INF/jsp/timeOffers/createOrUpdateTimeOfferForm.jsp b/src/main/webapp/WEB-INF/jsp/offers/time/createOrUpdateTimeOfferForm.jsp similarity index 96% rename from src/main/webapp/WEB-INF/jsp/timeOffers/createOrUpdateTimeOfferForm.jsp rename to src/main/webapp/WEB-INF/jsp/offers/time/createOrUpdateTimeOfferForm.jsp index e483ed81b..cd12ad6fc 100644 --- a/src/main/webapp/WEB-INF/jsp/timeOffers/createOrUpdateTimeOfferForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/offers/time/createOrUpdateTimeOfferForm.jsp @@ -20,7 +20,8 @@ - + +
diff --git a/src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersDisable.jsp b/src/main/webapp/WEB-INF/jsp/offers/time/timeOffersDisable.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersDisable.jsp rename to src/main/webapp/WEB-INF/jsp/offers/time/timeOffersDisable.jsp diff --git a/src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersShow.jsp b/src/main/webapp/WEB-INF/jsp/offers/time/timeOffersShow.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/timeOffers/timeOffersShow.jsp rename to src/main/webapp/WEB-INF/jsp/offers/time/timeOffersShow.jsp diff --git a/src/test/java/org/springframework/cheapy/web/FoodOfferControllerTests.java b/src/test/java/org/springframework/cheapy/web/FoodOfferControllerTests.java new file mode 100644 index 000000000..c2e4a4348 --- /dev/null +++ b/src/test/java/org/springframework/cheapy/web/FoodOfferControllerTests.java @@ -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")); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/cheapy/web/NuOfferControllerTests.java b/src/test/java/org/springframework/cheapy/web/NuOfferControllerTests.java new file mode 100644 index 000000000..8e418a679 --- /dev/null +++ b/src/test/java/org/springframework/cheapy/web/NuOfferControllerTests.java @@ -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")); + } + + +} \ No newline at end of file diff --git a/src/test/java/org/springframework/cheapy/web/SpeedOfferControllerTests.java b/src/test/java/org/springframework/cheapy/web/SpeedOfferControllerTests.java new file mode 100644 index 000000000..c20615d3d --- /dev/null +++ b/src/test/java/org/springframework/cheapy/web/SpeedOfferControllerTests.java @@ -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")); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/cheapy/web/TimeOfferControllerTests.java b/src/test/java/org/springframework/cheapy/web/TimeOfferControllerTests.java new file mode 100644 index 000000000..df93d9ac6 --- /dev/null +++ b/src/test/java/org/springframework/cheapy/web/TimeOfferControllerTests.java @@ -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")); + } + + + + +} \ No newline at end of file