-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
#1236) ## Linked issues closes: #1194 #991 ## Details * Corresponding JS PR: #1232 for implementation details. ## Attestation Checklist - [x] My code follows the style guidelines of this project - I have checked for/fixed spelling, linting, and other errors - I have commented my code for clarity - I have made corresponding changes to the documentation (updating the doc strings in the code is sufficient) - My changes generate no new warnings - I have added tests that validates my changes, and provides sufficient test coverage. I have tested with: - Local testing - E2E testing in Teams - New and existing unit tests pass locally with my changes --------- Co-authored-by: Corina <[email protected]>
- Loading branch information
There are no files selected for viewing
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"); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |