Skip to content

Commit

Permalink
test actualisation
Browse files Browse the repository at this point in the history
  • Loading branch information
mackmarton committed Aug 28, 2024
1 parent 82c218b commit f80c541
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests
{
Expand All @@ -10,11 +12,20 @@ public class AppStartupTest
[TestMethod]
public async Task AppStartupSucceeds()
{
var startup = new Startup();
using var host = new HostBuilder()
.ConfigureWebJobs(startup.Configure)
.Build();
var host = new HostBuilder()
.ConfigureServices(services =>
{
// Register any services that are needed for the test
services.AddSingleton<Services.IGitHubClientFactory, Services.GitHubClientFactory>();
// Add more service registrations as needed
})
.Build();

await host.StartAsync();

// Additional assertions or operations can be performed here

await host.StopAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -14,8 +15,9 @@ public class FunctionInvokeTest
[TestMethod]
public async Task NoAppConfigsReturnsError()
{
var log = new Mock<ILogger<GitHubMonitorFunction>>();
var eds = new Mock<Services.IEventDispatchService>();
var func = new GitHubMonitorFunction(eds.Object, Options.Create(new GitHubMonitorConfig()));
var func = new GitHubMonitorFunction(eds.Object, Options.Create(new GitHubMonitorConfig()), log.Object);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ internal static class FunctionBuilder
};

public static GitHubMonitorFunction Create(Services.IEventDispatchService dispatchService = null)
=> new GitHubMonitorFunction(dispatchService ?? new Mock<Services.IEventDispatchService>().Object, Options.Create(AppConfig));
=> new GitHubMonitorFunction(dispatchService ?? new Mock<Services.IEventDispatchService>().Object, Options.Create(AppConfig), new Mock<Microsoft.Extensions.Logging.ILogger<GitHubMonitorFunction>>().Object);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests
{
internal static class FunctionCallAssert
{
public static Task<IActionResult> Invoke(this GitHubMonitorFunction function, Action<HttpRequest> configureRequest)
public static Task<IActionResult> Invoke(this GitHubMonitorFunction function, Action<MockHttpRequestData> configureRequest)
{
var req = new DefaultHttpRequest(new DefaultHttpContext());
configureRequest(req);
// 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());

return function.Run(req, Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance);
// Configure the request (e.g., add headers, set body, etc.)
configureRequest(request);

// Invoke the function with the configured request and a null logger
return function.Run(request);
}

public static async Task<TResponse> InvokeAndGetResponseAs<TResponse>(this GitHubMonitorFunction function, Action<HttpRequest> configureRequest)
public static async Task<TResponse> InvokeAndGetResponseAs<TResponse>(this GitHubMonitorFunction function, Action<MockHttpRequestData> configureRequest)
where TResponse : IActionResult
{
var result = await function.Invoke(configureRequest);
Expand All @@ -33,18 +41,21 @@ public static async Task<TResponse> InvokeWithContentAndGetResponseAs<TResponse>
return (TResponse)result;
}

private static void configureRequest(HttpRequest req, SampleCallbackData data)
private static void configureRequest(MockHttpRequestData req, SampleCallbackData data)
{
// Add headers
req.Headers.Add("X-GitHub-Event", data.EventName);
req.Headers.Add("X-Hub-Signature-256", data.Signature);

var memStream = new System.IO.MemoryStream();
using var writer = new System.IO.StreamWriter(memStream, leaveOpen: true);
// 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();

memStream.Position = 0;
req.Body = memStream;
req = new MockHttpRequestData(req.FunctionContext, memStream);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Claims;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests;

public class MockHttpRequestData : HttpRequestData
{
private readonly HttpResponseData _response;

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

public override Stream Body { get; }

public override HttpHeadersCollection Headers { get; }

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

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

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

public override string Method { get; } = "POST";

public override HttpResponseData CreateResponse() => _response;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace Ahk.GitHub.Monitor.Tests.IntegrationTests;

public class MockHttpResponseData : HttpResponseData
{
public MockHttpResponseData(FunctionContext functionContext) : base(functionContext)
{
Headers = new HttpHeadersCollection();
Body = new MemoryStream();
}

public override HttpStatusCode StatusCode { get; set; }

public override HttpHeadersCollection Headers { get; set; }

public override Stream Body { get; set; }
public override HttpCookies Cookies { get; }
}

0 comments on commit f80c541

Please sign in to comment.