Skip to content

Commit 54f8c95

Browse files
committed
test: fix expected error message
1 parent 8d29903 commit 54f8c95

File tree

1 file changed

+92
-119
lines changed

1 file changed

+92
-119
lines changed
Lines changed: 92 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.weaviate.client;
22

3-
import io.weaviate.client.v1.auth.exception.AuthException;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
7+
import static org.mockserver.model.HttpRequest.request;
8+
import static org.mockserver.model.HttpResponse.response;
9+
410
import org.apache.http.client.methods.HttpGet;
511
import org.junit.AfterClass;
612
import org.junit.Test;
@@ -9,22 +15,16 @@
915
import org.mockserver.model.HttpRequest;
1016
import org.mockserver.verify.VerificationTimes;
1117

12-
import static org.junit.Assert.assertEquals;
13-
import static org.junit.Assert.assertNotNull;
14-
import static org.junit.jupiter.api.Assertions.assertThrows;
15-
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
16-
import static org.mockserver.model.HttpRequest.request;
17-
import static org.mockserver.model.HttpResponse.response;
18+
import io.weaviate.client.v1.auth.exception.AuthException;
1819

1920
public class WeaviateAuthClientTest {
2021

2122
private static final String MOCK_SERVER_HOST = "localhost";
2223
private static final int MOCK_SERVER_PORT = 8899;
2324
private static final int OIDC_MOCK_SERVER_PORT = 8999;
2425
private static final Config MOCK_SERVER_CONFIG = new Config(
25-
"http",
26-
String.format("%s:%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT)
27-
);
26+
"http",
27+
String.format("%s:%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT));
2828
private static final String OIDC_PATH = "/v1/.well-known/openid-configuration";
2929

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

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

4546
mockServer.reset();
4647
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
47-
.when(
48-
request().withMethod("GET").withPath(OIDC_PATH)
49-
)
50-
.respond(
51-
response().withStatusCode(404)
52-
);
48+
.when(
49+
request().withMethod("GET").withPath(OIDC_PATH))
50+
.respond(
51+
response().withStatusCode(404));
5352
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
54-
.retrieveRecordedRequests(
55-
request().withMethod("GET").withPath(OIDC_PATH)
56-
);
57-
//when
58-
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
59-
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
60-
);
61-
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
62-
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
63-
);
64-
AuthException exceptionBearerToken = assertThrows(AuthException.class, () ->
65-
WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token")
66-
);
67-
//then
53+
.retrieveRecordedRequests(
54+
request().withMethod("GET").withPath(OIDC_PATH));
55+
// when
56+
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
57+
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
58+
AuthException exceptionClientPassword = assertThrows(AuthException.class,
59+
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
60+
AuthException exceptionBearerToken = assertThrows(AuthException.class,
61+
() -> WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token"));
62+
// then
6863
assertEquals(msg, exceptionClientCredentials.getMessage());
6964
assertEquals(msg, exceptionClientPassword.getMessage());
7065
assertEquals(msg, exceptionBearerToken.getMessage());
7166

7267
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
73-
.verify(
74-
request()
75-
.withPath(OIDC_PATH),
76-
VerificationTimes.exactly(3)
77-
);
68+
.verify(
69+
request()
70+
.withPath(OIDC_PATH),
71+
VerificationTimes.exactly(3));
7872
}
7973

8074
@Test
8175
public void test503Case() {
82-
//given
76+
// given
8377
int statusCode = 503;
8478
String msg = String.format("OIDC configuration url %s returned status code %s",
85-
String.format("http://%s:%s%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT, OIDC_PATH), statusCode);
79+
String.format("http://%s:%s%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT, OIDC_PATH), statusCode);
8680

8781
mockServer.reset();
8882
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
89-
.when(
90-
request().withMethod("GET").withPath(OIDC_PATH)
91-
)
92-
.respond(
93-
response().withStatusCode(statusCode)
94-
);
83+
.when(
84+
request().withMethod("GET").withPath(OIDC_PATH))
85+
.respond(
86+
response().withStatusCode(statusCode));
9587
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
96-
.retrieveRecordedRequests(
97-
request().withMethod("GET").withPath(OIDC_PATH)
98-
);
99-
//when
100-
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
101-
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
102-
);
103-
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
104-
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
105-
);
106-
AuthException exceptionBearerToken = assertThrows(AuthException.class, () ->
107-
WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token")
108-
);
109-
//then
88+
.retrieveRecordedRequests(
89+
request().withMethod("GET").withPath(OIDC_PATH));
90+
// when
91+
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
92+
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
93+
AuthException exceptionClientPassword = assertThrows(AuthException.class,
94+
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
95+
AuthException exceptionBearerToken = assertThrows(AuthException.class,
96+
() -> WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token"));
97+
// then
11098
assertEquals(msg, exceptionClientCredentials.getMessage());
11199
assertEquals(msg, exceptionClientPassword.getMessage());
112100
assertEquals(msg, exceptionBearerToken.getMessage());
113101

114102
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
115-
.verify(
116-
request()
117-
.withPath(OIDC_PATH),
118-
VerificationTimes.exactly(6)
119-
);
103+
.verify(
104+
request()
105+
.withPath(OIDC_PATH),
106+
VerificationTimes.exactly(6));
120107
}
121108

122109
@Test
123110
public void test201OIDCHrefCase() {
124-
//given
111+
// given
125112
int statusCode = 201;
126113
String hrefURL = String.format("http://%s:%s/oidc", MOCK_SERVER_HOST, OIDC_MOCK_SERVER_PORT);
127114
String msg = String.format("OIDC configuration url %s returned status code %s",
128-
hrefURL, statusCode);
115+
hrefURL, statusCode);
129116

130117
mockServer.reset();
131118
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
132-
.when(
133-
request().withMethod("GET").withPath(OIDC_PATH)
134-
)
135-
.respond(
136-
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL))
137-
);
119+
.when(
120+
request().withMethod("GET").withPath(OIDC_PATH))
121+
.respond(
122+
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL)));
138123
oidcMockServer.reset();
139124
new MockServerClient(MOCK_SERVER_HOST, OIDC_MOCK_SERVER_PORT)
140-
.when(
141-
request().withMethod("GET").withPath("/oidc")
142-
)
143-
.respond(
144-
response().withStatusCode(statusCode)
145-
);
146-
//when
147-
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
148-
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
149-
);
150-
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
151-
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
152-
);
153-
AuthException exceptionBearerToken = assertThrows(AuthException.class, () ->
154-
WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token")
155-
);
156-
//then
125+
.when(
126+
request().withMethod("GET").withPath("/oidc"))
127+
.respond(
128+
response().withStatusCode(statusCode));
129+
// when
130+
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
131+
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
132+
AuthException exceptionClientPassword = assertThrows(AuthException.class,
133+
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
134+
AuthException exceptionBearerToken = assertThrows(AuthException.class,
135+
() -> WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "refresh-token"));
136+
// then
157137
assertEquals(msg, exceptionClientCredentials.getMessage());
158138
assertEquals(msg, exceptionClientPassword.getMessage());
159139
assertEquals(msg, exceptionBearerToken.getMessage());
160140
}
161141

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

168148
mockServer.reset();
169149
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
170-
.when(
171-
request().withMethod("GET").withPath(OIDC_PATH)
172-
)
173-
.respond(
174-
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL))
175-
);
150+
.when(
151+
request().withMethod("GET").withPath(OIDC_PATH))
152+
.respond(
153+
response().withStatusCode(200).withBody(String.format("{\"href\":\"%s\"}", hrefURL)));
176154
oidcMockServer.reset();
177155
new MockServerClient(MOCK_SERVER_HOST, OIDC_MOCK_SERVER_PORT)
178-
.when(
179-
request().withMethod("GET").withPath("/oidc")
180-
)
181-
.respond(
182-
response().withStatusCode(200).withBody("{parse-exception}")
183-
);
184-
//when
185-
AuthException exceptionClientCredentials = assertThrows(AuthException.class, () ->
186-
WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null)
187-
);
188-
AuthException exceptionClientPassword = assertThrows(AuthException.class, () ->
189-
WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null)
190-
);
156+
.when(
157+
request().withMethod("GET").withPath("/oidc"))
158+
.respond(
159+
response().withStatusCode(200).withBody("{parse-exception}"));
160+
// when
161+
AuthException exceptionClientCredentials = assertThrows(AuthException.class,
162+
() -> WeaviateAuthClient.clientCredentials(MOCK_SERVER_CONFIG, "some-secret", null));
163+
AuthException exceptionClientPassword = assertThrows(AuthException.class,
164+
() -> WeaviateAuthClient.clientPassword(MOCK_SERVER_CONFIG, "user", "pass", null));
191165
WeaviateClient weaviateClient = WeaviateAuthClient.bearerToken(MOCK_SERVER_CONFIG, "access-token", 0l, "");
192-
//then
166+
// then
193167
assertEquals(msg, exceptionClientCredentials.getMessage());
194168
assertEquals(msg, exceptionClientPassword.getMessage());
195169
assertNotNull(weaviateClient);
@@ -203,16 +177,15 @@ public void shouldAddApiKeyHeader() throws AuthException {
203177

204178
mockServer.reset();
205179
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
206-
.when(requestDefinition)
207-
.respond(response().withStatusCode(200));
180+
.when(requestDefinition)
181+
.respond(response().withStatusCode(200));
208182

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

211185
new MockServerClient(MOCK_SERVER_HOST, MOCK_SERVER_PORT)
212-
.verify(
213-
request().withMethod(HttpGet.METHOD_NAME).withPath(metaPath)
214-
.withHeader("Authorization", String.format("Bearer %s", apiKey)),
215-
VerificationTimes.once()
216-
);
186+
.verify(
187+
request().withMethod(HttpGet.METHOD_NAME).withPath(metaPath)
188+
.withHeader("Authorization", String.format("Bearer %s", apiKey)),
189+
VerificationTimes.once());
217190
}
218191
}

0 commit comments

Comments
 (0)