Skip to content

Commit

Permalink
Update to xunit v3
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Dec 25, 2024
1 parent 01dd27f commit 07394eb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Tingle.EventBus;
using Tingle.EventBus.Transports.InMemory;
using Xunit;
using Xunit.Abstractions;

namespace Tingle.Dependabot.Tests.PeriodicTasks;

Expand All @@ -28,10 +27,11 @@ public async Task CheckAsync_MissedScheduleIsDetected()
var lastUpdate1 = DateTimeOffset.Parse("2023-01-23T03:30:00+00:00");
await TestAsync(lastUpdate0, lastUpdate1, async (harness, context, pt) =>
{
await pt.CheckAsync(referencePoint);
await pt.CheckAsync(referencePoint, TestContext.Current.CancellationToken);

// Ensure the message was published
var evt_context = Assert.IsType<EventContext<TriggerUpdateJobsEvent>>(Assert.Single(await harness.PublishedAsync()));
var evt_context = Assert.IsType<EventContext<TriggerUpdateJobsEvent>>(
Assert.Single(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken)));
var inner = evt_context.Event;
Assert.NotNull(inner);
Assert.Equal(RepositoryId, inner.RepositoryId);
Expand All @@ -48,10 +48,11 @@ public async Task CheckAsync_MissedScheduleIsDetected_NotRun_Before()
var lastUpdate1 = (DateTimeOffset?)null;
await TestAsync(lastUpdate0, lastUpdate1, async (harness, context, pt) =>
{
await pt.CheckAsync(referencePoint);
await pt.CheckAsync(referencePoint, TestContext.Current.CancellationToken);

// Ensure the message was published
var evt_context = Assert.IsType<EventContext<TriggerUpdateJobsEvent>>(Assert.Single(await harness.PublishedAsync()));
var evt_context = Assert.IsType<EventContext<TriggerUpdateJobsEvent>>(
Assert.Single(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken)));
var inner = evt_context.Event;
Assert.NotNull(inner);
Assert.Equal(RepositoryId, inner.RepositoryId);
Expand All @@ -68,10 +69,10 @@ public async Task CheckAsync_NoMissedSchedule()
var lastUpdate1 = DateTimeOffset.Parse("2023-01-24T03:30:00+00:00");
await TestAsync(lastUpdate0, lastUpdate1, async (harness, context, pt) =>
{
await pt.CheckAsync(referencePoint);
await pt.CheckAsync(referencePoint, TestContext.Current.CancellationToken);

// Ensure nothing was published
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Tingle.EventBus;
using Tingle.EventBus.Transports.InMemory;
using Xunit;
using Xunit.Abstractions;

namespace Tingle.Dependabot.Tests.PeriodicTasks;

Expand All @@ -22,10 +21,11 @@ public async Task SynchronizationInnerAsync_Works()
{
await TestAsync(async (harness, pt) =>
{
await pt.SyncAsync();
await pt.SyncAsync(TestContext.Current.CancellationToken);

// Ensure the message was published
var evt_context = Assert.IsType<EventContext<ProcessSynchronization>>(Assert.Single(await harness.PublishedAsync()));
var evt_context = Assert.IsType<EventContext<ProcessSynchronization>>(
Assert.Single(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken)));
var inner = evt_context.Event;
Assert.NotNull(inner);
Assert.Null(inner.RepositoryId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Tingle.EventBus;
using Tingle.EventBus.Transports.InMemory;
using Xunit;
using Xunit.Abstractions;

namespace Tingle.Dependabot.Tests.PeriodicTasks;

Expand Down Expand Up @@ -38,7 +37,7 @@ await context.UpdateJobs.AddAsync(new UpdateJob
Resources = new(0.25, 0.2),
AuthKey = Guid.NewGuid().ToString("n"),
Status = UpdateJobStatus.Succeeded,
});
}, TestContext.Current.CancellationToken);
await context.UpdateJobs.AddAsync(new UpdateJob
{
Id = Guid.NewGuid().ToString(),
Expand All @@ -52,7 +51,7 @@ await context.UpdateJobs.AddAsync(new UpdateJob
Resources = new(0.25, 0.2),
AuthKey = Guid.NewGuid().ToString("n"),
Status = UpdateJobStatus.Succeeded,
});
}, TestContext.Current.CancellationToken);
await context.UpdateJobs.AddAsync(new UpdateJob
{
Id = targetId,
Expand All @@ -66,13 +65,14 @@ await context.UpdateJobs.AddAsync(new UpdateJob
Resources = new(0.25, 0.2),
AuthKey = Guid.NewGuid().ToString("n"),
Status = UpdateJobStatus.Running,
});
await context.SaveChangesAsync();
}, TestContext.Current.CancellationToken);
await context.SaveChangesAsync(TestContext.Current.CancellationToken);

await pt.CleanupAsync();
await pt.CleanupAsync(TestContext.Current.CancellationToken);

// Ensure the message was published
var evt_context = Assert.IsType<EventContext<UpdateJobCheckStateEvent>>(Assert.Single(await harness.PublishedAsync()));
var evt_context = Assert.IsType<EventContext<UpdateJobCheckStateEvent>>(
Assert.Single(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken)));
var inner = evt_context.Event;
Assert.NotNull(inner);
Assert.Equal(targetId, inner.JobId);
Expand All @@ -96,7 +96,7 @@ await context.UpdateJobs.AddAsync(new UpdateJob
Directories = null,
Resources = new(0.25, 0.2),
AuthKey = Guid.NewGuid().ToString("n"),
});
}, TestContext.Current.CancellationToken);
await context.UpdateJobs.AddAsync(new UpdateJob
{
Id = Guid.NewGuid().ToString(),
Expand All @@ -109,7 +109,7 @@ await context.UpdateJobs.AddAsync(new UpdateJob
Directories = null,
Resources = new(0.25, 0.2),
AuthKey = Guid.NewGuid().ToString("n"),
});
}, TestContext.Current.CancellationToken);
await context.UpdateJobs.AddAsync(new UpdateJob
{
Id = Guid.NewGuid().ToString(),
Expand All @@ -122,11 +122,11 @@ await context.UpdateJobs.AddAsync(new UpdateJob
Directories = ["**/*"],
Resources = new(0.25, 0.2),
AuthKey = Guid.NewGuid().ToString("n"),
});
await context.SaveChangesAsync();
}, TestContext.Current.CancellationToken);
await context.SaveChangesAsync(TestContext.Current.CancellationToken);

await pt.CleanupAsync();
Assert.Equal(1, await context.UpdateJobs.CountAsync());
await pt.CleanupAsync(TestContext.Current.CancellationToken);
Assert.Equal(1, await context.UpdateJobs.CountAsync(TestContext.Current.CancellationToken));
});
}

Expand Down
6 changes: 3 additions & 3 deletions server/Tingle.Dependabot.Tests/Tingle.Dependabot.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="All" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.4.0" />
<PackageReference Include="MartinCostello.Logging.XUnit.v3" Version="0.5.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="All" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0" PrivateAssets="All" />
<PackageReference Include="xunit.v3" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Tingle.EventBus;
using Tingle.EventBus.Transports.InMemory;
using Xunit;
using Xunit.Abstractions;

namespace Tingle.Dependabot.Tests;

Expand All @@ -28,18 +27,18 @@ await TestAsync(async (harness, client) =>
{
// without Authorization header
var request = new HttpRequestMessage(HttpMethod.Post, "/webhooks/azure");
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Empty(await response.Content.ReadAsStringAsync());
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));

// password does not match what is on record
request = new HttpRequestMessage(HttpMethod.Post, "/webhooks/azure");
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump5")));
response = await client.SendAsync(request);
response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Empty(await response.Content.ReadAsStringAsync());
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand All @@ -51,14 +50,14 @@ await TestAsync(async (harness, client) =>
var request = new HttpRequestMessage(HttpMethod.Post, "/webhooks/azure");
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump")));
request.Content = new StringContent("", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.Contains("\"type\":\"https://tools.ietf.org/html/rfc9110#section-15.5.1\"", body);
Assert.Contains("\"title\":\"One or more validation errors occurred.\"", body);
Assert.Contains("\"status\":400", body);
Assert.Contains("\"errors\":{\"\":[\"A non-empty request body is required.\"],\"model\":[\"The model field is required.\"]}", body);
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand All @@ -70,16 +69,16 @@ await TestAsync(async (harness, client) =>
var request = new HttpRequestMessage(HttpMethod.Post, "/webhooks/azure");
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump")));
request.Content = new StringContent("{}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.Contains("\"type\":\"https://tools.ietf.org/html/rfc9110#section-15.5.1\"", body);
Assert.Contains("\"title\":\"One or more validation errors occurred.\"", body);
Assert.Contains("\"status\":400", body);
Assert.Contains("\"SubscriptionId\":[\"The SubscriptionId field is required.\"]", body);
Assert.Contains("\"EventType\":[\"The EventType field is required.\"]", body);
Assert.Contains("\"Resource\":[\"The Resource field is required.\"]", body);
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand All @@ -92,13 +91,13 @@ await TestAsync(async (harness, client) =>
var request = new HttpRequestMessage(HttpMethod.Post, "/webhooks/azure");
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump")));
request.Content = new StreamContent(stream);
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.UnsupportedMediaType, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.Contains("\"type\":\"https://tools.ietf.org/html/rfc9110#section-15.5.16\"", body);
Assert.Contains("\"title\":\"Unsupported Media Type\"", body);
Assert.Contains("\"status\":415", body);
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand All @@ -112,12 +111,13 @@ await TestAsync(async (harness, client) =>
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump")));
request.Content = new StreamContent(stream);
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json", "utf-8");
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(await response.Content.ReadAsStringAsync());
Assert.Empty(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));

// Ensure the message was published
var context = Assert.IsType<EventContext<ProcessSynchronization>>(Assert.Single(await harness.PublishedAsync(TimeSpan.FromSeconds(1f))));
var context = Assert.IsType<EventContext<ProcessSynchronization>>(
Assert.Single(await harness.PublishedAsync(TimeSpan.FromSeconds(1f), TestContext.Current.CancellationToken)));
var inner = context.Event;
Assert.NotNull(inner);
Assert.Null(inner.RepositoryId);
Expand All @@ -136,10 +136,10 @@ await TestAsync(async (harness, client) =>
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump")));
request.Content = new StreamContent(stream);
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json", "utf-8");
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(await response.Content.ReadAsStringAsync());
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand All @@ -153,10 +153,10 @@ await TestAsync(async (harness, client) =>
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump")));
request.Content = new StreamContent(stream);
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json", "utf-8");
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(await response.Content.ReadAsStringAsync());
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand All @@ -170,10 +170,10 @@ await TestAsync(async (harness, client) =>
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ProjectId}:burp-bump")));
request.Content = new StreamContent(stream);
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json", "utf-8");
var response = await client.SendAsync(request);
var response = await client.SendAsync(request, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(await response.Content.ReadAsStringAsync());
Assert.Empty(await harness.PublishedAsync());
Assert.Empty(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
Assert.Empty(await harness.PublishedAsync(cancellationToken: TestContext.Current.CancellationToken));
});
}

Expand Down

0 comments on commit 07394eb

Please sign in to comment.