1
2
3
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
18
19
20 @OAuthScope(OAuthScope.Scope.ADMIN_PUBLIC_KEY)
21 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
22 final class RtPublicKeysITCase {
23
24
25
26
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
42
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
58
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
79
80
81 @Test
82 void createsKey() throws Exception {
83 final PublicKeys keys = RtPublicKeysITCase.keys();
84
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
111
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
124
125
126 private static PublicKeys keys() {
127 return GitHubIT.connect().users().self().keys();
128 }
129
130 }