|
| 1 | +using AniWorldReminder_API.Interfaces; |
| 2 | +using AniWorldReminder_API.Services; |
| 3 | +using Microsoft.Extensions.Logging.Abstractions; |
| 4 | +using System.Net; |
| 5 | +using Telegram.Bot.Types; |
| 6 | +using Telegram.Bot.Types.Enums; |
| 7 | +using Telegram.Bot.Types.ReplyMarkups; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace AniWorldReminder_API.Tests.Services |
| 11 | +{ |
| 12 | + public class AdminHttpFailureNotificationHandlerTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public async Task SendAsync_SendsAdminTelegramMessage_OnTransientHttpFailureResponse() |
| 16 | + { |
| 17 | + using CurrentDirectoryScope _ = CurrentDirectoryScope.CreateWithSettingsFile(adminChat: "123456"); |
| 18 | + FakeTelegramBotService telegramBotService = new(); |
| 19 | + using HttpMessageInvoker invoker = CreateInvoker( |
| 20 | + telegramBotService, |
| 21 | + new StubHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.InternalServerError))); |
| 22 | + |
| 23 | + HttpResponseMessage response = await invoker.SendAsync( |
| 24 | + new HttpRequestMessage(HttpMethod.Get, "https://example.com/episodes"), |
| 25 | + CancellationToken.None); |
| 26 | + |
| 27 | + Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); |
| 28 | + Assert.Single(telegramBotService.Messages); |
| 29 | + Assert.Equal(123456L, telegramBotService.Messages[0].ChatId); |
| 30 | + Assert.Contains("HTTP request failed after all retries", telegramBotService.Messages[0].Text); |
| 31 | + Assert.Contains("GET https://example.com/episodes", telegramBotService.Messages[0].Text); |
| 32 | + Assert.Contains("HTTP 500 InternalServerError", telegramBotService.Messages[0].Text); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public async Task SendAsync_DoesNotSendAdminTelegramMessage_OnSuccessfulResponse() |
| 37 | + { |
| 38 | + using CurrentDirectoryScope _ = CurrentDirectoryScope.CreateWithSettingsFile(adminChat: "123456"); |
| 39 | + FakeTelegramBotService telegramBotService = new(); |
| 40 | + using HttpMessageInvoker invoker = CreateInvoker( |
| 41 | + telegramBotService, |
| 42 | + new StubHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.OK))); |
| 43 | + |
| 44 | + HttpResponseMessage response = await invoker.SendAsync( |
| 45 | + new HttpRequestMessage(HttpMethod.Get, "https://example.com/episodes"), |
| 46 | + CancellationToken.None); |
| 47 | + |
| 48 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 49 | + Assert.Empty(telegramBotService.Messages); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task SendAsync_SendsAdminTelegramMessage_OnTransientHttpException() |
| 54 | + { |
| 55 | + using CurrentDirectoryScope _ = CurrentDirectoryScope.CreateWithSettingsFile(adminChat: "123456"); |
| 56 | + FakeTelegramBotService telegramBotService = new(); |
| 57 | + using HttpMessageInvoker invoker = CreateInvoker( |
| 58 | + telegramBotService, |
| 59 | + new StubHttpMessageHandler(_ => throw new HttpRequestException("network down"))); |
| 60 | + |
| 61 | + HttpRequestException exception = await Assert.ThrowsAsync<HttpRequestException>(() => invoker.SendAsync( |
| 62 | + new HttpRequestMessage(HttpMethod.Get, "https://example.com/episodes"), |
| 63 | + CancellationToken.None)); |
| 64 | + |
| 65 | + Assert.Equal("network down", exception.Message); |
| 66 | + Assert.Single(telegramBotService.Messages); |
| 67 | + Assert.Contains("network down", telegramBotService.Messages[0].Text); |
| 68 | + } |
| 69 | + |
| 70 | + private static HttpMessageInvoker CreateInvoker(ITelegramBotService telegramBotService, HttpMessageHandler innerHandler) |
| 71 | + { |
| 72 | + AdminHttpFailureNotificationHandler handler = new(NullLogger<AdminHttpFailureNotificationHandler>.Instance, telegramBotService) |
| 73 | + { |
| 74 | + InnerHandler = innerHandler |
| 75 | + }; |
| 76 | + |
| 77 | + return new HttpMessageInvoker(handler); |
| 78 | + } |
| 79 | + |
| 80 | + private sealed record SentTelegramMessage(long ChatId, string Text); |
| 81 | + |
| 82 | + private sealed class FakeTelegramBotService : ITelegramBotService |
| 83 | + { |
| 84 | + public List<SentTelegramMessage> Messages { get; } = []; |
| 85 | + |
| 86 | + public Task<bool> Init() |
| 87 | + { |
| 88 | + return Task.FromResult(true); |
| 89 | + } |
| 90 | + |
| 91 | + public Task SendChatAction(long chatId, ChatAction chatAction) |
| 92 | + { |
| 93 | + return Task.CompletedTask; |
| 94 | + } |
| 95 | + |
| 96 | + public Task<Message?> SendMessageAsync(long chatId, string text, int? replyId = null, bool showLinkPreview = true, ParseMode parseMode = ParseMode.Html, bool silentMessage = false, ReplyKeyboardMarkup? rkm = null) |
| 97 | + { |
| 98 | + Messages.Add(new SentTelegramMessage(chatId, text)); |
| 99 | + return Task.FromResult<Message?>(null); |
| 100 | + } |
| 101 | + |
| 102 | + public Task<Message?> SendPhotoAsync(long chatId, string photoUrl, string? text = null, ParseMode parseMode = ParseMode.Html, bool silentMessage = false) |
| 103 | + { |
| 104 | + return Task.FromResult<Message?>(null); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private sealed class StubHttpMessageHandler(Func<HttpRequestMessage, HttpResponseMessage> responseFactory) : HttpMessageHandler |
| 109 | + { |
| 110 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 111 | + { |
| 112 | + return Task.FromResult(responseFactory(request)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private sealed class CurrentDirectoryScope : IDisposable |
| 117 | + { |
| 118 | + private readonly string originalDirectory; |
| 119 | + private readonly string tempDirectory; |
| 120 | + |
| 121 | + private CurrentDirectoryScope(string originalDirectory, string tempDirectory) |
| 122 | + { |
| 123 | + this.originalDirectory = originalDirectory; |
| 124 | + this.tempDirectory = tempDirectory; |
| 125 | + } |
| 126 | + |
| 127 | + public static CurrentDirectoryScope CreateWithSettingsFile(string adminChat) |
| 128 | + { |
| 129 | + string originalDirectory = Directory.GetCurrentDirectory(); |
| 130 | + string tempDirectory = Path.Combine(Path.GetTempPath(), $"AniWorldReminder_API.Tests.{Guid.NewGuid():N}"); |
| 131 | + Directory.CreateDirectory(tempDirectory); |
| 132 | + |
| 133 | + string settingsJson = |
| 134 | + $$""" |
| 135 | + { |
| 136 | + "TelegramBot": { |
| 137 | + "Token": "test-token", |
| 138 | + "AdminChat": "{{adminChat}}" |
| 139 | + }, |
| 140 | + "Database": { |
| 141 | + "IP": "127.0.0.1", |
| 142 | + "Database": "test", |
| 143 | + "Username": "test", |
| 144 | + "Password": "test" |
| 145 | + }, |
| 146 | + "Proxy": { |
| 147 | + "URI": "", |
| 148 | + "Username": "", |
| 149 | + "Password": "" |
| 150 | + }, |
| 151 | + "AppSettings": { |
| 152 | + "AddSwagger": false, |
| 153 | + "EnableEpisodeReminderJob": true, |
| 154 | + "EpisodeReminderCron": "*/15 * * * *", |
| 155 | + "EnableHangfireDashboard": false, |
| 156 | + "HangfireDashboardPath": "/hangfire", |
| 157 | + "EpisodeReminderSeriesDelayMs": 1500, |
| 158 | + "EpisodeReminderNotificationDelayMs": 500 |
| 159 | + }, |
| 160 | + "Jwt": { |
| 161 | + "Key": "test-key", |
| 162 | + "Issuer": "test-issuer" |
| 163 | + }, |
| 164 | + "TMDB": { |
| 165 | + "AccessToken": "test-token" |
| 166 | + } |
| 167 | + } |
| 168 | + """; |
| 169 | + |
| 170 | + File.WriteAllText(Path.Combine(tempDirectory, "settings.json"), settingsJson); |
| 171 | + Directory.SetCurrentDirectory(tempDirectory); |
| 172 | + |
| 173 | + return new CurrentDirectoryScope(originalDirectory, tempDirectory); |
| 174 | + } |
| 175 | + |
| 176 | + public void Dispose() |
| 177 | + { |
| 178 | + Directory.SetCurrentDirectory(originalDirectory); |
| 179 | + |
| 180 | + if (Directory.Exists(tempDirectory)) |
| 181 | + { |
| 182 | + Directory.Delete(tempDirectory, true); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments