diff --git a/MareSynchronosServer/MareSynchronosServer.sln b/MareSynchronosServer/MareSynchronosServer.sln index 9801f30..8f013f3 100644 --- a/MareSynchronosServer/MareSynchronosServer.sln +++ b/MareSynchronosServer/MareSynchronosServer.sln @@ -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 diff --git a/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs b/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs index a578c16..0af9160 100644 --- a/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs +++ b/MareSynchronosServer/MareSynchronosServer/Discord/DiscordBot.cs @@ -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; } diff --git a/MareSynchronosServer/MareSynchronosServerTest/Discord/DiscordBotTest.cs b/MareSynchronosServer/MareSynchronosServerTest/Discord/DiscordBotTest.cs new file mode 100644 index 0000000..fe66a66 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosServerTest/Discord/DiscordBotTest.cs @@ -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 { + {"DiscordBotToken", "1234"} + }; + + IConfiguration configuration = new ConfigurationBuilder() + .AddInMemoryCollection(inMemorySettings) + .Build(); + + var spMock = new Mock(); + var loggerMock = new Mock>(); + + 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); + } + } +} diff --git a/MareSynchronosServer/MareSynchronosServerTest/MareSynchronosServerTest.csproj b/MareSynchronosServer/MareSynchronosServerTest/MareSynchronosServerTest.csproj new file mode 100644 index 0000000..63087e2 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosServerTest/MareSynchronosServerTest.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + + + + + + diff --git a/MareSynchronosServer/MareSynchronosServerTest/Usings.cs b/MareSynchronosServer/MareSynchronosServerTest/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosServerTest/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file