示例#1
0
        public async Task HandlesBotAndSkillsTestCases(SkillFlowTestCase testCase, bool shouldSendEoc)
        {
            var firstConversationId = Guid.NewGuid().ToString();
            var storage             = new MemoryStorage();

            var adaptiveDialog = CreateTestDialog(property: "conversation.name");

            await CreateFlow(adaptiveDialog, storage, firstConversationId, testCase : testCase, locale : "en-GB").Send("Hi")
            .AssertReply("Hello, what is your name?")
            .Send("SomeName")
            .AssertReply("Hello SomeName, nice to meet you!")
            .StartTestAsync();

            Assert.Equal(DialogTurnStatus.Complete, _dmTurnResult.TurnResult.Status);

            if (shouldSendEoc)
            {
                Assert.NotNull(_eocSent);
                Assert.Equal(ActivityTypes.EndOfConversation, _eocSent.Type);
                Assert.Equal("SomeName", _eocSent.Value);
                Assert.Equal("en-GB", _eocSent.Locale);
            }
            else
            {
                Assert.Null(_eocSent);
            }
        }
示例#2
0
        private TestFlow CreateFlow(Dialog dialog, IStorage storage, string conversationId, string dialogStateProperty = null, SkillFlowTestCase testCase = SkillFlowTestCase.RootBotOnly, string locale = null)
        {
            var convoState = new ConversationState(storage);
            var userState  = new UserState(storage);

            var adapter = new TestAdapter(TestAdapter.CreateConversation(conversationId));

            adapter
            .UseStorage(storage)
            .UseBotState(userState, convoState)
            .Use(new TranscriptLoggerMiddleware(new TraceTranscriptLogger(traceActivity: false)));

            if (!string.IsNullOrEmpty(locale))
            {
                adapter.Locale = locale;
            }

            var dm = new DialogManager(dialog, dialogStateProperty: dialogStateProperty);

            return(new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                if (testCase != SkillFlowTestCase.RootBotOnly)
                {
                    // Create a skill ClaimsIdentity and put it in TurnState so SkillValidation.IsSkillClaim() returns true.
                    var claimsIdentity = new ClaimsIdentity();
                    claimsIdentity.AddClaim(new Claim(AuthenticationConstants.VersionClaim, "2.0"));
                    claimsIdentity.AddClaim(new Claim(AuthenticationConstants.AudienceClaim, _skillBotId));
                    claimsIdentity.AddClaim(new Claim(AuthenticationConstants.AuthorizedParty, _parentBotId));
                    turnContext.TurnState.Add(BotAdapter.BotIdentityKey, claimsIdentity);

                    if (testCase == SkillFlowTestCase.RootBotConsumingSkill)
                    {
                        // Simulate the SkillConversationReference with a channel OAuthScope stored in TurnState.
                        // This emulates a response coming to a root bot through SkillHandler.
                        turnContext.TurnState.Add(SkillHandler.SkillConversationReferenceKey, new SkillConversationReference {
                            OAuthScope = AuthenticationConstants.ToChannelFromBotOAuthScope
                        });
                    }

                    if (testCase == SkillFlowTestCase.MiddleSkill)
                    {
                        // Simulate the SkillConversationReference with a parent Bot ID stored in TurnState.
                        // This emulates a response coming to a skill from another skill through SkillHandler.
                        turnContext.TurnState.Add(SkillHandler.SkillConversationReferenceKey, new SkillConversationReference {
                            OAuthScope = _parentBotId
                        });
                    }
                }

                // Interceptor to capture the EoC activity if it was sent so we can assert it in the tests.
                turnContext.OnSendActivities(async(tc, activities, next) =>
                {
                    _eocSent = activities.FirstOrDefault(activity => activity.Type == ActivityTypes.EndOfConversation);
                    return await next().ConfigureAwait(false);
                });

                // Capture the last DialogManager turn result for assertions.
                _dmTurnResult = await dm.OnTurnAsync(turnContext, cancellationToken: cancellationToken).ConfigureAwait(false);
            }));
        }