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.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.api.Test;
17
18
19
20
21
22 @OAuthScope(OAuthScope.Scope.ADMIN_PUBLIC_KEY)
23 final class RtDeployKeysITCase {
24
25
26
27
28 private static Repos repos;
29
30
31
32
33 private static Repo repo;
34
35
36
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
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
57
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
77
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
97
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
117
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
142
143
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 }