|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.test; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import io.serverlessworkflow.api.types.AuthenticationPolicyUnion; |
| 23 | +import io.serverlessworkflow.api.types.BasicAuthenticationPolicy; |
| 24 | +import io.serverlessworkflow.api.types.OAuth2AuthenticationData; |
| 25 | +import io.serverlessworkflow.api.types.OAuth2AuthenticationPolicy; |
| 26 | +import io.serverlessworkflow.api.types.OAuth2AuthenticationPolicyConfiguration; |
| 27 | +import io.serverlessworkflow.api.types.OAuth2ConnectAuthenticationProperties; |
| 28 | +import io.serverlessworkflow.api.types.OpenIdConnectAuthenticationPolicy; |
| 29 | +import io.serverlessworkflow.api.types.OpenIdConnectAuthenticationPolicyConfiguration; |
| 30 | +import io.serverlessworkflow.api.types.SecretBasedAuthenticationPolicy; |
| 31 | +import io.serverlessworkflow.impl.auth.OAuthPolicyData; |
| 32 | +import io.serverlessworkflow.impl.auth.OAuthScheme; |
| 33 | +import io.serverlessworkflow.impl.auth.OAuthUtils; |
| 34 | +import java.util.Optional; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +public class OAuthUtilsTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + void fromNullReturnsEmpty() { |
| 41 | + assertEquals(Optional.empty(), OAuthUtils.from(null)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void fromNonOAuthPolicyReturnsEmpty() { |
| 46 | + AuthenticationPolicyUnion union = |
| 47 | + new AuthenticationPolicyUnion() |
| 48 | + .withBasicAuthenticationPolicy(new BasicAuthenticationPolicy()); |
| 49 | + assertTrue(OAuthUtils.from(union).isEmpty()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void fromOAuth2InlineData() { |
| 54 | + OAuth2ConnectAuthenticationProperties props = new OAuth2ConnectAuthenticationProperties(); |
| 55 | + AuthenticationPolicyUnion union = |
| 56 | + new AuthenticationPolicyUnion() |
| 57 | + .withOAuth2AuthenticationPolicy( |
| 58 | + new OAuth2AuthenticationPolicy() |
| 59 | + .withOauth2( |
| 60 | + new OAuth2AuthenticationPolicyConfiguration() |
| 61 | + .withOAuth2ConnectAuthenticationProperties(props))); |
| 62 | + Optional<OAuthPolicyData> result = OAuthUtils.from(union); |
| 63 | + assertTrue(result.isPresent()); |
| 64 | + OAuthPolicyData data = result.get(); |
| 65 | + assertEquals(OAuthScheme.OAUTH2, data.scheme()); |
| 66 | + assertEquals(props, data.data()); |
| 67 | + assertNull(data.secret()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void fromOAuth2Secret() { |
| 72 | + SecretBasedAuthenticationPolicy secret = new SecretBasedAuthenticationPolicy("mySecret"); |
| 73 | + AuthenticationPolicyUnion union = |
| 74 | + new AuthenticationPolicyUnion() |
| 75 | + .withOAuth2AuthenticationPolicy( |
| 76 | + new OAuth2AuthenticationPolicy() |
| 77 | + .withOauth2( |
| 78 | + new OAuth2AuthenticationPolicyConfiguration() |
| 79 | + .withOAuth2AuthenticationPolicySecret(secret))); |
| 80 | + Optional<OAuthPolicyData> result = OAuthUtils.from(union); |
| 81 | + assertTrue(result.isPresent()); |
| 82 | + OAuthPolicyData data = result.get(); |
| 83 | + assertEquals(OAuthScheme.OAUTH2, data.scheme()); |
| 84 | + assertNull(data.data()); |
| 85 | + assertEquals(secret, data.secret()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void fromOidcInlineData() { |
| 90 | + OAuth2AuthenticationData oidcData = new OAuth2AuthenticationData(); |
| 91 | + AuthenticationPolicyUnion union = |
| 92 | + new AuthenticationPolicyUnion() |
| 93 | + .withOpenIdConnectAuthenticationPolicy( |
| 94 | + new OpenIdConnectAuthenticationPolicy() |
| 95 | + .withOidc( |
| 96 | + new OpenIdConnectAuthenticationPolicyConfiguration() |
| 97 | + .withOpenIdConnectAuthenticationProperties(oidcData))); |
| 98 | + Optional<OAuthPolicyData> result = OAuthUtils.from(union); |
| 99 | + assertTrue(result.isPresent()); |
| 100 | + OAuthPolicyData data = result.get(); |
| 101 | + assertEquals(OAuthScheme.OPENID_CONNECT, data.scheme()); |
| 102 | + assertEquals(oidcData, data.data()); |
| 103 | + assertNull(data.secret()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void fromOidcSecret() { |
| 108 | + SecretBasedAuthenticationPolicy secret = new SecretBasedAuthenticationPolicy("oidcSecret"); |
| 109 | + AuthenticationPolicyUnion union = |
| 110 | + new AuthenticationPolicyUnion() |
| 111 | + .withOpenIdConnectAuthenticationPolicy( |
| 112 | + new OpenIdConnectAuthenticationPolicy() |
| 113 | + .withOidc( |
| 114 | + new OpenIdConnectAuthenticationPolicyConfiguration() |
| 115 | + .withOpenIdConnectAuthenticationPolicySecret(secret))); |
| 116 | + Optional<OAuthPolicyData> result = OAuthUtils.from(union); |
| 117 | + assertTrue(result.isPresent()); |
| 118 | + OAuthPolicyData data = result.get(); |
| 119 | + assertEquals(OAuthScheme.OPENID_CONNECT, data.scheme()); |
| 120 | + assertNull(data.data()); |
| 121 | + assertEquals(secret, data.secret()); |
| 122 | + } |
| 123 | +} |
0 commit comments