-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
62 changed files
with
597 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...sAI/Microsoft.TeamsAI.Tests/Application/Authentication/Bot/OAuthBotAuthenticationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Builder.Integration.AspNet.Core; | ||
using Microsoft.Bot.Connector; | ||
using Microsoft.Bot.Connector.Authentication; | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.Teams.AI.State; | ||
using Microsoft.Teams.AI.Tests.TestUtils; | ||
using Moq; | ||
|
||
namespace Microsoft.Teams.AI.Tests.Application.Authentication.Bot | ||
{ | ||
internal class TestOAuthBotAuthentication : OAuthBotAuthentication<TurnState> | ||
Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/Application/Authentication/Bot/OAuthBotAuthenticationTests.cs
|
||
{ | ||
public TestOAuthBotAuthentication(Application<TurnState> app, OAuthSettings oauthSettings, string settingName, IStorage? storage = null) : base(app, oauthSettings, settingName, storage) | ||
{ | ||
} | ||
|
||
protected override Task<SignInResource> GetSignInResourceAsync(ITurnContext context, string connectionName, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(new SignInResource() | ||
{ | ||
SignInLink = "signInLink", | ||
TokenExchangeResource = new TokenExchangeResource() | ||
{ | ||
Id = "id", | ||
Uri = "uri" | ||
}, | ||
TokenPostResource = new TokenPostResource() | ||
{ | ||
SasUrl = "sasUrl", | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public class OAuthBotAuthenticationTests | ||
{ | ||
[Fact] | ||
public async void Test_CreateOAuthCard_WithSSOEnabled() | ||
{ | ||
// Arrange | ||
IActivity sentActivity; | ||
var testAdapter = new SimpleAdapter((activity) => sentActivity = activity); | ||
var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); | ||
|
||
var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext); | ||
var app = new TestApplication(new() { Adapter = testAdapter }); | ||
var authSettings = new OAuthSettings() { | ||
ConnectionName = "connectionName", | ||
Title = "title", | ||
Text = "text", | ||
EnableSso = true | ||
}; | ||
|
||
var botAuth = new TestOAuthBotAuthentication(app, authSettings, "connectionName"); | ||
|
||
// Act | ||
var result = await botAuth.CreateOAuthCard(turnContext); | ||
|
||
// Assert | ||
var card = result.Content as OAuthCard; | ||
Assert.NotNull(card); | ||
Assert.Equal(card.Text, authSettings.Text); | ||
Assert.Equal(card.ConnectionName, authSettings.ConnectionName); | ||
Assert.Equal(card.Buttons[0].Title, authSettings.Title); | ||
Assert.Equal(card.Buttons[0].Text, authSettings.Text); | ||
Assert.Equal(card.Buttons[0].Type, "signin"); | ||
Assert.Equal(card.Buttons[0].Value, "signInLink"); | ||
Assert.NotNull(card.TokenExchangeResource); | ||
Assert.Equal(card.TokenExchangeResource.Id, "id"); | ||
Assert.Equal(card.TokenExchangeResource.Uri, "uri"); | ||
Assert.NotNull(card.TokenPostResource); | ||
Assert.Equal(card.TokenPostResource.SasUrl, "sasUrl"); | ||
} | ||
|
||
[Fact] | ||
public async void Test_CreateOAuthCard_WithoutSSO() | ||
{ | ||
// Arrange | ||
IActivity sentActivity; | ||
var testAdapter = new SimpleAdapter((activity) => sentActivity = activity); | ||
var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); | ||
|
||
var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext); | ||
var app = new TestApplication(new() { Adapter = testAdapter }); | ||
var authSettings = new OAuthSettings() | ||
{ | ||
ConnectionName = "connectionName", | ||
Title = "title", | ||
Text = "text", | ||
EnableSso = false | ||
}; | ||
|
||
var botAuth = new TestOAuthBotAuthentication(app, authSettings, "connectionName"); | ||
|
||
// Act | ||
var result = await botAuth.CreateOAuthCard(turnContext); | ||
|
||
// Assert | ||
var card = result.Content as OAuthCard; | ||
Assert.NotNull(card); | ||
Assert.Equal(card.Text, authSettings.Text); | ||
Assert.Equal(card.ConnectionName, authSettings.ConnectionName); | ||
Assert.Equal(card.Buttons[0].Title, authSettings.Title); | ||
Assert.Equal(card.Buttons[0].Text, authSettings.Text); | ||
Assert.Equal(card.Buttons[0].Type, "signin"); | ||
Assert.Equal(card.Buttons[0].Value, "signInLink"); | ||
Assert.Null(card.TokenExchangeResource); | ||
Assert.NotNull(card.TokenPostResource); | ||
Assert.Equal(card.TokenPostResource.SasUrl, "sasUrl"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...TeamsAI.Tests/Application/Authentication/MessageExtensions/OAuthMessageExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.Teams.AI.Tests.TestUtils; | ||
|
||
namespace Microsoft.Teams.AI.Tests.Application.Authentication.MessageExtensions | ||
{ | ||
public class OAuthMessageExtensionsTests | ||
{ | ||
[Fact] | ||
public void Test_isSsoSignIn_False() | ||
{ | ||
// Arrange | ||
var turnContext = new TurnContext(new NotImplementedAdapter(), new Activity() | ||
{ | ||
Type = ActivityTypes.Invoke, | ||
Name = "composeExtension/query", | ||
}); | ||
var oauthSettings = new OAuthSettings() { EnableSso = false }; | ||
var messageExtensionsAuth = new OAuthMessageExtensionsAuthentication(oauthSettings); | ||
|
||
// Act | ||
var result = messageExtensionsAuth.IsSsoSignIn(turnContext); | ||
|
||
// Assert | ||
Assert.False(result); | ||
} | ||
|
||
[Fact] | ||
public void Test_isSsoSignIn_True() | ||
{ | ||
// Arrange | ||
var turnContext = new TurnContext(new NotImplementedAdapter(), new Activity() | ||
{ | ||
Type = ActivityTypes.Invoke, | ||
Name = "composeExtension/query", | ||
}); | ||
var oauthSettings = new OAuthSettings() { EnableSso = true }; | ||
var messageExtensionsAuth = new OAuthMessageExtensionsAuthentication(oauthSettings); | ||
|
||
// Act | ||
var result = messageExtensionsAuth.IsSsoSignIn(turnContext); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...ft.TeamsAI/Microsoft.TeamsAI.Tests/Application/Authentication/OAuthAuthenticationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.Teams.AI.State; | ||
using Microsoft.Teams.AI.Tests.TestUtils; | ||
|
||
namespace Microsoft.Teams.AI.Tests.Application.Authentication | ||
{ | ||
public class OAuthAuthenticationTests | ||
{ | ||
[Fact] | ||
public async void Test_IsUserSignedIn_ReturnsTokenString() | ||
{ | ||
// Arrange | ||
var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); | ||
var oauthSettings = new OAuthSettings() { ConnectionName = "connectionName" }; | ||
var app = new TestApplication(new() | ||
{ | ||
Adapter = new SimpleAdapter() | ||
}); | ||
var tokenResponse = new TokenResponse() | ||
{ | ||
Token = "validToken", | ||
Expiration = "validExpiration", | ||
ConnectionName = "connectionName", | ||
}; | ||
var auth = new TestOAuthAuthentication(tokenResponse, app, "name", oauthSettings, null); | ||
|
||
|
||
// Act | ||
var result = await auth.IsUserSignedInAsync(turnContext); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.True(result == "validToken"); | ||
} | ||
|
||
[Fact] | ||
public async void Test_IsUserSignedIn_ReturnsNull() | ||
{ | ||
// Arrange | ||
var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); | ||
var oauthSettings = new OAuthSettings() { ConnectionName = "connectionName" }; | ||
var app = new TestApplication(new() | ||
{ | ||
Adapter = new SimpleAdapter() | ||
}); | ||
var tokenResponse = new TokenResponse() | ||
{ | ||
Token = "", // Empty token | ||
Expiration = "", | ||
ConnectionName = "connectionName", | ||
}; | ||
var auth = new TestOAuthAuthentication(tokenResponse, app, "name", oauthSettings, null); | ||
|
||
|
||
// Act | ||
var result = await auth.IsUserSignedInAsync(turnContext); | ||
|
||
// Assert | ||
Assert.Null(result); | ||
} | ||
} | ||
|
||
public class TestOAuthAuthentication : OAuthAuthentication<TurnState> | ||
{ | ||
private TokenResponse _tokenResponse; | ||
|
||
internal TestOAuthAuthentication(TokenResponse tokenResponse, Application<TurnState> app, string name, OAuthSettings settings, IStorage? storage) : base(settings, new OAuthMessageExtensionsAuthentication(settings), new OAuthBotAuthentication<TurnState>(app, settings, name, storage)) | ||
{ | ||
_tokenResponse = tokenResponse; | ||
} | ||
|
||
internal TestOAuthAuthentication(TokenResponse tokenResponse, OAuthSettings settings, OAuthMessageExtensionsAuthentication messageExtensionAuth, OAuthBotAuthentication<TurnState> botAuthentication) : base(settings, messageExtensionAuth, botAuthentication) | ||
{ | ||
_tokenResponse = tokenResponse; | ||
} | ||
|
||
protected override Task<TokenResponse> GetUserToken(ITurnContext context, string connectionName, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(_tokenResponse); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.