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.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41
42
43
44
45
46
47
48 @OAuthScope(Scope.ADMIN_PUBLIC_KEY)
49 public final class RtDeployKeysITCase {
50
51
52
53
54 private static Repos repos;
55
56
57
58
59 private static Repo repo;
60
61
62
63
64
65 @BeforeClass
66 public static void setUp() throws Exception {
67 final Github github = new GithubIT().connect();
68 repos = github.repos();
69 repo = new RepoRule().repo(repos);
70 }
71
72
73
74
75
76 @AfterClass
77 public static void tearDown() throws Exception {
78 if (repos != null && repo != null) {
79 repos.remove(repo.coordinates());
80 }
81 }
82
83
84
85
86 @Test
87 public void canFetchAllDeployKeys() throws Exception {
88 final DeployKeys keys = repo.keys();
89 final String title = "Test Iterate Key";
90 final DeployKey key = keys.create(title, key());
91 try {
92 MatcherAssert.assertThat(
93 keys.iterate(),
94 Matchers.hasItem(key)
95 );
96 } finally {
97 key.remove();
98 }
99 }
100
101
102
103
104
105 @Test
106 public void createsDeployKey() throws Exception {
107 final DeployKeys keys = repo.keys();
108 final String title = "Test Create Key";
109 final DeployKey key = keys.create(title, key());
110 try {
111 MatcherAssert.assertThat(
112 new DeployKey.Smart(key).title(),
113 Matchers.is(title)
114 );
115 } finally {
116 key.remove();
117 }
118 }
119
120
121
122
123
124 @Test
125 public void getsDeployKey() throws Exception {
126 final DeployKeys keys = repo.keys();
127 final String title = "Test Get Key";
128 final DeployKey key = keys.create(title, key());
129 try {
130 MatcherAssert.assertThat(
131 keys.get(key.number()),
132 Matchers.is(key)
133 );
134 } finally {
135 key.remove();
136 }
137 }
138
139
140
141
142
143 @Test
144 public void removesDeployKey() throws Exception {
145 final DeployKeys keys = repo.keys();
146 final String title = "Test Remove Key";
147 final DeployKey key = keys.create(title, key());
148 try {
149 MatcherAssert.assertThat(
150 keys.get(key.number()),
151 Matchers.notNullValue()
152 );
153 } finally {
154 key.remove();
155 }
156 MatcherAssert.assertThat(
157 keys.iterate(),
158 Matchers.not(Matchers.contains(key))
159 );
160 }
161
162
163
164
165
166
167
168 private static String key() throws Exception {
169 final ByteArrayOutputStream stream = new ByteArrayOutputStream();
170 try {
171 final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
172 kpair.writePublicKey(stream, "");
173 kpair.dispose();
174 } finally {
175 stream.close();
176 }
177 return new String(stream.toByteArray());
178 }
179 }