1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.github;
31
32 import com.jcabi.github.OAuthScope.Scope;
33 import com.jcraft.jsch.JSch;
34 import com.jcraft.jsch.KeyPair;
35 import java.io.ByteArrayOutputStream;
36 import org.hamcrest.MatcherAssert;
37 import org.hamcrest.Matchers;
38 import org.junit.Test;
39
40
41
42
43
44
45
46 @OAuthScope(Scope.ADMIN_PUBLIC_KEY)
47 public class RtPublicKeysITCase {
48
49
50
51
52
53
54 @Test
55 public final void retrievesKeys() throws Exception {
56 final PublicKeys keys = this.keys();
57 final PublicKey key = keys.create("key", this.key());
58 MatcherAssert.assertThat(
59 keys.iterate(),
60 Matchers.hasItem(key)
61 );
62 keys.remove(key.number());
63 }
64
65
66
67
68
69
70 @Test
71 public final void retrievesSingleKey() throws Exception {
72 final PublicKeys keys = this.keys();
73 final PublicKey key = keys.create("Title", this.key());
74 MatcherAssert.assertThat(
75 keys.get(key.number()),
76 Matchers.equalTo(key)
77 );
78 keys.remove(key.number());
79 }
80
81
82
83
84
85
86 @Test
87 public final void removesKey() throws Exception {
88 final PublicKeys keys = this.keys();
89 final PublicKey key = keys.create("", this.key());
90 MatcherAssert.assertThat(
91 keys.iterate() ,
92 Matchers.hasItem(key)
93 );
94 keys.remove(key.number());
95 MatcherAssert.assertThat(
96 keys.iterate(),
97 Matchers.not(Matchers.hasItem(key))
98 );
99 }
100
101
102
103
104
105
106 @Test
107 public final void createsKey() throws Exception {
108 final PublicKeys keys = this.keys();
109
110 final PublicKey key = keys.create("rsa", this.key());
111 try {
112 MatcherAssert.assertThat(
113 keys.iterate(),
114 Matchers.hasItem(key)
115 );
116 MatcherAssert.assertThat(
117 key.user(),
118 Matchers.equalTo(
119 keys.user()
120 )
121 );
122 } finally {
123 keys.remove(key.number());
124 }
125 MatcherAssert.assertThat(
126 keys.iterate(),
127 Matchers.not(Matchers.hasItem(key))
128 );
129 }
130
131
132
133
134
135
136 private String key() throws Exception {
137 final ByteArrayOutputStream stream = new ByteArrayOutputStream();
138 try {
139 final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
140 kpair.writePublicKey(stream, "");
141 kpair.dispose();
142 } finally {
143 stream.close();
144 }
145 return new String(stream.toByteArray());
146 }
147
148
149
150
151
152 private PublicKeys keys() {
153 return new GithubIT().connect().users().self().keys();
154 }
155
156 }