-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
18 changed files
with
325 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/test/WebGQLClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* DBeaver - Universal Database Manager | ||
* Copyright (C) 2010-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.cloudbeaver.test; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.jkiss.code.NotNull; | ||
import org.jkiss.code.Nullable; | ||
import org.jkiss.dbeaver.DBException; | ||
import org.jkiss.dbeaver.model.data.json.JSONUtils; | ||
import org.jkiss.dbeaver.utils.GeneralUtils; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* GraphQL client for tests. | ||
*/ | ||
public class WebGQLClient { | ||
private static final Gson gson = new GsonBuilder() | ||
.setPrettyPrinting() | ||
.create(); | ||
public static final String GQL_AUTHENTICATE = """ | ||
query authLogin($provider: ID!, $configuration: ID, $credentials: Object, $linkUser: Boolean, $forceSessionsLogout: Boolean) { | ||
result: authLogin( | ||
provider: $provider | ||
configuration: $configuration | ||
credentials: $credentials | ||
linkUser: $linkUser | ||
forceSessionsLogout: $forceSessionsLogout | ||
) { | ||
authId | ||
authStatus | ||
} | ||
}"""; | ||
|
||
@NotNull | ||
private final HttpClient httpClient; | ||
@NotNull | ||
private final String apiUrl; | ||
|
||
public WebGQLClient(@NotNull HttpClient httpClient, @NotNull String apiUrl) { | ||
this.httpClient = httpClient; | ||
this.apiUrl = apiUrl; | ||
} | ||
|
||
/** | ||
* Sends GraphQL request without additional headers | ||
* | ||
* @param query GraphQL query | ||
* @param variables GraphQL query variables | ||
* @return GraphQL response | ||
*/ | ||
@NotNull | ||
public <T> T sendQuery(@NotNull String query, @Nullable Map<String, Object> variables) throws Exception { | ||
return sendQueryWithHeaders(query, variables, List.of()); | ||
} | ||
|
||
/** | ||
* Sends GraphQL request without additional headers | ||
* | ||
* @param query GraphQL query | ||
* @param variables GraphQL query variables | ||
* @param headers HTTP request headers | ||
* @return GraphQL response | ||
*/ | ||
@NotNull | ||
public <T> T sendQueryWithHeaders( | ||
@NotNull String query, | ||
@Nullable Map<String, Object> variables, | ||
@NotNull List<String> headers | ||
) throws Exception { | ||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() | ||
.uri(URI.create(apiUrl)) | ||
.POST(HttpRequest.BodyPublishers.ofString(makeGQLRequest(query, variables))) | ||
.setHeader("TE-Client-Version", GeneralUtils.getMajorVersion()) | ||
.header("Content-Type", "application/json"); | ||
if (!headers.isEmpty()) { | ||
requestBuilder.headers(headers.toArray(String[]::new)); | ||
} | ||
HttpRequest request = requestBuilder.build(); | ||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
Map<String, Object> body = gson.fromJson( | ||
response.body(), | ||
JSONUtils.MAP_TYPE_TOKEN | ||
); | ||
if (body.containsKey("errors")) { | ||
String message = JSONUtils.getString(JSONUtils.getObjectList(body, "errors").get(0), "message"); | ||
throw new DBException(message); | ||
} | ||
// graphql response will be in "data" key | ||
return (T) JSONUtils.getObject(body, "data").get("result"); | ||
} | ||
|
||
@NotNull | ||
private String makeGQLRequest(@NotNull String text, @Nullable Map<String, Object> variables) { | ||
Map<String, Object> request = new LinkedHashMap<>(); | ||
request.put("query", text); | ||
if (variables != null && !variables.isEmpty()) { | ||
request.put("variables", variables); | ||
} | ||
|
||
return gson.toJson(request, Map.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 37 additions & 40 deletions
77
...est/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/AuthenticationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,61 @@ | ||
/* | ||
* DBeaver - Universal Database Manager | ||
* Copyright (C) 2010-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.cloudbeaver.test.platform; | ||
|
||
import io.cloudbeaver.auth.provider.rp.RPAuthProvider; | ||
import io.cloudbeaver.utils.WebTestUtils; | ||
import io.cloudbeaver.test.WebGQLClient; | ||
import org.jkiss.dbeaver.model.auth.SMAuthStatus; | ||
import org.jkiss.dbeaver.model.data.json.JSONUtils; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class AuthenticationTest { | ||
public static final String GQL_TEMPLATE_OPEN_SESSION = "openSession.json"; | ||
public static final String GQL_TEMPLATE_ACTIVE_USER = "activeUser.json"; | ||
public static final String REVERSE_PROXY_TEST_USER = "reverseProxyTestUser"; | ||
private static final String GQL_OPEN_SESSION = """ | ||
mutation openSession($defaultLocale: String) { | ||
result: openSession(defaultLocale: $defaultLocale) { | ||
valid | ||
} | ||
}"""; | ||
private static final String GQL_ACTIVE_USER = """ | ||
query activeUser { | ||
result: activeUser { | ||
userId | ||
} | ||
}"""; | ||
|
||
@Test | ||
public void testLoginUser() throws Exception { | ||
HttpClient client = CEServerTestSuite.createClient(); | ||
Map<String, Object> authInfo = WebTestUtils.authenticateUser( | ||
client, CEServerTestSuite.getScriptsPath(), CEServerTestSuite.GQL_API_URL); | ||
WebGQLClient client = CEServerTestSuite.createClient(); | ||
Map<String, Object> authInfo = CEServerTestSuite.authenticateTestUser(client); | ||
Assert.assertEquals(SMAuthStatus.SUCCESS.name(), JSONUtils.getString(authInfo, "authStatus")); | ||
} | ||
|
||
@Test | ||
public void testReverseProxyAnonymousModeLogin() throws Exception { | ||
HttpClient client = CEServerTestSuite.createClient(); | ||
Map<String, Object> sessionInfo = openSession(client); | ||
WebGQLClient client = CEServerTestSuite.createClient(); | ||
String testUserId = "reverseProxyTestUser"; | ||
List<String> headers = List.of(RPAuthProvider.X_USER, testUserId, RPAuthProvider.X_TEAM, "user"); | ||
Map<String, Object> sessionInfo = client.sendQueryWithHeaders(GQL_OPEN_SESSION, null, headers); | ||
Assert.assertTrue(JSONUtils.getBoolean(sessionInfo, "valid")); | ||
Map<String, Object> activeUser = getActiveUser(client); | ||
Assert.assertEquals(REVERSE_PROXY_TEST_USER, JSONUtils.getString(activeUser, "userId")); | ||
} | ||
|
||
private Map<String, Object> openSession(HttpClient client) throws Exception { | ||
Map<String, Object> data = doPostQuery(client, GQL_TEMPLATE_OPEN_SESSION); | ||
if (data != null) { | ||
return JSONUtils.getObject(data, "session"); | ||
} | ||
return Collections.emptyMap(); | ||
} | ||
|
||
private Map<String, Object> getActiveUser(HttpClient client) throws Exception { | ||
Map<String, Object> data = doPostQuery(client, GQL_TEMPLATE_ACTIVE_USER); | ||
if (data != null) { | ||
return JSONUtils.getObject(data, "user"); | ||
} | ||
return Collections.emptyMap(); | ||
Map<String, Object> activeUser = client.sendQuery(GQL_ACTIVE_USER, null); | ||
Assert.assertEquals(testUserId, JSONUtils.getString(activeUser, "userId")); | ||
} | ||
|
||
private Map<String, Object> doPostQuery(HttpClient client, String gqlScript) throws Exception { | ||
String input = WebTestUtils.readScriptTemplate(gqlScript, CEServerTestSuite.getScriptsPath()); | ||
List<String> headers = List.of(RPAuthProvider.X_USER, REVERSE_PROXY_TEST_USER, RPAuthProvider.X_TEAM, "user"); | ||
Map<String, Object> map = WebTestUtils.doPostWithHeaders(CEServerTestSuite.GQL_API_URL, input, client, headers); | ||
return JSONUtils.getObjectOrNull(map, "data"); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.