1
1
package org .lowcoder .api .authentication ;
2
2
3
- import com .google .common .collect .Iterables ;
4
- import org .junit .Test ;
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 ;
3
+ import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
4
+ import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
5
+ import lombok .extern .slf4j .Slf4j ;
6
+ import org .junit .jupiter .api .Test ;
7
+ import org .junit .jupiter .api .extension .ExtendWith ;
11
8
import org .lowcoder .api .common .mockuser .WithMockUser ;
12
- import org .lowcoder .api .framework .view .ResponseView ;
13
9
import org .lowcoder .domain .authentication .AuthenticationService ;
14
- import org .lowcoder .domain .authentication .AuthenticationServiceImpl ;
15
10
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 .* ;
11
+ import org .lowcoder .domain .organization . model . Organization ;
12
+ import org .lowcoder .domain .user . model . User ;
13
+ import org .lowcoder .domain .user .model .UserState ;
19
14
import org .lowcoder .domain .user .repository .UserRepository ;
20
15
import org .lowcoder .sdk .auth .AbstractAuthConfig ;
16
+ import org .lowcoder .sdk .auth .Oauth2GenericAuthConfig ;
17
+ import org .lowcoder .sdk .auth .constants .AuthTypeConstants ;
21
18
import org .lowcoder .sdk .constants .AuthSourceConstants ;
22
19
import org .lowcoder .sdk .constants .GlobalContext ;
23
- import org .mockito .InjectMocks ;
24
- import org .mockito .Mock ;
25
20
import org .mockito .Mockito ;
21
+ import org .mockito .junit .jupiter .MockitoExtension ;
26
22
import org .springframework .beans .factory .annotation .Autowired ;
27
23
import org .springframework .boot .test .context .SpringBootTest ;
28
- import org .springframework .boot .test .mock .mockito .MockBean ;
29
- import org .springframework .http .ResponseCookie ;
30
24
import org .springframework .mock .http .server .reactive .MockServerHttpRequest ;
31
25
import org .springframework .mock .web .server .MockServerWebExchange ;
32
- import org .springframework .test .context .junit4 .SpringRunner ;
33
- import org .springframework .util .MultiValueMap ;
26
+ import org .springframework .test .context .ActiveProfiles ;
34
27
import reactor .core .publisher .Mono ;
35
28
import reactor .test .StepVerifier ;
36
29
import reactor .util .context .Context ;
37
30
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 ;
31
+ import static com .github .tomakehurst .wiremock .client .WireMock .*;
32
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
33
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
43
34
44
35
/**
45
36
* This class is for testing GenericAuth feature
46
37
*/
38
+ @ ActiveProfiles ("test" )
47
39
@ SpringBootTest
48
- @ RunWith (SpringRunner .class )
40
+ //@RunWith(SpringRunner.class)
41
+ @ WireMockTest
42
+ @ ExtendWith (MockitoExtension .class )
43
+ @ Slf4j
49
44
public class GenericAuthenticateTest {
50
45
51
46
@ Autowired
@@ -57,54 +52,56 @@ public class GenericAuthenticateTest {
57
52
58
53
@ Test
59
54
@ 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" ;
65
-
66
- GenericAuthRequest .setTest (true );
67
- String uid = "uId" ;
68
-
69
- MockServerHttpRequest request = MockServerHttpRequest .post ("" ).build ();
70
- MockServerWebExchange exchange = MockServerWebExchange .builder (request ).build ();
71
-
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 ));
55
+ public void testGoogleLoginSuccess (WireMockRuntimeInfo wmRuntimeInfo ) {
56
+ log .info ("Running mock server on port: {}" , wmRuntimeInfo .getHttpPort ());
57
+ //Begin mocking services
58
+ var authConfig = Oauth2GenericAuthConfig .builder ()
59
+ .source (AuthSourceConstants .GOOGLE )
60
+ .sourceName (AuthSourceConstants .GOOGLE_NAME )
61
+ .enable (true )
62
+ .enableRegister (true )
63
+ .authType (AuthTypeConstants .GENERIC )
64
+ .clientId ("clientid" )
65
+ .clientSecret ("clientsecret" )
66
+ .sourceDescription ("Google Auth" )
67
+ .sourceIcon ("" )
68
+ .sourceCategory ("cat" )
69
+ .issuerUri ("http://google.com" )
70
+ .authorizationEndpoint (wmRuntimeInfo .getHttpBaseUrl () + "/oauth2/v4/token" )
71
+ .tokenEndpoint (wmRuntimeInfo .getHttpBaseUrl () + "/oauth2/v4/token" )
72
+ .userInfoEndpoint (wmRuntimeInfo .getHttpBaseUrl () + "/oauth2/v2/userinfo" )
73
+ .scope ("scope" )
74
+ .userInfoIntrospection (true )
75
+ .build ();
76
+
77
+ var organization = Organization .builder ().build ();
78
+ var mockAuthConfig = new FindAuthConfig (authConfig , organization );
79
+ Mockito .when (authenticationService .findAuthConfigByAuthId (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
80
+ Mockito .when (authenticationService .findAuthConfigBySource (Mockito .any (), Mockito .any ())).thenReturn (Mono .just (mockAuthConfig ));
81
+
82
+ stubFor (post (urlPathEqualTo ("/oauth2/v4/token" ))
83
+ .willReturn (okJson ("{\" access_token\" :\" ya29.a0AfH6SMB...\" ,\" expires_in\" :3600,\" token_type\" :\" Bearer\" ,\" scope\" :\" https://www.googleapis.com/auth/userinfo.profile\" }" )));
84
+ stubFor (get (urlPathEqualTo ("/oauth2/v2/userinfo" ))
85
+ .willReturn (okJson ("{\" sub\" :\" user001\" ,\" email\" :\" user001@gmail.com\" }" )));
86
+ //
75
87
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
- })
83
- .verifyComplete ();
84
- }
85
-
86
- @ Test
87
- @ WithMockUser
88
- public void testGoogleLoginWithNoRefreshSuccess () {
89
88
String source = AuthSourceConstants .GOOGLE ;
90
89
String code = "test-code-123456" ;
91
90
String orgId = "org01" ;
92
91
String redirectUrl = "https://test.com" ;
93
92
94
- GenericAuthRequest .setTest (true );
95
- GenericAuthRequest .setTestCase01 (true );
96
- String uid = "uId" ;
93
+ String uid = "user001" ;
97
94
98
95
MockServerHttpRequest request = MockServerHttpRequest .post ("" ).build ();
99
96
MockServerWebExchange exchange = MockServerWebExchange .builder (request ).build ();
100
97
101
98
var authId = getGenericAuthConfigId (orgId ).block ();
102
- Mono <User > userMono = authenticationController .loginWithThirdParty (authId , source , code , null , redirectUrl , orgId , exchange )
99
+ Mono <User > userMono = authenticationController .loginWithThirdParty (authId , source , code , null , redirectUrl , orgId , exchange ). hasElement ()
103
100
.then (userRepository .findByConnections_SourceAndConnections_RawId (source , uid ));
104
101
105
102
StepVerifier .create (userMono )
106
103
.assertNext (user -> {
107
- assertEquals ("dummyname " , user .getName ());
104
+ assertEquals ("user001@gmail.com " , user .getName ());
108
105
assertEquals (UserState .ACTIVATED , user .getState ());
109
106
assertEquals (1 , user .getConnections ().size ());
110
107
assertTrue (user .getIsEnabled ());
0 commit comments