|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.wso2.carbon.identity.branding.preference.management.core.listener; |
| 20 | + |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.MockedStatic; |
| 23 | +import org.mockito.MockitoAnnotations; |
| 24 | +import org.testng.annotations.BeforeMethod; |
| 25 | +import org.testng.annotations.Test; |
| 26 | +import org.wso2.carbon.identity.branding.preference.management.core.BrandingPreferenceManagerImpl; |
| 27 | +import org.wso2.carbon.identity.branding.preference.management.core.exception.BrandingPreferenceMgtClientException; |
| 28 | +import org.wso2.carbon.identity.branding.preference.management.core.exception.BrandingPreferenceMgtException; |
| 29 | +import org.wso2.carbon.identity.branding.preference.management.core.model.BrandingPreference; |
| 30 | +import org.wso2.carbon.identity.core.ServiceURL; |
| 31 | +import org.wso2.carbon.identity.core.ServiceURLBuilder; |
| 32 | +import org.wso2.carbon.identity.core.URLBuilderException; |
| 33 | +import org.wso2.carbon.identity.flow.execution.engine.model.FlowExecutionContext; |
| 34 | + |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +import static org.mockito.ArgumentMatchers.anyString; |
| 39 | +import static org.mockito.Mockito.any; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.mockStatic; |
| 42 | +import static org.mockito.Mockito.never; |
| 43 | +import static org.mockito.Mockito.verify; |
| 44 | +import static org.mockito.Mockito.when; |
| 45 | +import static org.testng.Assert.assertFalse; |
| 46 | +import static org.testng.Assert.assertTrue; |
| 47 | +import static org.wso2.carbon.identity.branding.preference.management.core.constant.BrandingPreferenceMgtConstants.DEFAULT_LOCALE; |
| 48 | +import static org.wso2.carbon.identity.branding.preference.management.core.constant.BrandingPreferenceMgtConstants.ORGANIZATION_TYPE; |
| 49 | + |
| 50 | +/** |
| 51 | + * Unit tests for {@link PortalURLResolver}. |
| 52 | + */ |
| 53 | +public class PortalURLResolverTest { |
| 54 | + |
| 55 | + public static final String PORTAL_URL = "https://signup.wso2.com"; |
| 56 | + @Mock |
| 57 | + private BrandingPreferenceManagerImpl brandingPreferenceManager; |
| 58 | + |
| 59 | + @Mock |
| 60 | + private FlowExecutionContext flowContext; |
| 61 | + |
| 62 | + @Mock |
| 63 | + private BrandingPreference brandingPreference; |
| 64 | + |
| 65 | + private PortalURLResolver resolver; |
| 66 | + |
| 67 | + @BeforeMethod |
| 68 | + public void setup() { |
| 69 | + |
| 70 | + MockitoAnnotations.openMocks(this); |
| 71 | + resolver = new PortalURLResolver(brandingPreferenceManager); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void skipSetPortalUrl() { |
| 76 | + |
| 77 | + when(flowContext.getPortalUrl()).thenReturn("https://existing.url"); |
| 78 | + |
| 79 | + boolean result = resolver.doPreExecute(flowContext); |
| 80 | + |
| 81 | + assertTrue(result); |
| 82 | + verify(flowContext, never()).setPortalUrl(any()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void validPref_setsCustomSignupUrl() throws Exception { |
| 87 | + |
| 88 | + when(flowContext.getPortalUrl()).thenReturn(null); |
| 89 | + when(flowContext.getApplicationId()).thenReturn(null); |
| 90 | + when(flowContext.getTenantDomain()).thenReturn("wso2.com"); |
| 91 | + when(flowContext.getFlowType()).thenReturn("REGISTRATION"); |
| 92 | + |
| 93 | + Map<String, String> urlsMap = new HashMap<>(); |
| 94 | + urlsMap.put("selfSignUpURL", PORTAL_URL); |
| 95 | + Map<String, Object> prefMap = new HashMap<>(); |
| 96 | + prefMap.put("urls", urlsMap); |
| 97 | + |
| 98 | + when(brandingPreferenceManager.getBrandingPreference(ORGANIZATION_TYPE, "wso2.com", DEFAULT_LOCALE)) |
| 99 | + .thenReturn(brandingPreference); |
| 100 | + when(brandingPreference.getPreference()).thenReturn(prefMap); |
| 101 | + |
| 102 | + try (MockedStatic<ServiceURLBuilder> serviceURLBuilder = mockStatic(ServiceURLBuilder.class)) { |
| 103 | + |
| 104 | + mockServiceURLBuilder(); |
| 105 | + boolean result = resolver.doPreExecute(flowContext); |
| 106 | + assertTrue(result); |
| 107 | + verify(flowContext).setPortalUrl(PORTAL_URL); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void fallBackToDefaultUrl() throws Exception { |
| 113 | + |
| 114 | + when(flowContext.getPortalUrl()).thenReturn(""); |
| 115 | + when(flowContext.getApplicationId()).thenReturn(null); |
| 116 | + when(flowContext.getTenantDomain()).thenReturn("wso2.com"); |
| 117 | + when(flowContext.getFlowType()).thenReturn("REGISTRATION"); |
| 118 | + |
| 119 | + when(brandingPreferenceManager.getBrandingPreference(ORGANIZATION_TYPE, "wso2.com", DEFAULT_LOCALE)) |
| 120 | + .thenReturn(null); |
| 121 | + |
| 122 | + try (MockedStatic<ServiceURLBuilder> serviceURLBuilder = mockStatic(ServiceURLBuilder.class)) { |
| 123 | + |
| 124 | + mockServiceURLBuilder(); |
| 125 | + boolean result = resolver.doPreExecute(flowContext); |
| 126 | + assertTrue(result); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void prefWithoutSelfSignupUrl() throws Exception { |
| 132 | + |
| 133 | + when(flowContext.getPortalUrl()).thenReturn(null); |
| 134 | + when(flowContext.getApplicationId()).thenReturn(null); |
| 135 | + when(flowContext.getTenantDomain()).thenReturn("wso2.com"); |
| 136 | + when(flowContext.getFlowType()).thenReturn("REGISTRATION"); |
| 137 | + |
| 138 | + Map<String, Object> prefMap = new HashMap<>(); |
| 139 | + prefMap.put("urls", new HashMap<>()); |
| 140 | + when(brandingPreferenceManager.getBrandingPreference(ORGANIZATION_TYPE, "wso2.com", DEFAULT_LOCALE)) |
| 141 | + .thenReturn(brandingPreference); |
| 142 | + when(brandingPreference.getPreference()).thenReturn(prefMap); |
| 143 | + |
| 144 | + try (MockedStatic<ServiceURLBuilder> serviceURLBuilder = mockStatic(ServiceURLBuilder.class)) { |
| 145 | + |
| 146 | + mockServiceURLBuilder(); |
| 147 | + boolean result = resolver.doPreExecute(flowContext); |
| 148 | + assertTrue(result); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static void mockServiceURLBuilder() throws URLBuilderException { |
| 153 | + |
| 154 | + ServiceURLBuilder serviceURLBuilderMock = mock(ServiceURLBuilder.class); |
| 155 | + when(ServiceURLBuilder.create()).thenReturn(serviceURLBuilderMock); |
| 156 | + when(serviceURLBuilderMock.addPath(anyString())).thenReturn(serviceURLBuilderMock); |
| 157 | + ServiceURL serviceURL = mock(ServiceURL.class); |
| 158 | + when(serviceURLBuilderMock.build()).thenReturn(serviceURL); |
| 159 | + String url = "https://default.url"; |
| 160 | + when(serviceURL.getAbsolutePublicURL()).thenReturn(url); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void clientExceptionForMissingConfig() throws Exception { |
| 165 | + |
| 166 | + when(flowContext.getPortalUrl()).thenReturn(null); |
| 167 | + when(flowContext.getApplicationId()).thenReturn(null); |
| 168 | + when(flowContext.getTenantDomain()).thenReturn("foo.com"); |
| 169 | + when(flowContext.getFlowType()).thenReturn("REGISTRATION"); |
| 170 | + |
| 171 | + BrandingPreferenceMgtClientException ex = new BrandingPreferenceMgtClientException("Not configured", |
| 172 | + "BRAND-60001"); |
| 173 | + when(brandingPreferenceManager.getBrandingPreference(ORGANIZATION_TYPE, "foo.com", |
| 174 | + DEFAULT_LOCALE)).thenThrow(ex); |
| 175 | + |
| 176 | + try (MockedStatic<ServiceURLBuilder> serviceURLBuilder = mockStatic(ServiceURLBuilder.class)) { |
| 177 | + |
| 178 | + mockServiceURLBuilder(); |
| 179 | + boolean result = resolver.doPreExecute(flowContext); |
| 180 | + assertTrue(result); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void internalErrorDuringURLResolving() throws Exception { |
| 186 | + |
| 187 | + when(flowContext.getPortalUrl()).thenReturn(null); |
| 188 | + when(flowContext.getApplicationId()).thenReturn(null); |
| 189 | + when(flowContext.getTenantDomain()).thenReturn("foo.com"); |
| 190 | + when(flowContext.getFlowType()).thenReturn("REGISTRATION"); |
| 191 | + |
| 192 | + when(brandingPreferenceManager.getBrandingPreference(any(), any(), any())) |
| 193 | + .thenThrow(new BrandingPreferenceMgtException("Something broke", "BRAND-600xx")); |
| 194 | + |
| 195 | + boolean result = resolver.doPreExecute(flowContext); |
| 196 | + |
| 197 | + assertFalse(result); |
| 198 | + } |
| 199 | +} |
0 commit comments