From 0d873a662a2e97047f797e8de4bb0f754957b3b6 Mon Sep 17 00:00:00 2001 From: Adrian Clark Date: Sat, 20 Apr 2024 18:38:02 +1000 Subject: [PATCH] Use Cookies Even Without "RestoreCookies" Previously the code only checked the cookie container for relevant cookies if the "RestoreCookies" delegate was configured. This means that if the cookie container was populated some other way then the login process would be called again. Move the check for cookies which might be existing, valid authentication outside the check for "RestoreCookies" delegate to take advantage of any data the collection already contains. --- .../CookiePersistenceTests.cs | 31 +++++++++++++++++++ src/Aydsko.iRacingData/DataClient.cs | 14 ++++----- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/Aydsko.iRacingData.UnitTests/CookiePersistenceTests.cs b/src/Aydsko.iRacingData.UnitTests/CookiePersistenceTests.cs index 9cb0dc8..cb4eabf 100644 --- a/src/Aydsko.iRacingData.UnitTests/CookiePersistenceTests.cs +++ b/src/Aydsko.iRacingData.UnitTests/CookiePersistenceTests.cs @@ -97,4 +97,35 @@ public async Task GivenOptionsWithARestoreFuncTheFuncIsCalledToGetTheCookiesAsyn Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("irsso_members")); Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("authtoken_members")); } + + [Test] + public async Task GivenACookieContainerWithCookiesAndNoRestoreOrSaveFunctionsThenTheCookiesAreUsed() + { + CookieContainer.Add(new Cookie("irsso_members", "one", "/", "members-ng.iracing.com")); + CookieContainer.Add(new Cookie("authtoken_members", "two", "/", "members-ng.iracing.com")); + + var options = new iRacingDataClientOptions + { + Username = "test.user@example.com", + Password = "SuperSecretPassword", + RestoreCookies = null, + SaveCookies = null, + }; + + using var sut = new DataClient(HttpClient, + new TestLogger(), + options, + CookieContainer); + + await MessageHandler.QueueResponsesAsync(nameof(CapturedResponseValidationTests.GetLookupsSuccessfulAsync), false).ConfigureAwait(false); + await sut.GetLookupsAsync(CancellationToken.None).ConfigureAwait(false); + + Assert.That(sut.IsLoggedIn, Is.True); + + var cookies = CookieContainer.GetAllCookies(); + + Assert.That(cookies, Has.Count.EqualTo(2)); + Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("irsso_members")); + Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("authtoken_members")); + } } diff --git a/src/Aydsko.iRacingData/DataClient.cs b/src/Aydsko.iRacingData/DataClient.cs index 09db4d3..82158ec 100644 --- a/src/Aydsko.iRacingData/DataClient.cs +++ b/src/Aydsko.iRacingData/DataClient.cs @@ -1869,14 +1869,14 @@ private async Task LoginInternalAsync(CancellationToken cancellationToken) && options.RestoreCookies() is CookieCollection savedCookies) { cookieContainer.Add(savedCookies); + } - // Assume we're logged in if we have cookies for our target domain - if (cookieContainer.GetCookies(new Uri("https://members-ng.iracing.com")).Count > 0) - { - IsLoggedIn = true; - logger.LoginCookiesRestored(options.Username!); - return; - } + // Assume we're logged in if we have cookies for our target domain + if (cookieContainer.GetCookies(new Uri("https://members-ng.iracing.com")).Count > 0) + { + IsLoggedIn = true; + logger.LoginCookiesRestored(options.Username!); + return; } string? encodedHash = null;