Skip to content

Commit

Permalink
Addressing warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Psichorex committed Jan 20, 2024
1 parent 10a5c7f commit 4ec8de8
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 171 deletions.
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/BasicOrchardFeaturesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;
// writing tests for your app is not really about testing Orchard itself but nevertheless it's useful to check if all
// the important features like login work - keep in mind that you can break these from your own code. So, here we run
// the whole test suite.
public class BasicOrchardFeaturesTests : UITestBase
public class BasicOrchardFeaturesTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public BasicOrchardFeaturesTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// We could reuse the previously specified SetupHelpers.RecipeId const here but it's actually a different recipe for
// this test.
[Fact]
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
namespace Lombiq.Tests.UI.Samples.Tests;

// We'll see some simpler tests as a start. Each of them will teach us important concepts.
public class BasicTests : UITestBase
public class BasicTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public BasicTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// Checking that everything is OK with the homepage as an anonymous user. Note the [Fact] attribute: it's necessary
// for xUnit.
// Note that by default, tests are run via Chrome. Check out MultiBrowserTests for samples on using other browsers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@
namespace Lombiq.Tests.UI.Samples.Tests;

// In this basic test we will check visually the rendered content.
public class BasicVisualVerificationTests : UITestBase
public class BasicVisualVerificationTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public BasicVisualVerificationTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// This is a very basic sample to check that the header image is what we expect and looks as we expect. For this
// magic we are using the ImageSharp.Compare package. You can find more info about it here:
// https://github.com/Codeuctivity/ImageSharp.Compare. This looks really simple, but there is some trap to comparing
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/DatabaseSnapshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;
// and use that, because we don't have a pre-beaked database. Normally this feature is used to run tests when there is
// an already existing database, so you don't need to take a snapshot before using the
// "ExecuteTestFromExistingDBAsync()" method.
public class DatabaseSnapshotTests : UITestBase
public class DatabaseSnapshotTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public DatabaseSnapshotTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// Here, we set up the application, then we take a snapshot of it, then we use the
// "ExecuteTestFromExistingDBAsync()" to run the test on that. Finally, we test the basic Orchard features to check
// that the application was set up correctly.
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/EmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;

// In this test class we'll work with (wait for it!) e-mails. The UI Testing Toolbox provides services to run an SMTP
// server locally that the app can use to send out e-mails, which we can then immediately check.
public class EmailTests : UITestBase
public class EmailTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public EmailTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

[Fact]
public Task SendingTestEmailShouldWork() =>
ExecuteTestAfterSetupAsync(
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/ErrorHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@
namespace Lombiq.Tests.UI.Samples.Tests;

// Sometimes errors are expected. Let's check out what can be done with them!
public class ErrorHandlingTests : UITestBase
public class ErrorHandlingTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public ErrorHandlingTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// It's easier to diagnose a test failure if you know whether an element is missing because there something is
// actually missing or there was a server-side error. The below test visits a page where the action method throws an
// exception.
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/InteractiveModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;
// causes the test thread to wait until you've clicked on the "Continue Test" button in this tab. During that time you
// can interact with OC as if it was a normal execution.
// Note: this extension depends on Lombiq.Tests.UI.Shortcuts being enabled in your OC app.
public class InteractiveModeTests : UITestBase
public class InteractiveModeTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public InteractiveModeTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// If you want to try it out yourself, just remove the "Skip" parameter and run this test.
[Fact(Skip = "Use this to test to try out the interactive mode. This is not a real test you can run in CI.")]
[SuppressMessage("Usage", "xUnit1004:Test methods should not be skipped", Justification = "Only a demo.")]
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/MonkeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;
// It's possible to execute monkey tests that walk through site pages and do random interactions with pages, like click,
// scrolling, form filling, etc. Such random actions can uncover bugs that are otherwise difficult to find. Use such
// tests plug holes in your test suite which are not covered by explicit tests.
public class MonkeyTests : UITestBase
public class MonkeyTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public MonkeyTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// The basic idea is that you unleash monkey testing on specific pages or sections of the site, like a contact form
// or the content management UI. First, we test a single page.
[Fact]
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/MultiBrowserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;

// Up until now, all of our tests were run via Chrome. However, it's important that you can run tests with any of the
// other supported browsers too, even running a test with all of them at once! This class shows you how.
public class MultiBrowserTests : UITestBase
public class MultiBrowserTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public MultiBrowserTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// Remember that back in BasicTests we had AnonymousHomePageShouldExist()? We have similar super-simple tests here,
// just demonstrating how to drive different browsers.

Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/SecurityScanningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;

// Note that security scanning has cross-platform support, but due to the limitations of virtualization under Windows in
// GitHub Actions, these tests won't work there. They'll work on a Windows desktop though.
public class SecurityScanningTests : UITestBase
public class SecurityScanningTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public SecurityScanningTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// Let's see simple use case first: Running a built-in ZAP scan.

// We're running one of ZAP's built-in scans, the Baseline scan. This, as the name suggests, provides some
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/SqlServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ namespace Lombiq.Tests.UI.Samples.Tests;
// with it, should there be any incompatibilities). Note that for this, you need an SQL Server instance running; by
// default, this will be attempted under the default localhost server name. If you're using anything else, check out the
// settings in SqlServerConfiguration and Docs/Configuration.md, especially if you use Docker.
public class SqlServerTests : UITestBase
public class SqlServerTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
public SqlServerTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

// Here we have basically two of the same tests as in BasicTests but now we're using SQL Server as the site's
// database. If they still work and there are no errors in the log then the app works with SQL Server too.
[Fact]
Expand Down
7 changes: 1 addition & 6 deletions Lombiq.Tests.UI.Samples/Tests/TenantTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@ namespace Lombiq.Tests.UI.Samples.Tests;

// You can also test multi-tenant web apps. Creating tenants on the fly is supported as well with a shortcut. If you'd
// like to test the tenant creation-setup process itself, then look into using CreateNewTenantManuallyAsync() instead.
public class TenantTests : UITestBase
public class TenantTests(ITestOutputHelper testOutputHelper) : UITestBase(testOutputHelper)
{
private const string TestTenantName = "Test";
private const string TestTenantUrlPrefix = "test";
private const string TestTenantDisplayName = "Lombiq's OSOCE - Test Tenant";

public TenantTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

[Fact]
public Task CreatingTenantShouldWork() =>
ExecuteTestAfterSetupAsync(
Expand Down
17 changes: 4 additions & 13 deletions Lombiq.Tests.UI.Shortcuts/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,22 @@
namespace Lombiq.Tests.UI.Shortcuts.Controllers;

[DevelopmentAndLocalhostOnly]
public class AccountController : Controller
public class AccountController(UserManager<IUser> userManager, SignInManager<IUser> userSignInManager) : Controller
{
private readonly UserManager<IUser> _userManager;
private readonly SignInManager<IUser> _userSignInManager;

public AccountController(UserManager<IUser> userManager, SignInManager<IUser> userSignInManager)
{
_userManager = userManager;
_userSignInManager = userSignInManager;
}

[AllowAnonymous]
public async Task<IActionResult> SignInDirectly(string userName)
{
if (string.IsNullOrWhiteSpace(userName)) userName = "admin";
if (await _userManager.FindByNameAsync(userName) is not { } user) return NotFound();
if (await userManager.FindByNameAsync(userName) is not { } user) return NotFound();

await _userSignInManager.SignInAsync(user, isPersistent: false);
await userSignInManager.SignInAsync(user, isPersistent: false);

return Ok();
}

public async Task<IActionResult> SignOutDirectly()
{
await _userSignInManager.SignOutAsync();
await userSignInManager.SignOutAsync();

return Ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ namespace Lombiq.Tests.UI.Shortcuts.Controllers;

[AllowAnonymous]
[DevelopmentAndLocalhostOnly]
public class InteractiveModeController : Controller
public class InteractiveModeController(IInteractiveModeStatusAccessor interactiveModeStatusAccessor) : Controller
{
private readonly IInteractiveModeStatusAccessor _interactiveModeStatusAccessor;

public InteractiveModeController(IInteractiveModeStatusAccessor interactiveModeStatusAccessor) =>
_interactiveModeStatusAccessor = interactiveModeStatusAccessor;
private readonly IInteractiveModeStatusAccessor _interactiveModeStatusAccessor = interactiveModeStatusAccessor;

public IActionResult Index()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
namespace Lombiq.Tests.UI.Shortcuts.Controllers;

[Feature(ShortcutsFeatureIds.MediaCachePurge)]
public class MediaCachePurgeController : Controller
public class MediaCachePurgeController(IMediaFileStoreCache mediaFileStoreCache) : Controller
{
private readonly IMediaFileStoreCache _mediaFileStoreCache;

public MediaCachePurgeController(IMediaFileStoreCache mediaFileStoreCache)
=> _mediaFileStoreCache = mediaFileStoreCache;

public async Task<IActionResult> PurgeMediaCacheDirectly()
{
var hasErrors = await _mediaFileStoreCache.PurgeAsync();
var hasErrors = await mediaFileStoreCache.PurgeAsync();

return hasErrors ? StatusCode(500) : Ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ namespace Lombiq.Tests.UI.MonkeyTesting.UrlFilters;
/// <summary>
/// URL filter that matches the URL to see if it's NOT an admin page (i.e. an URL NOT under /admin).
/// </summary>
public class NotAdminMonkeyTestingUrlFilter : IMonkeyTestingUrlFilter
public class NotAdminMonkeyTestingUrlFilter(UITestContext context) : IMonkeyTestingUrlFilter
{
private readonly AdminMonkeyTestingUrlFilter _adminMonkeyTestingUrlFilter;

public NotAdminMonkeyTestingUrlFilter(UITestContext context) => _adminMonkeyTestingUrlFilter = new(context);
private readonly AdminMonkeyTestingUrlFilter _adminMonkeyTestingUrlFilter = new(context);

public bool AllowUrl(UITestContext context, Uri url) => !_adminMonkeyTestingUrlFilter.AllowUrl(context, url);
}
31 changes: 14 additions & 17 deletions Lombiq.Tests.UI/SecurityScanning/SecurityScanConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ namespace Lombiq.Tests.UI.SecurityScanning;
/// </remarks>
public class SecurityScanConfiguration
{
private readonly List<Uri> _additionalUris = new();
private readonly List<string> _excludedUrlRegexPatterns = new();
private readonly List<ScanRule> _disabledActiveScanRules = new();
private readonly Dictionary<ScanRule, (ScanRuleThreshold Threshold, ScanRuleStrength Strength)> _configuredActiveScanRules = new();
private readonly List<ScanRule> _disabledPassiveScanRules = new();
private readonly List<(string Url, int Id, string RuleName)> _disabledRulesForUrls = new();
private readonly List<(string Url, int Id, string RuleName, string Justification)> _falsePositives = new();
private readonly List<Func<YamlDocument, Task>> _zapPlanModifiers = new();
private readonly List<Uri> _additionalUris = [];
private readonly List<string> _excludedUrlRegexPatterns = [];
private readonly List<ScanRule> _disabledActiveScanRules = [];
private readonly Dictionary<ScanRule, (ScanRuleThreshold Threshold, ScanRuleStrength Strength)> _configuredActiveScanRules = [];
private readonly List<ScanRule> _disabledPassiveScanRules = [];
private readonly List<(string Url, int Id, string RuleName)> _disabledRulesForUrls = [];
private readonly List<(string Url, int Id, string RuleName, string Justification)> _falsePositives = [];
private readonly List<Func<YamlDocument, Task>> _zapPlanModifiers = [];

public Uri StartUri { get; private set; }
public bool AjaxSpiderIsUsed { get; private set; }
Expand Down Expand Up @@ -275,7 +275,10 @@ internal async Task ApplyToPlanAsync(YamlDocument yamlDocument, UITestContext co
// pollPostData: ""
}

#pragma warning disable S3878 // Arrays should not be created for params parameters
// We need to make an array here because there would be an error otherwise.
yamlDocument.AddExcludePathsRegex([.. _excludedUrlRegexPatterns]);
#pragma warning restore S3878 // Arrays should not be created for params parameters
foreach (var rule in _disabledActiveScanRules) yamlDocument.DisableActiveScanRule(rule.Id, rule.Name);

foreach (var ruleConfiguration in _configuredActiveScanRules)
Expand All @@ -293,15 +296,9 @@ internal async Task ApplyToPlanAsync(YamlDocument yamlDocument, UITestContext co
foreach (var modifier in _zapPlanModifiers) await modifier(yamlDocument);
}

public class ScanRule
public class ScanRule(int id, string name)
{
public int Id { get; }
public string Name { get; }

public ScanRule(int id, string name)
{
Id = id;
Name = name;
}
public int Id { get; } = id;
public string Name { get; } = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

namespace Lombiq.Tests.UI.Services.OrchardCoreHosting;

public class FakeViewCompilerProvider : IViewCompilerProvider
public class FakeViewCompilerProvider(IServiceProvider services) : IViewCompilerProvider
{
private readonly IServiceProvider _services;

public FakeViewCompilerProvider(IServiceProvider services) => _services = services;

public IViewCompiler GetCompiler() =>
_services
services
.GetServices<IViewCompilerProvider>()
.FirstOrDefault()
.GetCompiler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,19 @@

namespace Lombiq.Tests.UI.Services.OrchardCoreHosting;

public sealed class OrchardApplicationFactory<TStartup> : WebApplicationFactory<TStartup>, IProxyConnectionProvider
public sealed class OrchardApplicationFactory<TStartup>(
Action<IConfigurationBuilder> configureHost = null,
Action<IWebHostBuilder> configuration = null,
Action<ConfigurationManager, OrchardCoreBuilder> configureOrchard = null) : WebApplicationFactory<TStartup>, IProxyConnectionProvider
where TStartup : class
{
private readonly Action<IConfigurationBuilder> _configureHost;
private readonly Action<IWebHostBuilder> _configuration;
private readonly Action<ConfigurationManager, OrchardCoreBuilder> _configureOrchard;
private readonly List<IStore> _createdStores = new();

public OrchardApplicationFactory(
Action<IConfigurationBuilder> configureHost = null,
Action<IWebHostBuilder> configuration = null,
Action<ConfigurationManager, OrchardCoreBuilder> configureOrchard = null)
{
_configureHost = configureHost;
_configuration = configuration;
_configureOrchard = configureOrchard;
}
private readonly List<IStore> _createdStores = [];

public Uri BaseAddress => ClientOptions.BaseAddress;

protected override IHost CreateHost(IHostBuilder builder)
{
builder.ConfigureHostConfiguration(configurationBuilder => _configureHost?.Invoke(configurationBuilder));
builder.ConfigureHostConfiguration(configurationBuilder => configureHost?.Invoke(configurationBuilder));
// This lock is to avoid parallel start of the application.
// Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost() starts a new thread for the web
// application instance which can cause issues in e.g.:
Expand Down Expand Up @@ -86,7 +76,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
loggingBuilder.AddNLogWeb(factory, new NLogAspNetCoreOptions { ReplaceLoggerFactory = true });
});

_configuration?.Invoke(builder);
configuration?.Invoke(builder);
}

private void ConfigureTestServices(IServiceCollection services)
Expand All @@ -96,13 +86,13 @@ private void ConfigureTestServices(IServiceCollection services)
.ImplementationInstance as OrchardCoreBuilder
?? throw new InvalidOperationException(
"Please call WebApplicationBuilder.Services.AddOrchardCms() in your Program.cs!");
var configuration = services
var config = services
.LastOrDefault(descriptor => descriptor.ServiceType == typeof(ConfigurationManager))?
.ImplementationInstance as ConfigurationManager
?? throw new InvalidOperationException(
$"Please add {nameof(ConfigurationManager)} instance to WebApplicationBuilder.Services in your Program.cs!");

_configureOrchard?.Invoke(configuration, builder);
configureOrchard?.Invoke(config, builder);

builder.ConfigureServices(
builderServices =>
Expand Down
Loading

0 comments on commit 4ec8de8

Please sign in to comment.