Skip to content

Commit b76dbdb

Browse files
committed
Add test for AppInstallationToken
1 parent 1443730 commit b76dbdb

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,15 @@ static class AppInstallationToken implements Serializable {
210210
* The time-to-live for the token may be less than this if the initial expiration for the token when
211211
* it is returned from GitHub is less than this.
212212
*/
213-
private final static long MINIMUM_SECONDS_UNTIL_EXPIRATION = Duration.ofMinutes(45).getSeconds();
213+
static final long MINIMUM_SECONDS_UNTIL_EXPIRATION = Duration.ofMinutes(45).getSeconds();
214214

215215
/**
216216
* Any token older than this is considered stale.
217217
*
218218
* This is a back stop to ensure that, in case of unforeseen error,
219219
* expired tokens are not accidentally retained past their expiration.
220220
*/
221-
private static final long MAXIMUM_AGE_SECONDS = Duration.ofMinutes(30).getSeconds();
221+
static final long MAXIMUM_AGE_SECONDS = Duration.ofMinutes(30).getSeconds();
222222

223223
private final String token;
224224
private final long tokenStaleEpochSeconds;
@@ -227,25 +227,25 @@ static class AppInstallationToken implements Serializable {
227227
* Create a AppInstallationToken instance.
228228
*
229229
* @param token the token string
230-
* @param tokenExpirationEpochSeconds the time in epoch seconds that this token will expire
230+
* @param expirationEpochSeconds the time in epoch seconds that this token will expire
231231
*/
232-
public AppInstallationToken(String token, long tokenExpirationEpochSeconds) {
232+
public AppInstallationToken(String token, long expirationEpochSeconds) {
233233
long nextSecond = Instant.now().getEpochSecond() + 1;
234234

235235
// Tokens go stale a while before they will expire
236-
long tokenStaleEpochSeconds = tokenExpirationEpochSeconds - MINIMUM_SECONDS_UNTIL_EXPIRATION;
236+
long staleEpochSeconds = expirationEpochSeconds - MINIMUM_SECONDS_UNTIL_EXPIRATION;
237237

238238
// Tokens are not stale as soon as they are made
239-
if (tokenStaleEpochSeconds < nextSecond) {
240-
tokenStaleEpochSeconds = nextSecond;
239+
if (staleEpochSeconds < nextSecond) {
240+
staleEpochSeconds = nextSecond;
241241
} else {
242242
// Tokens have a maximum age at which they go stale
243-
tokenStaleEpochSeconds = Math.min(tokenExpirationEpochSeconds, nextSecond + MAXIMUM_AGE_SECONDS);
243+
staleEpochSeconds = Math.min(staleEpochSeconds, nextSecond + MAXIMUM_AGE_SECONDS);
244244
}
245-
LOGGER.log(Level.FINER, "Token stale time epoch seconds: {0}", tokenStaleEpochSeconds);
245+
LOGGER.log(Level.FINER, "Token stale time epoch seconds: {0}", staleEpochSeconds);
246246

247247
this.token = token;
248-
this.tokenStaleEpochSeconds = tokenStaleEpochSeconds;
248+
this.tokenStaleEpochSeconds = staleEpochSeconds;
249249
}
250250

251251
public String getToken() {
@@ -265,6 +265,10 @@ public boolean isStale() {
265265
return Instant.now().getEpochSecond() >= tokenStaleEpochSeconds;
266266
}
267267

268+
long getTokenStaleEpochSeconds() {
269+
return tokenStaleEpochSeconds;
270+
}
271+
268272
}
269273

270274
/**
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.jenkinsci.plugins.github_branch_source;
2+
3+
import org.junit.Test;
4+
5+
import java.time.Duration;
6+
import java.time.Instant;
7+
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static org.hamcrest.Matchers.*;
10+
11+
public class GitHubAppCredentialsTest {
12+
13+
@Test
14+
public void testAppInstallationTokenStale() throws Exception {
15+
16+
GitHubAppCredentials.AppInstallationToken token;
17+
long now;
18+
19+
now = Instant.now().getEpochSecond();
20+
token = new GitHubAppCredentials.AppInstallationToken("", now);
21+
assertThat(token.isStale(), is(false));
22+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));
23+
24+
Thread.sleep(1000);
25+
assertThat(token.isStale(), is(true));
26+
27+
now = Instant.now().getEpochSecond();
28+
token = new GitHubAppCredentials.AppInstallationToken("",
29+
now + Duration.ofMinutes(15).getSeconds());
30+
assertThat(token.isStale(), is(false));
31+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));
32+
33+
now = Instant.now().getEpochSecond();
34+
token = new GitHubAppCredentials.AppInstallationToken("",
35+
now + GitHubAppCredentials.AppInstallationToken.MINIMUM_SECONDS_UNTIL_EXPIRATION + 2);
36+
assertThat(token.isStale(), is(false));
37+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 2));
38+
39+
now = Instant.now().getEpochSecond();
40+
token = new GitHubAppCredentials.AppInstallationToken("",
41+
now + GitHubAppCredentials.AppInstallationToken.MINIMUM_SECONDS_UNTIL_EXPIRATION + Duration.ofMinutes(7).getSeconds());
42+
assertThat(token.isStale(), is(false));
43+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + Duration.ofMinutes(7).getSeconds()));
44+
45+
now = Instant.now().getEpochSecond();
46+
token = new GitHubAppCredentials.AppInstallationToken("",
47+
now + Duration.ofMinutes(90).getSeconds());
48+
assertThat(token.isStale(), is(false));
49+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.MAXIMUM_AGE_SECONDS + 1));
50+
}
51+
}

0 commit comments

Comments
 (0)