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.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.mock.MkQuery;
37 import com.jcabi.http.request.ApacheRequest;
38 import java.io.IOException;
39 import java.net.HttpURLConnection;
40 import javax.json.Json;
41 import javax.json.JsonObject;
42 import org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47
48
49
50
51
52
53
54 public final class RtPublicKeysTest {
55
56
57
58
59 @Rule
60 public final transient RandomPort resource = new RandomPort();
61
62
63
64
65
66
67 @Test
68 public void retrievesKeys() throws Exception {
69 try (
70 final MkContainer container = new MkGrizzlyContainer().next(
71 new MkAnswer.Simple(
72 HttpURLConnection.HTTP_OK,
73 Json.createArrayBuilder()
74 .add(key(1))
75 .add(key(2))
76 .build().toString()
77 )
78 ).start(this.resource.port())
79 ) {
80 final RtPublicKeys keys = new RtPublicKeys(
81 new ApacheRequest(container.home()),
82 Mockito.mock(User.class)
83 );
84 MatcherAssert.assertThat(
85 keys.iterate(),
86 Matchers.<PublicKey>iterableWithSize(2)
87 );
88 container.stop();
89 }
90 }
91
92
93
94
95
96
97 @Test
98 public void canFetchSingleKey() throws Exception {
99 try (
100 final MkContainer container = new MkGrizzlyContainer().next(
101 new MkAnswer.Simple(
102 HttpURLConnection.HTTP_OK,
103 ""
104 )
105 ).start(this.resource.port())
106 ) {
107 final RtPublicKeys keys = new RtPublicKeys(
108 new ApacheRequest(container.home()),
109 Mockito.mock(User.class)
110 );
111 MatcherAssert.assertThat(
112 keys.get(1),
113 Matchers.notNullValue()
114 );
115 container.stop();
116 }
117 }
118
119
120
121
122
123
124 @Test
125 public void canRemoveKey() throws Exception {
126 try (
127 final MkContainer container = new MkGrizzlyContainer().next(
128 new MkAnswer.Simple(
129 HttpURLConnection.HTTP_NO_CONTENT,
130 ""
131 )
132 ).start(this.resource.port())
133 ) {
134 final RtPublicKeys keys = new RtPublicKeys(
135 new ApacheRequest(container.home()),
136 Mockito.mock(User.class)
137 );
138 keys.remove(1);
139 final MkQuery query = container.take();
140 MatcherAssert.assertThat(
141 query.uri().toString(),
142 Matchers.endsWith("/user/keys/1")
143 );
144 MatcherAssert.assertThat(
145 query.method(),
146 Matchers.equalTo(Request.DELETE)
147 );
148 container.stop();
149 }
150 }
151
152
153
154
155
156 @Test
157 public void canCreatePublicKey() throws IOException {
158 try (
159 final MkContainer container = new MkGrizzlyContainer().next(
160 new MkAnswer.Simple(
161 HttpURLConnection.HTTP_CREATED, key(1).toString()
162 )
163 ).start(this.resource.port())
164 ) {
165 final RtPublicKeys keys = new RtPublicKeys(
166 new ApacheRequest(container.home()),
167 Mockito.mock(User.class)
168 );
169 MatcherAssert.assertThat(
170 keys.create("theTitle", "theKey").number(),
171 Matchers.is(1)
172 );
173 final MkQuery query = container.take();
174 MatcherAssert.assertThat(
175 query.uri().toString(),
176 Matchers.endsWith("/user/keys")
177 );
178 MatcherAssert.assertThat(
179 query.body(),
180 Matchers.equalTo(
181 "{\"title\":\"theTitle\",\"key\":\"theKey\"}"
182 )
183 );
184 container.stop();
185 }
186 }
187
188
189
190
191
192
193 private static JsonObject key(final int number) {
194 return Json.createObjectBuilder()
195 .add("id", number)
196 .add("key", "ssh-rsa AAA")
197 .build();
198 }
199 }