package com.ankurm.authdemo;

import com.ankurm.authdemo.auth.CustomAuthProvider;
import com.ankurm.authdemo.web.TestController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@SpringBootTest(classes = BasicLeakTests.LeakyConfig.class)
@AutoConfigureMockMvc
class BasicLeakTests {

    @TestConfiguration
    @EnableAutoConfiguration
    @EnableWebSecurity
    @Import(TestController.class)
    static class LeakyConfig {
        @Bean
        CustomAuthProvider customAuthProvider() {
            return new CustomAuthProvider();
        }

        @Bean
        @Order(1)
        SecurityFilterChain api(HttpSecurity http, JwtDecoder decoder) throws Exception {
            http.securityMatcher("/api/**")
                .authorizeHttpRequests(a -> a.anyRequest().authenticated())
                .oauth2ResourceServer(o -> o.jwt(j -> j.decoder(decoder)))
                .httpBasic(Customizer.withDefaults()) // "just for testing"
                .csrf(c -> c.disable());
            return http.build();
        }

        @Bean
        @Order(2)
        SecurityFilterChain web(HttpSecurity http) throws Exception {
            http.authenticationProvider(customAuthProvider())
                .authorizeHttpRequests(a -> a.anyRequest().authenticated())
                .formLogin(Customizer.withDefaults());
            return http.build();
        }
    }

    @Autowired
    MockMvc mvc;
    @MockitoBean
    JwtDecoder jwtDecoder;

    @Test
    void basicAuthReachesTheProvider() throws Exception {
        Transcript t = new Transcript("07-basic-leak");
        int good = mvc.perform(get("/api/ping").with(httpBasic("admin", "secret"))).andReturn().getResponse().getStatus();
        int bad = mvc.perform(get("/api/ping").with(httpBasic("admin", "wrong"))).andReturn().getResponse().getStatus();
        t.line("apiFilterChain with .httpBasic(...) added \"just for testing\"");
        t.line("GET /api/ping  Basic admin:secret  -> %d", good);
        t.line("GET /api/ping  Basic admin:wrong   -> %d", bad);
        t.write();
        assertThat(good).isEqualTo(200);
        assertThat(bad).isEqualTo(401);
    }
}
