Skip to content

Commit

Permalink
(#102) ContentController: add last tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 28, 2022
1 parent e8e8153 commit 067da2d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Emulsion.TestFramework/Emulsion.TestFramework.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<Compile Include="TelegramClientMock.fs" />
<Compile Include="WebFileStorage.fs" />
<Compile Include="SimpleHttpClientFactory.fs" />
<Compile Include="TestFileCache.fs" />
<Compile Include="FileCacheUtil.fs" />
<Compile Include="StreamUtils.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Emulsion.TestFramework.TestFileCache
module Emulsion.TestFramework.FileCacheUtil

open System.IO

Expand Down
10 changes: 10 additions & 0 deletions Emulsion.TestFramework/StreamUtils.fs
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 3 additions & 1 deletion Emulsion.TestFramework/WebFileStorage.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ type WebFileStorage(data: Map<string, byte[]>) =
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

Expand Down
14 changes: 4 additions & 10 deletions Emulsion.Tests/ContentProxy/FileCacheTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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<byte>(fileStorage.Content "a", content)
}

Expand All @@ -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<byte>(fileStorage.Content "a", content)
}
56 changes: 40 additions & 16 deletions Emulsion.Tests/Web/ContentControllerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -119,11 +117,35 @@ type ContentControllerTests(output: ITestOutputHelper) =
}

[<Fact>]
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<NotFoundResult> result |> ignore
})
}

[<Fact>]
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
Expand All @@ -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<RedirectResult> result
Assert.Equal(testLink, Uri redirect.Url)
let streamResult = Assert.IsType<FileStreamResult> result
let! content = StreamUtils.readAllBytes streamResult.FileStream
Assert.Equal<byte>(fileStorage.Content onServerFileId, content)
})


}

0 comments on commit 067da2d

Please sign in to comment.