Fix lodestone character id extraction

This commit is contained in:
Stefan Berg
2022-08-03 22:43:11 +02:00
parent aebb7fdbb0
commit 60439535e7
5 changed files with 93 additions and 6 deletions

View File

@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MareSynchronosServer", "Mar
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MareSynchronos.API", "..\MareAPI\MareSynchronosAPI\MareSynchronos.API.csproj", "{326BFB1B-5571-47A6-8513-1FFDB32D53B0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MareSynchronosServerTest", "MareSynchronosServerTest\MareSynchronosServerTest.csproj", "{25A82A2A-35C2-4EE0-A0E8-DFDD77978DDA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
{326BFB1B-5571-47A6-8513-1FFDB32D53B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{326BFB1B-5571-47A6-8513-1FFDB32D53B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{326BFB1B-5571-47A6-8513-1FFDB32D53B0}.Release|Any CPU.Build.0 = Release|Any CPU
{25A82A2A-35C2-4EE0-A0E8-DFDD77978DDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25A82A2A-35C2-4EE0-A0E8-DFDD77978DDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25A82A2A-35C2-4EE0-A0E8-DFDD77978DDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25A82A2A-35C2-4EE0-A0E8-DFDD77978DDA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -302,14 +302,16 @@ namespace MareSynchronosServer.Discord
return auth;
}
private int? ParseCharacterIdFromLodestoneUrl(string lodestoneUrl)
private int? ParseCharacterIdFromLodestoneUrl(string lodestoneUrl)
{
var isLodestoneUrl = Regex.Match(lodestoneUrl, @"https:\/\/(na|eu|de|fr|jp)\.finalfantasyxiv\.com\/lodestone\/character\/\d+").Success;
if (!isLodestoneUrl) return null;
var regex = new Regex(@"https:\/\/(na|eu|de|fr|jp)\.finalfantasyxiv\.com\/lodestone\/character\/\d+");
var matches = regex.Match(lodestoneUrl);
var isLodestoneUrl = matches.Success;
if (!isLodestoneUrl || matches.Groups.Count < 1) return null;
lodestoneUrl = lodestoneUrl.Split('/', StringSplitOptions.RemoveEmptyEntries).Last();
if (!int.TryParse(lodestoneUrl, out int lodestoneId))
{
lodestoneUrl = matches.Groups[0].ToString();
var stringId = lodestoneUrl.Split('/', StringSplitOptions.RemoveEmptyEntries).Last();
if (!int.TryParse(stringId, out int lodestoneId)) {
return null;
}

View File

@@ -0,0 +1,53 @@
using FluentAssertions;
using MareSynchronosServer.Discord;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace MareSynchronosServerTest.Discord {
public class DiscordBotTest {
[Test]
[TestCase("", null)]
[TestCase("abcd", null)]
[TestCase("www.google.de", null)]
[TestCase("https://www.google.de", null)]
[TestCase("de.finalfantasyxiv.com/lodestone/character/1234", null)]
[TestCase("https://cn.finalfantasyxiv.com/lodestone/character/1234", null)]
[TestCase("http://jp.finalfantasyxiv.com/lodestone/character/1234", null)]
[TestCase("https://jp.finalfantasyxiv.com/character/1234", null)]
[TestCase("https://jp.finalfantasyxiv.com/lodestone/1234", null)]
[TestCase("https://www.finalfantasyxiv.com/lodestone/character/1234", null)]
[TestCase("https://jp.finalfantasyxiv.com/lodestone/character/1234", 1234)]
[TestCase("https://fr.finalfantasyxiv.com/lodestone/character/1234", 1234)]
[TestCase("https://eu.finalfantasyxiv.com/lodestone/character/1234/", 1234)]
[TestCase("https://eu.finalfantasyxiv.com/lodestone/character/1234?myurlparameter=500", 1234)]
[TestCase("https://de.finalfantasyxiv.com/lodestone/character/1234/whatever/3456", 1234)]
[TestCase("https://na.finalfantasyxiv.com/lodestone/character/1234abcd4321/whatever/3456", 1234)]
public void Test1(string url, int? expectedId) {
var inMemorySettings = new Dictionary<string, string> {
{"DiscordBotToken", "1234"}
};
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();
var spMock = new Mock<IServiceProvider>();
var loggerMock = new Mock<ILogger<DiscordBot>>();
var sut = new DiscordBot(spMock.Object, configuration, loggerMock.Object);
MethodInfo methodInfo = sut.GetType().GetMethod("ParseCharacterIdFromLodestoneUrl", BindingFlags.NonPublic | BindingFlags.Instance);
var actualId = methodInfo.Invoke(sut, new object[] { url });
actualId.Should().Be(expectedId);
}
}
}

View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MareSynchronosServer\MareSynchronosServer.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1 @@
global using NUnit.Framework;