-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathProactiveRefreshTests.cs
More file actions
115 lines (96 loc) · 4.77 KB
/
ProactiveRefreshTests.cs
File metadata and controls
115 lines (96 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Platforms.Features.OpenTelemetry;
using Microsoft.Identity.Test.Common;
using Microsoft.Identity.Test.Common.Core.Helpers;
using Microsoft.Identity.Test.Integration.Infrastructure;
using Microsoft.Identity.Test.Integration.NetFx.Infrastructure;
using Microsoft.Identity.Test.LabInfrastructure;
using Microsoft.Identity.Test.Unit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTelemetry;
using OpenTelemetry.Metrics;
namespace Microsoft.Identity.Test.Integration.HeadlessTests
{
[TestClass]
public class ProactiveRefreshTests
{
[TestMethod]
public async Task ProactiveRefreshTriggers_WithTelemetry_Test()
{
Trace.WriteLine("Add exporter to test the metrics for proactive token refresh");
List<Metric> exportedMetrics = new();
var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(OtelInstrumentation.MeterName)
.AddInMemoryExporter(exportedMetrics)
.Build();
var appConfig = await LabResponseHelper.GetAppConfigAsync(KeyVaultSecrets.AppS2S).ConfigureAwait(false);
var cert = CertificateHelper.FindCertificateByName(TestConstants.AutomationTestCertName);
string[] appScopes = new[] { "https://vault.azure.net/.default" };
AuthenticationResult authResult;
Trace.WriteLine("Create a confidential client application with certificate.");
ConfidentialClientApplication confidentialApp = ConfidentialClientApplicationBuilder
.Create(appConfig.AppId)
.WithAuthority(appConfig.Authority, true)
.WithCertificate(cert)
.BuildConcrete();
Trace.WriteLine("Acquire a token from IDP.");
authResult = await confidentialApp
.AcquireTokenForClient(appScopes)
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);
Assert.IsNotNull(authResult);
Assert.IsNotNull(authResult.AccessToken);
Assert.AreEqual(TokenSource.IdentityProvider, authResult.AuthenticationResultMetadata.TokenSource);
Trace.WriteLine("Acquire a token from cache.");
authResult = await confidentialApp
.AcquireTokenForClient(appScopes)
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);
Assert.IsNotNull(authResult);
Assert.IsNotNull(authResult.AccessToken);
Assert.AreEqual(TokenSource.Cache, authResult.AuthenticationResultMetadata.TokenSource);
Assert.AreEqual(CacheRefreshReason.NotApplicable, authResult.AuthenticationResultMetadata.CacheRefreshReason);
Assert.IsLessThan(100, authResult.AuthenticationResultMetadata.DurationTotalInMs);
TestCommon.UpdateATWithRefreshOn(confidentialApp.AppTokenCacheInternal.Accessor);
Trace.WriteLine("Acquire a token from cache with proactive refresh.");
authResult = await confidentialApp
.AcquireTokenForClient(appScopes)
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);
Assert.IsNotNull(authResult);
Assert.IsNotNull(authResult.AccessToken);
Assert.AreEqual(TokenSource.Cache, authResult.AuthenticationResultMetadata.TokenSource);
Assert.AreEqual(CacheRefreshReason.ProactivelyRefreshed, authResult.AuthenticationResultMetadata.CacheRefreshReason);
Assert.IsLessThan(100, authResult.AuthenticationResultMetadata.DurationTotalInMs);
meterProvider.ForceFlush();
TestCommon.YieldTillSatisfied(()=>ValidateSuccessMetrics(meterProvider, exportedMetrics) == 4); // Wait for the background process to complete
exportedMetrics.Clear();
meterProvider.Dispose();
}
private long ValidateSuccessMetrics(MeterProvider meterProvider, List<Metric> exportedMetrics)
{
Assert.HasCount(6, exportedMetrics);
foreach (var metric in exportedMetrics)
{
if (metric.Name == "MsalSuccess")
{
long successfulRequests = 0;
foreach (var metricPoint in metric.GetMetricPoints())
{
successfulRequests += metricPoint.GetSumLong();
}
return successfulRequests;
}
}
return 0;
}
}
}