1 /**
2 * Copyright (c) 2013-2023, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
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 * Test case for {@link RtDeployKeys}.
44 * @author Andres Candal (andres.candal@rollasolution.com)
45 * @version $Id: a42f4c5aeee7637bc982a42a6fe494a6a70a1c2e $
46 * @since 0.8
47 */
48 @OAuthScope(Scope.ADMIN_PUBLIC_KEY)
49 public final class RtDeployKeysITCase {
50
51 /**
52 * Test repos.
53 */
54 private static Repos repos;
55
56 /**
57 * Test repo.
58 */
59 private static Repo repo;
60
61 /**
62 * Set up test fixtures.
63 * @throws Exception If some errors occurred.
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 * Tear down test fixtures.
74 * @throws Exception If some errors occurred.
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 * RtDeployKeys can iterate deploy keys.
84 * @throws Exception If some problem inside
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 * RtDeployKeys can create a deploy key.
103 * @throws Exception If something goes wrong
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 * RtDeployKeys can get a single deploy key.
122 * @throws Exception If something goes wrong
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 * RtDeployKeys can remove a deploy key.
141 * @throws Exception If something goes wrong
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 * Generates a random public key for test.
164 *
165 * @return The encoded SSH public key.
166 * @throws Exception If a problem occurs.
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 }