diff --git a/src/main/java/org/example/bookingappliation/config/SecurityConfig.java b/src/main/java/org/example/bookingappliation/config/SecurityConfig.java index bc53dc5..2b9a230 100644 --- a/src/main/java/org/example/bookingappliation/config/SecurityConfig.java +++ b/src/main/java/org/example/bookingappliation/config/SecurityConfig.java @@ -37,7 +37,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests( auth -> auth.requestMatchers("auth/**", "/error", "/swagger-ui**", - "payments/success/**", "payments/cancel/**", "types/**") + "payments/success/**", "payments/cancel/**", "types/**", + "accommodations**") .permitAll() .anyRequest() .authenticated() diff --git a/src/test/java/org/example/bookingappliation/controller/AccommodationControllerTest.java b/src/test/java/org/example/bookingappliation/controller/AccommodationControllerTest.java new file mode 100644 index 0000000..22853d8 --- /dev/null +++ b/src/test/java/org/example/bookingappliation/controller/AccommodationControllerTest.java @@ -0,0 +1,85 @@ +package org.example.bookingappliation.controller; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.sql.Connection; +import java.sql.SQLException; +import javax.sql.DataSource; +import lombok.SneakyThrows; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.datasource.init.ScriptUtils; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class AccommodationControllerTest { + private static final String ADD_THREE_ACCOMMODATIONS + = "database/accommodations/insert-accommodations.sql"; + private static final String ADD_TEST_USER + = "database/users/insert-users.sql"; + private static final String ADD_TEST_BOOKING + = "database/bookings/insert-booking.sql"; + private static final String DELETE_DATA + = "database/deletes/delete-users-accommodations-bookings-addresses.sql"; + private static MockMvc mockMvc; + @Autowired + private ObjectMapper objectMapper; + + @BeforeAll + static void beforeAll(@Autowired WebApplicationContext applicationContext, + @Autowired DataSource dataSource) { + teardown(dataSource); + mockMvc = MockMvcBuilders + .webAppContextSetup(applicationContext) + .apply(springSecurity()) + .build(); + } + + @BeforeEach + void setUp(@Autowired DataSource dataSource) throws SQLException { + teardown(dataSource); + try (Connection connection = dataSource.getConnection()) { + connection.setAutoCommit(true); + ScriptUtils.executeSqlScript(connection, + new ClassPathResource(ADD_THREE_ACCOMMODATIONS)); + ScriptUtils.executeSqlScript(connection, + new ClassPathResource(ADD_TEST_USER)); + ScriptUtils.executeSqlScript(connection, + new ClassPathResource(ADD_TEST_BOOKING)); + } + } + + @Test + void name() { + assertEquals(true, true); + } + + @AfterEach + void tearDown(@Autowired DataSource dataSource) { + teardown(dataSource); + } + + @AfterAll + static void afterAll(@Autowired DataSource dataSource) { + teardown(dataSource); + } + + @SneakyThrows + static void teardown(DataSource dataSource) { + try (Connection connection = dataSource.getConnection()) { + connection.setAutoCommit(true); + ScriptUtils.executeSqlScript(connection, + new ClassPathResource(DELETE_DATA)); + } + } +}