-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved banned information to separate table (#22)
- Loading branch information
Showing
11 changed files
with
226 additions
and
75 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,76 @@ | ||
module VahterBanBot.Tests.BanTests | ||
|
||
open System.Net | ||
open VahterBanBot.Tests.ContainerTestBase | ||
open VahterBanBot.Tests.TgMessageUtils | ||
open Xunit | ||
|
||
type BanTests(fixture: VahterTestContainers) = | ||
|
||
[<Fact>] | ||
let ``Vahter can ban on reply`` () = task { | ||
// record a message | ||
let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0]) | ||
let! _ = fixture.SendMessage msgUpdate | ||
|
||
// send the ban message | ||
let! banResp = | ||
Tg.replyMsg(msgUpdate.Message, "/ban", fixture.AdminUsers[0]) | ||
|> fixture.SendMessage | ||
Assert.Equal(HttpStatusCode.OK, banResp.StatusCode) | ||
|
||
// assert that the message got banned | ||
let! msgBanned = fixture.MessageBanned msgUpdate.Message | ||
Assert.True msgBanned | ||
} | ||
|
||
[<Fact>] | ||
let ``NON Vahter can't ban on reply`` () = task { | ||
// record a message | ||
let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0]) | ||
let! _ = fixture.SendMessage msgUpdate | ||
|
||
// send the ban message from a non-admin user | ||
let! banResp = | ||
Tg.replyMsg(msgUpdate.Message, "/ban", Tg.user()) | ||
|> fixture.SendMessage | ||
Assert.Equal(HttpStatusCode.OK, banResp.StatusCode) | ||
|
||
// assert that the message NOT banned | ||
let! msgNotBanned = fixture.MessageBanned msgUpdate.Message | ||
Assert.False msgNotBanned | ||
} | ||
|
||
[<Fact>] | ||
let ``Vahter can't ban on reply in non-allowed chat`` () = task { | ||
// record a message in a random chat | ||
let msgUpdate = Tg.quickMsg(chat = Tg.chat()) | ||
let! _ = fixture.SendMessage msgUpdate | ||
|
||
// send the ban message | ||
let! banResp = | ||
Tg.replyMsg(msgUpdate.Message, "/ban", fixture.AdminUsers[0]) | ||
|> fixture.SendMessage | ||
Assert.Equal(HttpStatusCode.OK, banResp.StatusCode) | ||
|
||
// assert that the message NOT banned | ||
let! msgNotBanned = fixture.MessageBanned msgUpdate.Message | ||
Assert.False msgNotBanned | ||
} | ||
|
||
[<Fact>] | ||
let ``Vahter can't ban another vahter`` () = task { | ||
// record a message in a random chat | ||
let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0], from = fixture.AdminUsers[0]) | ||
let! _ = fixture.SendMessage msgUpdate | ||
|
||
// send the ban message | ||
let! banResp = | ||
Tg.replyMsg(msgUpdate.Message, "/ban", fixture.AdminUsers[1]) | ||
|> fixture.SendMessage | ||
Assert.Equal(HttpStatusCode.OK, banResp.StatusCode) | ||
|
||
// assert that the message NOT banned | ||
let! msgNotBanned = fixture.MessageBanned msgUpdate.Message | ||
Assert.False msgNotBanned | ||
} |
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 |
---|---|---|
@@ -1,30 +1,48 @@ | ||
module VahterBanBot.Tests.TgMessageUtils | ||
|
||
open System | ||
open System.Threading | ||
open Telegram.Bot.Types | ||
|
||
type Tg() = | ||
static let rnd = Random.Shared | ||
static let mutable i = 0L | ||
static let nextInt64() = Interlocked.Increment &i | ||
static let next() = nextInt64() |> int | ||
static member user (?id: int64, ?username: string, ?firstName: string) = | ||
User( | ||
Id = (id |> Option.defaultValue (rnd.NextInt64())), | ||
Id = (id |> Option.defaultValue (nextInt64())), | ||
Username = (username |> Option.defaultValue null), | ||
FirstName = (firstName |> Option.defaultWith (fun () -> Guid.NewGuid().ToString())) | ||
) | ||
static member chat (?id: int64, ?username: string) = | ||
Chat( | ||
Id = (id |> Option.defaultValue (rnd.NextInt64())), | ||
Id = (id |> Option.defaultValue (nextInt64())), | ||
Username = (username |> Option.defaultValue null) | ||
) | ||
static member quickMsg (?text: string, ?chat: Chat, ?from: User, ?date: DateTime) = | ||
Update( | ||
Id = rnd.Next(), | ||
Id = next(), | ||
Message = | ||
Message( | ||
MessageId = rnd.Next(), | ||
MessageId = next(), | ||
Text = (text |> Option.defaultValue (Guid.NewGuid().ToString())), | ||
Chat = (chat |> Option.defaultValue (Tg.chat())), | ||
From = (from |> Option.defaultValue (Tg.user())), | ||
Date = (date |> Option.defaultValue DateTime.UtcNow) | ||
Date = (date |> Option.defaultValue DateTime.UtcNow), | ||
ReplyToMessage = null | ||
) | ||
) | ||
|
||
static member replyMsg (msg: Message, ?text: string, ?from: User, ?date: DateTime) = | ||
Update( | ||
Id = next(), | ||
Message = | ||
Message( | ||
MessageId = next(), | ||
Text = (text |> Option.defaultValue (Guid.NewGuid().ToString())), | ||
Chat = msg.Chat, | ||
From = (from |> Option.defaultValue (Tg.user())), | ||
Date = (date |> Option.defaultValue DateTime.UtcNow), | ||
ReplyToMessage = msg | ||
) | ||
) |
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
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
Oops, something went wrong.