|
1 | 1 | package org.lowcoder.api.authentication;
|
2 | 2 |
|
| 3 | +import com.google.common.collect.Iterables; |
3 | 4 | import org.junit.Test;
|
4 | 5 | import org.junit.runner.RunWith;
|
| 6 | +import org.lowcoder.api.authentication.request.AuthRequest; |
| 7 | +import org.lowcoder.api.authentication.request.AuthRequestFactory; |
| 8 | +import org.lowcoder.api.authentication.request.oauth2.Oauth2AuthRequestFactory; |
| 9 | +import org.lowcoder.api.authentication.request.oauth2.request.GenericAuthRequest; |
| 10 | +import org.lowcoder.api.authentication.service.AuthenticationApiServiceImpl; |
| 11 | +import org.lowcoder.api.common.mockuser.WithMockUser; |
| 12 | +import org.lowcoder.api.framework.view.ResponseView; |
| 13 | +import org.lowcoder.domain.authentication.AuthenticationService; |
5 | 14 | 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; |
| 15 | +import org.lowcoder.domain.authentication.FindAuthConfig; |
| 16 | +import org.lowcoder.domain.authentication.context.AuthRequestContext; |
| 17 | +import org.lowcoder.domain.encryption.EncryptionService; |
| 18 | +import org.lowcoder.domain.user.model.*; |
| 19 | +import org.lowcoder.domain.user.repository.UserRepository; |
9 | 20 | import org.lowcoder.sdk.auth.AbstractAuthConfig;
|
10 |
| -import org.lowcoder.sdk.config.AuthProperties; |
11 |
| -import org.lowcoder.sdk.config.CommonConfig; |
| 21 | +import org.lowcoder.sdk.constants.AuthSourceConstants; |
| 22 | +import org.lowcoder.sdk.constants.GlobalContext; |
12 | 23 | import org.mockito.InjectMocks;
|
13 | 24 | import org.mockito.Mock;
|
| 25 | +import org.mockito.Mockito; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
14 | 27 | import org.springframework.boot.test.context.SpringBootTest;
|
| 28 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 29 | +import org.springframework.http.ResponseCookie; |
| 30 | +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; |
| 31 | +import org.springframework.mock.web.server.MockServerWebExchange; |
15 | 32 | import org.springframework.test.context.junit4.SpringRunner;
|
| 33 | +import org.springframework.util.MultiValueMap; |
16 | 34 | import reactor.core.publisher.Mono;
|
17 | 35 | import reactor.test.StepVerifier;
|
| 36 | +import reactor.util.context.Context; |
18 | 37 |
|
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; |
| 38 | +import java.util.Objects; |
| 39 | + |
| 40 | +import static org.junit.Assert.*; |
| 41 | +import static org.junit.Assert.assertTrue; |
| 42 | +import static org.mockito.ArgumentMatchers.any; |
24 | 43 |
|
25 | 44 | /**
|
26 | 45 | * This class is for testing GenericAuth feature
|
|
29 | 48 | @RunWith(SpringRunner.class)
|
30 | 49 | public class GenericAuthenticateTest {
|
31 | 50 |
|
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; |
| 51 | + @Autowired |
| 52 | + private AuthenticationController authenticationController; |
| 53 | + @Autowired |
| 54 | + private UserRepository userRepository; |
| 55 | + @Autowired |
| 56 | + private AuthenticationService authenticationService; |
46 | 57 |
|
47 | 58 | @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(); |
| 59 | + @WithMockUser |
| 60 | + public void testGoogleLoginSuccess() { |
| 61 | + String source = AuthSourceConstants.GOOGLE; |
| 62 | + String code = "test-code-123456"; |
| 63 | + String orgId = "org01"; |
| 64 | + String redirectUrl = "https://test.com"; |
56 | 65 |
|
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); |
| 66 | + GenericAuthRequest.setIsTest(true); |
| 67 | + String uid = "uId"; |
63 | 68 |
|
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 | + MockServerHttpRequest request = MockServerHttpRequest.post("").build(); |
| 70 | + MockServerWebExchange exchange = MockServerWebExchange.builder(request).build(); |
69 | 71 |
|
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); |
| 72 | + var authId = getGenericAuthConfigId(orgId).block(); |
| 73 | + Mono<User> userMono = authenticationController.loginWithThirdParty(authId, source, code, null, redirectUrl, orgId, exchange) |
| 74 | + .then(userRepository.findByConnections_SourceAndConnections_RawId(source, uid)); |
77 | 75 |
|
78 |
| - // Act & Assert |
79 |
| - StepVerifier.create(mockAuthenticationService.findAllAuthConfigs(orgId, enableOnly)) |
80 |
| - .expectNextMatches(findAuthConfig -> findAuthConfig.authConfig().isEnable()) |
| 76 | + StepVerifier.create(userMono) |
| 77 | + .assertNext(user -> { |
| 78 | + assertEquals("dummyname", user.getName()); |
| 79 | + assertEquals(UserState.ACTIVATED, user.getState()); |
| 80 | + assertEquals(1, user.getConnections().size()); |
| 81 | + assertTrue(user.getIsEnabled()); |
| 82 | + }) |
81 | 83 | .verifyComplete();
|
82 | 84 | }
|
| 85 | + |
| 86 | + private Mono<String> getGenericAuthConfigId(String orgId) { |
| 87 | + return authenticationService.findAuthConfigBySource(orgId, AuthSourceConstants.GOOGLE) |
| 88 | + .map(FindAuthConfig::authConfig) |
| 89 | + .map(AbstractAuthConfig::getId) |
| 90 | + .contextWrite(Context.of(GlobalContext.DOMAIN, "avengers.com")); |
| 91 | + } |
83 | 92 | }
|
0 commit comments