public async Task SendsTokenOnSecondAttempt()
        {
            var mockConnector          = new MemoryConnectorClient();
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            int callCount = 0;
            var adapter   = new MockAdapter(mockCredentialProvider.Object, () =>
            {
                callCount++;
                if (callCount > 1)
                {
                    return(new TokenResponse()
                    {
                        Token = "1234"
                    });
                }

                return(null);
            });
            var originalActivity = CreateBasicActivity();
            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("tokens/response", eventActivity.Name);

            var tokenResponse = eventActivity.Value as TokenResponse;

            Assert.AreEqual("1234", tokenResponse.Token);
        }
        public async Task TokenResponsePropertiesEndPolling()
        {
            var mockConnector          = new MemoryConnectorClient();
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            int callCount = 0;
            var adapter   = new MockAdapter(
                mockCredentialProvider.Object,
                () =>
            {
                callCount++;
                return(new TokenResponse()
                {
                    Properties = MakeProperties(0, null),
                });
            },
                new MockLogger());

            var originalActivity = CreateBasicActivity();

            var eventActivity = await ProcessOAuthCardTest(adapter, mockConnector, originalActivity, null, false);

            // Wait a bit to let the polling Task run (enough for 3 potential polls)
            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"));
        }
        public async Task BotFrameworkAdapterFindsOAuthCards()
        {
            var mockConnector          = new MemoryConnectorClient();
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            var adapter = new MockAdapter(mockCredentialProvider.Object, () =>
            {
                return(new TokenResponse()
                {
                    Token = "12345"
                });
            });
            var originalActivity = CreateBasicActivity();

            var eventActivity = await ProcessOAuthCardTest(adapter, mockConnector, originalActivity);

            // 1 activity sent from bot to user
            Assert.AreEqual(1, ((MemoryConversations)mockConnector.Conversations).SentActivities.Count);

            // 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("tokens/response", eventActivity.Name);

            var tokenResponse = eventActivity.Value as TokenResponse;

            Assert.AreEqual("12345", tokenResponse.Token);
        }
        public async Task NoConnectionNameThrows()
        {
            var mockConnector          = new MemoryConnectorClient();
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            var adapter = new MockAdapter(
                mockCredentialProvider.Object,
                () =>
            {
                return(new TokenResponse()
                {
                    Token = "12345"
                });
            },
                new MockLogger());
            var originalActivity = CreateBasicActivity();
            var badOauth         = CreateOAuthCardActivity();

            ((OAuthCard)badOauth.Attachments.First().Content).ConnectionName = null;

            var mockClaims = new Mock <ClaimsIdentity>();

            bool threw = false;

            await adapter.ProcessActivityAsync(
                mockClaims.Object,
                originalActivity,
                async (context, token) =>
            {
                switch (context.Activity.Type)
                {
                case ActivityTypes.Message:
                    context.TurnState.Remove(typeof(IConnectorClient).FullName);
                    context.TurnState.Add <IConnectorClient>(mockConnector);

                    try
                    {
                        await context.SendActivityAsync(badOauth);
                    }
                    catch (InvalidOperationException)
                    {
                        threw = true;
                    }

                    break;
                }
            },
                CancellationToken.None);

            Assert.IsTrue(threw);
        }
        public async Task PollingEnds()
        {
            var mockConnector          = new MemoryConnectorClient();
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            int calls   = 0;
            var adapter = new MockAdapter(
                mockCredentialProvider.Object,
                () =>
            {
                calls++;
                return(new TokenResponse()
                {
                    Token = "12345"
                });
            });
            var originalActivity = CreateBasicActivity();

            var eventActivity = await ProcessOAuthCardTest(adapter, mockConnector, originalActivity);

            // Only 1 call to GetToken is called
            Assert.AreEqual(1, calls);
        }
        private async Task <Activity> ProcessOAuthCardTest(MockAdapter adapter, MemoryConnectorClient mockConnector, Activity originalActivity, Activity outhCardActivity = null, bool expectsEvent = true)
        {
            var      mockClaims    = new Mock <ClaimsIdentity>();
            Activity eventActivity = null;

            outhCardActivity = outhCardActivity ?? CreateOAuthCardActivity();

            TaskCompletionSource <string> receivedEventActivity = new TaskCompletionSource <string>();

            await adapter.ProcessActivityAsync(
                mockClaims.Object,
                originalActivity,
                async (context, token) =>
            {
                switch (context.Activity.Type)
                {
                case ActivityTypes.Message:
                    context.TurnState.Remove(typeof(IConnectorClient).FullName);
                    context.TurnState.Add <IConnectorClient>(mockConnector);
                    await context.SendActivityAsync(outhCardActivity);
                    break;

                case ActivityTypes.Event:
                    eventActivity = context.Activity;
                    receivedEventActivity.SetResult("done");
                    break;
                }

                if (!expectsEvent)
                {
                    receivedEventActivity.SetResult("done");
                }
            },
                CancellationToken.None);

            await receivedEventActivity.Task;

            return(eventActivity);
        }
        public async Task TokenResponsePropertiesCanChange()
        {
            var mockConnector          = new MemoryConnectorClient();
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            int callCount = 0;
            var adapter   = new MockAdapter(
                mockCredentialProvider.Object,
                () =>
            {
                callCount++;
                if (callCount < 2)
                {
                    return(new TokenResponse()
                    {
                        Properties = MakeProperties(50000, 500),
                    });
                }
                else
                {
                    return(new TokenResponse()
                    {
                        Token = "123",
                    });
                }
            },
                new MockLogger());

            var originalActivity = CreateBasicActivity();

            var eventActivity = await ProcessOAuthCardTest(adapter, mockConnector, originalActivity);

            // Wait a bit to let the polling Task run (enough for 3 potential polls)
            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"));
        }
示例#8
0
        public async Task OutgoingActivityIdsAreNotSent()
        {
            // Arrange
            var mockCredentialProvider = new Mock <ICredentialProvider>();
            var mockConnector          = new MemoryConnectorClient();
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();
            var httpClient             = new HttpClient(mockHttpMessageHandler.Object);
            var adapter = new BotFrameworkAdapter(mockCredentialProvider.Object, customHttpClient: httpClient);

            var incomingActivity = new Activity("test")
            {
                Id           = "testid",
                ChannelId    = Channels.Directline,
                ServiceUrl   = "https://fake.service.url",
                Conversation = new ConversationAccount
                {
                    Id = "cid",
                }
            };

            var reply = MessageFactory.Text("test");

            reply.Id = "TestReplyId";

            // Act
            using (var turnContext = new TurnContext(adapter, incomingActivity))
            {
                turnContext.TurnState.Add <IConnectorClient>(mockConnector);

                var responseIds = await turnContext.SendActivityAsync(reply, default);
            }

            var sentActivity = mockConnector.MemoryConversations.SentActivities.FirstOrDefault(f => f.Type == ActivityTypes.Message);

            // Assert - assert the reply's id is not sent
            Assert.IsNull(sentActivity.Id);
        }