Skip to content

Commit 5aea4eb

Browse files
committed
Add test for AppInstallationToken
1 parent 1443730 commit 5aea4eb

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.jenkinsci.plugins.github_branch_source;
2+
3+
import com.cloudbees.plugins.credentials.Credentials;
4+
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
5+
import com.cloudbees.plugins.credentials.casc.CredentialsRootConfigurator;
6+
import com.cloudbees.plugins.credentials.domains.DomainCredentials;
7+
import com.vladsch.flexmark.ext.ins.Ins;
8+
import io.jenkins.plugins.casc.ConfigurationContext;
9+
import io.jenkins.plugins.casc.ConfiguratorRegistry;
10+
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
11+
import io.jenkins.plugins.casc.misc.EnvVarsRule;
12+
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
13+
import io.jenkins.plugins.casc.model.CNode;
14+
import io.jenkins.plugins.casc.model.Mapping;
15+
import io.jenkins.plugins.casc.model.Sequence;
16+
import jenkins.model.Jenkins;
17+
import org.junit.ClassRule;
18+
import org.junit.Test;
19+
import org.junit.rules.RuleChain;
20+
import org.jvnet.hudson.test.JenkinsRule;
21+
22+
import java.time.Duration;
23+
import java.time.Instant;
24+
import java.util.List;
25+
import java.util.Objects;
26+
27+
import static io.jenkins.plugins.casc.misc.Util.toStringFromYamlFile;
28+
import static io.jenkins.plugins.casc.misc.Util.toYamlString;
29+
import static org.hamcrest.Matchers.*;
30+
import static org.junit.Assert.assertThat;
31+
import static org.jvnet.hudson.test.JenkinsMatchers.hasPlainText;
32+
33+
public class GitHubAppCredentialsTest {
34+
35+
@Test
36+
public void testAppInstallationTokenStale() throws Exception {
37+
38+
GitHubAppCredentials.AppInstallationToken token;
39+
long now;
40+
41+
now = Instant.now().getEpochSecond();
42+
token = new GitHubAppCredentials.AppInstallationToken("", now);
43+
assertThat(token.isStale(), is(false));
44+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));
45+
46+
Thread.sleep(1000);
47+
assertThat(token.isStale(), is(true));
48+
49+
now = Instant.now().getEpochSecond();
50+
token = new GitHubAppCredentials.AppInstallationToken("",
51+
now + Duration.ofMinutes(15).getSeconds());
52+
assertThat(token.isStale(), is(false));
53+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));
54+
55+
now = Instant.now().getEpochSecond();
56+
token = new GitHubAppCredentials.AppInstallationToken("",
57+
now + GitHubAppCredentials.AppInstallationToken.MINIMUM_SECONDS_UNTIL_EXPIRATION + 2);
58+
assertThat(token.isStale(), is(false));
59+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 2));
60+
61+
now = Instant.now().getEpochSecond();
62+
token = new GitHubAppCredentials.AppInstallationToken("",
63+
now + GitHubAppCredentials.AppInstallationToken.MINIMUM_SECONDS_UNTIL_EXPIRATION + Duration.ofMinutes(7).getSeconds());
64+
assertThat(token.isStale(), is(false));
65+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + Duration.ofMinutes(7).getSeconds()));
66+
67+
now = Instant.now().getEpochSecond();
68+
token = new GitHubAppCredentials.AppInstallationToken("",
69+
now + Duration.ofMinutes(90).getSeconds());
70+
assertThat(token.isStale(), is(false));
71+
assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + GitHubAppCredentials.AppInstallationToken.MAXIMUM_AGE_SECONDS + 1));
72+
}
73+
}

0 commit comments

Comments
 (0)