Skip to content

Commit

Permalink
(#147) Web: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jul 2, 2022
1 parent 9e7305e commit e80e7c5
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Emulsion.Tests/Emulsion.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<Compile Include="Telegram\Html.fs" />
<Compile Include="Telegram\FunogramTests.fs" />
<Compile Include="Telegram\LinkGeneratorTests.fs" />
<Compile Include="Xmpp\SharpXmppPingHandlerTests.fs" />
<Compile Include="Xmpp\XmppClientFactory.fs" />
<Compile Include="Xmpp\XmppMessageFactory.fs" />
<Compile Include="Xmpp\XmppClientTests.fs" />
Expand All @@ -33,6 +32,7 @@
<Compile Include="Database\DatabaseStructureTests.fs" />
<Compile Include="ContentProxy\ContentStorageTests.fs" />
<Compile Include="ContentProxy\ProxyTests.fs" />
<Compile Include="Web\ContentControllerTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Akka.TestKit" Version="1.3.12" />
Expand Down
79 changes: 79 additions & 0 deletions Emulsion.Tests/Web/ContentControllerTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace Emulsion.Tests.Web

open System

open System.Threading.Tasks
open Emulsion.ContentProxy
open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Logging
open Serilog.Extensions.Logging
open Xunit
open Xunit.Abstractions

open Emulsion.Database
open Emulsion.Database.Entities
open Emulsion.Settings
open Emulsion.Tests.TestUtils
open Emulsion.Tests.TestUtils.Logging
open Emulsion.Web

type ContentControllerTests(output: ITestOutputHelper) =

let hostingSettings = {
BaseUri = Uri "https://example.com/emulsion"
HashIdSalt = "test_salt"
}

let logger = xunitLogger output

let performTestWithPreparation prepareAction testAction = Async.StartAsTask(async {
return! TestDataStorage.doWithDatabase(fun databaseSettings -> async {
do! prepareAction databaseSettings

use loggerFactory = new SerilogLoggerFactory(logger)
let logger = loggerFactory.CreateLogger<ContentController>()
use context = new EmulsionDbContext(databaseSettings.ContextOptions)
let controller = ContentController(logger, hostingSettings, context)
return! testAction controller
})
})

let performTest = performTestWithPreparation(fun _ -> async.Return())

[<Fact>]
member _.``ContentController returns BadRequest on hashId deserialization error``(): Task =
performTest (fun controller -> async {
let hashId = "z-z-z-z-z"
let! result = Async.AwaitTask <| controller.Get hashId
Assert.IsType<BadRequestResult> result |> ignore
})

[<Fact>]
member _.``ContentController returns NotFound if the content doesn't exist``(): Task =
performTest (fun controller -> async {
let hashId = Proxy.encodeHashId hostingSettings.HashIdSalt 667L
let! result = Async.AwaitTask <| controller.Get hashId
Assert.IsType<NotFoundResult> result |> ignore
})

[<Fact>]
member _.``ContentController returns a correct result``(): Task =
let contentId = 343L
let chatUserName = "MySuperExampleChat"
let messageId = 777L
performTestWithPreparation (fun databaseOptions -> async {
use context = new EmulsionDbContext(databaseOptions.ContextOptions)
let content = {
Id = contentId
ChatUserName = chatUserName
MessageId = messageId
FileId = "foobar"
}
do! DataStorage.addAsync context.TelegramContents content
return! Async.Ignore <| Async.AwaitTask(context.SaveChangesAsync())
}) (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($"https://t.me/{chatUserName}/{string messageId}", redirect.Url)
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Emulsion.Web.Controllers
namespace Emulsion.Web

open System.Globalization
open System.Threading.Tasks

open Microsoft.AspNetCore.Mvc
Expand Down Expand Up @@ -30,12 +29,11 @@ type ContentController(logger: ILogger<ContentController>,
return
content
|> Option.map(fun c ->
let url = $"https://t.me/{c.ChatUserName}/{c.MessageId.ToString(CultureInfo.InvariantCulture)}"
let url = $"https://t.me/{c.ChatUserName}/{string c.MessageId}"
RedirectResult url
)
}


[<HttpGet>]
member this.Get(hashId: string): Task<IActionResult> = task {
match decodeHashId hashId with
Expand Down
6 changes: 5 additions & 1 deletion Emulsion.Web/Emulsion.Web.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Controllers\ContentController.fs" />
<Compile Include="ContentController.fs" />
<Compile Include="WebServer.fs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Emulsion.ContentProxy\Emulsion.ContentProxy.fsproj" />
<ProjectReference Include="..\Emulsion.Settings\Emulsion.Settings.fsproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
</ItemGroup>
</Project>
2 changes: 0 additions & 2 deletions Emulsion.Web/WebServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ open System.Threading.Tasks
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection

open Emulsion.Web.Controllers

let run(baseUri: Uri): Task =
// TODO: Pass baseUri
let builder = WebApplication.CreateBuilder()
Expand Down
2 changes: 1 addition & 1 deletion Emulsion/Emulsion.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="JetBrains.Lifetimes" Version="2020.2.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="SharpXMPP" Version="0.3.0" />
Expand Down

0 comments on commit e80e7c5

Please sign in to comment.