|
| 1 | +package org.lowcoder.api.authentication; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.lowcoder.domain.authentication.AuthenticationServiceImpl; |
| 6 | +import org.lowcoder.domain.organization.model.Organization; |
| 7 | +import org.lowcoder.domain.organization.service.OrgMemberService; |
| 8 | +import org.lowcoder.domain.organization.service.OrganizationService; |
| 9 | +import org.lowcoder.sdk.auth.AbstractAuthConfig; |
| 10 | +import org.lowcoder.sdk.config.AuthProperties; |
| 11 | +import org.lowcoder.sdk.config.CommonConfig; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.test.context.junit4.SpringRunner; |
| 16 | +import reactor.core.publisher.Mono; |
| 17 | +import reactor.test.StepVerifier; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +/** |
| 26 | + * This class is for testing GenericAuth feature |
| 27 | + */ |
| 28 | +@SpringBootTest |
| 29 | +@RunWith(SpringRunner.class) |
| 30 | +public class GenericAuthenticateTest { |
| 31 | + |
| 32 | + @InjectMocks |
| 33 | + AuthenticationServiceImpl mockAuthenticationService; |
| 34 | + |
| 35 | + @Mock |
| 36 | + OrgMemberService mockOrgMemberService; |
| 37 | + |
| 38 | + @Mock |
| 39 | + OrganizationService mockOrganizationService; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private AuthProperties authProperties; |
| 43 | + |
| 44 | + @Mock |
| 45 | + private CommonConfig commonConfig; |
| 46 | + |
| 47 | + @Test |
| 48 | + public void findAllAuthConfigsTest() throws Exception { |
| 49 | + String orgId = "org123"; |
| 50 | + boolean enableOnly = true; |
| 51 | + |
| 52 | + // Create mock objects |
| 53 | + Organization mockOrganization = mock(Organization.class); |
| 54 | + List<AbstractAuthConfig> mockAuthConfigs = Arrays.asList(); |
| 55 | + CommonConfig.Workspace mockedWorkspace = new CommonConfig.Workspace(); |
| 56 | + |
| 57 | + // Mock functions |
| 58 | + when(mockOrganization.getAuthConfigs()).thenReturn(mockAuthConfigs); |
| 59 | + when(mockOrganizationService.getByDomain()).thenReturn(Mono.just(mockOrganization)); |
| 60 | + when(mockOrganizationService.getById(orgId)).thenReturn(Mono.just(mockOrganization)); |
| 61 | + when(mockOrgMemberService.doesAtleastOneAdminExist()).thenReturn(Mono.just(true)); |
| 62 | + when(commonConfig.getWorkspace()).thenReturn(mockedWorkspace); |
| 63 | + |
| 64 | + // Mocking auth properties email configuration |
| 65 | + AuthProperties.Email emailConfig = new AuthProperties.Email(); |
| 66 | + emailConfig.setEnable(true); |
| 67 | + emailConfig.setEnableRegister(true); |
| 68 | + when(authProperties.getEmail()).thenReturn(emailConfig); |
| 69 | + |
| 70 | + // Use reflection to access the private method |
| 71 | + Method findAllAuthConfigsByDomain = AuthenticationServiceImpl.class.getDeclaredMethod("findAllAuthConfigsByDomain"); |
| 72 | + findAllAuthConfigsByDomain.setAccessible(true); |
| 73 | + Method findAllAuthConfigsForEnterpriseMode = AuthenticationServiceImpl.class.getDeclaredMethod("findAllAuthConfigsForEnterpriseMode"); |
| 74 | + findAllAuthConfigsForEnterpriseMode.setAccessible(true); |
| 75 | + Method findAllAuthConfigsForSaasMode = AuthenticationServiceImpl.class.getDeclaredMethod("findAllAuthConfigsForSaasMode", String.class); |
| 76 | + findAllAuthConfigsForSaasMode.setAccessible(true); |
| 77 | + |
| 78 | + // Act & Assert |
| 79 | + StepVerifier.create(mockAuthenticationService.findAllAuthConfigs(orgId, enableOnly)) |
| 80 | + .expectNextMatches(findAuthConfig -> findAuthConfig.authConfig().isEnable()) |
| 81 | + .verifyComplete(); |
| 82 | + } |
| 83 | +} |
0 commit comments