Skip to content

Commit

Permalink
All tests running
Browse files Browse the repository at this point in the history
  • Loading branch information
mackmarton committed Oct 24, 2024
1 parent 7eae16c commit 82af5bd
Show file tree
Hide file tree
Showing 40 changed files with 1,467 additions and 1,307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@
</PropertyGroup>

<ItemGroup>
<None Remove="SampleCallbacks\branch_create.json" />
<None Remove="SampleCallbacks\comment_command.json" />
<None Remove="SampleCallbacks\comment_delete.json" />
<None Remove="SampleCallbacks\comment_edit.json" />
<None Remove="SampleCallbacks\pr_open.json" />
<None Remove="SampleCallbacks\pr_reviewcomment.json" />
<None Remove="SampleCallbacks\pr_reviewrequested.json" />
<None Remove="SampleCallbacks\workflow_run.json" />
<None Remove="SampleCallbacks\branch_create.json"/>
<None Remove="SampleCallbacks\comment_command.json"/>
<None Remove="SampleCallbacks\comment_delete.json"/>
<None Remove="SampleCallbacks\comment_edit.json"/>
<None Remove="SampleCallbacks\pr_open.json"/>
<None Remove="SampleCallbacks\pr_reviewcomment.json"/>
<None Remove="SampleCallbacks\pr_reviewrequested.json"/>
<None Remove="SampleCallbacks\workflow_run.json"/>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="SampleCallbacks\workflow_run.json" />
<EmbeddedResource Include="SampleCallbacks\branch_create.json" />
<EmbeddedResource Include="SampleCallbacks\comment_delete.json" />
<EmbeddedResource Include="SampleCallbacks\comment_command.json" />
<EmbeddedResource Include="SampleCallbacks\comment_edit.json" />
<EmbeddedResource Include="SampleCallbacks\pr_reviewcomment.json" />
<EmbeddedResource Include="SampleCallbacks\pr_reviewrequested.json" />
<EmbeddedResource Include="SampleCallbacks\pr_open.json" />
<EmbeddedResource Include="SampleCallbacks\workflow_run.json"/>
<EmbeddedResource Include="SampleCallbacks\branch_create.json"/>
<EmbeddedResource Include="SampleCallbacks\comment_delete.json"/>
<EmbeddedResource Include="SampleCallbacks\comment_command.json"/>
<EmbeddedResource Include="SampleCallbacks\comment_edit.json"/>
<EmbeddedResource Include="SampleCallbacks\pr_reviewcomment.json"/>
<EmbeddedResource Include="SampleCallbacks\pr_reviewrequested.json"/>
<EmbeddedResource Include="SampleCallbacks\pr_open.json"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0"/>
<PackageReference Include="Moq" Version="4.16.1"/>
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8"/>
<PackageReference Include="MSTest.TestFramework" Version="2.2.8"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ahk.GitHub.Monitor\Ahk.GitHub.Monitor.csproj" />
<ProjectReference Include="..\Ahk.GitHub.Monitor\Ahk.GitHub.Monitor.csproj"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests
namespace Ahk.GitHub.Monitor.Tests.IntegrationTests;

[TestClass]
public class AppStartupTest
{
[TestClass]
public class AppStartupTest
[TestMethod]
public async Task AppStartupSucceeds()
{
[TestMethod]
public async Task AppStartupSucceeds()
{
var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<Services.IGitHubClientFactory, GitHubClientFactory>();
})
.Build();
IHost host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<Services.IGitHubClientFactory, GitHubClientFactory>();
})
.Build();

await host.StartAsync();
await host.StartAsync();

await host.StopAsync();
}
await host.StopAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,53 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests
namespace Ahk.GitHub.Monitor.Tests.IntegrationTests;

[TestClass]
public class FunctionInvokeTest
{
[TestClass]
public class FunctionInvokeTest
[TestMethod]
public async Task NoAppConfigsReturnsError()
{
[TestMethod]
public async Task NoAppConfigsReturnsError()
{
var log = new Mock<ILogger<GitHubMonitorFunction>>();
var eds = new Mock<IEventDispatchService>();
var func = new GitHubMonitorFunction(eds.Object, Options.Create(new GitHubMonitorConfig()), log.Object);

var resp = await func.InvokeAndGetResponseAs<ObjectResult>(req => { });

Assert.AreEqual(StatusCodes.Status500InternalServerError, resp.StatusCode);
eds.Verify(s => s.Process(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<WebhookResult>(), NullLogger.Instance), Times.Never());
}

[TestMethod]
public async Task MissingGitHubEventHeaderReturnsError()
{
var eds = new Mock<IEventDispatchService>();
var func = FunctionBuilder.Create(eds.Object);

var resp = await func.InvokeAndGetResponseAs<ObjectResult>(req => req.Headers.Add("X-Hub-Signature-256", "dummy"));

Assert.AreEqual(StatusCodes.Status400BadRequest, resp.StatusCode);
eds.Verify(s => s.Process(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<WebhookResult>(), NullLogger.Instance), Times.Never());
}

[TestMethod]
public async Task MissingGitHubSignatureHeaderReturnsError()
{
var eds = new Mock<IEventDispatchService>();
var func = FunctionBuilder.Create(eds.Object);

var resp = await func.InvokeAndGetResponseAs<ObjectResult>(req => req.Headers.Add("X-GitHub-Event", "dummy"));

Assert.AreEqual(StatusCodes.Status400BadRequest, resp.StatusCode);
eds.Verify(s => s.Process(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<WebhookResult>(), NullLogger.Instance), Times.Never());
}
var log = new Mock<ILogger<GitHubMonitorFunction>>();
var eds = new Mock<IEventDispatchService>();
var func = new GitHubMonitorFunction(eds.Object, Options.Create(new GitHubMonitorConfig()), log.Object);

ObjectResult resp = await func.InvokeAndGetResponseAs<ObjectResult>(req => { });

Assert.AreEqual(StatusCodes.Status500InternalServerError, resp.StatusCode);
eds.Verify(
s => s.Process(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<WebhookResult>(), NullLogger.Instance),
Times.Never());
}

[TestMethod]
public async Task MissingGitHubEventHeaderReturnsError()
{
var eds = new Mock<IEventDispatchService>();
GitHubMonitorFunction func = FunctionBuilder.Create(eds.Object);

ObjectResult resp =
await func.InvokeAndGetResponseAs<ObjectResult>(req => req.Headers.Add("X-Hub-Signature-256", "dummy"));

Assert.AreEqual(StatusCodes.Status400BadRequest, resp.StatusCode);
eds.Verify(
s => s.Process(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<WebhookResult>(), NullLogger.Instance),
Times.Never());
}

[TestMethod]
public async Task MissingGitHubSignatureHeaderReturnsError()
{
var eds = new Mock<IEventDispatchService>();
GitHubMonitorFunction func = FunctionBuilder.Create(eds.Object);

ObjectResult resp =
await func.InvokeAndGetResponseAs<ObjectResult>(req => req.Headers.Add("X-GitHub-Event", "dummy"));

Assert.AreEqual(StatusCodes.Status400BadRequest, resp.StatusCode);
eds.Verify(
s => s.Process(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<WebhookResult>(), NullLogger.Instance),
Times.Never());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using Ahk.GitHub.Monitor.Services.EventDispatch;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests
namespace Ahk.GitHub.Monitor.Tests.IntegrationTests;

internal static class FunctionBuilder
{
internal static class FunctionBuilder
public static readonly GitHubMonitorConfig AppConfig = new()
{
public static readonly GitHubMonitorConfig AppConfig = new GitHubMonitorConfig()
{
GitHubAppId = "appid",
GitHubAppPrivateKey = "appprivatekey",
GitHubWebhookSecret = "webhooksecret",
};
GitHubAppId = "appid", GitHubAppPrivateKey = "appprivatekey", GitHubWebhookSecret = "webhooksecret"
};

public static GitHubMonitorFunction Create(IEventDispatchService dispatchService = null)
=> new GitHubMonitorFunction(dispatchService ?? new Mock<IEventDispatchService>().Object, Options.Create(AppConfig), new Mock<Microsoft.Extensions.Logging.ILogger<GitHubMonitorFunction>>().Object);
}
public static GitHubMonitorFunction Create(IEventDispatchService dispatchService = null)
=> new(dispatchService ?? new Mock<IEventDispatchService>().Object,
Options.Create(AppConfig),
new Mock<ILogger<GitHubMonitorFunction>>().Object);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,69 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests
namespace Ahk.GitHub.Monitor.Tests.IntegrationTests;

internal static class FunctionCallAssert
{
internal static class FunctionCallAssert
public static Task<IActionResult> Invoke(this GitHubMonitorFunction function,
Action<MockHttpRequestData> configureRequest)
{
public static Task<IActionResult> Invoke(this GitHubMonitorFunction function, Action<MockHttpRequestData> configureRequest)
{
// Create a mock function context (you might need to implement this based on your test framework)
var functionContext = new Mock<FunctionContext>(); // You may need to create or use a mocking framework
var request = new MockHttpRequestData(functionContext.Object, new MemoryStream());

// Configure the request (e.g., add headers, set body, etc.)
configureRequest(request);
// Create a mock function context (you might need to implement this based on your test framework)
var functionContext = new Mock<FunctionContext>(); // You may need to create or use a mocking framework
var request = new MockHttpRequestData(functionContext.Object, new MemoryStream());

// Invoke the function with the configured request and a null logger
return function.Run(request);
}
// Configure the request (e.g., add headers, set body, etc.)
configureRequest(request);

public static Task<IActionResult> Invoke2(this GitHubMonitorFunction function, SampleCallbackData data)
{
// Create a mock function context (you might need to implement this based on your test framework)
var functionContext = new Mock<FunctionContext>(); // You may need to create or use a mocking framework
var request = new MockHttpRequestData(functionContext.Object, new MemoryStream());
// Invoke the function with the configured request and a null logger
return function.Run(request);
}

// Configure the request (e.g., add headers, set body, etc.)
request = configureRequest(request, data);
public static Task<IActionResult> Invoke2(this GitHubMonitorFunction function, SampleCallbackData data)
{
// Create a mock function context (you might need to implement this based on your test framework)
var functionContext = new Mock<FunctionContext>(); // You may need to create or use a mocking framework
var request = new MockHttpRequestData(functionContext.Object, new MemoryStream());

// Invoke the function with the configured request and a null logger
return function.Run(request);
}
// Configure the request (e.g., add headers, set body, etc.)
request = configureRequest(request, data);

public static async Task<TResponse> InvokeAndGetResponseAs<TResponse>(this GitHubMonitorFunction function, Action<MockHttpRequestData> configureRequest)
where TResponse : IActionResult
{
var result = await function.Invoke(configureRequest);
Assert.IsInstanceOfType(result, typeof(TResponse));
return (TResponse)result;
}
// Invoke the function with the configured request and a null logger
return function.Run(request);
}

public static async Task<TResponse> InvokeWithContentAndGetResponseAs<TResponse>(this GitHubMonitorFunction function, SampleCallbackData data)
where TResponse : IActionResult
{
var result = await function.Invoke2(data);
Assert.IsInstanceOfType(result, typeof(TResponse));
return (TResponse)result;
}
public static async Task<TResponse> InvokeAndGetResponseAs<TResponse>(this GitHubMonitorFunction function,
Action<MockHttpRequestData> configureRequest)
where TResponse : IActionResult
{
IActionResult result = await function.Invoke(configureRequest);
Assert.IsInstanceOfType(result, typeof(TResponse));
return (TResponse)result;
}

private static MockHttpRequestData configureRequest(MockHttpRequestData req, SampleCallbackData data)
{
// Write the body to the request stream
var memStream = new MemoryStream();
using var writer = new StreamWriter(memStream, leaveOpen: true);
writer.Write(data.Body);
writer.Flush();
public static async Task<TResponse> InvokeWithContentAndGetResponseAs<TResponse>(
this GitHubMonitorFunction function, SampleCallbackData data)
where TResponse : IActionResult
{
IActionResult result = await function.Invoke2(data);
Assert.IsInstanceOfType(result, typeof(TResponse));
return (TResponse)result;
}

memStream.Position = 0;
req = new MockHttpRequestData(req.FunctionContext, memStream);
private static MockHttpRequestData configureRequest(MockHttpRequestData req, SampleCallbackData data)
{
// Write the body to the request stream
var memStream = new MemoryStream();
using var writer = new StreamWriter(memStream, leaveOpen: true);
writer.Write(data.Body);
writer.Flush();

// Add headers
req.Headers.Add("X-GitHub-Event", data.EventName);
req.Headers.Add("X-Hub-Signature-256", data.Signature);
return req;
}
memStream.Position = 0;
req = new MockHttpRequestData(req.FunctionContext, memStream);

// Add headers
req.Headers.Add("X-GitHub-Event", data.EventName);
req.Headers.Add("X-Hub-Signature-256", data.Signature);
return req;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class MockHttpRequestData : HttpRequestData

public MockHttpRequestData(FunctionContext functionContext, Stream body) : base(functionContext)
{
Body = body;
Headers = new HttpHeadersCollection();
this.Body = body;
this.Headers = new HttpHeadersCollection();
_response = new MockHttpResponseData(functionContext);
}

Expand All @@ -24,7 +24,7 @@ public MockHttpRequestData(FunctionContext functionContext, Stream body) : base(

public override IReadOnlyCollection<IHttpCookie> Cookies { get; } = new List<IHttpCookie>();

public override Uri Url { get; } = new Uri("https://localhost");
public override Uri Url { get; } = new("https://localhost");

public override IEnumerable<ClaimsIdentity> Identities { get; } = new List<ClaimsIdentity>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class MockHttpResponseData : HttpResponseData
{
public MockHttpResponseData(FunctionContext functionContext) : base(functionContext)
{
Headers = new HttpHeadersCollection();
Body = new MemoryStream();
this.Headers = new HttpHeadersCollection();
this.Body = new MemoryStream();
}

public override HttpStatusCode StatusCode { get; set; }
Expand Down
Loading

0 comments on commit 82af5bd

Please sign in to comment.