From 067da2d51b3a55d64337a67425bc31117ce33efd Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Sun, 28 Aug 2022 12:57:30 +0200 Subject: [PATCH] (#102) ContentController: add last tests --- .../Emulsion.TestFramework.fsproj | 3 +- .../{TestFileCache.fs => FileCacheUtil.fs} | 2 +- Emulsion.TestFramework/StreamUtils.fs | 10 ++++ Emulsion.TestFramework/WebFileStorage.fs | 4 +- Emulsion.Tests/ContentProxy/FileCacheTests.fs | 14 ++--- Emulsion.Tests/Web/ContentControllerTests.fs | 56 +++++++++++++------ 6 files changed, 60 insertions(+), 29 deletions(-) rename Emulsion.TestFramework/{TestFileCache.fs => FileCacheUtil.fs} (92%) create mode 100644 Emulsion.TestFramework/StreamUtils.fs diff --git a/Emulsion.TestFramework/Emulsion.TestFramework.fsproj b/Emulsion.TestFramework/Emulsion.TestFramework.fsproj index 887232e1..8b0cbe0e 100644 --- a/Emulsion.TestFramework/Emulsion.TestFramework.fsproj +++ b/Emulsion.TestFramework/Emulsion.TestFramework.fsproj @@ -15,7 +15,8 @@ - + + diff --git a/Emulsion.TestFramework/TestFileCache.fs b/Emulsion.TestFramework/FileCacheUtil.fs similarity index 92% rename from Emulsion.TestFramework/TestFileCache.fs rename to Emulsion.TestFramework/FileCacheUtil.fs index b83eedc6..e062a4c8 100644 --- a/Emulsion.TestFramework/TestFileCache.fs +++ b/Emulsion.TestFramework/FileCacheUtil.fs @@ -1,4 +1,4 @@ -module Emulsion.TestFramework.TestFileCache +module Emulsion.TestFramework.FileCacheUtil open System.IO diff --git a/Emulsion.TestFramework/StreamUtils.fs b/Emulsion.TestFramework/StreamUtils.fs new file mode 100644 index 00000000..92fccf1c --- /dev/null +++ b/Emulsion.TestFramework/StreamUtils.fs @@ -0,0 +1,10 @@ +module Emulsion.TestFramework.StreamUtils + +open System.IO + +let readAllBytes(stream: Stream) = async { + use buffer = new MemoryStream() + let! ct = Async.CancellationToken + do! Async.AwaitTask(stream.CopyToAsync(buffer, ct)) + return buffer.ToArray() +} diff --git a/Emulsion.TestFramework/WebFileStorage.fs b/Emulsion.TestFramework/WebFileStorage.fs index 28e3e66a..ad3d886c 100644 --- a/Emulsion.TestFramework/WebFileStorage.fs +++ b/Emulsion.TestFramework/WebFileStorage.fs @@ -20,7 +20,9 @@ type WebFileStorage(data: Map) = let builder = WebApplication.CreateBuilder() let app = builder.Build() app.MapGet("/{entry}", Func<_, _>(fun (entry: string) -> task { - return Results.Bytes(data[entry]) + match Map.tryFind entry data with + | Some bytes -> return Results.Bytes bytes + | None -> return Results.NotFound() })) |> ignore app, app.RunAsync url diff --git a/Emulsion.Tests/ContentProxy/FileCacheTests.fs b/Emulsion.Tests/ContentProxy/FileCacheTests.fs index 724886de..3fbb21f7 100644 --- a/Emulsion.Tests/ContentProxy/FileCacheTests.fs +++ b/Emulsion.Tests/ContentProxy/FileCacheTests.fs @@ -17,10 +17,10 @@ type FileCacheTests(outputHelper: ITestOutputHelper) = let sha256 = SHA256.Create() - let cacheDirectory = lazy TestFileCache.newCacheDirectory() + let cacheDirectory = lazy FileCacheUtil.newCacheDirectory() let setUpFileCache(totalLimitBytes: uint64) = - TestFileCache.setUpFileCache outputHelper sha256 cacheDirectory.Value totalLimitBytes + FileCacheUtil.setUpFileCache outputHelper sha256 cacheDirectory.Value totalLimitBytes let assertCacheState(entries: (string * byte[]) seq) = let files = @@ -63,12 +63,6 @@ type FileCacheTests(outputHelper: ITestOutputHelper) = Assert.Equal(expectedMessage, error.Value.Message) ) - let readAllBytes (stream: Stream) = task { - use buffer = new MemoryStream() - do! stream.CopyToAsync buffer - return buffer.ToArray() - } - interface IDisposable with member _.Dispose() = sha256.Dispose() @@ -164,7 +158,7 @@ type FileCacheTests(outputHelper: ITestOutputHelper) = // Now there's only "b" item in the cache: assertCacheState [| "b", fileStorage.Content "b" |] // We should still be able to read "a" fully: - let! content = readAllBytes stream + let! content = StreamUtils.readAllBytes stream Assert.Equal(fileStorage.Content "a", content) } @@ -190,6 +184,6 @@ type FileCacheTests(outputHelper: ITestOutputHelper) = do! assertFileDownloaded fileCache fileStorage "a" size assertCacheState [| "a", fileStorage.Content "a" |] // We should still be able to read "a" fully: - let! content = readAllBytes stream + let! content = StreamUtils.readAllBytes stream Assert.Equal(fileStorage.Content "a", content) } diff --git a/Emulsion.Tests/Web/ContentControllerTests.fs b/Emulsion.Tests/Web/ContentControllerTests.fs index 0ff0876e..c2817eb2 100644 --- a/Emulsion.Tests/Web/ContentControllerTests.fs +++ b/Emulsion.Tests/Web/ContentControllerTests.fs @@ -29,13 +29,12 @@ type ContentControllerTests(output: ITestOutputHelper) = let logger = xunitLogger output let telegramClient = TelegramClientMock() - let sha256 = SHA256.Create() - let cacheDirectory = lazy TestFileCache.newCacheDirectory() + let cacheDirectory = lazy FileCacheUtil.newCacheDirectory() - let setUpFileCache(totalLimitBytes: uint64) = - TestFileCache.setUpFileCache output sha256 cacheDirectory.Value totalLimitBytes + let setUpFileCache() = + FileCacheUtil.setUpFileCache output sha256 cacheDirectory.Value 0UL let performTestWithPreparation fileCache prepareAction testAction = Async.StartAsTask(async { return! TestDataStorage.doWithDatabase(fun databaseSettings -> async { @@ -109,8 +108,7 @@ type ContentControllerTests(output: ITestOutputHelper) = telegramClient.SetResponse(fileId, None) - let cacheDir = TestFileCache.newCacheDirectory() - use fileCache = TestFileCache.setUpFileCache output sha256 cacheDir 1UL + use fileCache = setUpFileCache() do! performTestWithContent (Some fileCache) content (fun controller -> async { let hashId = Proxy.encodeHashId hostingSettings.HashIdSalt contentId let! result = Async.AwaitTask <| controller.Get hashId @@ -119,11 +117,35 @@ type ContentControllerTests(output: ITestOutputHelper) = } [] - member _.``ContentController returns 404 if the cache reports that a file was not found``(): unit = - Assert.True false + member _.``ContentController returns 404 if the cache reports that a file was not found``(): Task = task { + let contentId = 344L + let chatUserName = "MySuperExampleChat" + let messageId = 777L + let fileId = "foobar1" + let content = { + Id = contentId + ChatUserName = chatUserName + MessageId = messageId + FileId = fileId + } + + + use fileCache = setUpFileCache() + use fileStorage = new WebFileStorage(Map.empty) + telegramClient.SetResponse(fileId, Some { + TemporaryLink = fileStorage.Link fileId + Size = 1UL + }) + + do! performTestWithContent (Some fileCache) content (fun controller -> async { + let hashId = Proxy.encodeHashId hostingSettings.HashIdSalt contentId + let! result = Async.AwaitTask <| controller.Get hashId + Assert.IsType result |> ignore + }) + } [] - member _.``ContentController returns a downloaded file from cache``(): Task = + member _.``ContentController returns a downloaded file from cache``(): Task = task { let contentId = 343L let chatUserName = "MySuperExampleChat" let messageId = 777L @@ -135,18 +157,20 @@ type ContentControllerTests(output: ITestOutputHelper) = FileId = fileId } - let testLink = Uri "https://example.com/myFile" + let onServerFileId = "fileIdOnServer" + use fileCache = setUpFileCache() + use fileStorage = new WebFileStorage(Map.ofArray [| onServerFileId, [| 1uy; 2uy; 3uy |] |]) let testFileInfo = { - TemporaryLink = testLink + TemporaryLink = fileStorage.Link onServerFileId Size = 1UL } telegramClient.SetResponse(fileId, Some testFileInfo) - performTestWithContent None content (fun controller -> async { + do! performTestWithContent (Some fileCache) content (fun controller -> async { let hashId = Proxy.encodeHashId hostingSettings.HashIdSalt contentId let! result = Async.AwaitTask <| controller.Get hashId - let redirect = Assert.IsType result - Assert.Equal(testLink, Uri redirect.Url) + let streamResult = Assert.IsType result + let! content = StreamUtils.readAllBytes streamResult.FileStream + Assert.Equal(fileStorage.Content onServerFileId, content) }) - - + }