diff --git a/Octokit.Webhooks.sln b/Octokit.Webhooks.sln index c752a5e1..cba9cfe3 100644 --- a/Octokit.Webhooks.sln +++ b/Octokit.Webhooks.sln @@ -64,6 +64,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore", "samples\AspNe EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{174C8B3B-E8D3-4845-AE7C-8C0DEC43354F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Octokit.Webhooks.TestUtils", "test\Octokit.Webhooks.TestUtils\Octokit.Webhooks.TestUtils.csproj", "{48EB4752-1A23-4DD2-AF86-21ED12475F8B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -86,6 +88,10 @@ Global {2EE1581B-E8B6-409E-8879-6F1EFC8C682B}.Debug|Any CPU.Build.0 = Debug|Any CPU {2EE1581B-E8B6-409E-8879-6F1EFC8C682B}.Release|Any CPU.ActiveCfg = Release|Any CPU {2EE1581B-E8B6-409E-8879-6F1EFC8C682B}.Release|Any CPU.Build.0 = Release|Any CPU + {48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48EB4752-1A23-4DD2-AF86-21ED12475F8B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -98,6 +104,7 @@ Global {E46C1DB5-F21C-46A9-8D17-F08278BC0BF8} = {719809C2-A551-4C4A-9EFD-B10FB5E35BC0} {566DF0E2-1288-4083-9B55-4C8B69BB1432} = {EFE1E5ED-D337-4874-82EC-D9FA0BC7D3AB} {2EE1581B-E8B6-409E-8879-6F1EFC8C682B} = {174C8B3B-E8D3-4845-AE7C-8C0DEC43354F} + {48EB4752-1A23-4DD2-AF86-21ED12475F8B} = {E1B24F25-B8A4-46EE-B7EB-7803DCFC543F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {73F36209-F8D6-4066-8951-D97729F773CF} diff --git a/test/Directory.Build.props b/test/Directory.Build.props index 40b4c1ff..eab4674e 100644 --- a/test/Directory.Build.props +++ b/test/Directory.Build.props @@ -7,6 +7,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/Octokit.Webhooks.Test/Octokit.Webhooks.Test.csproj b/test/Octokit.Webhooks.Test/Octokit.Webhooks.Test.csproj index e5d716dd..6fd65af5 100644 --- a/test/Octokit.Webhooks.Test/Octokit.Webhooks.Test.csproj +++ b/test/Octokit.Webhooks.Test/Octokit.Webhooks.Test.csproj @@ -6,10 +6,7 @@ - - - - + diff --git a/test/Octokit.Webhooks.Test/TestWebhookEventProcessor.cs b/test/Octokit.Webhooks.Test/TestWebhookEventProcessor.cs new file mode 100644 index 00000000..73ec936d --- /dev/null +++ b/test/Octokit.Webhooks.Test/TestWebhookEventProcessor.cs @@ -0,0 +1,6 @@ +namespace Octokit.Webhooks.Test +{ + public class TestWebhookEventProcessor : WebhookEventProcessor + { + } +} diff --git a/test/Octokit.Webhooks.Test/WebhookEventProcessorTests.cs b/test/Octokit.Webhooks.Test/WebhookEventProcessorTests.cs new file mode 100644 index 00000000..e8a68d04 --- /dev/null +++ b/test/Octokit.Webhooks.Test/WebhookEventProcessorTests.cs @@ -0,0 +1,22 @@ +namespace Octokit.Webhooks.Test +{ + using System; + using FluentAssertions; + using Xunit; + + public class WebhookEventProcessorTests + { + private readonly WebhookEventProcessor webhookEventProcessor; + + public WebhookEventProcessorTests() => this.webhookEventProcessor = new TestWebhookEventProcessor(); + + [Theory] + [ClassData(typeof(WebhookEventProcessorTestsData))] + public void CanDeserialize(WebhookHeaders headers, string payload, Type expectedType) + { + var result = this.webhookEventProcessor.DeserializeWebhookEvent(headers, payload); + + result.Should().BeAssignableTo(expectedType); + } + } +} diff --git a/test/Octokit.Webhooks.Test/WebhookEventProcessorTestsData.cs b/test/Octokit.Webhooks.Test/WebhookEventProcessorTestsData.cs new file mode 100644 index 00000000..93c49d8a --- /dev/null +++ b/test/Octokit.Webhooks.Test/WebhookEventProcessorTestsData.cs @@ -0,0 +1,36 @@ +namespace Octokit.Webhooks.Test +{ + using System.Collections; + using System.Collections.Generic; + using System.IO; + using CaseExtensions; + using Octokit.Webhooks.TestUtils; + + public class WebhookEventProcessorTestsData : IEnumerable + { + public IEnumerator GetEnumerator() + { + var resourcesDirectory = ResourceUtils.GetResources(); + var files = Directory.GetFiles(resourcesDirectory, "*.json", SearchOption.AllDirectories); + foreach (var file in files) + { + var relativeResource = file.Replace($"{resourcesDirectory}{Path.DirectorySeparatorChar}", string.Empty); + var parts = relativeResource.Split(Path.DirectorySeparatorChar); + var headers = new WebhookHeaders + { + Event = parts[0], + }; + var expectedType = ClassUtils.GetEventTypeByName(parts[0].ToPascalCase()); + var content = ResourceUtils.ReadResource(relativeResource); + yield return new object[] + { + headers, + content, + expectedType, + }; + } + } + + IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); + } +} diff --git a/test/Octokit.Webhooks.TestUtils/ClassUtils.cs b/test/Octokit.Webhooks.TestUtils/ClassUtils.cs new file mode 100644 index 00000000..2acc79ee --- /dev/null +++ b/test/Octokit.Webhooks.TestUtils/ClassUtils.cs @@ -0,0 +1,15 @@ +namespace Octokit.Webhooks.TestUtils +{ + using System; + using System.Linq; + using System.Reflection; + + public static class ClassUtils + { + public static Type GetEventTypeByName(string name) => Assembly.Load("Octokit.Webhooks") + .GetTypes() + .Single(t => + t.FullName!.StartsWith("Octokit.Webhooks.Events.", StringComparison.InvariantCulture) && + t.Name == $"{name}Event"); + } +} diff --git a/test/Octokit.Webhooks.TestUtils/Octokit.Webhooks.TestUtils.csproj b/test/Octokit.Webhooks.TestUtils/Octokit.Webhooks.TestUtils.csproj new file mode 100644 index 00000000..e4047aa1 --- /dev/null +++ b/test/Octokit.Webhooks.TestUtils/Octokit.Webhooks.TestUtils.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/test/Octokit.Webhooks.TestUtils/ResourceUtils.cs b/test/Octokit.Webhooks.TestUtils/ResourceUtils.cs new file mode 100644 index 00000000..2616e83e --- /dev/null +++ b/test/Octokit.Webhooks.TestUtils/ResourceUtils.cs @@ -0,0 +1,21 @@ +namespace Octokit.Webhooks.TestUtils +{ + using System; + using System.IO; + using System.Reflection; + + public static class ResourceUtils + { + public static string GetResources() + { + var baseUrl = new Uri(Assembly.GetExecutingAssembly().Location); + var basePath = Uri.UnescapeDataString(baseUrl.AbsolutePath); + var dirPath = Path.GetDirectoryName(basePath); + return Path.Combine(dirPath!, "Resources"); + } + + public static string ReadResource(string relativePath) => File.ReadAllText(GetResourcePath(relativePath)); + + private static string GetResourcePath(string relativePath) => Path.Combine(GetResources(), relativePath); + } +}