1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . IO ;
7
+ using System . Linq ;
8
+ using System . Runtime . Versioning ;
9
+ using System . Text ;
10
+ using System . Threading . Tasks ;
11
+ using Common ;
12
+ using Microsoft . Identity . Lab . Api ;
13
+ using Microsoft . Playwright ;
14
+ using Xunit ;
15
+ using Xunit . Abstractions ;
16
+ using Process = System . Diagnostics . Process ;
17
+ using TC = Common . TestConstants ;
18
+
19
+
20
+ namespace GraphUserTokenCacheTest
21
+ {
22
+ public class GraphUserTokenCacheTest
23
+ {
24
+ private const uint ClientPort = 44321 ;
25
+ private const uint NumProcessRetries = 3 ;
26
+ private const string SampleSlnFileName = "2-2-TokenCache.sln" ;
27
+ private const string SignOutPageUriPath = @"/MicrosoftIdentity/Account/SignedOut" ;
28
+ private const string SqlDbName = "MY_TOKEN_CACHE_DATABASE" ;
29
+ private const string SqlServerConnectionString = "Server=(localdb)\\ mssqllocaldb;Integrated Security=true" ;
30
+ private const string SqlTableName = "TokenCache" ;
31
+ private const string TraceFileClassName = "GraphUserTokenCacheTest" ;
32
+ private readonly LocatorAssertionsToBeVisibleOptions _assertVisibleOptions = new ( ) { Timeout = 25000 } ;
33
+ private readonly string _sampleAppPath = "2-WebApp-graph-user" + Path . DirectorySeparatorChar + "2-2-TokenCache" + Path . DirectorySeparatorChar . ToString ( ) ;
34
+ private readonly string _testAppsettingsPath = "UiTests" + Path . DirectorySeparatorChar + "GraphUserTokenCache" + Path . DirectorySeparatorChar . ToString ( ) + TC . AppSetttingsDotJson ;
35
+ private readonly string _testAssemblyLocation = typeof ( GraphUserTokenCacheTest ) . Assembly . Location ;
36
+ private readonly ITestOutputHelper _output ;
37
+
38
+ public GraphUserTokenCacheTest ( ITestOutputHelper output )
39
+ {
40
+ _output = output ;
41
+ }
42
+
43
+ [ Fact ]
44
+ public async Task ChallengeUser_MicrosoftIdFlow_LocalApp_ValidEmailPasswordCreds_LoginLogoutAsync ( )
45
+ {
46
+ // Setup web app and api environmental variables.
47
+ var clientEnvVars = new Dictionary < string , string >
48
+ {
49
+ { "ASPNETCORE_ENVIRONMENT" , "Development" } ,
50
+ { TC . KestrelEndpointEnvVar , TC . HttpsStarColon + ClientPort }
51
+ } ;
52
+
53
+ Dictionary < string , Process > ? processes = null ;
54
+
55
+ // Arrange Playwright setup, to see the browser UI set Headless = false.
56
+ const string TraceFileName = TraceFileClassName + "_LoginLogout" ;
57
+ using IPlaywright playwright = await Playwright . CreateAsync ( ) ;
58
+ IBrowser browser = await playwright . Chromium . LaunchAsync ( new ( ) { Headless = true } ) ;
59
+ IBrowserContext context = await browser . NewContextAsync ( new BrowserNewContextOptions { IgnoreHTTPSErrors = true } ) ;
60
+ await context . Tracing . StartAsync ( new ( ) { Screenshots = true , Snapshots = true , Sources = true } ) ;
61
+ IPage page = await context . NewPageAsync ( ) ;
62
+ string uriWithPort = TC . LocalhostUrl + ClientPort ;
63
+
64
+ try
65
+ {
66
+ // Make sure database and table for cache exist, if not they will be created.
67
+ UiTestHelpers . EnsureDatabaseAndTokenCacheTableExist ( SqlServerConnectionString , SqlDbName , SqlTableName , _output ) ;
68
+
69
+ // Build the sample app with correct appsettings file.
70
+ UiTestHelpers . BuildSampleUsingTestAppsettings ( _testAssemblyLocation , _sampleAppPath , _testAppsettingsPath , SampleSlnFileName ) ;
71
+
72
+ // Start the web app and api processes.
73
+ // The delay before starting client prevents transient devbox issue where the client fails to load the first time after rebuilding
74
+ var clientProcessOptions = new ProcessStartOptions ( _testAssemblyLocation , _sampleAppPath , TC . s_oidcWebAppExe , clientEnvVars ) ;
75
+
76
+ bool areProcessesRunning = UiTestHelpers . StartAndVerifyProcessesAreRunning ( [ clientProcessOptions ] , out processes , NumProcessRetries ) ;
77
+
78
+ if ( ! areProcessesRunning )
79
+ {
80
+ _output . WriteLine ( $ "Process not started after { NumProcessRetries } attempts.") ;
81
+ StringBuilder runningProcesses = new ( ) ;
82
+ foreach ( var process in processes )
83
+ {
84
+ #pragma warning disable CA1305 // Specify IFormatProvider
85
+ runningProcesses . AppendLine ( $ "Is { process . Key } running: { UiTestHelpers . ProcessIsAlive ( process . Value ) } ") ;
86
+ #pragma warning restore CA1305 // Specify IFormatProvider
87
+ }
88
+ Assert . Fail ( TC . WebAppCrashedString + " " + runningProcesses . ToString ( ) ) ;
89
+ }
90
+
91
+ LabResponse labResponse = await LabUserHelper . GetSpecificUserAsync ( TC . MsidLab4User ) ;
92
+
93
+ // Initial sign in
94
+ _output . WriteLine ( "Starting web app sign-in flow." ) ;
95
+ string email = labResponse . User . Upn ;
96
+ await UiTestHelpers . NavigateToWebApp ( uriWithPort , page ) ;
97
+ await UiTestHelpers . FirstLogin_MicrosoftIdFlow_ValidEmailPassword ( page , email , labResponse . User . GetOrFetchPassword ( ) ) ;
98
+ await Assertions . Expect ( page . GetByText ( "Integrating Azure AD V2" ) ) . ToBeVisibleAsync ( _assertVisibleOptions ) ;
99
+ await Assertions . Expect ( page . GetByText ( email ) ) . ToBeVisibleAsync ( _assertVisibleOptions ) ;
100
+ _output . WriteLine ( "Web app sign-in flow successful." ) ;
101
+
102
+ // Sign out
103
+ _output . WriteLine ( "Starting web app sign-out flow." ) ;
104
+ await page . GetByRole ( AriaRole . Link , new ( ) { Name = "Sign out" } ) . ClickAsync ( ) ;
105
+ await UiTestHelpers . PerformSignOut_MicrosoftIdFlow ( page , email , TC . LocalhostUrl + ClientPort + SignOutPageUriPath , _output ) ;
106
+ _output . WriteLine ( "Web app sign out successful." ) ;
107
+ }
108
+ catch ( Exception ex )
109
+ {
110
+ // Adding guid in case of multiple test runs. This will allow screenshots to be matched to their appropriate test runs.
111
+ var guid = Guid . NewGuid ( ) . ToString ( ) ;
112
+ try
113
+ {
114
+ if ( page != null )
115
+ {
116
+ await page . ScreenshotAsync ( new PageScreenshotOptions ( ) { Path = $ "ChallengeUser_MicrosoftIdFlow_LocalApp_ValidEmailPasswordCreds_TodoAppFunctionsCorrectlyScreenshotFail{ guid } .png", FullPage = true } ) ;
117
+ }
118
+ }
119
+ catch
120
+ {
121
+ _output . WriteLine ( "No Screenshot." ) ;
122
+ }
123
+
124
+ string runningProcesses = UiTestHelpers . GetRunningProcessAsString ( processes ) ;
125
+ Assert . Fail ( $ "the UI automation failed: { ex } output: { ex . Message } .\n { runningProcesses } \n Test run: { guid } ") ;
126
+ }
127
+ finally
128
+ {
129
+ // Make sure all processes and their children are stopped.
130
+ UiTestHelpers . EndProcesses ( processes ) ;
131
+
132
+ // Stop tracing and export it into a zip archive.
133
+ string path = UiTestHelpers . GetTracePath ( _testAssemblyLocation , TraceFileName ) ;
134
+ await context . Tracing . StopAsync ( new ( ) { Path = path } ) ;
135
+ _output . WriteLine ( $ "Trace data for { TraceFileName } recorded to { path } .") ;
136
+
137
+ // Close the browser and stop Playwright.
138
+ await browser . CloseAsync ( ) ;
139
+ playwright . Dispose ( ) ;
140
+ }
141
+ }
142
+ }
143
+ }
0 commit comments