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

Migrate unit tests for Protocols.Connector project #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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 @@ -12,6 +12,9 @@
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Agents.Protocols.Serializer;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Agents.Connector.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]

namespace Microsoft.Agents.Protocols.Connector
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core.Pipeline;
using Azure;
using Microsoft.Agents.Protocols.Primitives;
using System;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Agents.Protocols.Connector.Tests
sw-joelmut marked this conversation as resolved.
Show resolved Hide resolved
{
public class BotSignInRestClientTests
{
private static readonly Uri Endpoint = new("http://localhost");
private const string State = "state";
private const string CodeCallenge = "code-challenge";
private const string EmulatorUrl = "emulator-url";
private const string FinalRedirect = "final-redirect";

[Fact]
public void Constructor_ShouldInstantiateCorrectly()
{
var client = UseClient();
Assert.NotNull(client);
}

[Fact]
public void Constructor_ShouldThrowOnNullEndpoint()
{
var pipeline = CreateHttpPipeline();
Assert.Throws<UriFormatException>(() => new BotSignInRestClient(pipeline, null));
}

[Fact]
public void Constructor_ShouldThrowOnNullHttpPipeline()
{
Assert.Throws<ArgumentNullException>(() => new BotSignInRestClient(null, Endpoint));
}


[Fact]
public async Task GetSignInUrlAsync_ShouldThrowOnNullState()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSignInUrlAsync(null));
}

[Fact]
public async Task GetSignInUrlAsync_ShouldThrowWithoutLocalBot()
{
var client = UseClient();
await Assert.ThrowsAsync<RequestFailedException>(() => client.GetSignInUrlAsync(State, CodeCallenge, EmulatorUrl, FinalRedirect));
}

[Fact]
public async Task GetSignInResourceAsync_ShouldThrowOnNullState()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSignInResourceAsync(null, null));
}

[Fact]
public async Task GetSignInResourceAsync_ShouldThrowWithoutLocalBot()
{
var client = UseClient();
await Assert.ThrowsAsync<RequestFailedException>(() => client.GetSignInResourceAsync(State, CodeCallenge, EmulatorUrl, FinalRedirect));
}

private static HttpPipeline CreateHttpPipeline(int maxRetries = 0)
{
var options = new ConnectorClientOptions();
options.Retry.MaxRetries = maxRetries;
var pipeline = HttpPipelineBuilder.Build(options, new DefaultHeadersPolicy(options));
return pipeline;
}

private static BotSignInRestClient UseClient()
{
return new BotSignInRestClient(CreateHttpPipeline(), Endpoint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Moq" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Agents.Protocols.Primitives;
using Xunit;
using Moq;
using Microsoft.Agents.Authentication;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;

namespace Microsoft.Agents.Protocols.Connector.Tests
{
public class RestUserTokenClientTests
{
private const string AppId = "test-app-id";
private const string Audience = "audience";
private const string UserId = "user-id";
private const string ConnectionName = "connection-name";
private const string ChannelId = "channel-id";
private const string MagicCode = "magic-code";
private const string FinalRedirect = "final-redirect";
private const string IncludeFilter = "include-filter";
private static readonly Uri OauthEndpoint = new("https://test.endpoint");
private static readonly List<string> Scopes = [];
private readonly string[] ResourceUrls = ["https://test.url"];
private static readonly Mock<IAccessTokenProvider> AccessTokenMock = new();
private readonly TokenExchangeRequest TokenExchangeRequest = new();

[Fact]
public void Constructor_ShouldInstantiateCorrectly()
{
var client = UseClient();
Assert.NotNull(client);
}

[Fact]
public void Constructor_ShouldThrowOnNullAppId()
{
Assert.Throws<ArgumentNullException>(() => new RestUserTokenClient(null, OauthEndpoint, AccessTokenMock.Object, Audience, Scopes, null));
}

[Fact]
public async Task GetUserTokenAsync_ShouldThrowOnDisposed()
{
var client = UseClient();
client.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => client.GetUserTokenAsync(UserId, ConnectionName, ChannelId, MagicCode, CancellationToken.None));
}

[Fact]
public async Task GetUserTokenAsync_ShouldThrowOnNullUserId()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetUserTokenAsync(null, ConnectionName, ChannelId, MagicCode, CancellationToken.None));
}

[Fact]
public async Task GetUserTokenAsync_ShouldThrowOnNullConnectionName()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetUserTokenAsync(UserId, null, ChannelId, MagicCode, CancellationToken.None));
}

[Fact]
public async Task GetSignInResourceAsync_ShouldThrowOnDisposed()
{
var client = UseClient();
client.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => client.GetSignInResourceAsync(ConnectionName, new Activity(), FinalRedirect, CancellationToken.None));
}

[Fact]
public async Task GetSignInResourceAsync_ShouldThrowOnNullConnectionName()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSignInResourceAsync(null, new Activity(), FinalRedirect, CancellationToken.None));
}

[Fact]
public async Task GetSignInResourceAsync_ShouldThrowOnNullActivity()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetSignInResourceAsync(ConnectionName, null, FinalRedirect, CancellationToken.None));
}

[Fact]
public async Task SignOutUserAsync_ShouldThrowOnDisposed()
{
var client = UseClient();
client.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => client.SignOutUserAsync(UserId, ConnectionName, ChannelId, CancellationToken.None));
}

[Fact]
public async Task SignOutUserAsync_ShouldThrowOnNullUserId()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.SignOutUserAsync(null, ConnectionName, ChannelId, CancellationToken.None));
}

[Fact]
public async Task SignOutUserAsync_ShouldThrowOnNullConnectionName()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.SignOutUserAsync(UserId, null, ChannelId, CancellationToken.None));
}

[Fact]
public async Task GetTokenStatusAsync_ShouldThrowOnDisposed()
{
var client = UseClient();
client.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => client.GetTokenStatusAsync(UserId, ConnectionName, ChannelId, CancellationToken.None));
}

[Fact]
public async Task GetTokenStatusAsync_ShouldThrowOnNullUserId()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetTokenStatusAsync(null, ChannelId, IncludeFilter, CancellationToken.None));
}

[Fact]
public async Task GetTokenStatusAsync_ShouldThrowOnNullChannelId()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetTokenStatusAsync(UserId, null, IncludeFilter, CancellationToken.None));
}

[Fact]
public async Task GetAadTokensAsync_ShouldThrowOnDisposed()
{
var client = UseClient();
client.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => client.GetAadTokensAsync(UserId, ConnectionName, ResourceUrls, ChannelId, CancellationToken.None));
}

[Fact]
public async Task GetAadTokensAsync_ShouldThrowOnNullUserId()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAadTokensAsync(null, ChannelId, ResourceUrls, ChannelId, CancellationToken.None));
sw-joelmut marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public async Task GetAadTokensAsync_ShouldThrowOnNullConnectionName()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAadTokensAsync(UserId, null, ResourceUrls, ChannelId, CancellationToken.None));
}

[Fact]
public async Task ExchangeTokenAsync_ShouldThrowOnDisposed()
{
var client = UseClient();
client.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => client.ExchangeTokenAsync(UserId, ConnectionName, ChannelId, TokenExchangeRequest, CancellationToken.None));
}

[Fact]
public async Task ExchangeTokenAsync_ShouldThrowOnNullUserId()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ExchangeTokenAsync(null, ConnectionName, ChannelId, TokenExchangeRequest, CancellationToken.None));
}

[Fact]
public async Task ExchangeTokenAsync_ShouldThrowOnNullConnectionName()
{
var client = UseClient();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ExchangeTokenAsync(UserId, null, ChannelId, TokenExchangeRequest, CancellationToken.None));
}

[Fact]
public void Constructor_ShouldDisposeTwiceCorrectly()
{
var client = UseClient();
client.Dispose();
client.Dispose();
}

private static RestUserTokenClient UseClient()
{
return new RestUserTokenClient(AppId, OauthEndpoint, AccessTokenMock.Object, Audience, Scopes, null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Agents.Protocols.Primitives;
using Xunit;

namespace Microsoft.Agents.Protocols.Connector.Tests
{
public class RetryParamTests
{
[Fact]
public void Constructor_ShouldStopRetrying()
{
var retryParams = RetryParams.StopRetrying;
Assert.False(retryParams.ShouldRetry);
}

[Fact]
public void Constructor_ShouldRetryByDefault()
{
var retryParams = RetryParams.DefaultBackOff(0);
Assert.True(retryParams.ShouldRetry);
Assert.Equal(TimeSpan.FromMilliseconds(50), retryParams.RetryAfter);
}

[Fact]
public void Constructor_ShouldEnforceOnMaxRetries()
{
var retryParams = RetryParams.DefaultBackOff(10);
sw-joelmut marked this conversation as resolved.
Show resolved Hide resolved
Assert.False(retryParams.ShouldRetry);
}

[Fact]
public void Constructor_ShouldEnforceOnMaxDelay()
{
var retryParams = new RetryParams(TimeSpan.FromSeconds(11), true);
Assert.Equal(TimeSpan.FromSeconds(10), retryParams.RetryAfter);
}
}
}
Loading