public async Task TestAdapter_GetTokenStatusWithFilter() { TestAdapter adapter = new TestAdapter(); string channelId = "directline"; string userId = "testUser"; string token = "abc123"; Activity activity = new Activity() { ChannelId = channelId, From = new ChannelAccount() { Id = userId, }, }; TurnContext turnContext = new TurnContext(adapter, activity); adapter.AddUserToken("ABC", channelId, userId, token); adapter.AddUserToken("DEF", channelId, userId, token); var status = await adapter.GetTokenStatusAsync(turnContext, userId, "DEF"); Assert.NotNull(status); Assert.Single(status); var oAuthAppCredentials = MicrosoftAppCredentials.Empty; status = await adapter.GetTokenStatusAsync(turnContext, oAuthAppCredentials, userId, "DEF"); Assert.NotNull(status); Assert.Single(status); }
public async Task TestAdapter_SignOut() { TestAdapter adapter = new TestAdapter(); string connectionName = "myConnection"; string channelId = "directline"; string userId = "testUser"; string token = "abc123"; Activity activity = new Activity() { ChannelId = channelId, From = new ChannelAccount() { Id = userId, }, }; TurnContext turnContext = new TurnContext(adapter, activity); adapter.AddUserToken(connectionName, channelId, userId, token); var tokenResponse = await adapter.GetUserTokenAsync(turnContext, connectionName, null, CancellationToken.None); Assert.IsNotNull(tokenResponse); Assert.AreEqual(token, tokenResponse.Token); Assert.AreEqual(connectionName, tokenResponse.ConnectionName); await adapter.SignOutUserAsync(turnContext, connectionName, userId); tokenResponse = await adapter.GetUserTokenAsync(turnContext, connectionName, null, CancellationToken.None); Assert.IsNull(tokenResponse); }
public async Task TestAdapter_GetUserTokenAsyncReturnsToken() { TestAdapter adapter = new TestAdapter(); string connectionName = "myConnection"; string channelId = "directline"; string userId = "testUser"; string token = "abc123"; Activity activity = new Activity() { ChannelId = channelId, From = new ChannelAccount() { Id = userId, }, }; TurnContext turnContext = new TurnContext(adapter, activity); adapter.AddUserToken(connectionName, channelId, userId, token); var tokenResponse = await adapter.GetUserTokenAsync(turnContext, connectionName, null, CancellationToken.None); Assert.NotNull(tokenResponse); Assert.Equal(token, tokenResponse.Token); Assert.Equal(connectionName, tokenResponse.ConnectionName); var oAuthAppCredentials = MicrosoftAppCredentials.Empty; tokenResponse = await adapter.GetUserTokenAsync(turnContext, oAuthAppCredentials, connectionName, null, CancellationToken.None); Assert.NotNull(tokenResponse); Assert.Equal(token, tokenResponse.Token); Assert.Equal(connectionName, tokenResponse.ConnectionName); }
public async Task GetUserTokenShouldReturnToken() { var oauthPromptSettings = new OAuthPromptSettings { ConnectionName = ConnectionName, Text = "Please sign in", Title = "Sign in", }; var prompt = new OAuthPrompt("OAuthPrompt", oauthPromptSettings); var convoState = new ConversationState(new MemoryStorage()); var dialogState = convoState.CreateProperty <DialogState>("dialogState"); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(convoState)); adapter.AddUserToken(ConnectionName, ChannelId, UserId, Token); // Create new DialogSet. var dialogs = new DialogSet(dialogState); dialogs.Add(prompt); var activity = new Activity { ChannelId = ChannelId, From = new ChannelAccount { Id = UserId } }; var turnContext = new TurnContext(adapter, activity); var userToken = await prompt.GetUserTokenAsync(turnContext, CancellationToken.None); Assert.Equal(Token, userToken.Token); }
public async Task OAuthPromptWithMagicCode() { var convoState = new ConversationState(new MemoryStorage()); var dialogState = convoState.CreateProperty <DialogState>("dialogState"); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(convoState)); var connectionName = "myConnection"; var token = "abc123"; var magicCode = "888999"; // 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 a magic code to the adapter adapter.AddUserToken(connectionName, activity.ChannelId, activity.Recipient.Id, token, magicCode); }) .Send(magicCode) .AssertReply("Logged in.") .StartTestAsync(); }
public override void Setup(TestAdapter adapter) { var conversation = adapter.Conversation; var channelId = string.IsNullOrEmpty(ChannelId) ? conversation.ChannelId : ChannelId; var userId = string.IsNullOrEmpty(UserId) ? conversation.User.Id : UserId; adapter.AddUserToken(ConnectionName, channelId, userId, Token, MagicCode); }
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(); }
private void ApplyAdapterConfiguration(TestAdapter adapter) { foreach (var userToken in this.botAdapterConfiguration.UserAccessTokens) { adapter.AddUserToken( userToken.ConnectionName, userToken.ChannelId, userToken.UserId, userToken.Token, userToken.MagicCode); } }
public async Task OAuthPromptDoesNotDetectCodeInBeginDialog() { var convoState = new ConversationState(new MemoryStorage()); var dialogState = convoState.CreateProperty <DialogState>("dialogState"); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(convoState)); var connectionName = "myConnection"; var token = "abc123"; var magicCode = "888999"; // 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) => { // Add a magic code to the adapter preemptively so that we can test if the message that triggers BeginDialogAsync uses magic code detection adapter.AddUserToken(connectionName, turnContext.Activity.ChannelId, turnContext.Activity.From.Id, token, magicCode); var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken); var results = await dc.ContinueDialogAsync(cancellationToken); if (results.Status == DialogTurnStatus.Empty) { // If magicCode is detected when prompting, this will end the dialog and return the token in tokenResult var tokenResult = await dc.PromptAsync("OAuthPrompt", new PromptOptions(), cancellationToken : cancellationToken); if (tokenResult.Result is TokenResponse) { throw new XunitException(); } } }; // Call BeginDialogAsync by sending the magic code as the first message. It SHOULD respond with an OAuthPrompt since we haven't authenticated yet await new TestFlow(adapter, botCallbackHandler) .Send(magicCode) .AssertReply(activity => { Assert.Single(((Activity)activity).Attachments); Assert.Equal(OAuthCard.ContentType, ((Activity)activity).Attachments[0].ContentType); Assert.Equal(InputHints.AcceptingInput, ((Activity)activity).InputHint); }) .StartTestAsync(); }
private static TestFlow CreateMultiAuthDialogTestFlow() { var convoState = new ConversationState(new MemoryStorage()); var dialogState = convoState.CreateProperty <DialogState>("dialogState"); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(convoState)); var testFlow = new TestFlow(adapter, async(turnContext, cancellationToken) => { var state = await dialogState.GetAsync(turnContext, () => new DialogState(), cancellationToken); var dialogs = new DialogSet(dialogState); // Adapter add token adapter.AddUserToken("Test", "test", "test", "test"); // Add MicrosoftAPPId to configuration var listOfOauthConnections = new List <OAuthConnection> { new OAuthConnection { Name = "Test", Provider = "Test" } }; var steps = new WaterfallStep[] { GetAuthTokenAsync, AfterGetAuthTokenAsync, }; dialogs.Add(new MultiProviderAuthDialog(listOfOauthConnections)); dialogs.Add(new WaterfallDialog("Auth", steps)); var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken); var results = await dc.ContinueDialogAsync(cancellationToken); if (results.Status == DialogTurnStatus.Empty) { results = await dc.BeginDialogAsync("Auth", null, cancellationToken); } if (results.Status == DialogTurnStatus.Cancelled) { await turnContext.SendActivityAsync(MessageFactory.Text($"Component dialog cancelled (result value is {results.Result?.ToString()})."), cancellationToken); } else if (results.Status == DialogTurnStatus.Complete) { var value = (int)results.Result; await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received the number '{value}'."), cancellationToken); } }); return(testFlow); }
public async Task TestAdapter_GetTokenStatusWithFilter() { TestAdapter adapter = new TestAdapter(); string channelId = "directline"; string userId = "testUser"; string token = "abc123"; Activity activity = new Activity() { ChannelId = channelId, From = new ChannelAccount() { Id = userId, }, }; TurnContext turnContext = new TurnContext(adapter, activity); adapter.AddUserToken("ABC", channelId, userId, token); adapter.AddUserToken("DEF", channelId, userId, token); var status = await adapter.GetTokenStatusAsync(turnContext, userId, "DEF"); Assert.IsNotNull(status); Assert.AreEqual(1, status.Length); }
public async Task TestAdapter_GetUserTokenAsyncReturnsTokenWithMagicCode() { TestAdapter adapter = new TestAdapter(); string connectionName = "myConnection"; string channelId = "directline"; string userId = "testUser"; string token = "abc123"; string magicCode = "888999"; Activity activity = new Activity() { ChannelId = channelId, From = new ChannelAccount() { Id = userId, }, }; TurnContext turnContext = new TurnContext(adapter, activity); adapter.AddUserToken(connectionName, channelId, userId, token, magicCode); // First it's null var tokenResponse = await adapter.GetUserTokenAsync(turnContext, connectionName, null, CancellationToken.None); Assert.Null(tokenResponse); // Can be retrieved with magic code tokenResponse = await adapter.GetUserTokenAsync(turnContext, connectionName, magicCode, CancellationToken.None); Assert.NotNull(tokenResponse); Assert.Equal(token, tokenResponse.Token); Assert.Equal(connectionName, tokenResponse.ConnectionName); // Then can be retrieved without magic code tokenResponse = await adapter.GetUserTokenAsync(turnContext, connectionName, null, CancellationToken.None); Assert.NotNull(tokenResponse); Assert.Equal(token, tokenResponse.Token); Assert.Equal(connectionName, tokenResponse.ConnectionName); // Then can be retrieved using customized AppCredentials var oAuthAppCredentials = MicrosoftAppCredentials.Empty; tokenResponse = await adapter.GetUserTokenAsync(turnContext, oAuthAppCredentials, connectionName, null, CancellationToken.None); Assert.NotNull(tokenResponse); Assert.Equal(token, tokenResponse.Token); Assert.Equal(connectionName, tokenResponse.ConnectionName); }
private Activity CreateEventResponse(TestAdapter adapter, IActivity activity, string connectionName, string token) { // add the token to the TestAdapter adapter.AddUserToken(connectionName, activity.ChannelId, activity.Recipient.Id, token); // send an event TokenResponse activity to the botCallback handler var eventActivity = ((Activity)activity).CreateReply(); eventActivity.Type = ActivityTypes.Event; var from = eventActivity.From; eventActivity.From = eventActivity.Recipient; eventActivity.Recipient = from; eventActivity.Name = "tokens/response"; eventActivity.Value = JObject.FromObject(new TokenResponse() { ConnectionName = connectionName, Token = token, }); return(eventActivity); }
public async Task TestAdapter_SignOutAll() { TestAdapter adapter = new TestAdapter(); string channelId = "directline"; string userId = "testUser"; string token = "abc123"; Activity activity = new Activity() { ChannelId = channelId, From = new ChannelAccount() { Id = userId, }, }; TurnContext turnContext = new TurnContext(adapter, activity); adapter.AddUserToken("ABC", channelId, userId, token); adapter.AddUserToken("DEF", channelId, userId, token); var tokenResponse = await adapter.GetUserTokenAsync(turnContext, "ABC", null, CancellationToken.None); Assert.IsNotNull(tokenResponse); Assert.AreEqual(token, tokenResponse.Token); Assert.AreEqual("ABC", tokenResponse.ConnectionName); tokenResponse = await adapter.GetUserTokenAsync(turnContext, "DEF", null, CancellationToken.None); Assert.IsNotNull(tokenResponse); Assert.AreEqual(token, tokenResponse.Token); Assert.AreEqual("DEF", tokenResponse.ConnectionName); await adapter.SignOutUserAsync(turnContext, connectionName : null, userId); tokenResponse = await adapter.GetUserTokenAsync(turnContext, "ABC", null, CancellationToken.None); Assert.IsNull(tokenResponse); tokenResponse = await adapter.GetUserTokenAsync(turnContext, "DEF", null, CancellationToken.None); Assert.IsNull(tokenResponse); adapter.AddUserToken("ABC", channelId, userId, token); adapter.AddUserToken("DEF", channelId, userId, token); var oAuthAppCredentials = MicrosoftAppCredentials.Empty; tokenResponse = await adapter.GetUserTokenAsync(turnContext, oAuthAppCredentials, "ABC", null, CancellationToken.None); Assert.IsNotNull(tokenResponse); Assert.AreEqual(token, tokenResponse.Token); Assert.AreEqual("ABC", tokenResponse.ConnectionName); tokenResponse = await adapter.GetUserTokenAsync(turnContext, oAuthAppCredentials, "DEF", null, CancellationToken.None); Assert.IsNotNull(tokenResponse); Assert.AreEqual(token, tokenResponse.Token); Assert.AreEqual("DEF", tokenResponse.ConnectionName); await adapter.SignOutUserAsync(turnContext, connectionName : null, userId); tokenResponse = await adapter.GetUserTokenAsync(turnContext, oAuthAppCredentials, "ABC", null, CancellationToken.None); Assert.IsNull(tokenResponse); tokenResponse = await adapter.GetUserTokenAsync(turnContext, oAuthAppCredentials, "DEF", null, CancellationToken.None); Assert.IsNull(tokenResponse); }