|
| 1 | +package com.baeldung.mcp.oauth2authorizationserver.config; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.context.ApplicationContext; |
| 10 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 11 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 12 | +import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; |
| 13 | +import org.springframework.security.web.SecurityFilterChain; |
| 14 | + |
| 15 | +@SpringBootTest(classes = AuthorizationServerConfig.class) |
| 16 | +class AuthorizationServerConfigTest { |
| 17 | + |
| 18 | + private final ApplicationContext context; |
| 19 | + |
| 20 | + private final RegisteredClientRepository registeredClientRepository; |
| 21 | + |
| 22 | + private final AuthorizationServerSettings authorizationServerSettings; |
| 23 | + |
| 24 | + public AuthorizationServerConfigTest(ApplicationContext context, RegisteredClientRepository registeredClientRepository, |
| 25 | + AuthorizationServerSettings authorizationServerSettings) { |
| 26 | + this.authorizationServerSettings = authorizationServerSettings; |
| 27 | + this.registeredClientRepository = registeredClientRepository; |
| 28 | + this.context = context; |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void givenContext_whenLoaded_thenSecurityFilterChainsPresent() { |
| 33 | + SecurityFilterChain chain1 = (SecurityFilterChain) context.getBean("authorizationServerSecurityFilterChain"); |
| 34 | + SecurityFilterChain chain2 = (SecurityFilterChain) context.getBean("defaultSecurityFilterChain"); |
| 35 | + assertNotNull(chain1); |
| 36 | + assertNotNull(chain2); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void givenRegisteredClientRepository_whenQueried_thenContainsExpectedClient() { |
| 41 | + RegisteredClient client = registeredClientRepository.findByClientId("mcp-client"); |
| 42 | + assertNotNull(client); |
| 43 | + assertEquals("mcp-client", client.getClientId()); |
| 44 | + assertTrue(client.getClientAuthenticationMethods() |
| 45 | + .stream() |
| 46 | + .anyMatch(m -> m.getValue() |
| 47 | + .equals("client_secret_basic"))); |
| 48 | + assertTrue(client.getAuthorizationGrantTypes() |
| 49 | + .stream() |
| 50 | + .anyMatch(g -> g.getValue() |
| 51 | + .equals("authorization_code"))); |
| 52 | + assertTrue(client.getScopes() |
| 53 | + .contains("mcp.read")); |
| 54 | + assertTrue(client.getScopes() |
| 55 | + .contains("mcp.write")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void givenAuthorizationServerSettings_whenLoaded_thenIssuerIsCorrect() { |
| 60 | + assertEquals("http://localhost:9000", authorizationServerSettings.getIssuer()); |
| 61 | + } |
| 62 | +} |
| 63 | + |
0 commit comments