11using System . Diagnostics ;
2-
3- namespace KoenZomers . Tado . UnitTest ;
2+ namespace KoenZomers . Tado . Api ;
43
54/// <summary>
6- /// Unit Tests to validate authenticating against the Tado API
5+ /// Integration test to validate the authenticating against the Tado API
6+ /// Run all tests in the correct order. Make sure to register the device with the code between while running Test2!!
77/// </summary>
88[ TestClass ]
9- public class AuthenticationTest : BaseTest
9+ public class AuthenticationIntegrationTest : IntegrationTestBase
1010{
11- /// <summary>
12- /// Test being able to retrieve an authorization URL
13- /// </summary>
14- /// <returns></returns>
15- [ TestMethod ]
16- public async Task GetDeviceCodeAuthenticationTest ( )
17- {
18- if ( Service is null ) Assert . Fail ( "Service not available" ) ;
19-
20- var authenticationRequest = await Service . GetDeviceCodeAuthentication ( CancellationToken . None ) ;
21-
22- Assert . IsNotNull ( authenticationRequest , "Failed to instantiate device authentication flow" ) ;
23- }
24-
25- /// <summary>
26- /// Test requesting a device authorization code and waiting for it to be completed
27- /// </summary>
28- /// <returns></returns>
2911 [ TestMethod ]
30- public async Task WaitForDeviceCodeAuthenticationToCompleteTest ( )
12+ public async Task Run ( )
3113 {
32- if ( Service is null ) Assert . Fail ( "Service not available" ) ;
33-
34- var authenticationRequest = await Service . GetDeviceCodeAuthentication ( CancellationToken . None ) ;
35-
36- Assert . IsNotNull ( authenticationRequest , "Failed to instantiate device authentication flow" ) ;
37-
38- Debug . WriteLine ( $ "URL for authentication: { authenticationRequest . VerificationUriComplete } ") ;
39-
40- var tokenResponse = await Service . WaitForDeviceCodeAuthenticationToComplete ( authenticationRequest , CancellationToken . None ) ;
41-
42- Assert . IsNotNull ( tokenResponse , "Failed to complete device authentication flow" ) ;
43-
44- Debug . WriteLine ( $ "Access token: { tokenResponse . AccessToken } ") ;
45- Debug . WriteLine ( $ "Refresh token: { tokenResponse . RefreshToken } ") ;
14+ Assert . IsNotNull ( Service , "Service not available." ) ;
15+ Assert . IsNotNull ( Configuration , "Configuration not available (appsettings.json)." ) ;
16+ Assert . IsNotNull ( Configuration . TadoHomeId , "TadoHomeId must be set in the appsettings.json for this test to work." ) ;
17+
18+ // Step 1: Retrieve a new device code
19+ var deviceAuthorizationResponse = await Service . GetDeviceCodeAuthentication ( TestContext . CancellationToken ) ;
20+ Assert . IsNotNull ( deviceAuthorizationResponse , "Failed to instantiate device authentication flow." ) ;
21+
22+ // Step2: Go to the tado URL and authenticate your device
23+ Debug . WriteLine ( "URL for authentication: {0}" , deviceAuthorizationResponse . VerificationUriComplete ) ;
24+ Console . WriteLine ( "URL for authentication: {0}" , deviceAuthorizationResponse . VerificationUriComplete ) ;
25+ TestContext . WriteLine ( "URL for authentication: {0}" , deviceAuthorizationResponse . VerificationUriComplete ) ;
26+
27+ // Step3: Wait for the user to complete the authentication
28+ var token = await Service . WaitForDeviceCodeAuthenticationToComplete ( deviceAuthorizationResponse , TestContext . CancellationToken ) ;
29+ Assert . IsNotNull ( token , "Failed to complete device authentication flow." ) ;
30+
31+ // Step4: Try to refresh the access token with the refresh token
32+ var newToken = await Service . GetAccessTokenWithRefreshToken ( token . RefreshToken ! , TestContext . CancellationToken ) ;
33+ Assert . IsNotNull ( newToken , "Failed to retrieve new access token with refresh token." ) ;
34+ Assert . AreNotEqual ( token . RefreshToken , newToken . RefreshToken ) ;
35+ Assert . AreNotEqual ( token . AccessToken , newToken . AccessToken ) ;
36+
37+ // Step5: Try to fetch device data with the access token and let the thermostat say 'Hi'.
38+ Service . Authenticate ( newToken ) ;
39+ var devices = await Service . GetDevices ( Configuration ! . TadoHomeId ! . Value ) ;
40+ Assert . IsNotNull ( devices , "Failed to retrieve devices." ) ;
41+ Assert . IsNotEmpty ( devices , "No devices found." ) ;
42+
43+ var thermostatDevices = devices . Where ( x => x . DeviceType ! . StartsWith ( "RU" ) ) . ToArray ( ) ;
44+ var success = thermostatDevices . Length > 0 ;
45+ foreach ( var device in thermostatDevices )
46+ {
47+ success &= await Service . SayHi ( device . SerialNo ! , TestContext . CancellationToken ) ;
48+ }
49+ Assert . IsTrue ( success , "Failed to retrieve information from Tado and say Hi to devices." ) ;
4650 }
4751
48- /// <summary>
49- /// Test requesting an access token with a refresh token
50- /// </summary>
51- /// <returns></returns>
52- [ TestMethod ]
53- public async Task GetAccessTokenWithRefreshTokenTest ( )
54- {
55- if ( Service is null ) Assert . Fail ( "Service not available" ) ;
56-
57- var tokenResponse = await Service . GetAccessTokenWithRefreshToken ( "xxx" , CancellationToken . None ) ;
58-
59- Assert . IsNotNull ( tokenResponse , "Failed to retrieve access token with refresh token" ) ;
60- }
61-
62- /// <summary>
63- /// Test requesting the Me endpoint from Tado
64- /// </summary>
65- /// <returns></returns>
66- [ TestMethod ]
67- public async Task GetMeTest ( )
68- {
69- if ( Service is null ) Assert . Fail ( "Service not available" ) ;
70-
71- var authenticationRequest = await Service . GetDeviceCodeAuthentication ( CancellationToken . None ) ;
72-
73- Assert . IsNotNull ( authenticationRequest , "Failed to instantiate device authentication flow" ) ;
74-
75- Debug . WriteLine ( $ "URL for authentication: { authenticationRequest . VerificationUriComplete } ") ;
76-
77- var tokenResponse = await Service . WaitForDeviceCodeAuthenticationToComplete ( authenticationRequest , CancellationToken . None ) ;
78-
79- Assert . IsNotNull ( tokenResponse , "Failed to complete device authentication flow" ) ;
80-
81- Service . Authenticate ( tokenResponse ) ;
82-
83- var devices = await Service . GetDevices ( Configuration . TadoHomeId . Value ) ;
84-
85- var me = await Service . SayHi ( "xxxx" ) ;
86-
87- Assert . IsNotNull ( me , "Failed to retrieve information from Tado" ) ;
88- }
52+ public TestContext TestContext { get ; set ; }
8953}
0 commit comments