From 61af22834174f3c7ce37cda54bc5dd2e039e1c70 Mon Sep 17 00:00:00 2001 From: andreas-henning <976431@outlook.com> Date: Sat, 30 Jul 2022 11:57:25 +0200 Subject: [PATCH] Renamed project 'Tests' to 'Anarchy.Tests' --- .gitignore | 2 +- .../Anarchy.Tests.csproj | 2 +- Anarchy.Tests/Directory.Build.props | 6 + {Tests => Anarchy.Tests}/Globals.cs | 19 +- .../REST/Messsage/AttachmentTests.cs | 54 ++++ Anarchy.Tests/REST/Messsage/MessageTests.cs | 18 ++ {Tests => Anarchy.Tests}/Resources/image1.png | Bin {Tests => Anarchy.Tests}/Resources/image2.jpg | Bin {Tests => Anarchy.Tests}/Resources/poetry.txt | 0 Anarchy.Tests/Settings/App.cs | 9 + Anarchy.Tests/Settings/Proxy.cs | 15 + {Tests => Anarchy.Tests}/Usings.cs | 0 Anarchy.Tests/appsettings.json | 4 + Anarchy.sln | 295 +----------------- Tests/AppSettings.cs | 13 - Tests/Directory.Build.props | 6 - Tests/MessageTests.cs | 73 ----- Tests/appsettings.json | 4 - 18 files changed, 132 insertions(+), 388 deletions(-) rename Tests/Tests.csproj => Anarchy.Tests/Anarchy.Tests.csproj (95%) create mode 100644 Anarchy.Tests/Directory.Build.props rename {Tests => Anarchy.Tests}/Globals.cs (67%) create mode 100644 Anarchy.Tests/REST/Messsage/AttachmentTests.cs create mode 100644 Anarchy.Tests/REST/Messsage/MessageTests.cs rename {Tests => Anarchy.Tests}/Resources/image1.png (100%) rename {Tests => Anarchy.Tests}/Resources/image2.jpg (100%) rename {Tests => Anarchy.Tests}/Resources/poetry.txt (100%) create mode 100644 Anarchy.Tests/Settings/App.cs create mode 100644 Anarchy.Tests/Settings/Proxy.cs rename {Tests => Anarchy.Tests}/Usings.cs (100%) create mode 100644 Anarchy.Tests/appsettings.json delete mode 100644 Tests/AppSettings.cs delete mode 100644 Tests/Directory.Build.props delete mode 100644 Tests/MessageTests.cs delete mode 100644 Tests/appsettings.json diff --git a/.gitignore b/.gitignore index 3b44f7f3..d742fdba 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ # Excluded from versioning because it contains discord access credentials use by Anarchy's unit tests. # Use appsettings.json as a template to create your own version of this file. -Tests/appsettings.Development.json +Anarchy.Tests/appsettings.Development.json # User-specific files *.rsuser diff --git a/Tests/Tests.csproj b/Anarchy.Tests/Anarchy.Tests.csproj similarity index 95% rename from Tests/Tests.csproj rename to Anarchy.Tests/Anarchy.Tests.csproj index 03564756..2299b803 100644 --- a/Tests/Tests.csproj +++ b/Anarchy.Tests/Anarchy.Tests.csproj @@ -9,7 +9,7 @@ Discord - ..\.bin\Tests + ..\.bin\$(MSBuildProjectName) diff --git a/Anarchy.Tests/Directory.Build.props b/Anarchy.Tests/Directory.Build.props new file mode 100644 index 00000000..c88ec56b --- /dev/null +++ b/Anarchy.Tests/Directory.Build.props @@ -0,0 +1,6 @@ + + + ..\.obj\Anarchy.Tests + ..\.obj\Anarchy.Tests + + \ No newline at end of file diff --git a/Tests/Globals.cs b/Anarchy.Tests/Globals.cs similarity index 67% rename from Tests/Globals.cs rename to Anarchy.Tests/Globals.cs index 98e04524..0db6bd8f 100644 --- a/Tests/Globals.cs +++ b/Anarchy.Tests/Globals.cs @@ -6,7 +6,7 @@ namespace Discord [TestClass] public static class Globals { - internal static AppSettings Settings { get; private set; } = GetAppSettings(); + internal static Settings.App Settings { get; private set; } = GetAppSettings(); public static class FileNames { @@ -25,7 +25,18 @@ public static void AssemblyInit(TestContext context) { var autoResetEvent = new AutoResetEvent(false); - var client = new DiscordSocketClient(); + // All tests will use the proxy specified in appsettings.json, which can be added like so: + // , + // "Proxy": { + // "Host": "127.0.0.1", + // "Port": 8888 + // } + + var client = new DiscordSocketClient(new DiscordSocketConfig() + { + Proxy = Settings.Proxy?.CreateProxy() + }); + client.OnLoggedIn += OnLoggedIn; client.Login(Settings.Token); @@ -40,13 +51,13 @@ void OnLoggedIn(DiscordSocketClient client, LoginEventArgs args) Client = client; } - private static AppSettings GetAppSettings() + private static Settings.App GetAppSettings() { var path = File.Exists(FileNames.SettingDevelopment) ? FileNames.SettingDevelopment : FileNames.Setting; - return JsonSerializer.Deserialize(File.ReadAllText(path))!; + return JsonSerializer.Deserialize(File.ReadAllText(path))!; } } } \ No newline at end of file diff --git a/Anarchy.Tests/REST/Messsage/AttachmentTests.cs b/Anarchy.Tests/REST/Messsage/AttachmentTests.cs new file mode 100644 index 00000000..25143314 --- /dev/null +++ b/Anarchy.Tests/REST/Messsage/AttachmentTests.cs @@ -0,0 +1,54 @@ +using System.Text; +using Microsoft.Maui.Graphics; + +namespace Discord +{ + [TestClass] + public class AttachmentTests + { + [TestMethod] + public void SendFile() + { + var client = Globals.Client; + var channelId = Globals.Settings.ChannelId; + + var msg = client.SendFile(channelId, Globals.FileNames.PoetryTxt, $"{nameof(SendFile)} attachment test."); + + Assert.AreEqual(1, msg.Attachments.Count); + } + + [TestMethod] + public void SendMessageProperties() + { + var image1 = DiscordImageSource.FromStream(File.OpenRead(Globals.FileNames.Image1), ImageFormat.Png); + var image2 = DiscordImageSource.FromStream(File.OpenRead(Globals.FileNames.Image2), ImageFormat.Jpeg); + var poetryText = File.ReadAllText(Globals.FileNames.PoetryTxt); + + var props = new MessageProperties + { + Content = $"{nameof(MessageProperties)} attachment test.", + Attachments = new List() + { + // Attach an arbitrary file. + new PartialDiscordAttachment(Globals.FileNames.Image1), + // Attach an existing DiscordImage. + new PartialDiscordAttachment(image2, Globals.FileNames.Image2), + // Attach an arbitrary file with custom specifications for everything. + new PartialDiscordAttachment( + new DiscordAttachmentFile(Encoding.UTF8.GetBytes(poetryText), MediaTypeNames.Text.Plain), + Globals.FileNames.PoetryTxt, + "Text File") + } + }; + + var client = Globals.Client; + var channelId = Globals.Settings.ChannelId; + + var msg = client.SendMessage(channelId, props); + Assert.AreEqual(props.Attachments.Count, msg.Attachments.Count); + + var postedImage1 = DiscordImageSource.FromUrl(msg.Attachments[0].Url).Result; + CollectionAssert.AreEqual(postedImage1.PlatformImage.Bytes, image1.PlatformImage.Bytes); + } + } +} \ No newline at end of file diff --git a/Anarchy.Tests/REST/Messsage/MessageTests.cs b/Anarchy.Tests/REST/Messsage/MessageTests.cs new file mode 100644 index 00000000..238b59b9 --- /dev/null +++ b/Anarchy.Tests/REST/Messsage/MessageTests.cs @@ -0,0 +1,18 @@ +namespace Discord +{ + [TestClass] + public class MessageTests + { + [TestMethod] + public void SendMessage() + { + const string content = "The simplest possible message."; + + var msg = Globals.Client.SendMessage( + Globals.Settings.ChannelId, + content); + + Assert.AreEqual(content, msg.Content); + } + } +} diff --git a/Tests/Resources/image1.png b/Anarchy.Tests/Resources/image1.png similarity index 100% rename from Tests/Resources/image1.png rename to Anarchy.Tests/Resources/image1.png diff --git a/Tests/Resources/image2.jpg b/Anarchy.Tests/Resources/image2.jpg similarity index 100% rename from Tests/Resources/image2.jpg rename to Anarchy.Tests/Resources/image2.jpg diff --git a/Tests/Resources/poetry.txt b/Anarchy.Tests/Resources/poetry.txt similarity index 100% rename from Tests/Resources/poetry.txt rename to Anarchy.Tests/Resources/poetry.txt diff --git a/Anarchy.Tests/Settings/App.cs b/Anarchy.Tests/Settings/App.cs new file mode 100644 index 00000000..f5a50800 --- /dev/null +++ b/Anarchy.Tests/Settings/App.cs @@ -0,0 +1,9 @@ +namespace Discord.Settings +{ + internal class App + { + public string Token { get; set; } = string.Empty; + public ulong ChannelId { get; set; } + public ProxySettings? Proxy { get; set; } + } +} diff --git a/Anarchy.Tests/Settings/Proxy.cs b/Anarchy.Tests/Settings/Proxy.cs new file mode 100644 index 00000000..d7b80d56 --- /dev/null +++ b/Anarchy.Tests/Settings/Proxy.cs @@ -0,0 +1,15 @@ +using System.Net; + +namespace Discord.Settings +{ + internal class ProxySettings + { + public string Host { get; set; } = String.Empty; + public int Port { get; set; } + + public IWebProxy CreateProxy() + { + return new WebProxy(Host, Port); + } + } +} diff --git a/Tests/Usings.cs b/Anarchy.Tests/Usings.cs similarity index 100% rename from Tests/Usings.cs rename to Anarchy.Tests/Usings.cs diff --git a/Anarchy.Tests/appsettings.json b/Anarchy.Tests/appsettings.json new file mode 100644 index 00000000..be940c77 --- /dev/null +++ b/Anarchy.Tests/appsettings.json @@ -0,0 +1,4 @@ +{ + "Token": "", + "ChannelId": " - - ..\.obj\Tests - ..\.obj\Tests - - \ No newline at end of file diff --git a/Tests/MessageTests.cs b/Tests/MessageTests.cs deleted file mode 100644 index 81da4fd1..00000000 --- a/Tests/MessageTests.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System.Text; -using Microsoft.Maui.Graphics; - -namespace Discord -{ - [TestClass] - public class MessageTests - { - [TestMethod] - public void SendMessage() - { - const string content = "The simplest possible message."; - - var msg = Globals.Client.SendMessage( - Globals.Settings.ChannelId, - content); - - Assert.AreEqual(content, msg.Content); - } - - [TestMethod] - public void SendFile() - { - const string content = $"{nameof(SendFile)} attachment test."; - - var msg = Globals.Client.SendFile( - Globals.Settings.ChannelId, - Globals.FileNames.PoetryTxt, - content); - - Assert.AreEqual(1, msg.Attachments.Count); - Assert.IsTrue(msg.Attachments[0].ContentType.StartsWith(MediaTypeNames.Text.Plain)); - } - - [TestMethod] - public void SendMessagePropertiesWithAttachments() - { - var image1 = DiscordImageSource.FromStream( - File.OpenRead(Globals.FileNames.Image1), ImageFormat.Png); - var image2 = DiscordImageSource.FromStream( - File.OpenRead(Globals.FileNames.Image2), ImageFormat.Jpeg); - - var props = new MessageProperties - { - Content = $"{nameof(MessageProperties)} attachment test.", - Attachments = new List() - { - // Attach an arbitrary file. - new PartialDiscordAttachment(Globals.FileNames.Image1), - // Attach an existing DiscordImage. - new PartialDiscordAttachment(image2, Globals.FileNames.Image2), - // Attach an arbitrary file with custom specifications for everything. - new PartialDiscordAttachment( - new DiscordAttachmentFile(Encoding.UTF8.GetBytes(File.ReadAllText(Globals.FileNames.PoetryTxt))), - Globals.FileNames.PoetryTxt, - "Text File") - } - }; - - var msg = Globals.Client.SendMessage( - Globals.Settings.ChannelId, - props); - - Assert.AreEqual(props.Attachments.Count, msg.Attachments.Count); - - var file = msg.Attachments[0].GetAttachmentFile().Result; - var postedImage1 = file.IsImage() ? DiscordImageSource.FromFile(file) : null; - Assert.IsNotNull(postedImage1); - - CollectionAssert.AreEqual(postedImage1.PlatformImage.Bytes, image1.PlatformImage.Bytes); - } - } -} \ No newline at end of file diff --git a/Tests/appsettings.json b/Tests/appsettings.json deleted file mode 100644 index 08645fce..00000000 --- a/Tests/appsettings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "Token": "", // string The user token. - "ChannelId": 0 // ulong The channel the unit tests will post to. The user identified by "Token" must have access to this channel. -} \ No newline at end of file