Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework filters #32

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions GuildWarsPartySearch.Common/Converters/CampaignJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
using GuildWarsPartySearch.Common.Models.GuildWars;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Converters;
public sealed class CampaignJsonConverter : JsonConverter
public sealed class CampaignJsonConverter : JsonConverter<Campaign>
{
public override bool CanConvert(Type objectType) => true;

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
public override Campaign? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!objectType.IsAssignableTo(typeof(Campaign)))
{
return default;
}

switch (reader.TokenType)
{
case JsonToken.String:
var name = reader.Value as string;
case JsonTokenType.String:
var name = reader.GetString();
if (name is null ||
!Campaign.TryParse(name, out var namedCampaign))
{
return default;
}

return namedCampaign;
case JsonToken.Integer:
var id = reader.Value as long?;
if (id is not long ||
!Campaign.TryParse((int)id.Value, out var parsedCampaign))
case JsonTokenType.Number:
reader.TryGetInt64(out var id);
if (!Campaign.TryParse((int)id, out var parsedCampaign))
{
return default;
}
Expand All @@ -39,13 +32,13 @@ public sealed class CampaignJsonConverter : JsonConverter
}
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Campaign value, JsonSerializerOptions options)
{
if (value is not Campaign campaign)
{
return;
}

writer.WriteValue(campaign.Name);
writer.WriteStringValue(campaign.Name);
}
}
29 changes: 11 additions & 18 deletions GuildWarsPartySearch.Common/Converters/ContinentJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
using GuildWarsPartySearch.Common.Models.GuildWars;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Converters;
public sealed class ContinentJsonConverter : JsonConverter
public sealed class ContinentJsonConverter : JsonConverter<Continent>
{
public override bool CanConvert(Type objectType) => true;

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
public override Continent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!objectType.IsAssignableTo(typeof(Continent)))
{
return default;
}

switch (reader.TokenType)
{
case JsonToken.String:
var name = reader.Value as string;
case JsonTokenType.String:
var name = reader.GetString();
if (name is null ||
!Continent.TryParse(name, out var namedContinent))
{
return default;
}

return namedContinent;
case JsonToken.Integer:
var id = reader.Value as long?;
if (id is not long ||
!Continent.TryParse((int)id.Value, out var parsedContinent))
case JsonTokenType.Number:
reader.TryGetInt64(out var id);
if (!Continent.TryParse((int)id, out var parsedContinent))
{
return default;
}
Expand All @@ -39,13 +32,13 @@ public sealed class ContinentJsonConverter : JsonConverter
}
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Continent value, JsonSerializerOptions options)
{
if (value is not Continent continent)
{
return;
}

writer.WriteValue(continent.Name);
writer.WriteStringValue(continent.Name);
}
}
29 changes: 11 additions & 18 deletions GuildWarsPartySearch.Common/Converters/MapJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
using GuildWarsPartySearch.Common.Models.GuildWars;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Converters;
public sealed class MapJsonConverter : JsonConverter
public sealed class MapJsonConverter : JsonConverter<Map>
{
public override bool CanConvert(Type objectType) => true;

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
public override Map? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!objectType.IsAssignableTo(typeof(Map)))
{
return default;
}

switch (reader.TokenType)
{
case JsonToken.String:
var name = reader.Value as string;
case JsonTokenType.String:
var name = reader.GetString();
if (name is null ||
!Map.TryParse(name, out var namedMap))
{
return default;
}

return namedMap;
case JsonToken.Integer:
var id = reader.Value as long?;
if (id is not long ||
!Map.TryParse((int)id.Value, out var parsedMap))
case JsonTokenType.Number:
reader.TryGetInt64(out var id);
if (!Map.TryParse((int)id, out var parsedMap))
{
return default;
}
Expand All @@ -39,13 +32,13 @@ public sealed class MapJsonConverter : JsonConverter
}
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Map value, JsonSerializerOptions options)
{
if (value is not Map map)
{
return;
}

writer.WriteValue(map.Name);
writer.WriteStringValue(map.Name);
}
}
29 changes: 11 additions & 18 deletions GuildWarsPartySearch.Common/Converters/RegionJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
using GuildWarsPartySearch.Common.Models.GuildWars;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Converters;
public sealed class RegionJsonConverter : JsonConverter
public sealed class RegionJsonConverter : JsonConverter<Region>
{
public override bool CanConvert(Type objectType) => true;

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
public override Region? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!objectType.IsAssignableTo(typeof(Region)))
{
return default;
}

switch (reader.TokenType)
{
case JsonToken.String:
var name = reader.Value as string;
case JsonTokenType.String:
var name = reader.GetString();
if (name is null ||
!Region.TryParse(name, out var namedRegion))
{
return default;
}

return namedRegion;
case JsonToken.Integer:
var id = reader.Value as long?;
if (id is not long ||
!Region.TryParse((int)id.Value, out var parsedRegion))
case JsonTokenType.Number:
reader.TryGetInt64(out var id);
if (!Region.TryParse((int)id, out var parsedRegion))
{
return default;
}
Expand All @@ -39,13 +32,13 @@ public sealed class RegionJsonConverter : JsonConverter
}
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, Region value, JsonSerializerOptions options)
{
if (value is not Region region)
{
return;
}

writer.WriteValue(region.Name);
writer.WriteStringValue(region.Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
<Configurations>Debug;Release;Local</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Campaign.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GuildWarsPartySearch.Common.Converters;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Models.GuildWars;

Expand Down
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Continent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GuildWarsPartySearch.Common.Converters;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Models.GuildWars;

Expand Down
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Map.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GuildWarsPartySearch.Common.Converters;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Models.GuildWars;

Expand Down
2 changes: 1 addition & 1 deletion GuildWarsPartySearch.Common/Models/GuildWars/Region.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GuildWarsPartySearch.Common.Converters;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Common.Models.GuildWars;

Expand Down
5 changes: 5 additions & 0 deletions GuildWarsPartySearch.Tests/GuildWarsPartySearch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GuildWarsPartySearch\GuildWarsPartySearch.Server.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using FluentAssertions;
using GuildWarsPartySearch.Server.Services.CharName;

namespace GuildWarsPartySearch.Tests.Services.CharName;

[TestClass]
public sealed class CharNameValidatorTests
{
private readonly CharNameValidator charNameValidator;

public CharNameValidatorTests()
{
this.charNameValidator = new CharNameValidator();
}

[TestMethod]
[DataRow("-eq", false)] //Invalid chars
[DataRow(";_;_;", false)] //Invalid chars
[DataRow("D@ddy UwU", false)] //Invalid chars
[DataRow("Kormir Fr3@kin Sucks", false)] // Invalid chars
[DataRow("Myself", false)] // Only one word
[DataRow("Fentanyl Ascetic", true)] //Correct
[DataRow("Kormir", false)] // Only one word
[DataRow("Kormir Sucks", true)] // Correct
[DataRow("Bob'); DROP TABLE", false)] // Invalid chars
[DataRow("Why are we still here, just to suffer", false)] // Too long + Invalid chars
[DataRow("Why are we still here just to suffer", false)] // Too long
[DataRow(null, false)] // Null
[DataRow("", false)] // Empty
[DataRow(" ", false)] // Whitespace
[DataRow(" ", false)] // Whitespace
[DataRow("Me", false)] // Too short
public void ValidateNames_ShouldReturnExpected(string name, bool expected)
{
var result = this.charNameValidator.Validate(name);
result.Should().Be(expected);
}
}
6 changes: 6 additions & 0 deletions GuildWarsPartySearch/Attributes/DoNotInjectAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace GuildWarsPartySearch.Server.Attributes;

[AttributeUsage(AttributeTargets.Constructor)]
public class DoNotInjectAttribute : Attribute
{
}
15 changes: 8 additions & 7 deletions GuildWarsPartySearch/Converters/Base64ToCertificateConverter.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
using Newtonsoft.Json;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Server.Converters;

public sealed class Base64ToCertificateConverter : JsonConverter<X509Certificate2>
{
public override X509Certificate2? ReadJson(JsonReader reader, Type objectType, X509Certificate2? existingValue, bool hasExistingValue, JsonSerializer serializer)
public override X509Certificate2? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.Value is not string base64)
if (reader.GetString() is not string base64)
{
throw new InvalidOperationException($"Cannot deserialize {nameof(X509Certificate2)} from {reader.Value}");
throw new InvalidOperationException($"Cannot deserialize {nameof(X509Certificate2)} from {reader.GetString()}");
}

var bytes = Convert.FromBase64String(base64);
return new X509Certificate2(bytes);
}

public override void WriteJson(JsonWriter writer, X509Certificate2? value, JsonSerializer serializer)
public override void Write(Utf8JsonWriter writer, X509Certificate2 value, JsonSerializerOptions options)
{
var base64 = Convert.ToBase64String(value!.GetRawCertData());
writer.WriteValue(base64);
writer.WriteStringValue(base64);
}
}
Loading
Loading