Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added some tests! #16

Merged
merged 14 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Build

on:
pull_request:
branches: [ "main" ]
workflow_dispatch:

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
global-json-file: global.json
- name: Tests
run: dotnet test -c Release --logger "trx;LogFileName=test-results.trx"

# upload test results
- name: Upload dotnet test results
uses: actions/upload-artifact@v4
with:
name: test-results
path: "**/*.trx"
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and deploy a container to an Azure Container Registry
name: Deploy

on:
push:
Expand All @@ -14,14 +14,31 @@ env:
SCHEMAS: public

jobs:
build:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Apply migrations
# run tests
- uses: actions/setup-dotnet@v4
with:
global-json-file: global.json
- name: Tests
run: dotnet test -c Release --logger "trx;LogFileName=test-results.trx"

# upload test results
- name: Upload dotnet test results
uses: actions/upload-artifact@v4
with:
name: test-results
path: "**/*.trx"
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}

# if tests are ok, deploy to production
- name: Apply migrations to production DB
run: >-
docker run --rm
--volume ${{ github.workspace }}/src/migrations:/flyway/sql:ro
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/test-results.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test results

on:
# Run this workflow after the CI/CD workflow completes
workflow_run:
workflows: [Build, Deploy]
types:
- completed

jobs:
build:
runs-on: ubuntu-latest
steps:
# Extract the test result files from the artifacts
- uses: dorny/test-reporter@v1
with:
name: Test results
artifact: test-results
path: "**/*.trx"
reporter: dotnet-trx
fail-on-error: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
.idea
.env
flyway.conf
*sln.DotSettings.user
TestResults
*.trx
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0.402-jammy as build-env

### workaround for testcontainers resource reaper issue
ARG RESOURCE_REAPER_SESSION_ID="00000000-0000-0000-0000-000000000000"
LABEL "org.testcontainers.resource-reaper-session"=$RESOURCE_REAPER_SESSION_ID
### end of workaround

WORKDIR /src/VahterBanBot
COPY src/VahterBanBot/VahterBanBot.fsproj .
RUN dotnet restore
Expand Down
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
Database setup

```postgresql
CREATE ROLE vahter_bot_ban_service WITH LOGIN PASSWORD 'vahter_bot_ban_service';
GRANT vahter_bot_ban_service TO postgres;
CREATE DATABASE vahter_bot_ban OWNER vahter_bot_ban_service ENCODING 'UTF8';
GRANT ALL ON DATABASE vahter_bot_ban TO vahter_bot_ban_service;
GRANT USAGE, CREATE ON SCHEMA public TO vahter_bot_ban_service;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
```
- run init.sql

Run migrations

Expand Down
6 changes: 6 additions & 0 deletions init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE ROLE vahter_bot_ban_service WITH LOGIN PASSWORD 'vahter_bot_ban_service';
GRANT vahter_bot_ban_service TO postgres;
CREATE DATABASE vahter_bot_ban OWNER vahter_bot_ban_service ENCODING 'UTF8';
GRANT ALL ON DATABASE vahter_bot_ban TO vahter_bot_ban_service;
GRANT USAGE, CREATE ON SCHEMA public TO vahter_bot_ban_service;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
137 changes: 137 additions & 0 deletions src/VahterBanBot.Tests/ContainerTestBase.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
module VahterBanBot.Tests.ContainerTestBase

open System
open System.IO
open System.Net.Http
open System.Text
open DotNet.Testcontainers.Builders
open DotNet.Testcontainers.Configurations
open DotNet.Testcontainers.Containers
open Newtonsoft.Json
open Telegram.Bot.Types
open Testcontainers.PostgreSql
open Xunit

type VahterTestContainers() =
let solutionDir = CommonDirectoryPath.GetSolutionDirectory()
let dbAlias = "vahter-db"
let pgImage = "postgres:15.6" // same as in Azure

// will be filled in IAsyncLifetime.InitializeAsync
let mutable uri: Uri = null
let mutable httpClient: HttpClient = null

// base image for the app, we'll build exactly how we build it in Azure
let image =
ImageFromDockerfileBuilder()
.WithDockerfileDirectory(solutionDir, String.Empty)
.WithDockerfile("./Dockerfile")
.WithName("vahter-bot-ban-test")
// workaround for multi-stage builds cleanup
.WithBuildArgument("RESOURCE_REAPER_SESSION_ID", ResourceReaper.DefaultSessionId.ToString("D"))
// it might speed up the process to not clean up the base image
.WithCleanUp(false)
.Build()

// private network for the containers
let network =
NetworkBuilder()
.Build()

// PostgreSQL container. Important to have the same image as in Azure
// and assign network alias to it as it will be "host" in DB connection string for the app
let dbContainer =
PostgreSqlBuilder()
.WithImage(pgImage)
.WithNetwork(network)
.WithNetworkAliases(dbAlias)
.Build()

// Flyway container to run migrations
let flywayContainer =
ContainerBuilder()
.WithImage("redgate/flyway")
.WithNetwork(network)
.WithBindMount(CommonDirectoryPath.GetSolutionDirectory().DirectoryPath + "/src/migrations", "/flyway/sql", AccessMode.ReadOnly)
.WithEnvironment("FLYWAY_URL", "jdbc:postgresql://vahter-db:5432/vahter_bot_ban")
.WithEnvironment("FLYWAY_USER", "vahter_bot_ban_service")
.WithEnvironment("FLYWAY_PASSWORD", "vahter_bot_ban_service")
.WithCommand("migrate", "-schemas=public")
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Successfully applied \d+ migrations"))
.DependsOn(dbContainer)
.Build()

// the app container
// we'll pass all the necessary environment variables to it
let appContainer =
ContainerBuilder()
.WithImage(image)
.WithNetwork(network)
.WithPortBinding(80, true)
.WithEnvironment("BOT_TELEGRAM_TOKEN", "TELEGRAM_SECRET")
.WithEnvironment("BOT_AUTH_TOKEN", "OUR_SECRET")
.WithEnvironment("LOGS_CHANNEL_ID", "-123")
.WithEnvironment("CHATS_TO_MONITOR", """{"pro.hell": -666, "dotnetru": -42}""")
.WithEnvironment("ALLOWED_USERS", """{"vahter_1": 34, "vahter_2": 69}""")
.WithEnvironment("SHOULD_DELETE_CHANNEL_MESSAGES", "true")
.WithEnvironment("IGNORE_SIDE_EFFECTS", "false")
.WithEnvironment("USE_POLLING", "false")
.WithEnvironment("DATABASE_URL", $"Server={dbAlias};Database=vahter_bot_ban;Port=5432;User Id=vahter_bot_ban_service;Password=vahter_bot_ban_service;Include Error Detail=true;Minimum Pool Size=1;Maximum Pool Size=20;Max Auto Prepare=100;Auto Prepare Min Usages=1;Trust Server Certificate=true;")
.DependsOn(flywayContainer)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80))
.Build()

interface IAsyncLifetime with
member this.InitializeAsync() = task {
// start building the image and spin up db at the same time
let imageTask = image.CreateAsync()
let dbTask = dbContainer.StartAsync()

// wait for both to finish
do! imageTask
do! dbTask

// initialize DB with the schema, database and a DB user
let script = File.ReadAllText(CommonDirectoryPath.GetSolutionDirectory().DirectoryPath + "/init.sql")
let! initResult = dbContainer.ExecScriptAsync(script)
if initResult.Stderr <> "" then
failwith initResult.Stderr

// run migrations
do! flywayContainer.StartAsync()

// seed some test data
// inserting the only admin users we have
// TODO might be a script in test assembly
let! _ = dbContainer.ExecAsync([|"""INSERT INTO "user"(id, username, banned_by, banned_at, ban_reason) VALUES (34, 'vahter_1', NULL, NULL, NULL), (69, 'vahter_2', NULL, NULL, NULL);"""|])

// start the app container
do! appContainer.StartAsync()

// initialize the http client with correct hostname and port
httpClient <- new HttpClient()
uri <- Uri($"http://{appContainer.Hostname}:{appContainer.GetMappedPublicPort(80)}")
httpClient.BaseAddress <- uri
httpClient.DefaultRequestHeaders.Add("X-Telegram-Bot-Api-Secret-Token", "OUR_SECRET")
}
member this.DisposeAsync() = task {
// stop all the containers, flyway might be dead already
do! flywayContainer.DisposeAsync()
do! appContainer.DisposeAsync()
do! dbContainer.DisposeAsync()
// do! image.DisposeAsync() // might be faster not to dispose base image to cache?
}

member _.Http = httpClient
member _.Uri = uri

member this.SendMessage(update: Update) = task {
let json = JsonConvert.SerializeObject(update)
return! this.SendMessage(json)
}

member _.SendMessage(json: string) = task {
let content = new StringContent(json, Encoding.UTF8, "application/json")
let! resp = httpClient.PostAsync("/bot", content)
return resp
}
7 changes: 7 additions & 0 deletions src/VahterBanBot.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
open Xunit
open Xunit.Extensions.AssemblyFixture

[<assembly: TestFramework(AssemblyFixtureFramework.TypeName, AssemblyFixtureFramework.AssemblyName)>]
do ()

module Program = let [<EntryPoint>] main _ = 0
37 changes: 37 additions & 0 deletions src/VahterBanBot.Tests/Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Tests

open System
open System.Net.Http
open System.Text
open Telegram.Bot.Types
open VahterBanBot.Tests.ContainerTestBase
open Xunit
open Xunit.Extensions.AssemblyFixture

type Tests(containers: VahterTestContainers) =
[<Fact>]
let ``Random path returns OK`` () = task {
let! resp = containers.Http.GetAsync("/" + Guid.NewGuid().ToString())
let! body = resp.Content.ReadAsStringAsync()
Assert.Equal(System.Net.HttpStatusCode.OK, resp.StatusCode)
Assert.Equal("OK", body)
}

[<Fact>]
let ``Not possible to interact with the bot without authorization`` () = task {
let http = new HttpClient()
let content = new StringContent("""{"update_id":123}""", Encoding.UTF8, "application/json")
let uri = containers.Uri.ToString() + "bot"
let! resp = http.PostAsync(uri, content)
Assert.Equal(System.Net.HttpStatusCode.Unauthorized, resp.StatusCode)
}

[<Fact>]
let ``Should be possible to interact with the bot`` () = task {
let! resp = Update(Id = 123) |> containers.SendMessage
let! body = resp.Content.ReadAsStringAsync()
Assert.Equal(System.Net.HttpStatusCode.OK, resp.StatusCode)
Assert.Equal("null", body)
}

interface IAssemblyFixture<VahterTestContainers>
33 changes: 33 additions & 0 deletions src/VahterBanBot.Tests/VahterBanBot.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<IsTestProject>true</IsTestProject>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<Compile Include="ContainerTestBase.fs" />
<Compile Include="Tests.fs"/>
<Compile Include="Program.fs"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Testcontainers" Version="3.8.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.8.0" />
<PackageReference Include="xunit" Version="2.8.1"/>
<PackageReference Include="Xunit.Extensions.AssemblyFixture" Version="2.6.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VahterBanBot\VahterBanBot.fsproj" />
</ItemGroup>

</Project>
24 changes: 1 addition & 23 deletions src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ open Telegram.Bot.Types
open Giraffe
open Microsoft.Extensions.DependencyInjection
open Telegram.Bot.Types.Enums
open VahterBanBot
open VahterBanBot.Cleanup
open VahterBanBot.Utils
open VahterBanBot.Bot
Expand Down Expand Up @@ -136,30 +135,9 @@ let app = builder.Build()
app.UseGiraffe(webApp)
let server = app.RunAsync()

let telegramClient = app.Services.GetRequiredService<ITelegramBotClient>()

let getStartLogMsg() =
let sb = System.Text.StringBuilder()
%sb.AppendLine("Bot started with following configuration")
%sb.AppendLine("AllowedUsers:")
for KeyValue(username, userId) in botConf.AllowedUsers do
%sb.AppendLine($" {prependUsername username} ({userId})")
%sb.AppendLine("ChatsToMonitor:")
for KeyValue(username, chatId) in botConf.ChatsToMonitor do
%sb.AppendLine($" {prependUsername username} ({chatId})")

let totalStats = (DB.getVahterStats None).Result
%sb.AppendLine (string totalStats)

sb.ToString()

if not botConf.IgnoreSideEffects then
let startLogMsg = getStartLogMsg()
app.Logger.LogInformation startLogMsg
telegramClient.SendTextMessageAsync(ChatId(botConf.LogsChannelId), startLogMsg).Wait()

// Dev mode only
if botConf.UsePolling then
let telegramClient = app.Services.GetRequiredService<ITelegramBotClient>()
let pollingHandler = {
new IUpdateHandler with
member x.HandleUpdateAsync (botClient: ITelegramBotClient, update: Update, cancellationToken: CancellationToken) =
Expand Down
Loading
Loading