sharee.bike-App/SharedBusinessLogic.Tests/Model/Bikes/BikeInfoNS/BikeNS/Command/TestAuthCommand.cs
2024-04-09 12:53:23 +02:00

109 lines
3 KiB
C#

using System;
using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
using ShareeBike.Model.Connector;
using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.AuthCommand;
using ShareeBike.Repository.Exception;
namespace SharedBusinessLogic.Tests.Model.Bikes.BikeInfoNS.BikeNS.Command
{
[TestFixture]
public class TestAuthCommand
{
/// <summary>
/// Use case: Authenticate.
/// Final state: Authentication successfully
/// </summary>
[Test]
public async Task TestAuth()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IAuthCommandListener>();
await InvokeAsync<TestAuthCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener);
await connector.Command.CalculateAuthKeys(Arg.Any<IBikeInfoMutable>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.Authenticate);
await connector.Command.CalculateAuthKeys(bike);
});
}
/// <summary>
/// Use case: Authenticate.
/// Final state: Authentication failed
/// </summary>
[Test]
public void TestAuthNoWebException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IAuthCommandListener>();
bike.LockInfo.State.Returns(LockingState.Closed);
connector.Command.CalculateAuthKeys(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new WebConnectFailureException("Context info.", new System.Exception("hoppla")));
Assert.That(
async () => await InvokeAsync<TestAuthCommand>(
bike,
() => false, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<WebConnectFailureException>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.Authenticate);
await connector.Command.CalculateAuthKeys(bike);
await listener.ReportStateAsync(State.WebConnectFailed, "Context info.");
});
}
/// <summary>
/// Use case: Authenticate.
/// Final state: Authentication failed
/// </summary>
[Test]
public void TestAuthGeneralException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var listener = Substitute.For<IAuthCommandListener>();
bike.LockInfo.State.Returns(LockingState.Closed);
connector.Command.CalculateAuthKeys(Arg.Any<IBikeInfoMutable>()).Returns(x
=> throw new Exception("Context info."));
Assert.That(
async () => await InvokeAsync<TestAuthCommand>(
bike,
() => true, // isConnectedDelegate
(isConnected) => connector,
listener),
Throws.InstanceOf<Exception>());
// Verify behavior
Received.InOrder(async () =>
{
listener.ReportStep(Step.Authenticate);
await connector.Command.CalculateAuthKeys(bike);
await listener.ReportStateAsync(State.GeneralAuthError, "Context info.");
});
}
}
}