This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3736b26
commit 466fd90
Showing
6 changed files
with
224 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...aries/bot-builder/src/test/java/com/microsoft/bot/builder/ChannelServiceHandlerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MT License. | ||
|
||
package com.microsoft.bot.builder; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import com.microsoft.bot.connector.authentication.AuthenticationConfiguration; | ||
import com.microsoft.bot.connector.authentication.AuthenticationConstants; | ||
import com.microsoft.bot.connector.authentication.ClaimsIdentity; | ||
import com.microsoft.bot.connector.authentication.JwtTokenValidation; | ||
import com.microsoft.bot.connector.authentication.SimpleCredentialProvider; | ||
import com.microsoft.bot.schema.Activity; | ||
import com.microsoft.bot.schema.ActivityTypes; | ||
import com.microsoft.bot.schema.ResourceResponse; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class ChannelServiceHandlerTests { | ||
|
||
@Test | ||
public void AuthenticateSetsAnonymousSkillClaim() { | ||
TestChannelServiceHandler sut = new TestChannelServiceHandler(); | ||
sut.handleReplyToActivity(null, "123", "456", new Activity(ActivityTypes.MESSAGE)); | ||
|
||
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_AUTH_TYPE, | ||
sut.getClaimsIdentity().getType()); | ||
Assert.assertEquals(AuthenticationConstants.ANONYMOUS_SKILL_APPID, | ||
JwtTokenValidation.getAppIdFromClaims(sut.getClaimsIdentity().claims())); | ||
} | ||
|
||
/** | ||
* A {@link ChannelServiceHandler} with overrides for testings. | ||
*/ | ||
private class TestChannelServiceHandler extends ChannelServiceHandler { | ||
TestChannelServiceHandler() { | ||
super(new SimpleCredentialProvider(), new AuthenticationConfiguration(), null); | ||
} | ||
|
||
private ClaimsIdentity claimsIdentity; | ||
|
||
@Override | ||
protected CompletableFuture<ResourceResponse> onReplyToActivity( | ||
ClaimsIdentity claimsIdentity, | ||
String conversationId, | ||
String activityId, | ||
Activity activity | ||
) { | ||
this.claimsIdentity = claimsIdentity; | ||
return CompletableFuture.completedFuture(new ResourceResponse()); | ||
} | ||
/** | ||
* Gets the {@link ClaimsIdentity} sent to the different methods after | ||
* auth is done. | ||
* @return the ClaimsIdentity value as a getClaimsIdentity(). | ||
*/ | ||
public ClaimsIdentity getClaimsIdentity() { | ||
return this.claimsIdentity; | ||
} | ||
|
||
/** | ||
* Gets the {@link ClaimsIdentity} sent to the different methods after | ||
* auth is done. | ||
* @param withClaimsIdentity The ClaimsIdentity value. | ||
*/ | ||
private void setClaimsIdentity(ClaimsIdentity withClaimsIdentity) { | ||
this.claimsIdentity = withClaimsIdentity; | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
libraries/bot-connector/src/test/java/com/microsoft/bot/connector/AppCredentialsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MT License. | ||
|
||
package com.microsoft.bot.connector; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
|
||
import com.microsoft.bot.connector.authentication.AppCredentials; | ||
import com.microsoft.bot.connector.authentication.AppCredentialsInterceptor; | ||
import com.microsoft.bot.connector.authentication.AuthenticationConstants; | ||
import com.microsoft.bot.connector.authentication.Authenticator; | ||
import com.microsoft.bot.restclient.ServiceClient; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Protocol; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import retrofit2.Retrofit; | ||
|
||
public class AppCredentialsTests { | ||
|
||
@Test | ||
public void ConstructorTests() { | ||
TestAppCredentials shouldDefaultToChannelScope = new TestAppCredentials("irrelevant"); | ||
Assert.assertEquals(AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, | ||
shouldDefaultToChannelScope.oAuthScope()); | ||
|
||
TestAppCredentials shouldDefaultToCustomScope = new TestAppCredentials("irrelevant", "customScope"); | ||
Assert.assertEquals("customScope", shouldDefaultToCustomScope.oAuthScope()); | ||
} | ||
|
||
@Test | ||
public void basicCredentialsTest() throws Exception { | ||
TestAppCredentials credentials = new TestAppCredentials("irrelevant", "pass"); | ||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); | ||
credentials.applyCredentialsFilter(clientBuilder); | ||
clientBuilder.addInterceptor( | ||
new Interceptor() { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
String header = chain.request().header("Authorization"); | ||
Assert.assertNull(header); | ||
return new Response.Builder() | ||
.request(chain.request()) | ||
.code(200) | ||
.message("OK") | ||
.protocol(Protocol.HTTP_1_1) | ||
.body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")) | ||
.build(); | ||
} | ||
}); | ||
ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; | ||
Response response = serviceClient.httpClient().newCall( | ||
new Request.Builder().url("http://localhost").build()).execute(); | ||
Assert.assertEquals(200, response.code()); | ||
} | ||
|
||
private class TestAppCredentials extends AppCredentials { | ||
TestAppCredentials(String channelAuthTenant) { | ||
super(channelAuthTenant); | ||
} | ||
|
||
TestAppCredentials(String channelAuthTenant, String oAuthScope) { | ||
super(channelAuthTenant, oAuthScope); | ||
} | ||
|
||
@Override | ||
protected Authenticator buildAuthenticator() throws MalformedURLException { | ||
return null; | ||
} | ||
|
||
/** | ||
* Apply the credentials to the HTTP request. | ||
* | ||
* <p> | ||
* Note: Provides the same functionality as dotnet ProcessHttpRequestAsync | ||
* </p> | ||
* | ||
* @param clientBuilder the builder for building up an {@link OkHttpClient} | ||
*/ | ||
@Override | ||
public void applyCredentialsFilter(OkHttpClient.Builder clientBuilder) { | ||
clientBuilder.interceptors().add(new AppCredentialsInterceptor(this)); | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters