Added MicrosoftAppCredentialsTests, SimpleChannelProviderTest, and SimpleCredentialProviderTests.

This commit is contained in:
Tracy Boehrer 2019-08-21 15:50:32 -05:00
Родитель c939b93326
Коммит 2491f71a91
5 изменённых файлов: 171 добавлений и 7 удалений

Просмотреть файл

@ -119,7 +119,13 @@ public class MicrosoftAppCredentials implements ServiceClientCredentials {
}
public void setChannelAuthTenant(String authTenant) throws MalformedURLException {
channelAuthTenant = new URL(authTenant).toString();
String originalAuthTenant = channelAuthTenant;
try {
channelAuthTenant = authTenant;
new URL(oAuthEndpoint()).toString();
} catch(MalformedURLException e) {
channelAuthTenant = originalAuthTenant;
}
}
public MicrosoftAppCredentials withChannelAuthTenant(String authTenant) throws MalformedURLException {
@ -135,7 +141,7 @@ public class MicrosoftAppCredentials implements ServiceClientCredentials {
return AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE;
}
public Future<AuthenticationResult> getToken() throws MalformedURLException {
public Future<AuthenticationResult> getToken() {
return getAuthenticator().acquireToken();
}
@ -143,12 +149,19 @@ public class MicrosoftAppCredentials implements ServiceClientCredentials {
return isTrustedServiceUrl(url);
}
private AdalAuthenticator getAuthenticator() throws MalformedURLException {
if (this.authenticator == null) {
this.authenticator = new AdalAuthenticator(
new ClientCredential(this.appId, this.appPassword),
new OAuthConfiguration(oAuthEndpoint(), oAuthScope()));
private AdalAuthenticator getAuthenticator() {
try {
if (this.authenticator == null) {
this.authenticator = new AdalAuthenticator(
new ClientCredential(this.appId, this.appPassword),
new OAuthConfiguration(oAuthEndpoint(), oAuthScope()));
}
} catch(MalformedURLException e) {
// intentional no-op. This class validates the URL on construction or setChannelAuthTenant.
// That is... this will never happen.
LoggerFactory.getLogger(MicrosoftAppCredentials.class).error("getAuthenticator", e);
}
return this.authenticator;
}

Просмотреть файл

@ -47,16 +47,35 @@ public class SimpleCredentialProvider implements CredentialProvider {
this.password = password;
}
/**
* Validates an app ID.
*
* @param appId The app ID to validate.
* @return If the task is successful, the result is true if appId is valid for the controller; otherwise, false.
*/
@Override
public CompletableFuture<Boolean> isValidAppIdAsync(String appId) {
return CompletableFuture.completedFuture(StringUtils.equals(appId, this.appId));
}
/**
* Gets the app password for a given bot app ID.
*
* @param appId The ID of the app to get the password for.
* @return If the task is successful and the app ID is valid, the result
* contains the password; otherwise, null.
*/
@Override
public CompletableFuture<String> getAppPasswordAsync(String appId) {
return CompletableFuture.completedFuture(StringUtils.equals(appId, this.appId) ? this.password : null);
}
/**
* Checks whether bot authentication is disabled.
*
* @return A task that represents the work queued to execute If the task is successful and bot authentication
* is disabled, the result is true; otherwise, false.
*/
@Override
public CompletableFuture<Boolean> isAuthenticationDisabledAsync() {
return CompletableFuture.completedFuture(StringUtils.isEmpty(this.appId));

Просмотреть файл

@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.bot.connector;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.bot.connector.authentication.MicrosoftAppCredentials;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutionException;
public class MicrosoftAppCredentialsTests {
@Test
public void ValidUrlTrusted() {
MicrosoftAppCredentials.trustServiceUrl("https://goodurl.com");
Assert.assertTrue(MicrosoftAppCredentials.isTrustedServiceUrl("https://goodurl.com"));
}
@Test
public void InvalidUrlTrusted() {
MicrosoftAppCredentials.trustServiceUrl("badurl");
Assert.assertFalse(MicrosoftAppCredentials.isTrustedServiceUrl("badurl"));
}
@Test
public void TrustedUrlExpiration() throws InterruptedException {
// There is a +5 minute window for an expired url
MicrosoftAppCredentials.trustServiceUrl("https://goodurl.com", LocalDateTime.now().minusMinutes(6));
Assert.assertFalse(MicrosoftAppCredentials.isTrustedServiceUrl("https://goodurl.com"));
MicrosoftAppCredentials.trustServiceUrl("https://goodurl.com", LocalDateTime.now().minusMinutes(4));
Assert.assertTrue(MicrosoftAppCredentials.isTrustedServiceUrl("https://goodurl.com"));
}
@Test
public void ValidateAuthEndpoint() {
try {
// In Java, about the only thing that can cause a MalformedURLException in a missing or unknown protocol.
// At any rate, this should validate someone didn't mess up the oAuth Endpoint for the class.
MicrosoftAppCredentials credentials = new MicrosoftAppCredentials("2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F");
new URL(credentials.oAuthEndpoint());
credentials.setChannelAuthTenant("tenant.com");
MicrosoftAppCredentials credentialsWithTenant =
new MicrosoftAppCredentials("2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F", "tenant.com");
} catch(MalformedURLException e) {
Assert.fail("Should not have thrown MalformedURLException");
}
}
@Test
public void GetToken() throws InterruptedException, ExecutionException {
MicrosoftAppCredentials credentials = new MicrosoftAppCredentials("2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F");
AuthenticationResult token = credentials.getToken().get();
Assert.assertFalse(StringUtils.isEmpty(token.getAccessToken()));
}
}

Просмотреть файл

@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.bot.connector;
import com.microsoft.bot.connector.authentication.GovernmentAuthenticationConstants;
import com.microsoft.bot.connector.authentication.SimpleChannelProvider;
import org.junit.Assert;
import org.junit.Test;
public class SimpleChannelProviderTests {
@Test
public void PublicChannelProvider() {
SimpleChannelProvider channel = new SimpleChannelProvider();
Assert.assertTrue(channel.isPublicAzure());
Assert.assertFalse(channel.isGovernment());
}
@Test
public void GovernmentChannelProvider() {
SimpleChannelProvider channel = new SimpleChannelProvider(GovernmentAuthenticationConstants.CHANNELSERVICE);
Assert.assertFalse(channel.isPublicAzure());
Assert.assertTrue(channel.isGovernment());
}
@Test
public void GetChannelService() {
try {
SimpleChannelProvider channel = new SimpleChannelProvider(GovernmentAuthenticationConstants.CHANNELSERVICE);
String service = channel.getChannelService().join();
Assert.assertEquals(service, GovernmentAuthenticationConstants.CHANNELSERVICE);
} catch (Throwable t) {
Assert.fail("Should not have thrown " + t.getClass().getName());
}
}
}

Просмотреть файл

@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.bot.connector;
import com.microsoft.bot.connector.authentication.SimpleCredentialProvider;
import org.junit.Assert;
import org.junit.Test;
public class SimpleCredentialProviderTests {
@Test
public void ValidAppIdAsync() {
SimpleCredentialProvider credentialProvider = new SimpleCredentialProvider("appid", "pwd");
Assert.assertTrue(credentialProvider.isValidAppIdAsync("appid").join());
Assert.assertFalse(credentialProvider.isValidAppIdAsync("wrongappid").join());
}
@Test
public void AppPasswordAsync() {
SimpleCredentialProvider credentialProvider = new SimpleCredentialProvider("appid", "pwd");
Assert.assertEquals(credentialProvider.getAppPasswordAsync("appid").join(), "pwd");
Assert.assertNull(credentialProvider.getAppPasswordAsync("wrongappid").join());
}
@Test
public void AuthenticationDisabledAsync() {
Assert.assertFalse(new SimpleCredentialProvider("appid", "pwd").isAuthenticationDisabledAsync().join());
Assert.assertTrue(new SimpleCredentialProvider(null, null).isAuthenticationDisabledAsync().join());
}
}