|
2 | 2 | // Licensed under the MIT License. |
3 | 3 | package com.azure.spring.autoconfigure.b2c; |
4 | 4 |
|
| 5 | +import org.jetbrains.annotations.NotNull; |
5 | 6 | import org.junit.jupiter.api.Assertions; |
6 | 7 | import org.junit.jupiter.api.Test; |
| 8 | +import org.mockito.MockedStatic; |
| 9 | +import org.mockito.Mockito; |
| 10 | +import org.springframework.beans.BeanUtils; |
7 | 11 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
8 | 12 | import org.springframework.boot.test.context.FilteredClassLoader; |
9 | 13 | import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 14 | +import org.springframework.core.io.ClassPathResource; |
10 | 15 | import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken; |
11 | 16 |
|
12 | 17 | import java.util.Arrays; |
13 | 18 | import java.util.HashSet; |
14 | 19 | import java.util.Map; |
15 | 20 | import java.util.Set; |
16 | 21 |
|
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.Mockito.atLeastOnce; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.mockStatic; |
| 26 | +import static org.mockito.Mockito.never; |
| 27 | +import static org.mockito.Mockito.spy; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | + |
17 | 30 | public class AADB2CAutoConfigurationTest extends AbstractAADB2COAuth2ClientTestConfiguration { |
18 | 31 |
|
19 | 32 | public AADB2CAutoConfigurationTest() { |
20 | 33 | contextRunner = new WebApplicationContextRunner() |
21 | 34 | .withConfiguration(AutoConfigurations.of(WebOAuth2ClientApp.class, AADB2CAutoConfiguration.class)) |
22 | 35 | .withClassLoader(new FilteredClassLoader(BearerTokenAuthenticationToken.class)) |
23 | | - .withPropertyValues( |
24 | | - String.format("%s=%s", AADB2CConstants.BASE_URI, AADB2CConstants.TEST_BASE_URI), |
25 | | - String.format("%s=%s", AADB2CConstants.TENANT_ID, AADB2CConstants.TEST_TENANT_ID), |
26 | | - String.format("%s=%s", AADB2CConstants.CLIENT_ID, AADB2CConstants.TEST_CLIENT_ID), |
27 | | - String.format("%s=%s", AADB2CConstants.CLIENT_SECRET, AADB2CConstants.TEST_CLIENT_SECRET), |
28 | | - String.format("%s=%s", AADB2CConstants.LOGOUT_SUCCESS_URL, AADB2CConstants.TEST_LOGOUT_SUCCESS_URL), |
29 | | - String.format("%s=%s", AADB2CConstants.LOGIN_FLOW, AADB2CConstants.TEST_KEY_SIGN_UP_OR_IN), |
30 | | - String.format("%s.%s=%s", AADB2CConstants.USER_FLOWS, |
31 | | - AADB2CConstants.TEST_KEY_SIGN_UP_OR_IN, AADB2CConstants.TEST_SIGN_UP_OR_IN_NAME), |
32 | | - String.format("%s.%s=%s", AADB2CConstants.USER_FLOWS, |
33 | | - AADB2CConstants.TEST_KEY_SIGN_IN, AADB2CConstants.TEST_SIGN_IN_NAME), |
34 | | - String.format("%s.%s=%s", AADB2CConstants.USER_FLOWS, |
35 | | - AADB2CConstants.TEST_KEY_SIGN_UP, AADB2CConstants.TEST_SIGN_UP_NAME), |
36 | | - String.format("%s=%s", AADB2CConstants.CONFIG_PROMPT, AADB2CConstants.TEST_PROMPT), |
37 | | - String.format("%s=%s", AADB2CConstants.CONFIG_LOGIN_HINT, AADB2CConstants.TEST_LOGIN_HINT), |
38 | | - String.format("%s=%s", AADB2CConstants.USER_NAME_ATTRIBUTE_NAME, AADB2CConstants.TEST_ATTRIBUTE_NAME), |
39 | | - String.format("%s=%s", AADB2CConstants.USER_NAME_ATTRIBUTE_NAME, AADB2CConstants.TEST_ATTRIBUTE_NAME) |
40 | | - ); |
| 36 | + .withPropertyValues(getWebappCommonPropertyValues()); |
| 37 | + } |
| 38 | + |
| 39 | + @NotNull |
| 40 | + private String[] getWebappCommonPropertyValues() { |
| 41 | + return new String[] { String.format("%s=%s", AADB2CConstants.BASE_URI, AADB2CConstants.TEST_BASE_URI), |
| 42 | + String.format("%s=%s", AADB2CConstants.TENANT_ID, AADB2CConstants.TEST_TENANT_ID), |
| 43 | + String.format("%s=%s", AADB2CConstants.CLIENT_ID, AADB2CConstants.TEST_CLIENT_ID), |
| 44 | + String.format("%s=%s", AADB2CConstants.CLIENT_SECRET, AADB2CConstants.TEST_CLIENT_SECRET), |
| 45 | + String.format("%s=%s", AADB2CConstants.LOGOUT_SUCCESS_URL, AADB2CConstants.TEST_LOGOUT_SUCCESS_URL), |
| 46 | + String.format("%s=%s", AADB2CConstants.LOGIN_FLOW, AADB2CConstants.TEST_KEY_SIGN_UP_OR_IN), |
| 47 | + String.format("%s.%s=%s", AADB2CConstants.USER_FLOWS, |
| 48 | + AADB2CConstants.TEST_KEY_SIGN_UP_OR_IN, AADB2CConstants.TEST_SIGN_UP_OR_IN_NAME), |
| 49 | + String.format("%s.%s=%s", AADB2CConstants.USER_FLOWS, |
| 50 | + AADB2CConstants.TEST_KEY_SIGN_IN, AADB2CConstants.TEST_SIGN_IN_NAME), |
| 51 | + String.format("%s.%s=%s", AADB2CConstants.USER_FLOWS, |
| 52 | + AADB2CConstants.TEST_KEY_SIGN_UP, AADB2CConstants.TEST_SIGN_UP_NAME), |
| 53 | + String.format("%s=%s", AADB2CConstants.CONFIG_PROMPT, AADB2CConstants.TEST_PROMPT), |
| 54 | + String.format("%s=%s", AADB2CConstants.CONFIG_LOGIN_HINT, AADB2CConstants.TEST_LOGIN_HINT), |
| 55 | + String.format("%s=%s", AADB2CConstants.USER_NAME_ATTRIBUTE_NAME, AADB2CConstants.TEST_ATTRIBUTE_NAME) }; |
41 | 56 | } |
42 | 57 |
|
43 | 58 | @Test |
@@ -88,4 +103,46 @@ public void testLogoutSuccessHandlerBean() { |
88 | 103 | Assertions.assertNotNull(handler); |
89 | 104 | }); |
90 | 105 | } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testWebappConditionsIsInvokedWhenAADB2CEnableFileExists() { |
| 109 | + try (MockedStatic<BeanUtils> beanUtils = mockStatic(BeanUtils.class, Mockito.CALLS_REAL_METHODS)) { |
| 110 | + AADB2CConditions.UserFlowCondition userFlowCondition = spy(AADB2CConditions.UserFlowCondition.class); |
| 111 | + AADB2CConditions.ClientRegistrationCondition clientRegistrationCondition = |
| 112 | + spy(AADB2CConditions.ClientRegistrationCondition.class); |
| 113 | + beanUtils.when(() -> BeanUtils.instantiateClass(AADB2CConditions.UserFlowCondition.class)) |
| 114 | + .thenReturn(userFlowCondition); |
| 115 | + beanUtils.when(() -> BeanUtils.instantiateClass(AADB2CConditions.ClientRegistrationCondition.class)) |
| 116 | + .thenReturn(clientRegistrationCondition); |
| 117 | + this.contextRunner |
| 118 | + .run(c -> { |
| 119 | + Assertions.assertTrue(c.getResource(AAD_B2C_ENABLE_CONFIG_FILE_NAME).exists()); |
| 120 | + verify(userFlowCondition, atLeastOnce()).getMatchOutcome(any(), any()); |
| 121 | + verify(clientRegistrationCondition, atLeastOnce()).getMatchOutcome(any(), any()); |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testWebappConditionsIsNotInvokedWhenAADB2CEnableFileDoesNotExists() { |
| 128 | + try (MockedStatic<BeanUtils> beanUtils = mockStatic(BeanUtils.class, Mockito.CALLS_REAL_METHODS)) { |
| 129 | + AADB2CConditions.UserFlowCondition userFlowCondition = mock(AADB2CConditions.UserFlowCondition.class); |
| 130 | + AADB2CConditions.ClientRegistrationCondition clientRegistrationCondition = |
| 131 | + spy(AADB2CConditions.ClientRegistrationCondition.class); |
| 132 | + beanUtils.when(() -> BeanUtils.instantiateClass(AADB2CConditions.UserFlowCondition.class)) |
| 133 | + .thenReturn(userFlowCondition); |
| 134 | + beanUtils.when(() -> BeanUtils.instantiateClass(AADB2CConditions.ClientRegistrationCondition.class)) |
| 135 | + .thenReturn(clientRegistrationCondition); |
| 136 | + new WebApplicationContextRunner() |
| 137 | + .withClassLoader(new FilteredClassLoader(new ClassPathResource(AAD_B2C_ENABLE_CONFIG_FILE_NAME))) |
| 138 | + .withConfiguration(AutoConfigurations.of(WebResourceServerApp.class, |
| 139 | + AADB2CResourceServerAutoConfiguration.class)) |
| 140 | + .withPropertyValues(getWebappCommonPropertyValues()) |
| 141 | + .run(c -> { |
| 142 | + Assertions.assertFalse(c.getResource(AAD_B2C_ENABLE_CONFIG_FILE_NAME).exists()); |
| 143 | + verify(userFlowCondition, never()).getMatchOutcome(any(), any()); |
| 144 | + verify(clientRegistrationCondition, never()).getMatchOutcome(any(), any()); |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
91 | 148 | } |
0 commit comments