Skip to content

Commit

Permalink
Move the api for ITelemetryClient to Confidential Client app (#3656)
Browse files Browse the repository at this point in the history
Move the api for ITelemetryClient to Confidential Client app and update the tests
  • Loading branch information
neha-bhargava authored Sep 8, 2022
1 parent fe1ba8c commit 86ffe42
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -542,38 +542,6 @@ public T WithTelemetry(ITelemetryConfig telemetryConfig)
return (T)this;
}

/// <summary>
/// Sets telemetry client for the application.
/// </summary>
/// <param name="telemetryClients">List of telemetry clients to add telemetry logs.</param>
/// <returns>The builder to chain the .With methods</returns>
public T WithTelemetryClient(params ITelemetryClient[] telemetryClients)
{
ValidateUseOfExperimentalFeature("ITelemetryClient");

if (telemetryClients == null)
{
throw new ArgumentNullException(nameof(telemetryClients));
}

if (telemetryClients.Length > 0)
{
foreach (var telemetryClient in telemetryClients)
{
if (telemetryClient == null)
{
throw new ArgumentNullException(nameof(telemetryClient));
}

telemetryClient.Initialize();
}

Config.TelemetryClients = telemetryClients;
}

return (T)this;
}

internal virtual void Validate()
{
if (string.IsNullOrWhiteSpace(Config.ClientId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Identity.Client.Extensibility;
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.Internal.ClientCredential;
using Microsoft.IdentityModel.Abstractions;

namespace Microsoft.Identity.Client
{
Expand Down Expand Up @@ -313,6 +314,38 @@ public ConfidentialClientApplicationBuilder WithCacheSynchronization(bool enable
return this;
}

/// <summary>
/// Sets telemetry client for the application.
/// </summary>
/// <param name="telemetryClients">List of telemetry clients to add telemetry logs.</param>
/// <returns>The builder to chain the .With methods</returns>
public ConfidentialClientApplicationBuilder WithTelemetryClient(params ITelemetryClient[] telemetryClients)
{
ValidateUseOfExperimentalFeature("ITelemetryClient");

if (telemetryClients == null)
{
throw new ArgumentNullException(nameof(telemetryClients));
}

if (telemetryClients.Length > 0)
{
foreach (var telemetryClient in telemetryClients)
{
if (telemetryClient == null)
{
throw new ArgumentNullException(nameof(telemetryClient));
}

telemetryClient.Initialize();
}

Config.TelemetryClients = telemetryClients;
}

return this;
}

internal ConfidentialClientApplicationBuilder WithAppTokenCacheInternalForTest(ITokenCacheInternal tokenCacheInternal)
{
Config.AppTokenCacheInternalForTest = tokenCacheInternal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.TelemetryCore;
Expand All @@ -20,7 +21,7 @@ namespace Microsoft.Identity.Test.Unit.PublicApiTests
public class TelemetryClientTests : TestBase
{
private MockHttpAndServiceBundle _harness;
private PublicClientApplication _pca;
private ConfidentialClientApplication _cca;
private TelemetryClient _telemetryClient;

[TestInitialize]
Expand Down Expand Up @@ -95,23 +96,23 @@ public async Task AcquireTokenSuccessfulTelemetryTestAsync()
_harness.HttpManager.AddInstanceDiscoveryMockHandler();

CreateApplication();
_pca.ServiceBundle.ConfigureMockWebUI();
_harness.HttpManager.AddSuccessTokenResponseMockHandlerForPost();
_harness.HttpManager.AddMockHandlerSuccessfulClientCredentialTokenResponseMessage();

// Acquire token interactively
var result = await _pca
.AcquireTokenInteractive(TestConstants.s_scope)
.ExecuteAsync()
.ConfigureAwait(false);
var result = await _cca.AcquireTokenForClient(TestConstants.s_scope)
.WithAuthority(TestConstants.AuthorityUtidTenant)
.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);

Assert.IsNotNull(result);

MsalTelemetryEventDetails eventDetails = _telemetryClient.TestTelemetryEventDetails;
AssertLoggedTelemetry(result, eventDetails, TokenSource.IdentityProvider, CacheRefreshReason.NotApplicable);
AssertLoggedTelemetry(result, eventDetails, TokenSource.IdentityProvider, CacheRefreshReason.NoCachedAccessToken);

// Acquire token silently
var account = (await _pca.GetAccountsAsync().ConfigureAwait(false)).Single();
result = await _pca.AcquireTokenSilent(TestConstants.s_scope, account).ExecuteAsync().ConfigureAwait(false);
var account = (await _cca.GetAccountsAsync().ConfigureAwait(false)).Single();
result = await _cca.AcquireTokenSilent(TestConstants.s_scope, account)
.WithAuthority(TestConstants.AuthorityUtidTenant)
.ExecuteAsync().ConfigureAwait(false);
Assert.IsNotNull(result);

eventDetails = _telemetryClient.TestTelemetryEventDetails;
Expand All @@ -132,14 +133,15 @@ private void AssertLoggedTelemetry(AuthenticationResult authenticationResult, Ms

private void CreateApplication()
{
_pca = PublicClientApplicationBuilder.Create(TestConstants.ClientId)
.WithHttpManager(_harness.HttpManager)
.WithDefaultRedirectUri()
.WithExperimentalFeatures()
.WithTelemetryClient(_telemetryClient)
.BuildConcrete();

TokenCacheHelper.PopulateCache(_pca.UserTokenCacheInternal.Accessor);
_cca = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithClientSecret(TestConstants.ClientSecret)
.WithHttpManager(_harness.HttpManager)
.WithExperimentalFeatures()
.WithTelemetryClient(_telemetryClient)
.BuildConcrete();

TokenCacheHelper.PopulateCache(_cca.UserTokenCacheInternal.Accessor);
}
}

Expand Down

0 comments on commit 86ffe42

Please sign in to comment.