174 lines
5.2 KiB
C#
Raw Permalink Normal View History

using Capnp.Rpc;
2022-05-10 13:35:23 +02:00
using FabAccessAPI;
using FabAccessAPI.Exceptions;
2023-01-25 01:48:54 +01:00
using FabAccessAPI.Exceptions.SASL;
2022-05-10 13:35:23 +02:00
using NUnit.Framework;
using System;
using System.Collections.Generic;
2022-05-12 23:08:37 +02:00
using System.Threading;
2022-05-11 15:02:17 +02:00
using System.Threading.Tasks;
2022-05-10 13:35:23 +02:00
namespace FabAccessAPI_Test
{
public class API_Test
{
2022-05-12 23:08:37 +02:00
[Test]
public async Task Connect_HostUnreachable()
2022-05-10 13:35:23 +02:00
{
API api = new API();
ConnectionData connectionData = new ConnectionData()
{
2022-05-12 23:08:37 +02:00
Host = new UriBuilder(TestEnv.SCHEMA, "UnkownHost" + TestEnv.TESTSERVER, TestEnv.TESTSERVER_PORT).Uri,
2023-01-25 01:48:54 +01:00
Mechanism = SASLMechanismEnum.PLAIN,
2022-05-12 23:08:37 +02:00
Username = "UnkownUser",
2022-05-10 13:35:23 +02:00
Properties = new Dictionary<string, object>()
{
{ "Username", "UnkownUser" },
2022-05-10 13:35:23 +02:00
{ "Password", TestEnv.PASSWORD }
}
};
2022-05-12 23:08:37 +02:00
try
{
await api.Connect(connectionData);
}
2023-01-25 01:48:54 +01:00
catch (ConnectionException)
2022-05-12 23:08:37 +02:00
{
Assert.Pass();
}
Assert.Fail();
2022-05-10 13:35:23 +02:00
}
2023-01-25 01:48:54 +01:00
[Test]
2022-05-12 23:08:37 +02:00
public async Task Connect_InvalidCredentials()
2022-05-10 13:35:23 +02:00
{
API api = new API();
2022-05-12 23:08:37 +02:00
ConnectionData connectionData = TestEnv.CreateConnetionData("UnkownUser");
2022-05-10 13:35:23 +02:00
2022-05-11 15:02:17 +02:00
try
2022-05-10 13:35:23 +02:00
{
2022-05-11 15:02:17 +02:00
await api.Connect(connectionData);
}
2023-01-25 01:48:54 +01:00
catch(AuthenticationException exception) when (exception.InnerException is InvalidCredentialsException)
2022-05-11 15:02:17 +02:00
{
Assert.Pass();
}
Assert.Fail();
2022-05-10 13:35:23 +02:00
}
2022-05-12 23:08:37 +02:00
[TestCase("Admin1")]
public async Task ConnectDisconnect(string username)
2022-05-10 13:35:23 +02:00
{
API api = new API();
2022-05-12 23:08:37 +02:00
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
bool event_Connected = false;
bool event_Disconnected = false;
api.ConnectionStatusChanged += (sender, eventArgs) =>
2022-05-10 13:35:23 +02:00
{
2023-01-25 01:48:54 +01:00
if (eventArgs == ConnectionStatusChanged.Connected)
2022-05-10 13:35:23 +02:00
{
2022-05-12 23:08:37 +02:00
event_Connected = true;
}
2023-01-25 01:48:54 +01:00
if(eventArgs == ConnectionStatusChanged.Disconnected)
2022-05-10 13:35:23 +02:00
{
2022-05-12 23:08:37 +02:00
event_Disconnected = true;
}
};
await api.Connect(connectionData);
bool HasSesion = api.Session != null;
bool HasConnectionData = api.ConnectionData != null;
2023-01-25 01:48:54 +01:00
bool HasServerData = api.ServerData != null;
2022-05-12 23:08:37 +02:00
bool IsConnected = api.IsConnected;
await api.Disconnect();
Thread.Sleep(3000);
Assert.Multiple(() =>
{
Assert.IsTrue(event_Connected, "event_Connected");
Assert.IsTrue(event_Disconnected, "event_Disconnected");
Assert.IsTrue(HasSesion, "HasSesion");
Assert.IsTrue(HasConnectionData, "HasConnectionData");
2023-01-25 01:48:54 +01:00
Assert.IsTrue(HasServerData, "HasServerData");
2022-05-12 23:08:37 +02:00
Assert.IsTrue(IsConnected, "IsConnected");
Assert.IsFalse(api.IsConnected, "api.IsConnected");
});
}
[TestCase("Admin1")]
public async Task TestConnectíon(string username)
{
API api = new API();
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
2023-01-25 01:48:54 +01:00
ServerData serverData = await api.TryToConnect(connectionData);
2022-05-12 23:08:37 +02:00
2023-01-25 01:48:54 +01:00
Assert.IsNotNull(serverData);
2022-05-12 23:08:37 +02:00
}
[TestCase("Admin1"), Explicit]
public async Task Reconnect(string username)
{
API api = new API();
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
2023-01-25 01:48:54 +01:00
int event_Connected = 0;
int event_ConnectionLoss = 0;
int event_Disconnected = 0;
2022-05-12 23:08:37 +02:00
api.ConnectionStatusChanged += (sender, eventArgs) =>
{
2023-01-25 01:48:54 +01:00
if (eventArgs == ConnectionStatusChanged.Connected)
2022-05-12 23:08:37 +02:00
{
2023-01-25 01:48:54 +01:00
event_Connected++;
2022-05-12 23:08:37 +02:00
}
2023-01-25 01:48:54 +01:00
if (eventArgs == ConnectionStatusChanged.ConnectionLoss)
2022-05-12 23:08:37 +02:00
{
2023-01-25 01:48:54 +01:00
event_ConnectionLoss++;
2022-05-12 23:08:37 +02:00
}
2023-01-25 01:48:54 +01:00
if (eventArgs == ConnectionStatusChanged.Disconnected)
2022-05-12 23:08:37 +02:00
{
2023-01-25 01:48:54 +01:00
event_Disconnected++;
2022-05-10 13:35:23 +02:00
}
};
2022-05-12 23:08:37 +02:00
TcpRpcClient tcpRpcClient = new TcpRpcClient();
await api.Connect(connectionData, tcpRpcClient);
2022-05-11 15:02:17 +02:00
try
{
2022-05-12 23:08:37 +02:00
// Stop here and cut internet connection
await api.Session.MachineSystem.Info.GetMachineList().ConfigureAwait(false);
2022-05-11 15:02:17 +02:00
}
2022-05-12 23:08:37 +02:00
catch
2022-05-10 13:35:23 +02:00
{
2022-05-12 23:08:37 +02:00
2022-05-11 15:02:17 +02:00
}
2022-05-12 23:08:37 +02:00
Thread.Sleep(3000);
// Stop here and connect with internet again
await api.Disconnect();
2023-01-25 01:48:54 +01:00
Thread.Sleep(1000);
2022-05-12 23:08:37 +02:00
Assert.Multiple(() =>
{
2023-01-25 01:48:54 +01:00
Assert.AreEqual(2, event_Connected, "event_Connected");
Assert.AreEqual(1, event_ConnectionLoss, "event_ConnectionLoss");
Assert.AreEqual(1, event_Disconnected, "event_Disconnected");
2022-05-12 23:08:37 +02:00
});
2022-05-10 13:35:23 +02:00
}
2022-05-12 23:08:37 +02:00
2022-05-10 13:35:23 +02:00
}
}