Skip to content

Commit

Permalink
Merge branch 'main' into cg/ut-6
Browse files Browse the repository at this point in the history
  • Loading branch information
corinagum authored Feb 21, 2024
2 parents ca7aa0d + f10afdf commit 37b3201
Show file tree
Hide file tree
Showing 62 changed files with 597 additions and 142 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup NodeJS ${{ env.node-version }}
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ env.node-version }}
- name: Install Dependencies
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
parallel: true
files: ${{ env.SOLUTION_DIR }}TestResults/coverage/lcov.info
- name: Upload Tests
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: testresults-dotnet-${{ env.dotnet-version }}
path: ${{ env.SOLUTION_DIR }}TestResults
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-build-test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
reporttypes: 'HtmlInline;lcov'
toolpath: ${{ env.SOLUTION_DIR }}report-generator-tool
- name: Upload Tests
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: testresults-dotnet-${{ matrix.dotnet-version }}
path: ${{ env.SOLUTION_DIR }}TestResults
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Publish
run: dotnet nuget push *.nupkg --api-key ${{ secrets.GITHUB_TOKEN }} --source https://nuget.pkg.github.com/microsoft/index.json --skip-duplicate --no-symbols
- name: Upload
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: nupkg-dotnet-${{ matrix.dotnet-version }}
path: dotnet/packages/Microsoft.TeamsAI/*.nupkg
2 changes: 1 addition & 1 deletion .github/workflows/js-build-test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup NodeJS ${{ matrix.node-version }}
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ matrix.node-version }}
- name: Install Dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scorecards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
# format to the repository Actions tab.
- name: "Upload artifact"
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: SARIF file
path: results.sarif
Expand Down
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

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (6.0)

Type 'TestOAuthBotAuthentication' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check warning on line 12 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/Application/Authentication/Bot/OAuthBotAuthenticationTests.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (7.0)

Type 'TestOAuthBotAuthentication' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
{
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
Expand Up @@ -40,6 +40,11 @@ public override Task<TokenResponse> HandleSsoTokenExchange(ITurnContext context)
}
return Task.FromResult(_tokenExchangeResponse);
}

public override bool IsSsoSignIn(ITurnContext context)
{
return context.Activity.Name == MessageExtensionsInvokeNames.QUERY_INVOKE_NAME;
}
}

internal sealed class TokenExchangeRequest
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.12" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.21.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.6.6" />
Expand Down
Loading

0 comments on commit 37b3201

Please sign in to comment.