tests: more tests
This commit is contained in:
@@ -67,7 +67,7 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
const string ytCallback = "yt-callback";
|
||||
|
||||
app.MapGet(ytCallback, VerifySubscriptionHandler.HandleAsync);
|
||||
app.MapGet(ytCallback, VerifySubscriptionHandler.Handle);
|
||||
app.MapPost(ytCallback, NotificationHandler.HandleAsync);
|
||||
|
||||
app.Run();
|
||||
@@ -2,7 +2,7 @@ namespace StevesBot.Webhook.YouTube.Handlers;
|
||||
|
||||
internal static class VerifySubscriptionHandler
|
||||
{
|
||||
public static IResult HandleAsync(
|
||||
public static IResult Handle(
|
||||
[FromQuery(Name = "hub.mode")] string mode,
|
||||
[FromQuery(Name = "hub.topic")] string topic,
|
||||
[FromQuery(Name = "hub.reason")] string? reason,
|
||||
@@ -19,7 +19,7 @@ internal static class VerifySubscriptionHandler
|
||||
return Results.BadRequest("Subscription denied");
|
||||
}
|
||||
|
||||
if (topic != subOptions.Value.TopicUrl)
|
||||
if (string.Equals(topic, subOptions.Value.TopicUrl, StringComparison.OrdinalIgnoreCase) is false)
|
||||
{
|
||||
logger.LogInformation("Received verification request for wrong topic: {Topic}", topic);
|
||||
return Results.NotFound();
|
||||
@@ -34,7 +34,7 @@ internal static class VerifySubscriptionHandler
|
||||
leaseSeconds
|
||||
);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(leaseSeconds) || !long.TryParse(leaseSeconds, out var parsedSeconds))
|
||||
if (string.IsNullOrWhiteSpace(leaseSeconds) || long.TryParse(leaseSeconds, out var parsedSeconds) is false)
|
||||
{
|
||||
logger.LogWarning("Invalid or missing lease_seconds parameter: {LeaseSeconds}", leaseSeconds);
|
||||
return Results.BadRequest("Invalid lease_seconds parameter");
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using StevesBot.Library.Discord.Rest;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace StevesBot.Webhook.Tests.Unit;
|
||||
|
||||
public class VerifySubscriptionHandlerTests
|
||||
{
|
||||
private readonly Mock<IOptions<SubscriptionOptions>> _mockSubOptions = new();
|
||||
private readonly Mock<ILogger<Program>> _mockLogger = new();
|
||||
private readonly ConcurrentQueue<SubscribeTask> _subscriptionQueue = new();
|
||||
|
||||
[Fact]
|
||||
public void Handle_WhenModeIsDenied_ItShouldReturnBadRequest()
|
||||
{
|
||||
var result = Handle("denied", "topic");
|
||||
|
||||
result.Should().BeOfType<BadRequest<string>>();
|
||||
}
|
||||
|
||||
private IResult Handle(string mode, string topic, string? reason = null, string? challenge = null, string? leaseSeconds = null)
|
||||
{
|
||||
return VerifySubscriptionHandler.Handle(
|
||||
mode,
|
||||
topic,
|
||||
reason,
|
||||
challenge,
|
||||
leaseSeconds,
|
||||
_mockSubOptions.Object,
|
||||
_mockLogger.Object,
|
||||
_subscriptionQueue
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,19 @@
|
||||
global using System.Collections.Concurrent;
|
||||
global using System.Net;
|
||||
global using System.Text;
|
||||
|
||||
global using Microsoft.AspNetCore.Http;
|
||||
global using Microsoft.AspNetCore.Http.HttpResults;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
global using Microsoft.Extensions.Options;
|
||||
|
||||
global using Moq;
|
||||
|
||||
global using RichardSzalay.MockHttp;
|
||||
|
||||
global using StevesBot.Library.Discord.Rest;
|
||||
global using StevesBot.Library.Discord.Rest.Requests;
|
||||
global using StevesBot.Webhook.YouTube;
|
||||
global using StevesBot.Webhook.YouTube.Data;
|
||||
global using StevesBot.Webhook.YouTube.Tasks;
|
||||
global using StevesBot.Webhook.YouTube.Handlers;
|
||||
global using StevesBot.Webhook.YouTube.Tasks;
|
||||
Reference in New Issue
Block a user