Skip to content

Commit 889a488

Browse files
committed
test(oidc): await token expiry
1 parent f5db0a9 commit 889a488

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

src/it/java/io/weaviate/integration/OIDCSupportITest.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.net.InetSocketAddress;
55
import java.net.Socket;
6+
import java.time.Duration;
67
import java.util.List;
78
import java.util.UUID;
89

@@ -13,6 +14,7 @@
1314
import io.weaviate.ConcurrentTest;
1415
import io.weaviate.client6.v1.api.Authentication;
1516
import io.weaviate.client6.v1.internal.TokenProvider;
17+
import io.weaviate.client6.v1.internal.TokenProvider.Token;
1618
import io.weaviate.client6.v1.internal.rest.RestTransport;
1719
import io.weaviate.containers.Weaviate;
1820

@@ -88,9 +90,10 @@ public void test_resourceOwnerPassword() throws Exception {
8890
// Get the token obtained by the wrapped TokenProvider.
8991
var t = auth.getToken();
9092

91-
// Now make all tokens expire immediately, forcing the client to refresh..
92-
// Verify the new token is different from the one before.
93-
auth.setExpiresIn(0);
93+
// Wait for the token to expire. Because we're testing against a live IdP
94+
// and cannot control it at test, all other "mocking" options are more
95+
// intrusive. Verify the new token is different from the one before.
96+
waitForExpiry(t);
9497
pingWeaviate(wcsContainer, auth);
9598

9699
var newT = auth.getToken();
@@ -113,9 +116,10 @@ public void test_clientCredentials() throws Exception {
113116
// Get the token obtained by the wrapped TokenProvider.
114117
var t = auth.getToken();
115118

116-
// Now make all tokens expire immediately, forcing the client to refresh..
117-
// Verify the new token is different from the one before.
118-
auth.setExpiresIn(0);
119+
// Wait for the token to expire. Because we're testing against a live IdP
120+
// and cannot control it at test, all other "mocking" options are more
121+
// intrusive. Verify the new token is different from the one before.
122+
waitForExpiry(t);
119123
pingWeaviate(oktaContainer, auth);
120124

121125
var newT = auth.getToken();
@@ -170,6 +174,20 @@ private static boolean ping(String site) {
170174
}
171175
}
172176

177+
private static void waitForExpiry(Token t) {
178+
if (!t.isValid()) {
179+
return;
180+
}
181+
if (t.neverExpires()) {
182+
throw new IllegalStateException("token never expires");
183+
}
184+
try {
185+
Thread.sleep(Duration.ofSeconds(t.expiresIn()).toMillis());
186+
} catch (InterruptedException e) {
187+
Thread.currentThread().interrupt();
188+
}
189+
}
190+
173191
/**
174192
* SpyTokenProvider is an Authentication implementation that spies on the
175193
* TokenProvider it creates and can expose tokens generated by it.
@@ -188,7 +206,6 @@ static Token stealToken(Authentication auth) throws Exception {
188206
return spy.getToken();
189207
}
190208

191-
private Long expiresIn;
192209
private Authentication authentication;
193210
private TokenProvider tokenProvider;
194211

@@ -204,17 +221,7 @@ public TokenProvider getTokenProvider(RestTransport transport) {
204221

205222
@Override
206223
public Token getToken() {
207-
var t = tokenProvider.getToken();
208-
if (expiresIn != null) {
209-
t = Token.expireAfter(t.accessToken(), t.refreshToken(), expiresIn);
210-
}
211-
return t;
212-
}
213-
214-
/** Expire all tokens in {@code expiresIn} seconds. */
215-
void setExpiresIn(long expiresIn) {
216-
this.expiresIn = expiresIn;
224+
return tokenProvider.getToken();
217225
}
218-
219226
}
220227
}

src/test/java/io/weaviate/client6/v1/api/WeaviateClientAsyncTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public void testFailedConnection() {
1414

1515
@Test(expected = WeaviateConnectException.class)
1616
public void testFailedConnection_Local() {
17+
// You might see a warning from gRPC saying that the channel has been
18+
// garbage-collected before it was closed. The stack trace will probably
19+
// show that it's related to this test.
1720
WeaviateClientAsync.connectToLocal(conn -> conn.port(1234));
1821
}
1922

src/test/java/io/weaviate/client6/v1/api/WeaviateClientTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@ public void testFailedConnection() {
1414

1515
@Test(expected = WeaviateConnectException.class)
1616
public void testFailedConnection_Local() throws Exception {
17-
// This test will fail if SOME Weaviate container is running on your machine
18-
// with default :8080 port exposed. All Testcontainer instances started by
19-
// the client's test suite expose random ports, which will not interferen with
20-
// this test.
21-
//
22-
// You might also see a warning from gRPC saying that the channel has been
17+
// You might see a warning from gRPC saying that the channel has been
2318
// garbage-collected before it was closed. The stack trace will probably
2419
// show that it's related to this test.
25-
WeaviateClient.connectToLocal();
20+
WeaviateClient.connectToLocal(conn -> conn.port(1234));
2621
}
2722

2823
@Test(expected = WeaviateConnectException.class)

0 commit comments

Comments
 (0)