Skip to content

Commit

Permalink
Use collection expressions
Browse files Browse the repository at this point in the history
- Add more usage of C# 12 collection expressions.
- Remove redundant null checks.
  • Loading branch information
martincostello committed Oct 23, 2023
1 parent c832407 commit 173898c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
10 changes: 6 additions & 4 deletions src/LondonTravel.Site/Controllers/ManageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable SA1010

namespace MartinCostello.LondonTravel.Site.Controllers;

[Authorize]
Expand Down Expand Up @@ -368,22 +370,22 @@ private async Task<IdentityResult> UpdateClaimsAsync(LondonTravelUser user, Exte

if (user.RoleClaims == null)
{
user.RoleClaims = new List<LondonTravelRole>();
user.RoleClaims = [];
commitUpdate = true;
}

foreach (var claim in info.Principal.Claims)
{
bool hasClaim = user?.RoleClaims
bool hasClaim = user.RoleClaims
.Where((p) => p.ClaimType == claim.Type)
.Where((p) => p.Issuer == claim.Issuer)
.Where((p) => p.Value == claim.Value)
.Where((p) => p.ValueType == claim.ValueType)
.Any() == true;
.Any();

if (!hasClaim)
{
user!.RoleClaims.Add(LondonTravelRole.FromClaim(claim));
user.RoleClaims.Add(LondonTravelRole.FromClaim(claim));
commitUpdate = true;
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/LondonTravel.Site/Middleware/CustomHttpHeadersMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,33 +212,33 @@ private string BuildContentSecurityPolicy(string? nonce, bool allowInlineStyles,
var options = _options.CurrentValue;
string? cdn = GetCdnOriginForContentSecurityPolicy(options);

var scriptDirectives = new List<string>()
{
List<string> scriptDirectives =
[
Csp.Self,
};
];

var styleDirectives = new List<string>()
{
List<string> styleDirectives =
[
Csp.Self,
};
];

var policies = new Dictionary<string, IList<string>>()
{
["default-src"] = new[] { Csp.Self, Csp.Data },
["default-src"] = [Csp.Self, Csp.Data],
["script-src"] = scriptDirectives,
["style-src"] = styleDirectives,
["img-src"] = new[] { Csp.Self, Csp.Data, cdn },
["font-src"] = new[] { Csp.Self },
["connect-src"] = new[] { Csp.Self },
["media-src"] = new[] { Csp.None },
["object-src"] = new[] { Csp.None },
["child-src"] = new[] { Csp.Self },
["frame-ancestors"] = new[] { Csp.None },
["form-action"] = new[] { Csp.Self },
["block-all-mixed-content"] = Array.Empty<string>(),
["base-uri"] = new[] { Csp.Self },
["manifest-src"] = new[] { Csp.Self },
["worker-src"] = new[] { Csp.Self },
["img-src"] = [Csp.Self, Csp.Data, cdn],
["font-src"] = [Csp.Self],
["connect-src"] = [Csp.Self],
["media-src"] = [Csp.None],
["object-src"] = [Csp.None],
["child-src"] = [Csp.Self],
["frame-ancestors"] = [Csp.None],
["form-action"] = [Csp.Self],
["block-all-mixed-content"] = [],
["base-uri"] = [Csp.Self],
["manifest-src"] = [Csp.Self],
["worker-src"] = [Csp.Self],
};

if (allowInlineStyles)
Expand Down
2 changes: 1 addition & 1 deletion tests/LondonTravel.Site.Tests/BrowserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected virtual void Dispose(bool disposing)

private static void InstallPlaywright()
{
int exitCode = Microsoft.Playwright.Program.Main(new[] { "install" });
int exitCode = Microsoft.Playwright.Program.Main(["install"]);

if (exitCode != 0)
{
Expand Down
10 changes: 5 additions & 5 deletions tests/LondonTravel.Site.Tests/BrowsersTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ public sealed class BrowsersTestData : IEnumerable<object?[]>
{
public IEnumerator<object?[]> GetEnumerator()
{
yield return new[] { BrowserType.Chromium, null };
yield return new[] { BrowserType.Chromium, "chrome" };
yield return [BrowserType.Chromium, null];
yield return [BrowserType.Chromium, "chrome"];

if (!OperatingSystem.IsLinux())
{
yield return new[] { BrowserType.Chromium, "msedge" };
yield return [BrowserType.Chromium, "msedge"];
}

yield return new[] { BrowserType.Firefox, null };
yield return [BrowserType.Firefox, null];

if (OperatingSystem.IsMacOS())
{
yield return new[] { BrowserType.Webkit, null };
yield return [BrowserType.Webkit, null];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void GetAvatarUrl_Returns_Correct_Url_If_No_Email_Claim()
public static void GetAvatarUrl_Returns_Correct_Url(string email, string expected)
{
// Arrange
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Email, email) });
var identity = new ClaimsIdentity([new Claim(ClaimTypes.Email, email)]);
var principal = new ClaimsPrincipal(identity);

// Act
Expand All @@ -52,7 +52,7 @@ public static void GetAvatarUrl_Returns_Correct_Url(string email, string expecte
public static void GetAvatarUrl_Returns_Correct_Url_With_Explicit_Size()
{
// Arrange
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Email, "[email protected]") });
var identity = new ClaimsIdentity([new Claim(ClaimTypes.Email, "[email protected]")]);
var principal = new ClaimsPrincipal(identity);

// Act
Expand Down Expand Up @@ -87,7 +87,7 @@ public static void GetDisplayName_Returns_Correct_Value_If_No_GivenName_Or_Name(
public static void GetDisplayName_Returns_Correct_Value_If_No_GivenName_But_Name(string name, string expected)
{
// Arrange
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name) });
var identity = new ClaimsIdentity([new Claim(ClaimTypes.Name, name)]);
var principal = new ClaimsPrincipal(identity);

// Act
Expand All @@ -103,7 +103,7 @@ public static void GetDisplayName_Returns_Correct_Value_If_No_GivenName_But_Name
public static void GetDisplayName_Returns_Correct_Value_If_Blank_GivenName_But_Name(string name, string expected)
{
// Arrange
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.GivenName, string.Empty) });
var identity = new ClaimsIdentity([new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.GivenName, string.Empty)]);
var principal = new ClaimsPrincipal(identity);

// Act
Expand All @@ -119,7 +119,7 @@ public static void GetDisplayName_Returns_Correct_Value_If_Blank_GivenName_But_N
public static void GetDisplayName_Returns_Correct_Value_If_GivenName(string givenName, string expected)
{
// Arrange
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.GivenName, givenName) });
var identity = new ClaimsIdentity([new Claim(ClaimTypes.GivenName, givenName)]);
var principal = new ClaimsPrincipal(identity);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private static TflOptions CreateOptions()
AppId = "My-App-Id",
AppKey = "My-App-Key",
BaseUri = new Uri("https://api.tfl.gov.uk/"),
SupportedModes = new[] { "dlr", "elizabeth-line", "overground", "tube" },
SupportedModes = ["dlr", "elizabeth-line", "overground", "tube"],
};
}
}

0 comments on commit 173898c

Please sign in to comment.