forked from microsoftgraph/msgraph-sdk-java-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCredentialProviderTests.java
More file actions
72 lines (61 loc) · 3.23 KB
/
Copy pathClientCredentialProviderTests.java
File metadata and controls
72 lines (61 loc) · 3.23 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
package com.microsoft.graph.auth.confidentialClient;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.junit.Ignore;
import org.junit.Test;
import com.microsoft.graph.auth.enums.NationalCloud;
import com.microsoft.graph.authentication.IAuthenticationProvider;
import com.microsoft.graph.httpcore.HttpClients;
import com.microsoft.graph.httpcore.ICoreAuthenticationProvider;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class ClientCredentialProviderTests {
public static String CLIENT_ID = "CLIENT_ID";
public static String SCOPE = "https://graph.microsoft.com/.default";
public static List<String > SCOPES = Arrays.asList(SCOPE);
public static String CLIENT_SECRET = "CLIENT_SECRET";
public static String CLIENT_ASSERTION = "CLIENT_ASSERTION";
public static String TENANT = "TENANT_GUID_OR_DOMAIN_NAME";
public static NationalCloud NATIONAL_CLOUD = NationalCloud.Global;
public static String tenantGUID = "TENANT_GUID";
@Test
public void createInstanceClientSecretTest() {
IAuthenticationProvider authenticationProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NATIONAL_CLOUD);
assertNotNull(authenticationProvider);
}
@Test
public void getTokenRequestMessageTest() throws OAuthSystemException {
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NATIONAL_CLOUD);
String expected = "grant_type=client_credentials&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default+&client_secret=CLIENT_SECRET&client_id=CLIENT_ID";
String actual = authenticationProvider.getTokenRequestMessage().getBody();
String expectedLocationUri = "https://login.microsoftonline.com/TENANT_GUID_OR_DOMAIN_NAME/oauth2/v2.0/token";
String actualLocationUri = authenticationProvider.getTokenRequestMessage().getLocationUri();
assertEquals(expected, actual);
assertEquals(expectedLocationUri, actualLocationUri);
}
@Ignore
@Test
public void getAccessTokenNewRequestTest() throws OAuthSystemException, OAuthProblemException {
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NATIONAL_CLOUD);
OAuthClientRequest request = authenticationProvider.getTokenRequestMessage();
String accessToken = authenticationProvider.getAccessTokenNewRequest(request);
assertNotNull(accessToken);
}
@Ignore
@Test
public void authenticateRequestTest() throws IOException {
ICoreAuthenticationProvider iCoreAuthenticationProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, tenantGUID, NationalCloud.Global);
OkHttpClient httpclient = HttpClients.createDefault(iCoreAuthenticationProvider);
Request httpget = new Request.Builder().url("https://graph.microsoft.com/v1.0/groups").build();
Response response = httpclient.newCall(httpget).execute();
assertNotNull(response);
assertNotNull(response.body().string());
}
}