forked from auth0/auth0-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagementAPITest.java
More file actions
282 lines (232 loc) · 9.63 KB
/
ManagementAPITest.java
File metadata and controls
282 lines (232 loc) · 9.63 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.auth0.client.mgmt;
import com.auth0.client.MockServer;
import com.auth0.exception.Auth0Exception;
import com.auth0.net.client.Auth0HttpClient;
import com.auth0.net.client.Auth0HttpRequest;
import com.auth0.net.client.Auth0HttpResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CompletableFuture;
import static com.auth0.AssertsUtil.verifyThrows;
import static com.auth0.client.UrlMatcher.isUrl;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
public class ManagementAPITest {
private static final String DOMAIN = "domain.auth0.com";
private static final String API_TOKEN = "apiToken";
private MockServer server;
private ManagementAPI api;
@BeforeEach
public void setUp() throws Exception {
server = new MockServer();
api = ManagementAPI.newBuilder(server.getBaseUrl(), API_TOKEN).build();
}
@AfterEach
public void tearDown() throws Exception {
server.stop();
}
// Configuration
@Test
public void shouldAcceptDomainWithNoScheme() {
ManagementAPI api = ManagementAPI.newBuilder("me.something.com", API_TOKEN).build();
assertThat(api.getBaseUrl(), is(notNullValue()));
assertThat(api.getBaseUrl().toString(), isUrl("https", "me.something.com"));
}
@Test
@SuppressWarnings("deprecation")
public void shouldCreateWithDomainAndToken() {
ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN);
assertThat(api, is(notNullValue()));
}
@Test
public void shouldCreateWithHttpClient() {
Auth0HttpClient httpClient = new Auth0HttpClient() {
@Override
public Auth0HttpResponse sendRequest(Auth0HttpRequest request) {
return null;
}
@Override
public CompletableFuture<Auth0HttpResponse> sendRequestAsync(Auth0HttpRequest request) {
return null;
}
};
ManagementAPI api = ManagementAPI.newBuilder(
DOMAIN,
SimpleTokenProvider.create(API_TOKEN)
)
.withHttpClient(httpClient)
.build();
assertThat(api, is(notNullValue()));
assertThat(api.getHttpClient(), is(httpClient));
}
@Test
public void shouldAcceptDomainWithHttpScheme() {
ManagementAPI api = ManagementAPI.newBuilder("http://me.something.com", API_TOKEN).build();
assertThat(api.getBaseUrl(), is(notNullValue()));
assertThat(api.getBaseUrl().toString(), isUrl("http", "me.something.com"));
}
@Test
public void shouldThrowWhenDomainIsInvalid() {
verifyThrows(IllegalArgumentException.class,
() -> ManagementAPI.newBuilder("", API_TOKEN).build(),
"The domain had an invalid format and couldn't be parsed as an URL.");
}
@Test
public void shouldThrowWhenDomainIsNull() {
verifyThrows(IllegalArgumentException.class,
() -> ManagementAPI.newBuilder(null, API_TOKEN).build(),
"'domain' cannot be null!");
}
@Test
public void shouldThrowWhenApiTokenIsNull() {
verifyThrows(IllegalArgumentException.class,
() -> ManagementAPI.newBuilder(DOMAIN, (String) null).build(),
"'api token' cannot be null!");
}
@Test
public void shouldThrowOnUpdateWhenApiTokenIsNull() {
ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN).build();
verifyThrows(IllegalArgumentException.class,
() -> api.setApiToken(null),
"'api token' cannot be null!");
}
@Test
public void shouldUpdateApiToken() throws Auth0Exception {
//Initialize with a token
ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, "first token").build();
assertThat(api.blacklists().tokenProvider.getToken(), is("first token"));
assertThat(api.clientGrants().tokenProvider.getToken(), is("first token"));
assertThat(api.clients().tokenProvider.getToken(), is("first token"));
assertThat(api.connections().tokenProvider.getToken(), is("first token"));
assertThat(api.deviceCredentials().tokenProvider.getToken(), is("first token"));
assertThat(api.emailProvider().tokenProvider.getToken(), is("first token"));
assertThat(api.emailTemplates().tokenProvider.getToken(), is("first token"));
assertThat(api.grants().tokenProvider.getToken(), is("first token"));
assertThat(api.guardian().tokenProvider.getToken(), is("first token"));
assertThat(api.jobs().tokenProvider.getToken(), is("first token"));
assertThat(api.logEvents().tokenProvider.getToken(), is("first token"));
assertThat(api.resourceServers().tokenProvider.getToken(), is("first token"));
assertThat(api.rules().tokenProvider.getToken(), is("first token"));
assertThat(api.stats().tokenProvider.getToken(), is("first token"));
assertThat(api.tenants().tokenProvider.getToken(), is("first token"));
assertThat(api.tickets().tokenProvider.getToken(), is("first token"));
assertThat(api.userBlocks().tokenProvider.getToken(), is("first token"));
assertThat(api.users().tokenProvider.getToken(), is("first token"));
//Update the token
api.setApiToken("new token");
assertThat(api.blacklists().tokenProvider.getToken(), is("new token"));
assertThat(api.clientGrants().tokenProvider.getToken(), is("new token"));
assertThat(api.clients().tokenProvider.getToken(), is("new token"));
assertThat(api.connections().tokenProvider.getToken(), is("new token"));
assertThat(api.deviceCredentials().tokenProvider.getToken(), is("new token"));
assertThat(api.emailProvider().tokenProvider.getToken(), is("new token"));
assertThat(api.emailTemplates().tokenProvider.getToken(), is("new token"));
assertThat(api.grants().tokenProvider.getToken(), is("new token"));
assertThat(api.guardian().tokenProvider.getToken(), is("new token"));
assertThat(api.jobs().tokenProvider.getToken(), is("new token"));
assertThat(api.logEvents().tokenProvider.getToken(), is("new token"));
assertThat(api.resourceServers().tokenProvider.getToken(), is("new token"));
assertThat(api.rules().tokenProvider.getToken(), is("new token"));
assertThat(api.stats().tokenProvider.getToken(), is("new token"));
assertThat(api.tenants().tokenProvider.getToken(), is("new token"));
assertThat(api.tickets().tokenProvider.getToken(), is("new token"));
assertThat(api.userBlocks().tokenProvider.getToken(), is("new token"));
assertThat(api.users().tokenProvider.getToken(), is("new token"));
}
@Test
@SuppressWarnings("deprecation")
public void acceptsHttpOptions() {
com.auth0.client.HttpOptions httpOptions = new com.auth0.client.HttpOptions();
httpOptions.setConnectTimeout(15);
ManagementAPI api = new ManagementAPI(DOMAIN, "CLIENT_ID", httpOptions);
assertThat(api, is(notNullValue()));
}
@Test
@SuppressWarnings("deprecation")
public void httpOptionsShouldThrowWhenNull() {
verifyThrows(IllegalArgumentException.class, () -> new ManagementAPI(DOMAIN, API_TOKEN, null));
}
@Test
@SuppressWarnings("deprecation")
public void shouldThrowOnInValidMaxRequestsPerHostConfiguration() {
com.auth0.client.HttpOptions options = new com.auth0.client.HttpOptions();
verifyThrows(IllegalArgumentException.class,
() -> options.setMaxRequestsPerHost(0),
"maxRequestsPerHost must be one or greater.");
}
//Entities
@Test
public void shouldGetBlacklists() {
assertThat(api.blacklists(), notNullValue());
}
@Test
public void shouldGetClientGrants() {
assertThat(api.clientGrants(), notNullValue());
}
@Test
public void shouldGetClients() {
assertThat(api.clients(), notNullValue());
}
@Test
public void shouldGetConnections() {
assertThat(api.connections(), notNullValue());
}
@Test
public void shouldGetDeviceCredentials() {
assertThat(api.deviceCredentials(), notNullValue());
}
@Test
public void shouldGetEmailProvider() {
assertThat(api.emailProvider(), notNullValue());
}
@Test
public void shouldGetEmailTemplates() {
assertThat(api.emailTemplates(), notNullValue());
}
@Test
public void shouldGetGrants() {
assertThat(api.grants(), notNullValue());
}
@Test
public void shouldGetGuardian() {
assertThat(api.guardian(), notNullValue());
}
@Test
public void shouldGetJobs() {
assertThat(api.jobs(), notNullValue());
}
@Test
public void shouldGetLogEvents() {
assertThat(api.logEvents(), notNullValue());
}
@Test
public void shouldGetResourceServers() {
assertThat(api.resourceServers(), notNullValue());
}
@Test
public void shouldGetRules() {
assertThat(api.rules(), notNullValue());
}
@Test
public void shouldGetStats() {
assertThat(api.stats(), notNullValue());
}
@Test
public void shouldGetTenants() {
assertThat(api.tenants(), notNullValue());
}
@Test
public void shouldGetTickets() {
assertThat(api.tickets(), notNullValue());
}
@Test
public void shouldGetUserBlocks() {
assertThat(api.userBlocks(), notNullValue());
}
@Test
public void shouldGetUsers() {
assertThat(api.users(), notNullValue());
}
}