public async Task ShouldNotInterceptOAuthCardsForEmptyConnectionName() { var connectionName = "connectionName"; var firstResponse = new ExpectedReplies(new List <Activity> { CreateOAuthCardAttachmentActivity("https://test") }); var mockSkillClient = new Mock <BotFrameworkClient>(); mockSkillClient .Setup(x => x.PostActivityAsync <ExpectedReplies>(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Uri>(), It.IsAny <Uri>(), It.IsAny <string>(), It.IsAny <Activity>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(new InvokeResponse <ExpectedReplies> { Status = 200, Body = firstResponse })); var conversationState = new ConversationState(new MemoryStorage()); var dialogOptions = CreateSkillDialogOptions(conversationState, mockSkillClient); var sut = new SkillDialog(dialogOptions); var activityToSend = CreateSendActivity(); var testAdapter = new TestAdapter(Channels.Test) .Use(new AutoSaveStateMiddleware(conversationState)); var client = new DialogTestClient(testAdapter, sut, new BeginSkillDialogOptions { Activity = activityToSend }, conversationState: conversationState); testAdapter.AddExchangeableToken(connectionName, Channels.Test, "user1", "https://test", "https://test1"); var finalActivity = await client.SendActivityAsync <IMessageActivity>("irrelevant"); Assert.IsNotNull(finalActivity); Assert.IsTrue(finalActivity.Attachments.Count == 1); }
private async Task PromptTimeoutEndsDialogTest(IActivity oauthPromptActivity) { var convoState = new ConversationState(new MemoryStorage()); var dialogState = convoState.CreateProperty <DialogState>("dialogState"); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(convoState)); // Create new DialogSet. var dialogs = new DialogSet(dialogState); // Set timeout to zero, so the prompt will end immediately. dialogs.Add(new OAuthPrompt("OAuthPrompt", new OAuthPromptSettings() { Text = "Please sign in", ConnectionName = ConnectionName, Title = "Sign in", Timeout = 0 })); BotCallbackHandler botCallbackHandler = async(turnContext, cancellationToken) => { var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken); var results = await dc.ContinueDialogAsync(cancellationToken); if (results.Status == DialogTurnStatus.Empty) { await dc.PromptAsync("OAuthPrompt", new PromptOptions(), cancellationToken : cancellationToken); } else if (results.Status == DialogTurnStatus.Complete) { // If the TokenResponse comes back, the timeout did not occur. if (results.Result is TokenResponse) { await turnContext.SendActivityAsync("failed", cancellationToken : cancellationToken); } else { await turnContext.SendActivityAsync("ended", cancellationToken : cancellationToken); } } }; await new TestFlow(adapter, botCallbackHandler) .Send("hello") .AssertReply(activity => { Assert.Single(((Activity)activity).Attachments); Assert.Equal(OAuthCard.ContentType, ((Activity)activity).Attachments[0].ContentType); // Add a magic code to the adapter adapter.AddUserToken(ConnectionName, activity.ChannelId, activity.Recipient.Id, Token, MagicCode); // Add an exchangable token to the adapter adapter.AddExchangeableToken(ConnectionName, activity.ChannelId, activity.Recipient.Id, ExchangeToken, Token); }) .Send(oauthPromptActivity) .AssertReply("ended") .StartTestAsync(); }
public async Task TestAdapterTokenExchange() { var convoState = new ConversationState(new MemoryStorage()); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(convoState)); var connectionName = "myConnection"; var exchangeToken = "exch123"; var token = "abc123"; BotCallbackHandler botCallbackHandler = async(turnContext, cancellationToken) => { var userId = "fred"; adapter.AddExchangeableToken(connectionName, turnContext.Activity.ChannelId, userId, exchangeToken, token); // Positive case: Token var result = await adapter.ExchangeTokenAsync(turnContext, connectionName, userId, new TokenExchangeRequest() { Token = exchangeToken }); Assert.NotNull(result); Assert.Equal(token, result.Token); Assert.Equal(connectionName, result.ConnectionName); // Positive case: URI result = await adapter.ExchangeTokenAsync(turnContext, connectionName, userId, new TokenExchangeRequest() { Uri = exchangeToken }); Assert.NotNull(result); Assert.Equal(token, result.Token); Assert.Equal(connectionName, result.ConnectionName); // Negative case: Token result = await adapter.ExchangeTokenAsync(turnContext, connectionName, userId, new TokenExchangeRequest() { Token = "beeboop" }); Assert.Null(result); // Negative case: URI result = await adapter.ExchangeTokenAsync(turnContext, connectionName, userId, new TokenExchangeRequest() { Uri = "beeboop" }); Assert.Null(result); }; await new TestFlow(adapter, botCallbackHandler) .Send("hello") .StartTestAsync(); }
public async Task OAuthPromptWithTokenExchangeInvoke() { var convoState = new ConversationState(new MemoryStorage()); var dialogState = convoState.CreateProperty <DialogState>("dialogState"); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(convoState)); var connectionName = "myConnection"; var exchangeToken = "exch123"; var token = "abc123"; // Create new DialogSet. var dialogs = new DialogSet(dialogState); dialogs.Add(new OAuthPrompt("OAuthPrompt", new OAuthPromptSettings() { Text = "Please sign in", ConnectionName = connectionName, Title = "Sign in" })); BotCallbackHandler botCallbackHandler = async(turnContext, cancellationToken) => { var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken); var results = await dc.ContinueDialogAsync(cancellationToken); if (results.Status == DialogTurnStatus.Empty) { await dc.PromptAsync("OAuthPrompt", new PromptOptions(), cancellationToken : cancellationToken); } else if (results.Status == DialogTurnStatus.Complete) { if (results.Result is TokenResponse) { await turnContext.SendActivityAsync(MessageFactory.Text("Logged in."), cancellationToken); } else { await turnContext.SendActivityAsync(MessageFactory.Text("Failed."), cancellationToken); } } }; await new TestFlow(adapter, botCallbackHandler) .Send("hello") .AssertReply(activity => { Assert.Single(((Activity)activity).Attachments); Assert.Equal(OAuthCard.ContentType, ((Activity)activity).Attachments[0].ContentType); Assert.Equal(InputHints.AcceptingInput, ((Activity)activity).InputHint); // Add an exchangable token to the adapter adapter.AddExchangeableToken(connectionName, activity.ChannelId, activity.Recipient.Id, exchangeToken, token); }) .Send(new Activity() { Type = ActivityTypes.Invoke, Name = SignInConstants.TokenExchangeOperationName, Value = JObject.FromObject(new TokenExchangeInvokeRequest() { ConnectionName = connectionName, Token = exchangeToken }) }) .AssertReply(a => { Assert.Equal("invokeResponse", a.Type); var response = ((Activity)a).Value as InvokeResponse; Assert.NotNull(response); Assert.Equal(200, response.Status); var body = response.Body as TokenExchangeInvokeResponse; Assert.Equal(connectionName, body.ConnectionName); Assert.Null(body.FailureDetail); }) .AssertReply("Logged in.") .StartTestAsync(); }