2
2
3
3
import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
4
4
import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
5
+ import com .google .common .collect .Iterables ;
5
6
import lombok .extern .slf4j .Slf4j ;
6
7
import org .junit .jupiter .api .BeforeAll ;
7
8
import org .junit .jupiter .api .BeforeEach ;
11
12
import org .lowcoder .api .common .mockuser .WithMockUser ;
12
13
import org .lowcoder .domain .authentication .AuthenticationService ;
13
14
import org .lowcoder .domain .authentication .FindAuthConfig ;
15
+ import org .lowcoder .domain .encryption .EncryptionService ;
14
16
import org .lowcoder .domain .organization .model .Organization ;
17
+ import org .lowcoder .domain .user .model .Connection ;
15
18
import org .lowcoder .domain .user .model .User ;
16
19
import org .lowcoder .domain .user .model .UserState ;
17
20
import org .lowcoder .domain .user .repository .UserRepository ;
18
21
import org .lowcoder .sdk .auth .AbstractAuthConfig ;
22
+ import org .lowcoder .sdk .auth .EmailAuthConfig ;
19
23
import org .lowcoder .sdk .auth .Oauth2GenericAuthConfig ;
20
24
import org .lowcoder .sdk .auth .constants .AuthTypeConstants ;
21
25
import org .lowcoder .sdk .constants .AuthSourceConstants ;
24
28
import org .mockito .junit .jupiter .MockitoExtension ;
25
29
import org .springframework .beans .factory .annotation .Autowired ;
26
30
import org .springframework .boot .test .context .SpringBootTest ;
31
+ import org .springframework .http .ResponseCookie ;
27
32
import org .springframework .mock .http .server .reactive .MockServerHttpRequest ;
28
33
import org .springframework .mock .web .server .MockServerWebExchange ;
29
34
import org .springframework .test .context .ActiveProfiles ;
35
+ import org .springframework .util .MultiValueMap ;
30
36
import reactor .core .publisher .Mono ;
31
37
import reactor .test .StepVerifier ;
32
38
import reactor .util .context .Context ;
33
39
34
40
import java .util .HashMap ;
41
+ import java .util .Map ;
42
+ import java .util .Objects ;
35
43
36
44
import 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 ;
39
47
40
48
/**
41
49
* This class is for testing GenericAuth feature
@@ -55,6 +63,8 @@ public class GenericAuthenticateTest {
55
63
@ Autowired
56
64
private AuthenticationService authenticationService ;
57
65
@ Autowired
66
+ private EncryptionService encryptionService ;
67
+ @ Autowired
58
68
private InitData initData ;
59
69
60
70
@ BeforeEach
@@ -70,6 +80,8 @@ public void testGoogleLoginSuccess(WireMockRuntimeInfo wmRuntimeInfo) {
70
80
sourceMappings .put ("jwt" , "id_token" );
71
81
sourceMappings .put ("uid" , "sub" );
72
82
sourceMappings .put ("username" , "name" );
83
+ sourceMappings .put ("email" , "email" );
84
+ sourceMappings .put ("avatar" , "picture" );
73
85
var authConfig = Oauth2GenericAuthConfig .builder ()
74
86
.source (AuthSourceConstants .GOOGLE )
75
87
.sourceName (AuthSourceConstants .GOOGLE_NAME )
@@ -91,15 +103,17 @@ public void testGoogleLoginSuccess(WireMockRuntimeInfo wmRuntimeInfo) {
91
103
.userCanSelectAccounts (true )
92
104
.build ();
93
105
94
- var organization = Organization .builder ().build ();
106
+ var organization = Organization .builder ()
107
+ .id ("org01" )
108
+ .build ();
95
109
var mockAuthConfig = new FindAuthConfig (authConfig , organization );
96
110
Mockito .when (authenticationService .findAuthConfigByAuthId (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
97
111
Mockito .when (authenticationService .findAuthConfigBySource (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
98
112
99
113
stubFor (post (urlPathEqualTo ("/oauth2/v4/token" ))
100
114
.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\" }" )));
101
115
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 \" }" )));
103
117
//
104
118
105
119
String source = AuthSourceConstants .GOOGLE ;
@@ -124,9 +138,61 @@ public void testGoogleLoginSuccess(WireMockRuntimeInfo wmRuntimeInfo) {
124
138
assertTrue (user .getIsEnabled ());
125
139
})
126
140
.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
+
127
185
Mockito .framework ().clearInlineMocks ();
128
186
}
129
187
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
+
130
196
private Mono <String > getGenericAuthConfigId (String orgId ) {
131
197
return authenticationService .findAuthConfigBySource (orgId , AuthSourceConstants .GOOGLE )
132
198
.map (FindAuthConfig ::authConfig )
0 commit comments