package org.oasis_eu.portal.front;


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.oasis_eu.portal.main.OasisPortal;
import org.oasis_eu.spring.kernel.security.OpenIdCAuthProvider;
import org.oasis_eu.spring.kernel.security.OpenIdCAuthentication;
import org.oasis_eu.spring.kernel.security.RefreshTokenNeedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.util.NestedServletException;

import javax.servlet.Filter;
import javax.servlet.http.HttpSession;
import java.time.Instant;
import java.time.temporal.ChronoUnit;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * User: schambon
 * Date: 9/1/14
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes={OasisPortal.class})
@DirtiesContext
public class TestTokenRefresh {

    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private Filter springSecurityFilterChain;

    @Autowired
    private OpenIdCAuthProvider openIdCAuthProvider;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(wac)
                .addFilters(springSecurityFilterChain)
                .build();

        // force not loading user info from the Kernel
        openIdCAuthProvider.setFetchUserInfo(false);
    }


    /**
     * Test that the access token's being expired correctly, that is to say the interceptor throws a RefreshTokenNeededException
     * (wrapped in a NestedServletException as may be).
     * Note that we get the exception rather than a redirect to
     * @throws Throwable
     */
    @Test
    public void testExpiry() throws Exception {


        OpenIdCAuthentication auth = new OpenIdCAuthentication("-test-subject-", "-test-accesstoken-", "-test-idtoken-", Instant.now(), Instant.now().plus(3, ChronoUnit.SECONDS));
        SecurityContextHolder.getContext().setAuthentication(auth);

        MockHttpSession session = new MockHttpSession();
        session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());

        mockMvc.perform(get("/my").session(session)).andExpect(status().is3xxRedirection()).andExpect(redirectedUrlPattern("http://localhost/login?ui_locales=en"));


    }



}

@@ -2903,16 +2903,29 @@
 SECONDS)
+, true, false
 );%0A     

Fix error in OpenIdCAuthentication constructor