Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.release>8</maven.compiler.release>
<lombok.version>1.18.36</lombok.version>
<gson.version>2.11.0</gson.version>
<httpclient.version>5.4.1</httpclient.version>
<gson.version>2.12.1</gson.version>
<httpclient.version>5.4.2</httpclient.version>
<lang3.version>3.17.0</lang3.version>
<junit.version>5.11.3</junit.version>
<testcontainers.version>1.20.4</testcontainers.version>
<assertj-core.version>3.26.3</assertj-core.version>
<junit.version>5.12.0</junit.version>
<testcontainers.version>1.20.5</testcontainers.version>
<assertj-core.version>3.27.3</assertj-core.version>
<jparams.version>1.0.4</jparams.version>
<mockito.version>5.14.2</mockito.version>
<slf4j.version>2.0.16</slf4j.version>
<logback.version>1.5.12</logback.version>
<mockito.version>5.15.2</mockito.version>
<slf4j.version>2.0.17</slf4j.version>
<logback.version>1.5.17</logback.version>
<mock-server.version>5.14.0</mock-server.version>
<jackson.version>2.18.2</jackson.version>
<oauth2-oidc-sdk.version>11.20.1</oauth2-oidc-sdk.version>
<jackson.version>2.18.3</jackson.version>
<oauth2-oidc-sdk.version>11.23.1</oauth2-oidc-sdk.version>
<mock-server.version>5.15.0</mock-server.version>
<protobuf.java.version>4.29.1</protobuf.java.version>
<protobuf.java-util.version>4.29.1</protobuf.java-util.version>
<protobuf.java.version>4.29.3</protobuf.java.version>
<protobuf.java-util.version>4.29.3</protobuf.java-util.version>
<grpc-netty-shaded.version>1.68.2</grpc-netty-shaded.version>
<grpc-protobuf.version>1.68.2</grpc-protobuf.version>
<grpc-protobuf.version>1.70.0</grpc-protobuf.version>
<grpc-stub.version>1.68.2</grpc-stub.version>
<annotations-api.version>6.0.53</annotations-api.version>
</properties>
Expand Down
211 changes: 92 additions & 119 deletions src/test/java/io/weaviate/client/WeaviateAuthClientTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package io.weaviate.client;

import io.weaviate.client.v1.auth.exception.AuthException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import org.apache.http.client.methods.HttpGet;
import org.junit.AfterClass;
import org.junit.Test;
Expand All @@ -9,22 +15,16 @@
import org.mockserver.model.HttpRequest;
import org.mockserver.verify.VerificationTimes;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import io.weaviate.client.v1.auth.exception.AuthException;

public class WeaviateAuthClientTest {

private static final String MOCK_SERVER_HOST = "localhost";
private static final int MOCK_SERVER_PORT = 8899;
private static final int OIDC_MOCK_SERVER_PORT = 8999;
private static final Config MOCK_SERVER_CONFIG = new Config(
"http",
String.format("%s:%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT)
);
"http",
String.format("%s:%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT));
private static final String OIDC_PATH = "/v1/.well-known/openid-configuration";

private static final ClientAndServer mockServer = startClientAndServer(MOCK_SERVER_PORT);
Expand All @@ -38,158 +38,132 @@ public static void after() {

@Test
public void test404Case() {
//given
String msg = "Auth001: The client was configured to use authentication, but weaviate is configured without authentication. Are you sure this is " +
"correct?";
// given
String msg = "Auth001: The client was configured to use authentication, but weaviate is configured without authentication. Are you sure this is "
+
"correct?";

mockServer.reset();
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.when(
request().withMethod("GET").withPath(OIDC_PATH)
)
.respond(
response().withStatusCode(404)
);
.when(
request().withMethod("GET").withPath(OIDC_PATH))
.respond(
response().withStatusCode(404));
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.retrieveRecordedRequests(
request().withMethod("GET").withPath(OIDC_PATH)
);
//when
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
);
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
);
AuthException exceptionBearerToken = assertThrows(AuthException.class, () ->
WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token")
);
//then
.retrieveRecordedRequests(
request().withMethod("GET").withPath(OIDC_PATH));
// when
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
AuthException exceptionClientPassword = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
AuthException exceptionBearerToken = assertThrows(AuthException.class,
() -> WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token"));
// then
assertEquals(msg, exceptionClientCredentials.getMessage());
assertEquals(msg, exceptionClientPassword.getMessage());
assertEquals(msg, exceptionBearerToken.getMessage());

new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.verify(
request()
.withPath(OIDC_PATH),
VerificationTimes.exactly(3)
);
.verify(
request()
.withPath(OIDC_PATH),
VerificationTimes.exactly(3));
}

@Test
public void test503Case() {
//given
// given
int statusCode = 503;
String msg = String.format("OIDC configuration url %s returned status code %s",
String.format("http://%s:%s%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT, OIDC_PATH), statusCode);
String.format("http://%s:%s%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT, OIDC_PATH), statusCode);

mockServer.reset();
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.when(
request().withMethod("GET").withPath(OIDC_PATH)
)
.respond(
response().withStatusCode(statusCode)
);
.when(
request().withMethod("GET").withPath(OIDC_PATH))
.respond(
response().withStatusCode(statusCode));
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.retrieveRecordedRequests(
request().withMethod("GET").withPath(OIDC_PATH)
);
//when
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
);
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
);
AuthException exceptionBearerToken = assertThrows(AuthException.class, () ->
WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token")
);
//then
.retrieveRecordedRequests(
request().withMethod("GET").withPath(OIDC_PATH));
// when
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
AuthException exceptionClientPassword = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
AuthException exceptionBearerToken = assertThrows(AuthException.class,
() -> WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token"));
// then
assertEquals(msg, exceptionClientCredentials.getMessage());
assertEquals(msg, exceptionClientPassword.getMessage());
assertEquals(msg, exceptionBearerToken.getMessage());

new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.verify(
request()
.withPath(OIDC_PATH),
VerificationTimes.exactly(6)
);
.verify(
request()
.withPath(OIDC_PATH),
VerificationTimes.exactly(6));
}

@Test
public void test201OIDCHrefCase() {
//given
// given
int statusCode = 201;
String hrefURL = String.format("http://%s:%s/oidc", MOCK_SERVER_HOST, OIDC_MOCK_SERVER_PORT);
String msg = String.format("OIDC configuration url %s returned status code %s",
hrefURL, statusCode);
hrefURL, statusCode);

mockServer.reset();
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.when(
request().withMethod("GET").withPath(OIDC_PATH)
)
.respond(
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL))
);
.when(
request().withMethod("GET").withPath(OIDC_PATH))
.respond(
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL)));
oidcMockServer.reset();
new MockServerClient(MOCK_SERVER_HOST, OIDC_MOCK_SERVER_PORT)
.when(
request().withMethod("GET").withPath("/oidc")
)
.respond(
response().withStatusCode(statusCode)
);
//when
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
);
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
);
AuthException exceptionBearerToken = assertThrows(AuthException.class, () ->
WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token")
);
//then
.when(
request().withMethod("GET").withPath("/oidc"))
.respond(
response().withStatusCode(statusCode));
// when
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
AuthException exceptionClientPassword = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
AuthException exceptionBearerToken = assertThrows(AuthException.class,
() -> WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token"));
// then
assertEquals(msg, exceptionClientCredentials.getMessage());
assertEquals(msg, exceptionClientPassword.getMessage());
assertEquals(msg, exceptionBearerToken.getMessage());
}

@Test
public void test200ParseException() throws AuthException {
//given
// given
String hrefURL = String.format("http://%s:%s/oidc", MOCK_SERVER_HOST, OIDC_MOCK_SERVER_PORT);
String msg = "Invalid JSON: Unexpected token parse-exception} at position 17.";
String msg = "Invalid JSON";

mockServer.reset();
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.when(
request().withMethod("GET").withPath(OIDC_PATH)
)
.respond(
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL))
);
.when(
request().withMethod("GET").withPath(OIDC_PATH))
.respond(
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL)));
oidcMockServer.reset();
new MockServerClient(MOCK_SERVER_HOST, OIDC_MOCK_SERVER_PORT)
.when(
request().withMethod("GET").withPath("/oidc")
)
.respond(
response().withStatusCode(200).withBody("{parse-exception}")
);
//when
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
);
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
);
.when(
request().withMethod("GET").withPath("/oidc"))
.respond(
response().withStatusCode(200).withBody("{parse-exception}"));
// when
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
AuthException exceptionClientPassword = assertThrows(AuthException.class,
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
WeaviateClient weaviateClient = WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "");
//then
// then
assertEquals(msg, exceptionClientCredentials.getMessage());
assertEquals(msg, exceptionClientPassword.getMessage());
assertNotNull(weaviateClient);
Expand All @@ -203,16 +177,15 @@ public void shouldAddApiKeyHeader() throws AuthException {

mockServer.reset();
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.when(requestDefinition)
.respond(response().withStatusCode(200));
.when(requestDefinition)
.respond(response().withStatusCode(200));

WeaviateAuthClient.apiKey(MOCK_SERVER_CONFIG, apiKey).misc().metaGetter().run();

new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
.verify(
request().withMethod(HttpGet.METHOD_NAME).withPath(metaPath)
.withHeader("Authorization", String.format("Bearer %s", apiKey)),
VerificationTimes.once()
);
.verify(
request().withMethod(HttpGet.METHOD_NAME).withPath(metaPath)
.withHeader("Authorization", String.format("Bearer %s", apiKey)),
VerificationTimes.once());
}
}