Skip to content

Josh/botbuilder tests #4128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 91 additions & 94 deletions tests/Microsoft.Bot.Builder.Tests/ActivityHandlerTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ await Assert.ThrowsAsync<Exception>(() =>
.Send("foo")
.AssertReply(
(activity) => throw new Exception(uniqueExceptionId), "should throw")

.StartTestAsync());
}

Expand Down
2 changes: 0 additions & 2 deletions tests/Microsoft.Bot.Builder.Tests/AllowNullIdTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;

namespace Microsoft.Bot.Builder.Adapters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Schema;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;

namespace Microsoft.Bot.Builder.Tests
{
[TestClass]
[TestCategory("State Management")]
public class AutoSaveStateMiddlewareTests
{
public TestContext TestContext { get; set; }

[TestMethod]
[Fact]
public async Task AutoSaveStateMiddleware_DualReadWrite()
{
var storage = new MemoryStorage();
Expand All @@ -27,7 +23,7 @@ public async Task AutoSaveStateMiddleware_DualReadWrite()
var convState = new ConversationState(storage);
var convProperty = convState.CreateProperty<int>("convCount");

var adapter = new TestAdapter(TestAdapter.CreateConversation(TestContext.TestName))
var adapter = new TestAdapter(TestAdapter.CreateConversation("AutoSaveStateMiddleware_DualReadWrite"))
.Use(new AutoSaveStateMiddleware(userState, convState));

const int USER_INITITAL_COUNT = 100;
Expand Down Expand Up @@ -89,7 +85,7 @@ public async Task AutoSaveStateMiddleware_DualReadWrite()
.StartTestAsync();
}

[TestMethod]
[Fact]
public async Task AutoSaveStateMiddleware_Chain()
{
var storage = new MemoryStorage();
Expand All @@ -104,7 +100,7 @@ public async Task AutoSaveStateMiddleware_Chain()
var bss = new AutoSaveStateMiddleware()
.Add(userState)
.Add(convState);
var adapter = new TestAdapter(TestAdapter.CreateConversation(TestContext.TestName))
var adapter = new TestAdapter(TestAdapter.CreateConversation("AutoSaveStateMiddleware_Chain"))
.Use(bss);

const int USER_INITITAL_COUNT = 100;
Expand Down
14 changes: 5 additions & 9 deletions tests/Microsoft.Bot.Builder.Tests/BotAdapterBracketingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;

namespace Microsoft.Bot.Builder.Tests
{
[TestClass]
[TestCategory("Middleware")]
public class BotAdapterBracketingTest
{
public TestContext TestContext { get; set; }

/// <summary>
/// Developer authored Middleware that looks like this:
/// public async Task ReceiveActivityAsync(ITurnContext turnContext,
Expand All @@ -30,10 +26,10 @@ public class BotAdapterBracketingTest
/// AFTER.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[TestMethod]
[Fact]
public async Task Middlware_BracketingValidation()
{
TestAdapter adapter = new TestAdapter(TestAdapter.CreateConversation(TestContext.TestName))
TestAdapter adapter = new TestAdapter(TestAdapter.CreateConversation("Middlware_BracketingValidation"))
.Use(new BeforeAFterMiddlware());

async Task Echo(ITurnContext ctx, CancellationToken cancellationToken)
Expand All @@ -57,12 +53,12 @@ async Task Echo(ITurnContext ctx, CancellationToken cancellationToken)
/// coming back is correct.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[TestMethod]
[Fact]
public async Task Middlware_ThrowException()
{
string uniqueId = Guid.NewGuid().ToString();

TestAdapter adapter = new TestAdapter(TestAdapter.CreateConversation(TestContext.TestName))
TestAdapter adapter = new TestAdapter(TestAdapter.CreateConversation("Middlware_ThrowException"))
.Use(new CatchExceptionMiddleware());

async Task EchoWithException(ITurnContext ctx, CancellationToken cancellationToken)
Expand Down
20 changes: 8 additions & 12 deletions tests/Microsoft.Bot.Builder.Tests/BotAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Schema;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;

namespace Microsoft.Bot.Builder.Tests
{
[TestClass]
[TestCategory("BotAdapter")]
public class BotAdapterTests
{
public TestContext TestContext { get; set; }

[TestMethod]
[Fact]
public void AdapterSingleUse()
{
var a = new SimpleAdapter();
a.Use(new CallCountingMiddleware());
}

[TestMethod]
[Fact]
public void AdapterUseChaining()
{
var a = new SimpleAdapter();
a.Use(new CallCountingMiddleware()).Use(new CallCountingMiddleware());
}

[TestMethod]
[Fact]
public async Task PassResourceResponsesThrough()
{
void ValidateResponses(Activity[] activities)
Expand All @@ -46,14 +42,14 @@ void ValidateResponses(Activity[] activities)
activity.Id = activityId;

var resourceResponse = await c.SendActivityAsync(activity);
Assert.IsTrue(resourceResponse.Id == activityId, "Incorrect response Id returned");
Assert.True(resourceResponse.Id == activityId, "Incorrect response Id returned");
}

[TestMethod]
[Fact]
public async Task ContinueConversation_DirectMsgAsync()
{
bool callbackInvoked = false;
var adapter = new TestAdapter(TestAdapter.CreateConversation(TestContext.TestName));
var adapter = new TestAdapter(TestAdapter.CreateConversation("ContinueConversation_DirectMsgAsync"));
ConversationReference cr = new ConversationReference
{
ActivityId = "activityId",
Expand Down Expand Up @@ -87,7 +83,7 @@ Task ContinueCallback(ITurnContext turnContext, CancellationToken cancellationTo
}

await adapter.ContinueConversationAsync("MyBot", cr, ContinueCallback, default(CancellationToken));
Assert.IsTrue(callbackInvoked);
Assert.True(callbackInvoked);
}
}
}
55 changes: 27 additions & 28 deletions tests/Microsoft.Bot.Builder.Tests/BotFrameworkAdapterOAuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Microsoft.Bot.Builder.Tests
{
[TestClass]
public class BotFrameworkAdapterOAuthTests
{
[TestMethod]
[Fact]
public async Task BotFrameworkAdapterFindsOAuthCards()
{
var mockConnector = new MemoryConnectorClient();
Expand All @@ -35,18 +34,18 @@ public async Task BotFrameworkAdapterFindsOAuthCards()
var eventActivity = await ProcessOAuthCardTest(adapter, mockConnector, originalActivity);

// bot received the event activity
Assert.IsNotNull(eventActivity);
Assert.AreEqual(originalActivity.Conversation.Id, eventActivity.Conversation.Id);
Assert.AreEqual(originalActivity.From.Id, eventActivity.From.Id);
Assert.AreEqual(originalActivity.From.Name, eventActivity.From.Name);
Assert.AreEqual(ActivityTypes.Event, eventActivity.Type);
Assert.AreEqual(SignInConstants.TokenResponseEventName, eventActivity.Name);
Assert.NotNull(eventActivity);
Assert.Equal(originalActivity.Conversation.Id, eventActivity.Conversation.Id);
Assert.Equal(originalActivity.From.Id, eventActivity.From.Id);
Assert.Equal(originalActivity.From.Name, eventActivity.From.Name);
Assert.Equal(ActivityTypes.Event, eventActivity.Type);
Assert.Equal(SignInConstants.TokenResponseEventName, eventActivity.Name);

var tokenResponse = eventActivity.Value as TokenResponse;
Assert.AreEqual("12345", tokenResponse.Token);
Assert.Equal("12345", tokenResponse.Token);
}

[TestMethod]
[Fact]
public async Task SendsTokenOnSecondAttempt()
{
var mockConnector = new MemoryConnectorClient();
Expand All @@ -66,18 +65,18 @@ public async Task SendsTokenOnSecondAttempt()
var eventActivity = await ProcessOAuthCardTest(adapter, mockConnector, originalActivity);

// bot received the event activity
Assert.IsNotNull(eventActivity);
Assert.AreEqual(originalActivity.Conversation.Id, eventActivity.Conversation.Id);
Assert.AreEqual(originalActivity.From.Id, eventActivity.From.Id);
Assert.AreEqual(originalActivity.From.Name, eventActivity.From.Name);
Assert.AreEqual(ActivityTypes.Event, eventActivity.Type);
Assert.AreEqual(SignInConstants.TokenResponseEventName, eventActivity.Name);
Assert.NotNull(eventActivity);
Assert.Equal(originalActivity.Conversation.Id, eventActivity.Conversation.Id);
Assert.Equal(originalActivity.From.Id, eventActivity.From.Id);
Assert.Equal(originalActivity.From.Name, eventActivity.From.Name);
Assert.Equal(ActivityTypes.Event, eventActivity.Type);
Assert.Equal(SignInConstants.TokenResponseEventName, eventActivity.Name);

var tokenResponse = eventActivity.Value as TokenResponse;
Assert.AreEqual("1234", tokenResponse.Token);
Assert.Equal("1234", tokenResponse.Token);
}

[TestMethod]
[Fact]
public async Task PollingEnds()
{
var mockConnector = new MemoryConnectorClient();
Expand All @@ -95,10 +94,10 @@ public async Task PollingEnds()
var eventActivity = await ProcessOAuthCardTest(adapter, mockConnector, originalActivity);

// Only 1 call to GetToken is called
Assert.AreEqual(1, calls);
Assert.Equal(1, calls);
}

[TestMethod]
[Fact]
public async Task TokenResponsePropertiesEndPolling()
{
var mockConnector = new MemoryConnectorClient();
Expand All @@ -124,11 +123,11 @@ public async Task TokenResponsePropertiesEndPolling()
await Task.Delay(3000);

// Make sure it only polled once and it ended
Assert.AreEqual(1, callCount);
Assert.IsTrue(adapter.Logger.LogData.Contains("PollForTokenAsync completed without receiving a token"));
Assert.Equal(1, callCount);
Assert.Contains("PollForTokenAsync completed without receiving a token", adapter.Logger.LogData);
}

[TestMethod]
[Fact]
public async Task TokenResponsePropertiesCanChange()
{
var mockConnector = new MemoryConnectorClient();
Expand Down Expand Up @@ -164,11 +163,11 @@ public async Task TokenResponsePropertiesCanChange()
await Task.Delay(2000);

// Make sure it only polled twice and it changed settings
Assert.AreEqual(2, callCount);
Assert.IsTrue(adapter.Logger.LogData.Contains("PollForTokenAsync received new polling settings: timeout=50000, interval=500"));
Assert.Equal(2, callCount);
Assert.Contains("PollForTokenAsync received new polling settings: timeout=50000, interval=500", adapter.Logger.LogData);
}

[TestMethod]
[Fact]
public async Task NoConnectionNameThrows()
{
var mockConnector = new MemoryConnectorClient();
Expand Down Expand Up @@ -213,7 +212,7 @@ await adapter.ProcessActivityAsync(
},
CancellationToken.None);

Assert.IsTrue(threw);
Assert.True(threw);
}

private async Task<Activity> ProcessOAuthCardTest(MockAdapter adapter, MemoryConnectorClient mockConnector, Activity originalActivity, Activity outhCardActivity = null, bool expectsEvent = true)
Expand Down
Loading