-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AAD: with ServerTelemetryChannel (#2292)
* AAD: with ServerTelemetryChannel
- Loading branch information
1 parent
d0ce00d
commit 9d7e28a
Showing
13 changed files
with
211 additions
and
47 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
BASE/Test/ServerTelemetryChannel.Test/TelemetryChannel.Tests/Helpers/MockCredential.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,27 @@ | ||
#if NET461 || NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 | ||
namespace Microsoft.ApplicationInsights.TestFramework.Helpers | ||
{ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Azure.Core; | ||
|
||
|
||
/// <remarks> | ||
/// Copied from (https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core.TestFramework/src/MockCredential.cs). | ||
/// </remarks> | ||
public class MockCredential : TokenCredential | ||
{ | ||
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
return new ValueTask<AccessToken>(GetToken(requestContext, cancellationToken)); | ||
} | ||
|
||
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
return new AccessToken("TEST TOKEN " + string.Join(" ", requestContext.Scopes), DateTimeOffset.MaxValue); | ||
} | ||
} | ||
} | ||
#endif |
68 changes: 68 additions & 0 deletions
68
...st/TelemetryChannel.Tests/Implementation/ServerTelemetryChannelCredentialEnvelopeTests.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,68 @@ | ||
#if !NET452 && !NET46 | ||
namespace Microsoft.ApplicationInsights.TestFramework.Implementation | ||
{ | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.ApplicationInsights.TestFramework.Helpers; | ||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
/// <summary> | ||
/// These tests verify that <see cref="TelemetryConfiguration"/> can receive and store an instance of <see cref="Azure.Core.TokenCredential"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// These tests do not run in NET452 OR NET46. | ||
/// In these cases, the test runner is NET452 or NET46 and Azure.Core.TokenCredential is NOT SUPPORTED in these frameworks. | ||
/// This does not affect the end user because we REQUIRE the end user to create their own instance of TokenCredential. | ||
/// This ensures that the end user is consuming the AI SDK in one of the newer frameworks. | ||
/// </remarks> | ||
[TestClass] | ||
[TestCategory("AAD")] | ||
public class ServerTelemetryChannelCredentialEnvelopeTests | ||
{ | ||
[TestMethod] | ||
public void VerifySetCredential_CorrectlySetsTelemetryChannel_CredentialFirst() | ||
{ | ||
// SETUP | ||
var tc = TelemetryConfiguration.CreateDefault(); | ||
Assert.IsInstanceOfType(tc.TelemetryChannel, typeof(InMemoryChannel)); | ||
Assert.IsTrue(tc.TelemetryChannel.EndpointAddress.Contains("v2")); // defaults to old api | ||
|
||
// ACT | ||
// set credential first | ||
tc.SetAzureTokenCredential(new MockCredential()); | ||
Assert.IsTrue(tc.TelemetryChannel.EndpointAddress.Contains("v2.1")); // api switch | ||
|
||
// test new channel | ||
var channel = new ServerTelemetryChannel(); | ||
Assert.IsNull(channel.EndpointAddress); // new channel defaults null | ||
|
||
// change config channel | ||
tc.TelemetryChannel = channel; | ||
Assert.IsTrue(channel.EndpointAddress.Contains("v2.1")); // configuration sets new api | ||
} | ||
|
||
[TestMethod] | ||
public void VerifySetCredential_CorrectlySetsTelemetryChannel_TelemetryChannelFirst() | ||
{ | ||
// SETUP | ||
var tc = TelemetryConfiguration.CreateDefault(); | ||
Assert.IsInstanceOfType(tc.TelemetryChannel, typeof(InMemoryChannel)); | ||
Assert.IsTrue(tc.TelemetryChannel.EndpointAddress.Contains("v2")); // defaults to old api | ||
|
||
// ACT | ||
// set new channel first | ||
var channel = new ServerTelemetryChannel(); | ||
Assert.IsNull(channel.EndpointAddress); // new channel defaults null | ||
|
||
// change config channel | ||
tc.TelemetryChannel = channel; | ||
Assert.IsTrue(channel.EndpointAddress.Contains("v2")); // configuration sets new api | ||
|
||
// set credential second | ||
tc.SetAzureTokenCredential(new MockCredential()); | ||
Assert.IsTrue(tc.TelemetryChannel.EndpointAddress.Contains("v2.1")); // api switch | ||
} | ||
} | ||
} | ||
#endif |
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
13 changes: 13 additions & 0 deletions
13
...icationInsights/Extensibility/Implementation/Authentication/ISupportCredentialEnvelope.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,13 @@ | ||
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Authentication | ||
{ | ||
/// <summary> | ||
/// This interface defines a class that accepts the <see cref="Authentication.CredentialEnvelope"/> as a property. | ||
/// </summary> | ||
internal interface ISupportCredentialEnvelope | ||
{ | ||
/// <summary> | ||
/// Gets or sets the <see cref="Authentication.CredentialEnvelope"/>. | ||
/// </summary> | ||
CredentialEnvelope CredentialEnvelope { get; set; } | ||
} | ||
} |
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
Oops, something went wrong.