|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.admin.aspect.controller; |
| 19 | + |
| 20 | +import com.google.common.base.Stopwatch; |
| 21 | +import org.apache.shenyu.admin.config.properties.DashboardProperties; |
| 22 | +import org.apache.shenyu.admin.model.custom.UserInfo; |
| 23 | +import org.apache.shenyu.admin.service.DashboardUserService; |
| 24 | +import org.apache.shenyu.admin.utils.SessionUtil; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 28 | +import org.mockito.InjectMocks; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.MockedStatic; |
| 31 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 33 | + |
| 34 | +import java.lang.reflect.Method; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +import static org.mockito.ArgumentMatchers.anyString; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | +import static org.mockito.Mockito.mockStatic; |
| 41 | +import static org.mockito.Mockito.never; |
| 42 | +import static org.mockito.Mockito.verify; |
| 43 | +import static org.mockito.Mockito.when; |
| 44 | + |
| 45 | +/** |
| 46 | + * Test cases for {@link SuperAdminPasswordSafeAdvice}. |
| 47 | + */ |
| 48 | +@ExtendWith(MockitoExtension.class) |
| 49 | +public class SuperAdminPasswordSafeAdviceTest { |
| 50 | + |
| 51 | + @Mock |
| 52 | + private DashboardProperties properties; |
| 53 | + |
| 54 | + @Mock |
| 55 | + private DashboardUserService userService; |
| 56 | + |
| 57 | + @InjectMocks |
| 58 | + private SuperAdminPasswordSafeAdvice advice; |
| 59 | + |
| 60 | + private Object bean; |
| 61 | + |
| 62 | + private Method method; |
| 63 | + |
| 64 | + private Stopwatch stopwatch; |
| 65 | + |
| 66 | + @BeforeEach |
| 67 | + void setUp() throws Exception { |
| 68 | + bean = new Object(); |
| 69 | + method = Object.class.getMethod("toString"); |
| 70 | + stopwatch = Stopwatch.createUnstarted(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testDoPreProcessWhenEnableOnlySuperAdminPermissionIsFalseShouldSkip() { |
| 75 | + when(properties.getEnableOnlySuperAdminPermission()).thenReturn(false); |
| 76 | + |
| 77 | + advice.doPreProcess(bean, method, stopwatch); |
| 78 | + |
| 79 | + verify(properties, never()).getEnableSuperAdminPasswordSafe(); |
| 80 | + verify(userService, never()).checkUserPassword(anyString()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testDoPreProcessWhenEnableSuperAdminPasswordSafeIsFalseShouldSkip() { |
| 85 | + when(properties.getEnableOnlySuperAdminPermission()).thenReturn(true); |
| 86 | + when(properties.getEnableSuperAdminPasswordSafe()).thenReturn(false); |
| 87 | + |
| 88 | + advice.doPreProcess(bean, method, stopwatch); |
| 89 | + |
| 90 | + verify(userService, never()).checkUserPassword(anyString()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testDoPreProcessWhenNotAdminShouldSkip() { |
| 95 | + when(properties.getEnableOnlySuperAdminPermission()).thenReturn(true); |
| 96 | + when(properties.getEnableSuperAdminPasswordSafe()).thenReturn(true); |
| 97 | + |
| 98 | + try (MockedStatic<SessionUtil> sessionUtilMock = mockStatic(SessionUtil.class)) { |
| 99 | + sessionUtilMock.when(SessionUtil::isAdmin).thenReturn(false); |
| 100 | + |
| 101 | + advice.doPreProcess(bean, method, stopwatch); |
| 102 | + |
| 103 | + verify(userService, never()).checkUserPassword(anyString()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testDoPreProcessWhenNoRequiresPermissionsShouldSkip() { |
| 109 | + when(properties.getEnableOnlySuperAdminPermission()).thenReturn(true); |
| 110 | + when(properties.getEnableSuperAdminPasswordSafe()).thenReturn(true); |
| 111 | + |
| 112 | + try (MockedStatic<SessionUtil> sessionUtilMock = mockStatic(SessionUtil.class)) { |
| 113 | + sessionUtilMock.when(SessionUtil::isAdmin).thenReturn(true); |
| 114 | + |
| 115 | + advice.doPreProcess(bean, method, stopwatch); |
| 116 | + |
| 117 | + verify(userService, never()).checkUserPassword(anyString()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testDoPreProcessWhenPermissionNotInListShouldSkip() throws Exception { |
| 123 | + when(properties.getEnableOnlySuperAdminPermission()).thenReturn(true); |
| 124 | + when(properties.getEnableSuperAdminPasswordSafe()).thenReturn(true); |
| 125 | + |
| 126 | + Method methodWithAnnotation = TestController.class.getMethod("testMethod"); |
| 127 | + UserInfo user = mock(UserInfo.class); |
| 128 | + |
| 129 | + try (MockedStatic<SessionUtil> sessionUtilMock = mockStatic(SessionUtil.class); |
| 130 | + MockedStatic<AnnotatedElementUtils> annotatedElementUtilsMock = mockStatic(AnnotatedElementUtils.class)) { |
| 131 | + sessionUtilMock.when(SessionUtil::isAdmin).thenReturn(true); |
| 132 | + sessionUtilMock.when(SessionUtil::visitor).thenReturn(user); |
| 133 | + annotatedElementUtilsMock.when(() -> AnnotatedElementUtils.findMergedAnnotation(methodWithAnnotation, org.apache.shiro.authz.annotation.RequiresPermissions.class)) |
| 134 | + .thenReturn(mock(org.apache.shiro.authz.annotation.RequiresPermissions.class)); |
| 135 | + |
| 136 | + advice.doPreProcess(bean, methodWithAnnotation, stopwatch); |
| 137 | + |
| 138 | + verify(userService, never()).checkUserPassword(anyString()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void testDoPreProcessWhenPermissionInListShouldCheckPassword() throws Exception { |
| 144 | + when(properties.getEnableOnlySuperAdminPermission()).thenReturn(true); |
| 145 | + when(properties.getEnableSuperAdminPasswordSafe()).thenReturn(true); |
| 146 | + List<String> permissions = Arrays.asList("admin:permission", "other:permission"); |
| 147 | + when(properties.getOnlySuperAdminPermission()).thenReturn(permissions); |
| 148 | + |
| 149 | + Method methodWithAnnotation = TestController.class.getMethod("testMethod"); |
| 150 | + org.apache.shiro.authz.annotation.RequiresPermissions requiresPermissions = mock(org.apache.shiro.authz.annotation.RequiresPermissions.class); |
| 151 | + when(requiresPermissions.value()).thenReturn(new String[]{"admin:permission"}); |
| 152 | + UserInfo user = mock(UserInfo.class); |
| 153 | + when(user.getUserId()).thenReturn("userId"); |
| 154 | + |
| 155 | + try (MockedStatic<SessionUtil> sessionUtilMock = mockStatic(SessionUtil.class); |
| 156 | + MockedStatic<AnnotatedElementUtils> annotatedElementUtilsMock = mockStatic(AnnotatedElementUtils.class)) { |
| 157 | + sessionUtilMock.when(SessionUtil::isAdmin).thenReturn(true); |
| 158 | + sessionUtilMock.when(SessionUtil::visitor).thenReturn(user); |
| 159 | + annotatedElementUtilsMock.when(() -> AnnotatedElementUtils.findMergedAnnotation(methodWithAnnotation, org.apache.shiro.authz.annotation.RequiresPermissions.class)) |
| 160 | + .thenReturn(requiresPermissions); |
| 161 | + |
| 162 | + advice.doPreProcess(bean, methodWithAnnotation, stopwatch); |
| 163 | + |
| 164 | + verify(userService).checkUserPassword("userId"); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private static class TestController { |
| 169 | + @org.apache.shiro.authz.annotation.RequiresPermissions("admin:permission") |
| 170 | + public void testMethod() { |
| 171 | + throw new UnsupportedOperationException("testMethod() used for unit test."); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments