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.Test;
15  
16  /**
17   * Test case for {@link RtPublicKeys}.
18   * @since 0.8
19   */
20  @OAuthScope(OAuthScope.Scope.ADMIN_PUBLIC_KEY)
21  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
22  final class RtPublicKeysITCase {
23  
24      /**
25       * RtPublicKeys should be able to retrieve its keys.
26       * @throws Exception If a problem occurs.
27       */
28      @Test
29      void retrievesKeys() throws Exception {
30          final PublicKeys keys = RtPublicKeysITCase.keys();
31          final PublicKey key = keys.create("key", RtPublicKeysITCase.key());
32          MatcherAssert.assertThat(
33              "Collection does not contain expected item",
34              keys.iterate(),
35              Matchers.hasItem(key)
36          );
37          keys.remove(key.number());
38      }
39  
40      /**
41       * RtPublicKeys should be able to retrieve a single key.
42       * @throws Exception If a problem occurs.
43       */
44      @Test
45      void retrievesSingleKey() throws Exception {
46          final PublicKeys keys = RtPublicKeysITCase.keys();
47          final PublicKey key = keys.create("Title", RtPublicKeysITCase.key());
48          MatcherAssert.assertThat(
49              "Values are not equal",
50              keys.get(key.number()),
51              Matchers.equalTo(key)
52          );
53          keys.remove(key.number());
54      }
55  
56      /**
57       * RtPublicKeys should be able to remove a key.
58       * @throws Exception If a problem occurs.
59       */
60      @Test
61      void removesKey() throws Exception {
62          final PublicKeys keys = RtPublicKeysITCase.keys();
63          final PublicKey key = keys.create("", RtPublicKeysITCase.key());
64          MatcherAssert.assertThat(
65              "Collection does not contain expected item",
66              keys.iterate(),
67              Matchers.hasItem(key)
68          );
69          keys.remove(key.number());
70          MatcherAssert.assertThat(
71              "Collection does not contain expected item",
72              keys.iterate(),
73              Matchers.not(Matchers.hasItem(key))
74          );
75      }
76  
77      /**
78       * RtPublicKeys should be able to create a key.
79       * @throws Exception If a problem occurs.
80       */
81      @Test
82      void createsKey() throws Exception {
83          final PublicKeys keys = RtPublicKeysITCase.keys();
84          // @checkstyle LineLength (1 line)
85          final PublicKey key = keys.create("rsa", RtPublicKeysITCase.key());
86          try {
87              MatcherAssert.assertThat(
88                  "Collection does not contain expected item",
89                  keys.iterate(),
90                  Matchers.hasItem(key)
91              );
92              MatcherAssert.assertThat(
93                  "Values are not equal",
94                  key.user(),
95                  Matchers.equalTo(
96                      keys.user()
97                  )
98              );
99          } finally {
100             keys.remove(key.number());
101         }
102         MatcherAssert.assertThat(
103             "Collection does not contain expected item",
104             keys.iterate(),
105             Matchers.not(Matchers.hasItem(key))
106         );
107     }
108 
109     /**
110      * Generates a random public key for test.
111      * @return The encoded SSH public key.
112      */
113     private static String key() throws JSchException, IOException {
114         try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
115             final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
116             kpair.writePublicKey(stream, "");
117             kpair.dispose();
118             return new String(stream.toByteArray());
119         }
120     }
121 
122     /**
123      * Create and return PublicKeys object to test.
124      * @return PublicKeys
125      */
126     private static PublicKeys keys() {
127         return GitHubIT.connect().users().self().keys();
128     }
129 
130 }