View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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.Test;
39  
40  /**
41   * Test case for {@link RtPublicKeys}.
42   *
43   * @author Carlos Miranda (miranda.cma@gmail.com)
44   * @version $Id: 7afa43adfd3fb37d2905a2d98f111761f69136e2 $
45   */
46  @OAuthScope(Scope.ADMIN_PUBLIC_KEY)
47  public class RtPublicKeysITCase {
48  
49      /**
50       * RtPublicKeys should be able to retrieve its keys.
51       *
52       * @throws Exception If a problem occurs.
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       * RtPublicKeys should be able to retrieve a single key.
67       *
68       * @throws Exception If a problem occurs.
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       * RtPublicKeys should be able to remove a key.
83       *
84       * @throws Exception If a problem occurs.
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      * RtPublicKeys should be able to create a key.
103      *
104      * @throws Exception If a problem occurs.
105      */
106     @Test
107     public final void createsKey() throws Exception {
108         final PublicKeys keys = this.keys();
109         // @checkstyle LineLength (1 line)
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      * Generates a random public key for test.
133      * @return The encoded SSH public key.
134      * @throws Exception If a problem occurs.
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      * Create and return PublicKeys object to test.
150      * @return PublicKeys
151      */
152     private PublicKeys keys() {
153         return new GithubIT().connect().users().self().keys();
154     }
155 
156 }