From 70c2fc0a9ec9076b80c4d4ce7c92acf2918483fb Mon Sep 17 00:00:00 2001 From: rockstardev <5191402+rockstardev@users.noreply.github.com> Date: Wed, 5 Mar 2025 22:14:27 -0600 Subject: [PATCH] Fixing bug on password not being cleared --- BTCPayServer.Tests/GreenfieldAPITests.cs | 19 ++++++++++++++++--- BTCPayServer/Services/Mails/EmailSettings.cs | 5 +++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index ba28e14ef..94a393f43 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -4104,6 +4104,9 @@ namespace BTCPayServer.Tests var admin = tester.NewAccount(); await admin.GrantAccessAsync(true); var adminClient = await admin.CreateClient(Policies.Unrestricted); + // validate that clear email settings will not throw an error + await adminClient.UpdateServerEmailSettings(new ServerEmailSettingsData()); + var data = new ServerEmailSettingsData { From = "admin@admin.com", @@ -4129,6 +4132,11 @@ namespace BTCPayServer.Tests { From = "invalid" })); + + // check that clear server email settings works + await adminClient.UpdateServerEmailSettings(new ServerEmailSettingsData()); + var clearedSettings = await adminClient.GetServerEmailSettings(); + Assert.Equal(JsonConvert.SerializeObject(new EmailSettingsData { PasswordSet = false }), JsonConvert.SerializeObject(clearedSettings)); // NOTE: This email test fails silently in EmailSender.cs#31, can't test, but leaving for the future as reminder //await adminClient.SendEmail(admin.StoreId, @@ -4144,8 +4152,8 @@ namespace BTCPayServer.Tests var admin = tester.NewAccount(); await admin.GrantAccessAsync(true); var adminClient = await admin.CreateClient(Policies.Unrestricted); - await adminClient.UpdateStoreEmailSettings(admin.StoreId, - new EmailSettingsData()); + // validate that clear email settings will not throw an error + await adminClient.UpdateStoreEmailSettings(admin.StoreId, new EmailSettingsData()); var data = new EmailSettingsData { @@ -4164,7 +4172,12 @@ namespace BTCPayServer.Tests await AssertValidationError(new[] { nameof(EmailSettingsData.From) }, async () => await adminClient.UpdateStoreEmailSettings(admin.StoreId, new EmailSettingsData { From = "invalid" })); - + + // clear store email settings + await adminClient.UpdateStoreEmailSettings(admin.StoreId, new EmailSettingsData()); + var clearedSettings = await adminClient.GetStoreEmailSettings(admin.StoreId); + Assert.Equal(JsonConvert.SerializeObject(new EmailSettingsData { PasswordSet = false }), JsonConvert.SerializeObject(clearedSettings)); + await adminClient.SendEmail(admin.StoreId, new SendEmailRequest { Body = "lol", Subject = "subj", Email = "to@example.org" }); } diff --git a/BTCPayServer/Services/Mails/EmailSettings.cs b/BTCPayServer/Services/Mails/EmailSettings.cs index 24d7f0dea..47246f349 100644 --- a/BTCPayServer/Services/Mails/EmailSettings.cs +++ b/BTCPayServer/Services/Mails/EmailSettings.cs @@ -36,8 +36,9 @@ namespace BTCPayServer.Services.Mails Server = data.Server, Port = data.Port, Login = data.Login, - // Retaining the password if it exists and was not provided in request - Password = string.IsNullOrEmpty(data.Password) ? existingPassword : data.Password, + // If login was provided but password was not, use the existing password from database + Password = !string.IsNullOrEmpty(data.Login) && string.IsNullOrEmpty(data.Password) ? + existingPassword : data.Password, From = data.From, DisableCertificateCheck = data.DisableCertificateCheck };