22
33import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
44import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
5+ import com .google .common .collect .Iterables ;
56import lombok .extern .slf4j .Slf4j ;
67import org .junit .jupiter .api .BeforeAll ;
78import org .junit .jupiter .api .BeforeEach ;
1112import org .lowcoder .api .common .mockuser .WithMockUser ;
1213import org .lowcoder .domain .authentication .AuthenticationService ;
1314import org .lowcoder .domain .authentication .FindAuthConfig ;
15+ import org .lowcoder .domain .encryption .EncryptionService ;
1416import org .lowcoder .domain .organization .model .Organization ;
17+ import org .lowcoder .domain .user .model .Connection ;
1518import org .lowcoder .domain .user .model .User ;
1619import org .lowcoder .domain .user .model .UserState ;
1720import org .lowcoder .domain .user .repository .UserRepository ;
1821import org .lowcoder .sdk .auth .AbstractAuthConfig ;
22+ import org .lowcoder .sdk .auth .EmailAuthConfig ;
1923import org .lowcoder .sdk .auth .Oauth2GenericAuthConfig ;
2024import org .lowcoder .sdk .auth .constants .AuthTypeConstants ;
2125import org .lowcoder .sdk .constants .AuthSourceConstants ;
2428import org .mockito .junit .jupiter .MockitoExtension ;
2529import org .springframework .beans .factory .annotation .Autowired ;
2630import org .springframework .boot .test .context .SpringBootTest ;
31+ import org .springframework .http .ResponseCookie ;
2732import org .springframework .mock .http .server .reactive .MockServerHttpRequest ;
2833import org .springframework .mock .web .server .MockServerWebExchange ;
2934import org .springframework .test .context .ActiveProfiles ;
35+ import org .springframework .util .MultiValueMap ;
3036import reactor .core .publisher .Mono ;
3137import reactor .test .StepVerifier ;
3238import reactor .util .context .Context ;
3339
3440import java .util .HashMap ;
41+ import java .util .Map ;
42+ import java .util .Objects ;
3543
3644import static com .github .tomakehurst .wiremock .client .WireMock .*;
37- import static org .junit .jupiter .api .Assertions .assertEquals ;
38- import static org .junit .jupiter .api .Assertions .assertTrue ;
45+ import static org .junit .jupiter .api .Assertions .* ;
46+ import static org .junit .jupiter .api .Assertions .assertNull ;
3947
4048/**
4149 * This class is for testing GenericAuth feature
@@ -55,6 +63,8 @@ public class GenericAuthenticateTest {
5563 @ Autowired
5664 private AuthenticationService authenticationService ;
5765 @ Autowired
66+ private EncryptionService encryptionService ;
67+ @ Autowired
5868 private InitData initData ;
5969
6070 @ BeforeEach
@@ -70,6 +80,8 @@ public void testGoogleLoginSuccess(WireMockRuntimeInfo wmRuntimeInfo) {
7080 sourceMappings .put ("jwt" , "id_token" );
7181 sourceMappings .put ("uid" , "sub" );
7282 sourceMappings .put ("username" , "name" );
83+ sourceMappings .put ("email" , "email" );
84+ sourceMappings .put ("avatar" , "picture" );
7385 var authConfig = Oauth2GenericAuthConfig .builder ()
7486 .source (AuthSourceConstants .GOOGLE )
7587 .sourceName (AuthSourceConstants .GOOGLE_NAME )
@@ -91,15 +103,17 @@ public void testGoogleLoginSuccess(WireMockRuntimeInfo wmRuntimeInfo) {
91103 .userCanSelectAccounts (true )
92104 .build ();
93105
94- var organization = Organization .builder ().build ();
106+ var organization = Organization .builder ()
107+ .id ("org01" )
108+ .build ();
95109 var mockAuthConfig = new FindAuthConfig (authConfig , organization );
96110 Mockito .when (authenticationService .findAuthConfigByAuthId (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
97111 Mockito .when (authenticationService .findAuthConfigBySource (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
98112
99113 stubFor (post (urlPathEqualTo ("/oauth2/v4/token" ))
100114 .willReturn (okJson ("{\" access_token\" :\" ya29.a0AfH6SMB...\" ,\" expires_in\" :3600,\" token_type\" :\" Bearer\" ,\" scope\" :\" https://www.googleapis.com/auth/userinfo.profile\" ,\" id_token\" : \" eyJhbGciOiJSUzI1NiIsImtpZCI6IjNkNTgwZjBhZjdhY2U2OThhMGNlZTdmMjMwYmNhNTk0ZGM2ZGJiNTUiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIyMjIwMTc1NDgwNzYtcWU1bzBnbWxiMjRtcHRmanE5aGlwazRzNHFvcnN2OGEuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiIyMjIwMTc1NDgwNzYtcWU1bzBnbWxiMjRtcHRmanE5aGlwazRzNHFvcnN2OGEuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTMyNDk5MjY4NDI2Mzg2NDkzNzEiLCJhdF9oYXNoIjoiblRwVUJzamRXbnlMcDZfM2RsM2MxUSIsIm5hbWUiOiJUZWNoIE5pbmphIiwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hL0FDZzhvY0xfS3Z5OXI5SVVibVoySmRlSnJwUUpOY2pUbGhqdHpEY2hvVlJabVdrUkxUUEozOTg9czk2LWMiLCJnaXZlbl9uYW1lIjoiVGVjaCIsImZhbWlseV9uYW1lIjoiTmluamEiLCJpYXQiOjE3MTg4MDUwNzQsImV4cCI6MTcxODgwODY3NH0.UiT0D77_jG5cwAHNZFOT-M2tX-ht2EztOSqZKDfEU7P1-Y6cv0CTESvueI7DC6M_5l942UxXnNBE5SCYluQGNNBg6gLU_lpLNoUnfVUE2ocPjKIxSZeTwyCxYbCtOGL7G5BdWAk-dJhyAudzG3_lLSuz8M7XgomWPC1s7Z5uaD8TH5BdNclV-3kYzNV0CtOPxQ00eAtHhi3MUswTCdwb0cFQl_2GXRdLiXjBGzBY4fe_tarihMDjXXXFrxHsIiB-VcLKWBfl8vRvMxR4Rb\" }" )));
101115 stubFor (get (urlPathEqualTo ("/oauth2/v2/userinfo" ))
102- .willReturn (okJson ("{\" sub\" :\" user001\" ,\" email\" :\" user001@gmail.com \" }" )));
116+ .willReturn (okJson ("{\" sub\" :\" user001\" ,\" email\" :\" test_register@ob.dev \" , \" name \" : \" Tech Ninja \" }" )));
103117 //
104118
105119 String source = AuthSourceConstants .GOOGLE ;
@@ -124,9 +138,61 @@ public void testGoogleLoginSuccess(WireMockRuntimeInfo wmRuntimeInfo) {
124138 assertTrue (user .getIsEnabled ());
125139 })
126140 .verifyComplete ();
141+
142+ String email = "test_register@ob.dev" ;
143+ String password = "lowcoder" ;
144+ String source1 = AuthSourceConstants .EMAIL ;
145+
146+ var authConfig1 = new EmailAuthConfig (AuthSourceConstants .EMAIL , true , true );
147+
148+ mockAuthConfig = new FindAuthConfig (authConfig1 , organization );
149+ Mockito .when (authenticationService .findAuthConfigByAuthId (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
150+ Mockito .when (authenticationService .findAuthConfigBySource (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
151+
152+ String authId1 = getEmailAuthConfigId ();
153+ AuthenticationEndpoints .FormLoginRequest formLoginRequest = new AuthenticationEndpoints .FormLoginRequest (email , password , true , source1 , authId1 );
154+ MockServerHttpRequest request1 = MockServerHttpRequest .post ("" ).build ();
155+ MockServerWebExchange exchange1 = MockServerWebExchange .builder (request1 ).build ();
156+
157+ Mono <User > userMono1 = authenticationController .formLogin (formLoginRequest , null , null , exchange1 )
158+ .then (userRepository .findByConnections_SourceAndConnections_RawId (source1 , email ));
159+
160+ StepVerifier .create (userMono1 )
161+ .assertNext (user -> {
162+ assertEquals (email , user .getEmail ());
163+ assertEquals (UserState .ACTIVATED , user .getState ());
164+ assertTrue (user .getIsEnabled ());
165+ assertTrue (encryptionService .matchPassword (password , user .getPassword ()));
166+ assertFalse (user .getIsAnonymous ());
167+ assertFalse (user .getIsNewUser ());//
168+ assertFalse (user .isHasSetNickname ());
169+ assertNotNull (user .getId ());
170+ //connections
171+ assertEquals (2 , user .getConnections ().size ());
172+ Connection connection = Iterables .getLast (user .getConnections (), null );
173+ assertNotNull (connection );
174+ assertEquals (authId1 , connection .getAuthId ());
175+ assertEquals (source1 , connection .getSource ());
176+ assertEquals (email , connection .getRawId ());
177+ assertEquals (email , connection .getName ());
178+ assertNull (connection .getAvatar ());
179+ assertEquals (1 , connection .getOrgIds ().size ());
180+ assertNull (connection .getAuthConnectionAuthToken ());
181+ assertEquals (Map .of ("email" , email ), connection .getRawUserInfo ());
182+ })
183+ .verifyComplete ();
184+
127185 Mockito .framework ().clearInlineMocks ();
128186 }
129187
188+ private String getEmailAuthConfigId () {
189+ return authenticationService .findAuthConfigBySource (null , AuthSourceConstants .EMAIL )
190+ .map (FindAuthConfig ::authConfig )
191+ .map (AbstractAuthConfig ::getId )
192+ .contextWrite (Context .of (GlobalContext .DOMAIN , "avengers.com" ))
193+ .block ();
194+ }
195+
130196 private Mono <String > getGenericAuthConfigId (String orgId ) {
131197 return authenticationService .findAuthConfigBySource (orgId , AuthSourceConstants .GOOGLE )
132198 .map (FindAuthConfig ::authConfig )
0 commit comments