View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcraft.jsch.JSch;
8   import com.jcraft.jsch.JSchException;
9   import com.jcraft.jsch.KeyPair;
10  import java.io.ByteArrayOutputStream;
11  import java.io.IOException;
12  import org.hamcrest.MatcherAssert;
13  import org.hamcrest.Matchers;
14  import org.junit.jupiter.api.AfterAll;
15  import org.junit.jupiter.api.BeforeAll;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Test case for {@link RtDeployKeys}.
20   * @since 0.8
21   */
22  @OAuthScope(OAuthScope.Scope.ADMIN_PUBLIC_KEY)
23  final class RtDeployKeysITCase {
24  
25      /**
26       * Test repos.
27       */
28      private static Repos repos;
29  
30      /**
31       * Test repo.
32       */
33      private static Repo repo;
34  
35      /**
36       * Set up test fixtures.
37       */
38      @BeforeAll
39      static void setUp() throws IOException {
40          final GitHub github = GitHubIT.connect();
41          RtDeployKeysITCase.repos = github.repos();
42          RtDeployKeysITCase.repo = new RepoRule().repo(RtDeployKeysITCase.repos);
43      }
44  
45      /**
46       * Tear down test fixtures.
47       */
48      @AfterAll
49      static void tearDown() throws IOException {
50          if (RtDeployKeysITCase.repos != null && RtDeployKeysITCase.repo != null) {
51              RtDeployKeysITCase.repos.remove(RtDeployKeysITCase.repo.coordinates());
52          }
53      }
54  
55      /**
56       * RtDeployKeys can iterate deploy keys.
57       * @throws Exception If some problem inside
58       */
59      @Test
60      void canFetchAllDeployKeys() throws Exception {
61          final DeployKeys keys = RtDeployKeysITCase.repo.keys();
62          final String title = "Test Iterate Key";
63          final DeployKey key = keys.create(title, RtDeployKeysITCase.key());
64          try {
65              MatcherAssert.assertThat(
66                  "Collection does not contain expected item",
67                  keys.iterate(),
68                  Matchers.hasItem(key)
69              );
70          } finally {
71              key.remove();
72          }
73      }
74  
75      /**
76       * RtDeployKeys can create a deploy key.
77       * @throws Exception If something goes wrong
78       */
79      @Test
80      void createsDeployKey() throws Exception {
81          final DeployKeys keys = RtDeployKeysITCase.repo.keys();
82          final String title = "Test Create Key";
83          final DeployKey key = keys.create(title, RtDeployKeysITCase.key());
84          try {
85              MatcherAssert.assertThat(
86                  "Values are not equal",
87                  new DeployKey.Smart(key).title(),
88                  Matchers.is(title)
89              );
90          } finally {
91              key.remove();
92          }
93      }
94  
95      /**
96       * RtDeployKeys can get a single deploy key.
97       * @throws Exception If something goes wrong
98       */
99      @Test
100     void getsDeployKey() throws Exception {
101         final DeployKeys keys = RtDeployKeysITCase.repo.keys();
102         final String title = "Test Get Key";
103         final DeployKey key = keys.create(title, RtDeployKeysITCase.key());
104         try {
105             MatcherAssert.assertThat(
106                 "Values are not equal",
107                 keys.get(key.number()),
108                 Matchers.is(key)
109             );
110         } finally {
111             key.remove();
112         }
113     }
114 
115     /**
116      * RtDeployKeys can remove a deploy key.
117      * @throws Exception If something goes wrong
118      */
119     @Test
120     void removesDeployKey() throws Exception {
121         final DeployKeys keys = RtDeployKeysITCase.repo.keys();
122         final String title = "Test Remove Key";
123         final DeployKey key = keys.create(title, RtDeployKeysITCase.key());
124         try {
125             MatcherAssert.assertThat(
126                 "Value is null",
127                 keys.get(key.number()),
128                 Matchers.notNullValue()
129             );
130         } finally {
131             key.remove();
132         }
133         MatcherAssert.assertThat(
134             "Assertion failed",
135             keys.iterate(),
136             Matchers.not(Matchers.contains(key))
137         );
138     }
139 
140     /**
141      * Generates a random public key for test.
142      *
143      * @return The encoded SSH public key.
144      */
145     private static String key() throws JSchException, IOException {
146         try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
147             final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
148             kpair.writePublicKey(stream, "");
149             kpair.dispose();
150             return new String(stream.toByteArray());
151         }
152     }
153 }