<%#
 Copyright 2013-2021 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_ const reactivePrefix = reactive ? 'Reactive' : ''; _%>
package <%= packageName %>.config;

import static org.mockito.Mockito.mock;

import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.client.InMemory<%= reactivePrefix %>OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.<%= reactivePrefix %>OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.<%= reactivePrefix %>ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemory<%= reactivePrefix %>ClientRegistrationRepository;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.security.oauth2.jwt.<%= reactivePrefix %>JwtDecoder;
<%_ if (applicationTypeMonolith) { _%>
import org.springframework.context.annotation.Import;
<%_ } _%>

/**
 * This class allows you to run unit and integration tests without an IdP.
 */
@TestConfiguration
<%_ if (applicationTypeMonolith) { _%>
@Import(OAuth2Configuration.class)
<%_ } _%>
public class TestSecurityConfiguration {

    @Bean
    ClientRegistration clientRegistration() {
        return clientRegistrationBuilder().build();
    }

    @Bean
    <%= reactivePrefix %>ClientRegistrationRepository clientRegistrationRepository(ClientRegistration clientRegistration) {
        return new InMemory<%= reactivePrefix %>ClientRegistrationRepository(clientRegistration);
    }

    private ClientRegistration.Builder clientRegistrationBuilder() {
        Map<String, Object> metadata = new HashMap<>();
        metadata.put("end_session_endpoint", "https://jhipster.org/logout");

        return ClientRegistration.withRegistrationId("oidc")
            .issuerUri("{baseUrl}")
            .redirectUri("{baseUrl}/{action}/oauth2/code/{registrationId}")
            .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
            .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
            .scope("read:user")
            .authorizationUri("https://jhipster.org/login/oauth/authorize")
            .tokenUri("https://jhipster.org/login/oauth/access_token")
            .jwkSetUri("https://jhipster.org/oauth/jwk")
            .userInfoUri("https://api.jhipster.org/user")
            .providerConfigurationMetadata(metadata)
            .userNameAttributeName("id")
            .clientName("Client Name")
            .clientId("client-id")
            .clientSecret("client-secret");
    }

    @Bean
    <%= reactivePrefix %>JwtDecoder jwtDecoder() {
        return mock(<%= reactivePrefix %>JwtDecoder.class);
    }

    @Bean
    <%= reactivePrefix %>OAuth2AuthorizedClientService authorizedClientService(<%= reactivePrefix %>ClientRegistrationRepository clientRegistrationRepository) {
        return new InMemory<%= reactivePrefix %>OAuth2AuthorizedClientService(clientRegistrationRepository);
    }
}
