|
| 1 | +using System.Text.Json.Nodes; |
| 2 | +using Moq; |
| 3 | +using OrchardCore.Https.Settings; |
| 4 | +using OrchardCore.Settings; |
| 5 | + |
| 6 | +namespace OrchardCore.Tests.Modules.OrchardCore.Https; |
| 7 | + |
| 8 | +public class HttpsSettingsMigrationsTests |
| 9 | +{ |
| 10 | + private const string LegacyEnableStrictTransportSecurityKey = "EnableStrictTransportSecurity"; |
| 11 | + |
| 12 | + [Fact] |
| 13 | + public async Task CreateAsyncMigratesLegacyHstsSetting() |
| 14 | + { |
| 15 | + var site = new SiteSettings(); |
| 16 | + site.Properties[nameof(HttpsSettings)] = new JsonObject |
| 17 | + { |
| 18 | + [LegacyEnableStrictTransportSecurityKey] = true, |
| 19 | + }; |
| 20 | + |
| 21 | + var siteService = new Mock<ISiteService>(); |
| 22 | + siteService.Setup(service => service.LoadSiteSettingsAsync()).ReturnsAsync(site); |
| 23 | + siteService.Setup(service => service.UpdateSiteSettingsAsync(site)).Returns(Task.CompletedTask); |
| 24 | + |
| 25 | + var version = await InvokeCreateAsync(siteService.Object); |
| 26 | + |
| 27 | + Assert.Equal(1, version); |
| 28 | + Assert.Equal( |
| 29 | + nameof(HttpStrictTransportSecurityMode.Enabled), |
| 30 | + site.Properties[nameof(HttpsSettings)]?[nameof(HttpsSettings.StrictTransportSecurityMode)]?.GetValue<string>()); |
| 31 | + Assert.Null(site.Properties[nameof(HttpsSettings)]?[LegacyEnableStrictTransportSecurityKey]); |
| 32 | + siteService.Verify(service => service.UpdateSiteSettingsAsync(site), Times.Once); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public async Task CreateAsyncRemovesLegacySettingWithoutOverwritingMigratedValue() |
| 37 | + { |
| 38 | + var site = new SiteSettings(); |
| 39 | + site.Properties[nameof(HttpsSettings)] = new JsonObject |
| 40 | + { |
| 41 | + [nameof(HttpsSettings.StrictTransportSecurityMode)] = nameof(HttpStrictTransportSecurityMode.Disabled), |
| 42 | + [LegacyEnableStrictTransportSecurityKey] = true, |
| 43 | + }; |
| 44 | + |
| 45 | + var siteService = new Mock<ISiteService>(); |
| 46 | + siteService.Setup(service => service.LoadSiteSettingsAsync()).ReturnsAsync(site); |
| 47 | + siteService.Setup(service => service.UpdateSiteSettingsAsync(site)).Returns(Task.CompletedTask); |
| 48 | + |
| 49 | + var version = await InvokeCreateAsync(siteService.Object); |
| 50 | + |
| 51 | + Assert.Equal(1, version); |
| 52 | + Assert.Equal( |
| 53 | + nameof(HttpStrictTransportSecurityMode.Disabled), |
| 54 | + site.Properties[nameof(HttpsSettings)]?[nameof(HttpsSettings.StrictTransportSecurityMode)]?.GetValue<string>()); |
| 55 | + Assert.Null(site.Properties[nameof(HttpsSettings)]?[LegacyEnableStrictTransportSecurityKey]); |
| 56 | + siteService.Verify(service => service.UpdateSiteSettingsAsync(site), Times.Once); |
| 57 | + } |
| 58 | + |
| 59 | + private static async Task<int> InvokeCreateAsync(ISiteService siteService) |
| 60 | + { |
| 61 | + var migrationType = typeof(HttpsSettings).Assembly.GetType("OrchardCore.Https.Migrations.HttpsSettingsMigrations"); |
| 62 | + if (migrationType is null) |
| 63 | + { |
| 64 | + throw new InvalidOperationException("Unable to locate the HTTPS settings migration type."); |
| 65 | + } |
| 66 | + |
| 67 | + var migration = Activator.CreateInstance(migrationType, siteService); |
| 68 | + if (migration is null) |
| 69 | + { |
| 70 | + throw new InvalidOperationException("Unable to create the HTTPS settings migration instance."); |
| 71 | + } |
| 72 | + |
| 73 | + var method = migrationType.GetMethod("CreateAsync"); |
| 74 | + if (method is null) |
| 75 | + { |
| 76 | + throw new InvalidOperationException("Unable to locate the HTTPS settings migration CreateAsync method."); |
| 77 | + } |
| 78 | + |
| 79 | + var result = method.Invoke(migration, null) as Task<int>; |
| 80 | + if (result is null) |
| 81 | + { |
| 82 | + throw new InvalidOperationException("Unable to invoke the HTTPS settings migration CreateAsync method."); |
| 83 | + } |
| 84 | + |
| 85 | + return await result; |
| 86 | + } |
| 87 | +} |
0 commit comments