|
| 1 | +package dev.findfirst.users.service; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import dev.findfirst.users.model.oauth2.Oauth2Source; |
| 10 | + |
| 11 | +import org.jsoup.Connection; |
| 12 | +import org.jsoup.Jsoup; |
| 13 | +import org.jsoup.nodes.Document; |
| 14 | +import org.jsoup.nodes.Element; |
| 15 | +import org.jsoup.select.Elements; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.mockito.MockedStatic; |
| 19 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 20 | +import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; |
| 21 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 22 | + |
| 23 | +class Oauth2SourceServiceTest { |
| 24 | + |
| 25 | + private Oauth2SourceService service; |
| 26 | + |
| 27 | + private ClientRegistration provider; |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + void setUp() { |
| 31 | + provider = ClientRegistration.withRegistrationId("test").clientName("Test Provider") |
| 32 | + .authorizationUri("https://www.example-auth.com") |
| 33 | + .authorizationGrantType(AuthorizationGrantType.JWT_BEARER) |
| 34 | + .tokenUri("https://www.example.com").build(); |
| 35 | + var providers = new InMemoryClientRegistrationRepository(List.of(provider)); |
| 36 | + service = new Oauth2SourceService(); |
| 37 | + service.setOauth2Providers(providers); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testReturnsOauth2SourcesWithIconUrl_WhenFoundedAtWebsite() throws IOException { |
| 42 | + try (MockedStatic<Jsoup> jsoupMock = mockStatic(Jsoup.class)) { |
| 43 | + Connection mockConnection = mock(Connection.class); |
| 44 | + Document mockDoc = mock(Document.class); |
| 45 | + Element mockElement = mock(Element.class); |
| 46 | + Elements mockElements = mock(Elements.class); |
| 47 | + |
| 48 | + when(mockConnection.userAgent(anyString())).thenReturn(mockConnection); |
| 49 | + when(mockConnection.get()).thenReturn(mockDoc); |
| 50 | + when(mockDoc.head()).thenReturn(mockElement); |
| 51 | + when(mockElement.select(anyString())).thenReturn(mockElements); |
| 52 | + when(mockElements.first()).thenReturn(mockElement); |
| 53 | + when(mockElement.attr("href")).thenReturn("https://example.com/assets/favicon.ico"); |
| 54 | + jsoupMock.when(() -> Jsoup.connect(anyString())).thenReturn(mockConnection); |
| 55 | + |
| 56 | + service.init(); |
| 57 | + |
| 58 | + List<Oauth2Source> oauth2Sources = service.oauth2Sources(); |
| 59 | + |
| 60 | + assertFalse(oauth2Sources.isEmpty()); |
| 61 | + var oauth2Source = oauth2Sources.getFirst(); |
| 62 | + assertEquals(provider.getClientName(), oauth2Source.provider()); |
| 63 | + assertEquals("https://example.com/assets/favicon.ico", oauth2Source.iconUrl()); |
| 64 | + assertEquals("oauth2/authorization/" + provider.getRegistrationId(), |
| 65 | + oauth2Source.authEndpoint()); |
| 66 | + |
| 67 | + |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testReturnsOauth2SourcesWithoutIconUrl_WhenNotFoundedAtWebsite() throws IOException { |
| 73 | + try (MockedStatic<Jsoup> jsoupMock = mockStatic(Jsoup.class)) { |
| 74 | + Connection mockConnection = mock(Connection.class); |
| 75 | + Document mockDoc = mock(Document.class); |
| 76 | + Element mockElement = mock(Element.class); |
| 77 | + Elements mockElements = mock(Elements.class); |
| 78 | + |
| 79 | + when(mockConnection.userAgent(anyString())).thenReturn(mockConnection); |
| 80 | + when(mockConnection.get()).thenReturn(mockDoc); |
| 81 | + when(mockDoc.head()).thenReturn(mockElement); |
| 82 | + when(mockElement.select(anyString())).thenReturn(mockElements); |
| 83 | + when(mockElements.first()).thenReturn(null); |
| 84 | + jsoupMock.when(() -> Jsoup.connect(anyString())).thenReturn(mockConnection); |
| 85 | + |
| 86 | + service.init(); |
| 87 | + |
| 88 | + List<Oauth2Source> oauth2Sources = service.oauth2Sources(); |
| 89 | + |
| 90 | + assertFalse(oauth2Sources.isEmpty()); |
| 91 | + var oauth2Source = oauth2Sources.getFirst(); |
| 92 | + assertEquals(provider.getClientName(), oauth2Source.provider()); |
| 93 | + assertNull(oauth2Source.iconUrl()); |
| 94 | + assertEquals("oauth2/authorization/" + provider.getRegistrationId(), |
| 95 | + oauth2Source.authEndpoint()); |
| 96 | + |
| 97 | + |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testReturnsOauth2SourcesWithoutIconUrl_WhenNotConnectedAtWebsite() throws IOException { |
| 103 | + try (MockedStatic<Jsoup> jsoupMock = mockStatic(Jsoup.class)) { |
| 104 | + Connection mockConnection = mock(Connection.class); |
| 105 | + when(mockConnection.userAgent(anyString())).thenReturn(mockConnection); |
| 106 | + when(mockConnection.get()).thenThrow(IOException.class); |
| 107 | + |
| 108 | + jsoupMock.when(() -> Jsoup.connect(anyString())).thenReturn(mockConnection); |
| 109 | + |
| 110 | + service.init(); |
| 111 | + |
| 112 | + List<Oauth2Source> oauth2Sources = service.oauth2Sources(); |
| 113 | + |
| 114 | + assertFalse(oauth2Sources.isEmpty()); |
| 115 | + var oauth2Source = oauth2Sources.getFirst(); |
| 116 | + assertEquals(provider.getClientName(), oauth2Source.provider()); |
| 117 | + assertNull(oauth2Source.iconUrl()); |
| 118 | + assertEquals("oauth2/authorization/" + provider.getRegistrationId(), |
| 119 | + oauth2Source.authEndpoint()); |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testReturnsOauth2SourcesWithoutUntrustedTokenUri() { |
| 126 | + provider = ClientRegistration.withRegistrationId("test").clientName("Test Provider") |
| 127 | + .authorizationUri("https://www.example-auth.com") |
| 128 | + .authorizationGrantType(AuthorizationGrantType.JWT_BEARER) |
| 129 | + .tokenUri("http://www.example.com").build(); |
| 130 | + var providersWithoutSSL = new InMemoryClientRegistrationRepository(List.of(provider)); |
| 131 | + service.setOauth2Providers(providersWithoutSSL); |
| 132 | + service.init(); |
| 133 | + assertTrue(service.oauth2Sources().isEmpty()); |
| 134 | + |
| 135 | + } |
| 136 | +} |
0 commit comments