|
| 1 | +package com.walmartlabs.concord.server.process.form; |
| 2 | + |
| 3 | +/*- |
| 4 | + * ***** |
| 5 | + * Concord |
| 6 | + * ----- |
| 7 | + * Copyright (C) 2017 - 2025 Walmart Inc. |
| 8 | + * ----- |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * ===== |
| 21 | + */ |
| 22 | + |
| 23 | +import com.walmartlabs.concord.server.security.UserPrincipal; |
| 24 | +import com.walmartlabs.concord.server.security.ldap.LdapPrincipal; |
| 25 | +import com.walmartlabs.concord.server.security.ldap.LdapUserInfoProvider; |
| 26 | +import com.walmartlabs.concord.server.user.UserEntry; |
| 27 | +import com.walmartlabs.concord.server.user.UserInfoProvider; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | + |
| 33 | +import java.util.Set; |
| 34 | +import java.util.UUID; |
| 35 | +import java.util.function.Supplier; |
| 36 | + |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 39 | +import static org.mockito.ArgumentMatchers.any; |
| 40 | +import static org.mockito.Mockito.times; |
| 41 | +import static org.mockito.Mockito.verify; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +@ExtendWith(MockitoExtension.class) |
| 45 | +class FormAccessManagerTest { |
| 46 | + |
| 47 | + @Mock |
| 48 | + private LdapUserInfoProvider ldapUserInfoProvider; |
| 49 | + |
| 50 | + @Mock |
| 51 | + private Supplier<LdapPrincipal> ldapPrincipalSupplier; |
| 52 | + |
| 53 | + @Test |
| 54 | + void testGetLdapPrincipalGroupsApiKeyRealm() { |
| 55 | + when(ldapUserInfoProvider.getInfo(any(), any(), any())) |
| 56 | + .thenReturn(UserInfoProvider.UserInfo.builder() |
| 57 | + .groups(Set.of("mock-group")) |
| 58 | + .build()); |
| 59 | + |
| 60 | + var p = getUserPrincipal("apikey"); |
| 61 | + var userGroups = FormAccessManager.getLdapPrincipalGroups(p, ldapUserInfoProvider, ldapPrincipalSupplier); |
| 62 | + |
| 63 | + assertNotNull(userGroups); |
| 64 | + assertEquals(1, userGroups.size()); |
| 65 | + assertEquals("mock-group", userGroups.toArray()[0]); |
| 66 | + |
| 67 | + verify(ldapUserInfoProvider, times(1)).getInfo(any(), any(), any()); |
| 68 | + verify(ldapPrincipalSupplier, times(0)).get(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testGetLdapPrincipalGroupsLdapRealm() { |
| 73 | + when(ldapPrincipalSupplier.get()).thenReturn(getLdapPrincipal(Set.of("mock-group"))); |
| 74 | + |
| 75 | + var p = getUserPrincipal("ldap"); |
| 76 | + var userGroups = FormAccessManager.getLdapPrincipalGroups(p, ldapUserInfoProvider, ldapPrincipalSupplier); |
| 77 | + |
| 78 | + assertNotNull(userGroups); |
| 79 | + assertEquals(1, userGroups.size()); |
| 80 | + assertEquals("mock-group", userGroups.toArray()[0]); |
| 81 | + |
| 82 | + verify(ldapUserInfoProvider, times(0)).getInfo(any(), any(), any()); |
| 83 | + verify(ldapPrincipalSupplier, times(1)).get(); |
| 84 | + } |
| 85 | + |
| 86 | + private static UserPrincipal getUserPrincipal(String realm) { |
| 87 | + return new UserPrincipal(realm, new UserEntry(UUID.randomUUID(), null, null, null, null, null, null, null, false, null, false)); |
| 88 | + } |
| 89 | + |
| 90 | + private static LdapPrincipal getLdapPrincipal(Set<String> groups) { |
| 91 | + return new LdapPrincipal("test", "example.com", null, null, null, null, groups, null); |
| 92 | + } |
| 93 | +} |
0 commit comments