package org.synyx.urlaubsverwaltung.sickdays.web;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.synyx.urlaubsverwaltung.TestContainersBase;
import org.synyx.urlaubsverwaltung.person.Person;
import org.synyx.urlaubsverwaltung.person.PersonService;

import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
class SickDaysOverviewViewControllerSecurityIT extends TestContainersBase {

    @Autowired
    private WebApplicationContext context;

    @MockBean
    private PersonService personService;

    @Test
    @WithMockUser(authorities = "USER")
    void periodsSickNotesWithWrongRole() throws Exception {
        final LocalDateTime now = LocalDateTime.now();
        final ResultActions resultActions = perform(get("/web/sicknote")
            .param("from", dtf.format(now))
            .param("to", dtf.format(now.plusDays(1))));
        resultActions.andExpect(status().isForbidden());
    }

    @Test
    @WithMockUser(authorities = {"USER", "OFFICE"})
    void periodsSickNotesWithCorrectRole() throws Exception {

        when(personService.getSignedInUser()).thenReturn(new Person());

        final ResultActions resultActions = perform(get("/web/sicknote"));
        resultActions.andExpect(status().isOk());
    }

    private ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
        return MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build().perform(builder);
    }
}

@@ -766,16 +766,238 @@
 Service;
+%0Aimport org.synyx.urlaubsverwaltung.util.DateFormat;%0A%0Aimport java.time.Clock;%0Aimport java.time.Instant;%0Aimport java.time.LocalDateTime;%0Aimport java.time.format.DateTimeFormatter;%0Aimport java.time.temporal.TemporalAccessor;
 %0A%0Aimport
@@ -1523,16 +1523,102 @@
 rvice;%0A%0A
+    private DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DateFormat.PATTERN);%0A%0A
     @Tes
@@ -2205,24 +2205,79 @@
 Person());%0A%0A
+        final LocalDateTime now = LocalDateTime.now();%0A
         fina
@@ -2308,32 +2308,45 @@
 tions = perform(
+%0A            
 get(%22/web/sickno
@@ -2349,16 +2349,106 @@
 cknote%22)
+%0A        .param(%22from%22, dtf.format(now))%0A        .param(%22to%22, dtf.format(now.plusDays(1)))
 );%0A     

add missing parameter in test