diff --git a/src/NoteBookmark.BlazorApp.Tests/Tests/NewsFeedServiceTests.cs b/src/NoteBookmark.BlazorApp.Tests/Tests/NewsFeedServiceTests.cs new file mode 100644 index 0000000..7b7fbe5 --- /dev/null +++ b/src/NoteBookmark.BlazorApp.Tests/Tests/NewsFeedServiceTests.cs @@ -0,0 +1,152 @@ +using System.Net; +using System.Text; +using Bunit; +using FluentAssertions; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Moq; +using NoteBookmark.BlazorApp.Components.Pages; +using NoteBookmark.BlazorApp.Tests.Helpers; + +namespace NoteBookmark.BlazorApp.Tests.Tests; + +public sealed class NewsFeedServiceTests : BunitContext +{ + [Fact] + public async Task GetHtmlAsync_RendersMarkdownRewritesRelativeAssetsAndSanitizesUrls() + { + var requestedUrls = new List(); + var handler = new DelegateHttpMessageHandler(request => + { + requestedUrls.Add(request.RequestUri!.AbsoluteUri); + return new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent( + "# Product Updates\n\n![Logo](assets/logo.png)\n\n![Chart](./assets/sub/chart(1).png)\n\n![External](https://example.com/banner.png)\n\n\n\n[Safe](https://example.com) [Email](mailto:test@example.com) [Unsafe](javascript:alert(1))", + Encoding.UTF8, + "text/plain") + }; + }); + var service = CreateService(handler); + + var html = await service.GetHtmlAsync(); + + requestedUrls.Should().Equal("https://raw.githubusercontent.com/fboucher/NoteBookmark/main/docs/news.md"); + html.Should().Contain("

Product Updates

"); + html.Should().Contain("src=\"https://raw.githubusercontent.com/fboucher/NoteBookmark/main/docs/assets/logo.png\" alt=\"Logo\""); + html.Should().Contain("src=\"https://raw.githubusercontent.com/fboucher/NoteBookmark/main/docs/assets/sub/chart%281%29.png\" alt=\"Chart\""); + html.Should().Contain("src=\"https://example.com/banner.png\" alt=\"External\""); + html.Should().NotContain("