Skip to content
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

Add ShowSignInLink to OAuthPromptSettings #5906

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public static async Task SendOAuthCardAsync(OAuthPromptSettings settings, ITurnC
cardActionType = ActionTypes.OpenUrl;
}
}
else if (!ChannelRequiresSignInLink(turnContext.Activity.ChannelId))
else if ((settings.ShowSignInLink != null && settings.ShowSignInLink == false) ||
(settings.ShowSignInLink == null && !ChannelRequiresSignInLink(turnContext.Activity.ChannelId)))
{
value = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,12 @@ public class OAuthPromptSettings
/// <value>True if the <see cref="OAuthPrompt"/> should automatically end upon receiving
/// an invalid message.</value>
public bool EndOnInvalidMessage { get; set; }

/// <summary>
/// Gets or sets an optional boolean value to force the display of a Sign In link overriding
/// the default behavior.
/// </summary>
/// <value>True to display the SignInLink.</value>
public bool? ShowSignInLink { get; set; }
}
}
132 changes: 132 additions & 0 deletions tests/Microsoft.Bot.Builder.Dialogs.Tests/OAuthPromptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,108 @@ public async Task OAuthPromptInNotSupportedChannelShouldAddSignInCard()
.StartTestAsync();
}

[Fact]
mrivera-ms marked this conversation as resolved.
Show resolved Hide resolved
public async Task OAuthPromptWithShowSignInLinkSettingOverridesDefaultChannelBehavior()
{
var channelId = Channels.Test; //Default ChannelRequiresSingInLink() returns false

var oAuthPromptSettings = new OAuthPromptSettings();
oAuthPromptSettings.ShowSignInLink = true; //Value overrides default behavior

TestAdapter adapter = null;
BotCallbackHandler botCallbackHandler = null;
Activity initialActivity = null;
PrepareTest(channelId, ref adapter, ref oAuthPromptSettings, ref botCallbackHandler, ref initialActivity);

await new TestFlow(adapter, botCallbackHandler)
.Send(initialActivity)
.AssertReply(activity =>
{
Assert.Single(((Activity)activity).Attachments);
Assert.Equal(OAuthCard.ContentType, ((Activity)activity).Attachments[0].ContentType);
var oAuthCard = (OAuthCard)((Activity)activity).Attachments[0].Content;
var cardAction = oAuthCard.Buttons[0];
Assert.NotNull(cardAction.Value); //verify the link is not null
})
.StartTestAsync();
}

[Fact]
public async Task OAuthPromptWithoutShowSignInLinkSettingFollowsDefaultChannelBehavior_NoLink()
{
var channelId = Channels.Test; //Default ChannelRequiresSingInLink() returns false

var oAuthPromptSettings = new OAuthPromptSettings(); // settings do not include ShowSignInLink

TestAdapter adapter = null;
BotCallbackHandler botCallbackHandler = null;
Activity initialActivity = null;
PrepareTest(channelId, ref adapter, ref oAuthPromptSettings, ref botCallbackHandler, ref initialActivity);

await new TestFlow(adapter, botCallbackHandler)
.Send(initialActivity)
.AssertReply(activity =>
{
Assert.Single(((Activity)activity).Attachments);
Assert.Equal(OAuthCard.ContentType, ((Activity)activity).Attachments[0].ContentType);
var oAuthCard = (OAuthCard)((Activity)activity).Attachments[0].Content;
var cardAction = oAuthCard.Buttons[0];
Assert.Null(cardAction.Value); //verify the link is null
})
.StartTestAsync();
}

[Fact]
public async Task OAuthPromptWithShowSignInLinkSettingOverridesDefaultChannelBehavior_NoLink()
{
var channelId = Channels.Msteams; //Default ChannelRequiresSingInLink() returns true

var oAuthPromptSettings = new OAuthPromptSettings();
oAuthPromptSettings.ShowSignInLink = false; //Value overrides default behavior

TestAdapter adapter = null;
BotCallbackHandler botCallbackHandler = null;
Activity initialActivity = null;
PrepareTest(channelId, ref adapter, ref oAuthPromptSettings, ref botCallbackHandler, ref initialActivity);

await new TestFlow(adapter, botCallbackHandler)
.Send(initialActivity)
.AssertReply(activity =>
{
Assert.Single(((Activity)activity).Attachments);
Assert.Equal(OAuthCard.ContentType, ((Activity)activity).Attachments[0].ContentType);
var oAuthCard = (OAuthCard)((Activity)activity).Attachments[0].Content;
var cardAction = oAuthCard.Buttons[0];
Assert.Null(cardAction.Value); //verify the link is null
})
.StartTestAsync();
}

[Fact]
public async Task OAuthPromptWithoutShowSignInLinkSettingFollowsDefaultChannelBehavior_ShowLink()
{
var channelId = Channels.Msteams; //Default ChannelRequiresSingInLink() returns true

var oAuthPromptSettings = new OAuthPromptSettings(); // settings do not include ShowSignInLink

TestAdapter adapter = null;
BotCallbackHandler botCallbackHandler = null;
Activity initialActivity = null;
PrepareTest(channelId, ref adapter, ref oAuthPromptSettings, ref botCallbackHandler, ref initialActivity);

await new TestFlow(adapter, botCallbackHandler)
.Send(initialActivity)
.AssertReply(activity =>
{
Assert.Single(((Activity)activity).Attachments);
Assert.Equal(OAuthCard.ContentType, ((Activity)activity).Attachments[0].ContentType);
var oAuthCard = (OAuthCard)((Activity)activity).Attachments[0].Content;
var cardAction = oAuthCard.Buttons[0];
Assert.NotNull(cardAction.Value); //verify the link is not null
})
.StartTestAsync();
}

[Fact]
public async Task TestAdapterTokenExchange()
{
Expand Down Expand Up @@ -868,5 +970,35 @@ private Activity CreateEventResponse(TestAdapter adapter, IActivity activity, st

return eventActivity;
}

private void PrepareTest(string channelId, ref TestAdapter adapter, ref OAuthPromptSettings oAuthPromptSettings, ref BotCallbackHandler botCallbackHandler, ref Activity initialActivity)
{
var convoState = new ConversationState(new MemoryStorage());
var dialogState = convoState.CreateProperty<DialogState>("dialogState");

adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));

// Create new DialogSet
var dialogs = new DialogSet(dialogState);
dialogs.Add(new OAuthPrompt("OAuthPrompt", oAuthPromptSettings));

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);
}
};

initialActivity = new Activity()
{
ChannelId = channelId,
Text = "hello"
};
}
}
}