|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.time.Instant; |
| 7 | +import java.util.Date; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent; |
| 12 | +import static org.hamcrest.Matchers.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * The Class GHDeployKeyTest. |
| 16 | + * |
| 17 | + * @author Jonas van Vliet |
| 18 | + */ |
| 19 | +public class GHDeployKeyTest extends AbstractGitHubWireMockTest { |
| 20 | + private static final String DEPLOY_KEY_TEST_REPO_NAME = "hub4j-test-org/GHDeployKeyTest"; |
| 21 | + private static final String ED_25519_READONLY = "DeployKey - ed25519 - readonly"; |
| 22 | + private static final String RSA_4096_READWRITE = "Deploykey - rsa4096 - readwrite"; |
| 23 | + private static final String KEY_CREATOR_USERNAME = "van-vliet"; |
| 24 | + |
| 25 | + /** |
| 26 | + * Test get deploymentkeys. |
| 27 | + * |
| 28 | + * @throws IOException |
| 29 | + * Signals that an I/O exception has occurred. |
| 30 | + */ |
| 31 | + @Test |
| 32 | + public void testGetDeployKeys() throws IOException { |
| 33 | + final GHRepository repo = getRepository(); |
| 34 | + final List<GHDeployKey> deployKeys = repo.getDeployKeys(); |
| 35 | + assertThat("There should be 2 deploykeys in " + DEPLOY_KEY_TEST_REPO_NAME, deployKeys, hasSize(2)); |
| 36 | + |
| 37 | + Optional<GHDeployKey> ed25519Key = deployKeys.stream() |
| 38 | + .filter(key -> key.getTitle().equals(ED_25519_READONLY)) |
| 39 | + .findAny(); |
| 40 | + assertThat("The key exists", ed25519Key, isPresent()); |
| 41 | + assertThat("The key was created at the specified date", |
| 42 | + ed25519Key.get().getCreatedAt(), |
| 43 | + is(Date.from(Instant.parse("2023-02-08T10:00:15.00Z")))); |
| 44 | + assertThat("The key is created by " + KEY_CREATOR_USERNAME, |
| 45 | + ed25519Key.get().getAdded_by(), |
| 46 | + is(KEY_CREATOR_USERNAME)); |
| 47 | + assertThat("The key has a last_used value", |
| 48 | + ed25519Key.get().getLastUsedAt(), |
| 49 | + is(Date.from(Instant.parse("2023-02-08T10:02:11.00Z")))); |
| 50 | + assertThat("The key only has read access", ed25519Key.get().isRead_only(), is(true)); |
| 51 | + assertThat("Object has a toString()", ed25519Key.get().toString(), is(notNullValue())); |
| 52 | + |
| 53 | + Optional<GHDeployKey> rsa_4096Key = deployKeys.stream() |
| 54 | + .filter(key -> key.getTitle().equals(RSA_4096_READWRITE)) |
| 55 | + .findAny(); |
| 56 | + assertThat("The key exists", rsa_4096Key, isPresent()); |
| 57 | + assertThat("The key was created at the specified date", |
| 58 | + rsa_4096Key.get().getCreatedAt(), |
| 59 | + is(Date.from(Instant.parse("2023-01-26T14:12:12.00Z")))); |
| 60 | + assertThat("The key is created by " + KEY_CREATOR_USERNAME, |
| 61 | + rsa_4096Key.get().getAdded_by(), |
| 62 | + is(KEY_CREATOR_USERNAME)); |
| 63 | + assertThat("The key has never been used", rsa_4096Key.get().getLastUsedAt(), is(nullValue())); |
| 64 | + assertThat("The key only has read/write access", rsa_4096Key.get().isRead_only(), is(false)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets the repository. |
| 69 | + * |
| 70 | + * @return the repository |
| 71 | + * @throws IOException |
| 72 | + * Signals that an I/O exception has occurred. |
| 73 | + */ |
| 74 | + protected GHRepository getRepository() throws IOException { |
| 75 | + return getRepository(gitHub); |
| 76 | + } |
| 77 | + |
| 78 | + private GHRepository getRepository(final GitHub gitHub) throws IOException { |
| 79 | + return gitHub.getRepository(DEPLOY_KEY_TEST_REPO_NAME); |
| 80 | + } |
| 81 | +} |
0 commit comments