-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters