mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-12-03 10:34:04 +01:00
* feat(domain): add RequiresReRegistration and ThrottledDeviceRecovery policy - Add Account.RequiresReRegistration property and HasObsoleteDeviceRegistration predicate - Update AccountCredentialStatus to treat RequiresReRegistration as missing credentials - Introduce ThrottledDeviceRecovery service to detect throttling on obsolete profiles and mark accounts for re-registration - Add unit tests for Account, AccountCredentialStatus, and ThrottledDeviceRecovery * feat(core): enforce interactive re-registration across API and download queue - Intercept RequiresReRegistration in ApiExtended, invalidate tokens during login, and reset flag upon success - Add LicenseDeniedThrottledRecovered queue status and user message - Update ProcessBookViewModel, ProcessQueueViewModel, and _ProcessableOptionsBase to handle throttled recovery during liberation * feat(ui): add Widevine re-registration prompts and CLI status reporting - Prompt to mark accounts for re-registration when Widevine is enabled in Avalonia and WinForms - Guard settings toggle against passive navigation/loading events and revert cleanly on cancel - Show 're-login needed' in 'list-accounts' and automatically re-register in 'login-external' - Add CLI tests for re-registration detection and flow in LoginExternalOptionsTests * docs: document automatic registration recovery and rate limiting guidance - Clarify distinction between device-registration throttling and volume rate limits - Document automatic settings migration and re-registration flow across GUI and CLI - Advise using Daily Download Limit when bulk re-downloading after scanning for higher quality audio - Remove obsolete manual account deletion and JSON editing instructions * feat(ui): automatically enable Widevine and re-register accounts when upgrading book quality When upgrading audio quality via 'Scan Audible for Higher Quality Audio', marking books for re-download previously left Widevine disabled and accounts under legacy registrations, causing subsequent downloads to re-acquire the existing lower-quality AAC-LC stream. - Update FindBetterQualityBooksViewModel.MarkBooksAsync() to return a MarkBooksResult and automatically enable Widevine and Request xHE-AAC when higher-quality streams are selected. - Thunk accounts to CurrentAndroid and mark them for re-registration via ThrottledDeviceRecovery so that Widevine-protected and higher-quality codecs can be licensed without manual troubleshooting. - Alert user in Avalonia and WinForms dialogs when settings and registrations have been automatically upgraded. - Fix broken sentence and note daily download limits and rate limiting in FindBetterQualityBooksViewModel.InitialMessage. - Add unit tests in FindBetterQualityBooksViewModelTests covering clean and upgraded re-registration paths. * feat(accounts): add 'Signed in?' checkbox to mark accounts for re-registration on save * docs & fix(recovery): modernize license recovery guidance and document 'Signed in?' * feat(recovery): automatically enable Widevine and thunk account when ADRM fails (#2083) - Detects missing ADRM asset (Sable 000307 acr:null) and missing AAX fallback codecs in titles that only deliver via Widevine. - Instead of merely advising the user to change settings manually, automatically applies recovery via ThrottledDeviceRecovery.ApplyRecovery (sets UseWidevine = true, CurrentAndroid, and marks the account for re-registration). - Modernizes user-facing dialogs, process queue status, and CLI feedback to explain that Widevine DRM was enabled and a fresh login is required.
238 lines
8.3 KiB
C#
238 lines
8.3 KiB
C#
using AudibleApi;
|
|
using AudibleApi.Authorization;
|
|
using AudibleApi.Common;
|
|
using AudibleApi.Cryptography;
|
|
using AudibleUtilities;
|
|
using ApplicationServices;
|
|
using DataLayer;
|
|
using Dinah.Core.Security;
|
|
using LibationFileManager;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LibationUiBase.Tests;
|
|
|
|
[TestClass]
|
|
[DoNotParallelize]
|
|
public class FindBetterQualityBooksViewModelTests
|
|
{
|
|
private const string SamplePrivateKey = @"
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
MIIEpgIBAAKCAQEA5nPbGSVDmlEH2tJa6kz/P2HI8IeirhfPHdmi+X/nsb9i3WNf
|
|
tmEdZxfK26IValQDXvBH17a1gr0HD6pYse1XsV2w0HxiW1RW+ZnjL8/fzPdkSOb+
|
|
4xKlqRopCueBSdDGgAF06spZ3IeHLfEFOJX4dO1Y73pFBUkA0k53LT12L2Tjay/r
|
|
buZHJqIzxmwja7/nkiWL0Xo7UySHtQACYsKEatu6yHBS+cPTlGR/qeUpeJTHwDLP
|
|
7ZQ7kWzJGY1mfInYekjlZLsMsWswso3pg1vPyHgxzM2BWhY8m6mlXQ9G/USxBTib
|
|
MNuMtp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Vb0clG8ZDsK7u6s5OF0pO
|
|
sBS+oVl7rF/eSeFcFhUYP26ZhsbWo3z/bERuj926VO2AxDPRTsP5o3pQPGZhY0V9
|
|
irGgbUJrAoGBAOseS3J4BqYM4R3Hr7cRAhvzSjIkeTcDF1zTOa4FZDHBxZ6g2PNq
|
|
8ekhtfn1zPczsPTF1vNuqEISKLxaPkVPiw0mtaZQjVwpF/IOxMNjWVLp6oJf8Mm2
|
|
BxlXqPnQ4mG66oqSFQgDEmFdMhRb2of6xL1gYYL62C80G2T7QtmPfSab
|
|
-----END RSA PRIVATE KEY-----
|
|
";
|
|
|
|
private const string SampleAccessToken = "Atna|_CHAR_ACCESS_";
|
|
private const string SampleRefreshToken = "Atnr|_CHAR_REFRESH_";
|
|
private const string SampleAdpToken = "{enc:abcdefg}{key:1234}{iv:56789}{name:QURQVG9rZW5FbmNyeXB0aW9uS2V5}{serial:Mg==}";
|
|
|
|
private string tempDir = string.Empty;
|
|
|
|
[TestInitialize]
|
|
public void Initialize()
|
|
{
|
|
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
|
|
tempDir = Path.Combine(Path.GetTempPath(), $"libation-vm-test-{Guid.NewGuid():N}");
|
|
Directory.CreateDirectory(tempDir);
|
|
Environment.SetEnvironmentVariable(LibationFiles.LIBATION_FILES_DIR, tempDir);
|
|
Configuration.CreateMockInstance();
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void Cleanup()
|
|
{
|
|
Configuration.RestoreSingletonInstance();
|
|
Environment.SetEnvironmentVariable(LibationFiles.LIBATION_FILES_DIR, null);
|
|
SynchronizationContext.SetSynchronizationContext(null);
|
|
try
|
|
{
|
|
Directory.Delete(tempDir, recursive: true);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
private static Identity CreateIdentity(string deviceType, string? serialNumber)
|
|
{
|
|
var identity = new Identity(Localization.Get("us"));
|
|
identity.Update(
|
|
new PrivateKey(SamplePrivateKey),
|
|
new AdpToken(SampleAdpToken),
|
|
new AccessToken(SampleAccessToken, DateTime.UtcNow.AddHours(1)),
|
|
new RefreshToken(SampleRefreshToken),
|
|
new List<KeyValuePair<string, SecretString>> { new("session-id", new SecretString("cookie-val")) },
|
|
deviceSerialNumber: serialNumber,
|
|
deviceType: deviceType,
|
|
amazonAccountId: "amzn-123",
|
|
deviceName: "dev-name",
|
|
storeAuthenticationCookie: new SecretString("store-cookie"));
|
|
return identity;
|
|
}
|
|
|
|
private static Account CreateAccount(string email, string serialNumber, string? deviceType = null)
|
|
{
|
|
return new Account(email)
|
|
{
|
|
IdentityTokens = CreateIdentity(deviceType ?? AudibleApi.Resources.DeviceType, serialNumber)
|
|
};
|
|
}
|
|
|
|
private static LibraryBook InsertBook(string title, LiberatedStatus bookStatus = LiberatedStatus.Liberated)
|
|
{
|
|
var libraryBook = MockLibraryBook.CreateBook(title: title, bookStatus: bookStatus);
|
|
|
|
using (var context = DbContexts.GetContext())
|
|
{
|
|
context.LibraryBooks.Add(new LibraryBook(libraryBook.Book, libraryBook.DateAdded, libraryBook.Account));
|
|
context.SaveChanges();
|
|
}
|
|
|
|
return DbContexts.GetLibraryBook_Flat_NoTracking(libraryBook.Book.AudibleProductId)!;
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task MarkBooksAsync_NoSignificantBooks_ReturnsZeroAndDoesNotModifyConfig()
|
|
{
|
|
Configuration.Instance.UseWidevine = false;
|
|
Configuration.Instance.Request_xHE_AAC = false;
|
|
|
|
var vm = new FindBetterQualityBooksViewModel
|
|
{
|
|
Books = new List<BookDataViewModel>()
|
|
};
|
|
|
|
var result = await vm.MarkBooksAsync();
|
|
|
|
Assert.AreEqual(0, result.MarkedCount);
|
|
Assert.IsFalse(result.WidevineEnabled);
|
|
Assert.IsFalse(result.XHeAacRequested);
|
|
Assert.IsFalse(result.AccountsMarkedForReRegistration);
|
|
Assert.IsNull(result.GetSummaryMessage());
|
|
|
|
Assert.IsFalse(Configuration.Instance.UseWidevine);
|
|
Assert.IsFalse(Configuration.Instance.Request_xHE_AAC);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task MarkBooksAsync_SignificantWidevineBooks_EnablesWidevineAndMarksAccounts()
|
|
{
|
|
Configuration.Instance.UseWidevine = false;
|
|
Configuration.Instance.Request_xHE_AAC = false;
|
|
AudibleApiStorage.EnsureAccountsSettingsFileExists();
|
|
|
|
var account = CreateAccount("test@example.com", "0123456789abcdef0123");
|
|
using (var persister = AudibleApiStorage.GetAccountsSettingsPersister())
|
|
{
|
|
persister.AccountsSettings.Add(account);
|
|
}
|
|
|
|
var book = InsertBook("High Quality Book");
|
|
var bookVm = new BookDataViewModel(book)
|
|
{
|
|
Bitrate = 64,
|
|
AvailableBitrate = 128,
|
|
AvailableCodec = "xHE-AAC"
|
|
};
|
|
Assert.IsTrue(bookVm.IsSignificant);
|
|
|
|
var vm = new FindBetterQualityBooksViewModel
|
|
{
|
|
Books = new List<BookDataViewModel> { bookVm },
|
|
ScanWidevine = true
|
|
};
|
|
|
|
var result = await vm.MarkBooksAsync();
|
|
|
|
Assert.AreEqual(1, result.MarkedCount);
|
|
Assert.IsTrue(result.WidevineEnabled);
|
|
Assert.IsTrue(result.XHeAacRequested);
|
|
Assert.IsTrue(result.AccountsMarkedForReRegistration);
|
|
Assert.IsNotNull(result.GetSummaryMessage());
|
|
StringAssert.Contains(result.GetSummaryMessage()!, "Marked 1 book as Download Pending.");
|
|
StringAssert.Contains(result.GetSummaryMessage()!, "Libation has automatically configured your download settings");
|
|
|
|
Assert.IsTrue(Configuration.Instance.UseWidevine);
|
|
Assert.IsTrue(Configuration.Instance.Request_xHE_AAC);
|
|
Assert.AreEqual(DeviceRegistrationKind.CurrentAndroid, Configuration.Instance.DeviceRegistrationKind);
|
|
Assert.AreEqual(0, vm.Books.Count);
|
|
|
|
using (var persister = AudibleApiStorage.GetAccountsSettingsPersister())
|
|
{
|
|
var reloaded = persister.AccountsSettings.Accounts[0];
|
|
Assert.IsTrue(reloaded.RequiresReRegistration);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task MarkBooksAsync_AlreadyWidevine_OnlyMarksObsoleteAccounts()
|
|
{
|
|
Configuration.Instance.UseWidevine = true;
|
|
Configuration.Instance.Request_xHE_AAC = true;
|
|
AudibleApiStorage.EnsureAccountsSettingsFileExists();
|
|
|
|
var obsoleteAccount = CreateAccount("legacy@example.com", "0123456789abcdef"); // 16 hex: obsolete
|
|
var modernAccount = CreateAccount("modern@example.com", "0123456789abcdef0123"); // 20 hex: modern
|
|
using (var persister = AudibleApiStorage.GetAccountsSettingsPersister())
|
|
{
|
|
persister.AccountsSettings.Add(obsoleteAccount);
|
|
persister.AccountsSettings.Add(modernAccount);
|
|
}
|
|
|
|
var book = InsertBook("Another High Quality Book");
|
|
var bookVm = new BookDataViewModel(book)
|
|
{
|
|
Bitrate = 64,
|
|
AvailableBitrate = 128,
|
|
AvailableCodec = "xHE-AAC"
|
|
};
|
|
|
|
var vm = new FindBetterQualityBooksViewModel
|
|
{
|
|
Books = new List<BookDataViewModel> { bookVm },
|
|
ScanWidevine = true
|
|
};
|
|
|
|
var result = await vm.MarkBooksAsync();
|
|
|
|
Assert.AreEqual(1, result.MarkedCount);
|
|
Assert.IsFalse(result.WidevineEnabled); // was already true
|
|
Assert.IsFalse(result.XHeAacRequested); // was already true
|
|
Assert.IsTrue(result.AccountsMarkedForReRegistration);
|
|
|
|
using (var persister = AudibleApiStorage.GetAccountsSettingsPersister())
|
|
{
|
|
Assert.IsTrue(persister.AccountsSettings.Accounts[0].RequiresReRegistration);
|
|
Assert.IsFalse(persister.AccountsSettings.Accounts[1].RequiresReRegistration);
|
|
}
|
|
}
|
|
}
|