|
| 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