Add a basic chat bot integration test

This commit is contained in:
Jordan Brown
2020-03-25 14:06:12 -04:00
parent d804c1fb2d
commit 2c980aab2f
4 changed files with 89 additions and 2 deletions
+1
View File
@@ -9,6 +9,7 @@ environment:
secure: lJNGAXwiB5HlWdthz3K4PetqpTG5IEAyRgKaiKxFMQ8HW8CcOjRtB97B05op7BsK
TGS4_TEST_DISCORD_TOKEN:
secure: 5ENtMfhHDI6SOgZ9nP7oHX//nZ1w5cYSk3a17MO54+jmcZwEtwZPB4XvVKgtY2xIacjp96ZWNnQgIyfRHvnHSg==
TGS4_TEST_DISCORD_CHANNEL: 493119635319947269
TGS_RELEASE_NOTES_TOKEN:
secure: lJNGAXwiB5HlWdthz3K4PetqpTG5IEAyRgKaiKxFMQ8HW8CcOjRtB97B05op7BsK
+1
View File
@@ -34,6 +34,7 @@ $HOME/.dotnet/tools/coverlet bin/$CONFIG/netcoreapp2.1/Tgstation.Server.Host.Con
cd ../Tgstation.Server.Tests
export TGS4_TEST_DATABASE_TYPE=MySql
export TGS4_TEST_DISCORD_CHANNEL=493119635319947269
export TGS4_TEST_CONNECTION_STRING="server=127.0.0.1;uid=root;pwd=;database=tgs_test"
#token set in CI settings
dotnet build -c $CONFIG /p:CopyLocalLockFileAssemblies=true
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Client.Components;
namespace Tgstation.Server.Tests.Instance
{
sealed class ChatTest
{
readonly IChatBotsClient chatClient;
public ChatTest(IChatBotsClient chatBotsClient)
{
chatClient = chatBotsClient ?? throw new ArgumentNullException(nameof(chatBotsClient));
}
public async Task Run()
{
var firstBot = new ChatBot
{
ConnectionString = Environment.GetEnvironmentVariable("TGS4_TEST_DISCORD_TOKEN"),
Enabled = false,
Name = "r4407",
Provider = ChatProvider.Discord,
ReconnectionInterval = 1
};
firstBot = await chatClient.Create(firstBot, default);
Assert.AreNotEqual(0, firstBot.Id);
var bots = await chatClient.List(default);
Assert.AreEqual(1, bots.Count);
Assert.AreEqual(firstBot.Id, bots.First().Id);
var retrievedBot = await chatClient.GetId(firstBot, default);
Assert.AreEqual(firstBot.Id, retrievedBot);
firstBot.Enabled = true;
var updatedBot = await chatClient.Update(firstBot, default);
Assert.AreEqual(true, updatedBot.Enabled);
var channelId = UInt64.Parse(Environment.GetEnvironmentVariable("TGS4_TEST_DISCORD_CHANNEL"));
firstBot.Channels = new List<ChatChannel>
{
new ChatChannel
{
IsAdminChannel = true,
IsUpdatesChannel = false,
IsWatchdogChannel = true,
Tag = "butt",
DiscordChannelId = channelId
}
};
updatedBot = await chatClient.Update(firstBot, default);
Assert.AreEqual(true, updatedBot.Enabled);
Assert.IsNotNull(updatedBot.Channels);
Assert.AreEqual(1, updatedBot.Channels.Count);
Assert.AreEqual(true, updatedBot.Channels.First().IsAdminChannel);
Assert.AreEqual(true, updatedBot.Channels.First().IsUpdatesChannel);
Assert.AreEqual(true, updatedBot.Channels.First().IsWatchdogChannel);
Assert.AreEqual("butt", updatedBot.Channels.First().Tag);
Assert.AreEqual(channelId, updatedBot.Channels.First().DiscordChannelId);
Assert.IsNull(updatedBot.Channels.First().IrcChannel);
await chatClient.Delete(firstBot, default);
bots = await chatClient.List(default);
Assert.AreEqual(0, bots.Count);
}
}
}
@@ -24,13 +24,20 @@ namespace Tgstation.Server.Tests
{
readonly IServerClientFactory clientFactory = new ServerClientFactory(new ProductHeaderValue(Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Version.ToString()));
[TestMethod]
public async Task TestAutomaticDiscordReconnection()
static string RequireDiscordToken()
{
var discordToken = Environment.GetEnvironmentVariable("TGS4_TEST_DISCORD_TOKEN");
if (String.IsNullOrWhiteSpace(discordToken))
Assert.Inconclusive("The TGS4_TEST_DISCORD_TOKEN environment variable must be set to run this test!");
return discordToken;
}
[TestMethod]
public async Task TestAutomaticDiscordReconnection()
{
var discordToken = RequireDiscordToken();
using (var discordProvider = new DiscordProvider(Mock.Of<ILogger<DiscordProvider>>(), discordToken, 1))
{
var connectResult = await discordProvider.Connect(default).ConfigureAwait(false);
@@ -158,6 +165,7 @@ namespace Tgstation.Server.Tests
[TestMethod]
public async Task TestStandardOperation()
{
RequireDiscordToken();
var server = new TestingServer(clientFactory, null);
using (var serverCts = new CancellationTokenSource())
{