|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2024 Tracehub.git |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to read |
| 6 | + * the Software only. Permissions is hereby NOT GRANTED to use, copy, modify, |
| 7 | + * merge, publish, distribute, sublicense, and/or sell copies of the Software. |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 10 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 12 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 13 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 14 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 15 | + * SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +package it.security; |
| 19 | + |
| 20 | +import com.jcabi.http.mock.MkAnswer; |
| 21 | +import git.tracehub.pmo.PmoApplication; |
| 22 | +import git.tracehub.pmo.security.IdpToken; |
| 23 | +import it.PostgresIntegration; |
| 24 | +import it.ServerIntegration; |
| 25 | +import java.net.HttpURLConnection; |
| 26 | +import org.hamcrest.MatcherAssert; |
| 27 | +import org.hamcrest.core.IsEqual; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.springframework.boot.test.context.SpringBootTest; |
| 31 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 32 | +import org.springframework.test.context.ActiveProfiles; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration tests for {@link IdpToken}. |
| 36 | + * |
| 37 | + * @since 0.0.0 |
| 38 | + */ |
| 39 | +@ActiveProfiles("web") |
| 40 | +@SpringBootTest( |
| 41 | + classes = PmoApplication.class, |
| 42 | + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT |
| 43 | +) |
| 44 | +@SuppressWarnings("JTCOP.RuleAllTestsHaveProductionClass") |
| 45 | +final class IdpTokenIT |
| 46 | + implements ServerIntegration, PostgresIntegration { |
| 47 | + |
| 48 | + @Test |
| 49 | + void retrievesIdpTokenSuccessfully() { |
| 50 | + final String url = ServerIntegration.SERVER.home().toString(); |
| 51 | + final String expected = "token"; |
| 52 | + ServerIntegration.SERVER.next( |
| 53 | + new MkAnswer.Simple( |
| 54 | + HttpURLConnection.HTTP_OK, |
| 55 | + "access_token=%s&expires_in=3600&token_type=bearer" |
| 56 | + .formatted(expected) |
| 57 | + ) |
| 58 | + ); |
| 59 | + final String token = new IdpToken( |
| 60 | + Mockito.mock(Jwt.class), |
| 61 | + "provider", |
| 62 | + url.substring(0, url.length() - 1) |
| 63 | + ).value(); |
| 64 | + MatcherAssert.assertThat( |
| 65 | + "Access token %s isn't correct".formatted(token), |
| 66 | + token, |
| 67 | + new IsEqual<>(expected) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments