sharee.bike-App/SharedBusinessLogic.Tests/Model/Connector/TestCommandLoggedIn.cs
2024-04-09 12:53:23 +02:00

75 lines
2.5 KiB
C#

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using NUnit.Framework;
using ShareeBike.Model.Connector;
using ShareeBike.Repository;
using ShareeBike.Repository.Exception;
using ShareeBike.Repository.Response;
namespace SharedBusinessLogic.Tests.Fixtures.ObjectTests.Connector
{
[TestFixture]
public class TestCommandLoggedIn
{
/// <summary> Verifies, that logout leads to expected call on copri server. </summary>
[Test]
public void TestDoLogout()
{
var l_oServer = Substitute.For<ICopriServer>();
l_oServer.DoAuthoutAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject<AuthorizationoutResponse>("{ \"response_state\" : \"OK\", \"authcookie\" : \"1\"}")));
var l_oCmd = new CommandLoggedIn(l_oServer, "MeinKeks", "EMehl", () => DateTime.Now);
LoginStateChangedEventArgs l_oEventArgs = null;
l_oCmd.LoginStateChanged += (sender, eventargs) => l_oEventArgs = eventargs;
l_oCmd.DoLogout().Wait();
l_oServer.Received().DoAuthoutAsync();
Assert.That(l_oEventArgs, Is.Not.Null);
}
/// <summary> Verifies, that logout leads to expected call on copri server. </summary>
[Test]
public void TestDoLogout_AuthcookieNotDefined()
{
var l_oServer = Substitute.For<ICopriServer>();
l_oServer.When(x => x.DoAuthoutAsync()).Throw(new AuthcookieNotDefinedException("Testing action", JsonConvert.DeserializeObject<ResponseBase>(@"{ ""response_state"" : ""Some inner error description""}")));
var l_oCmd = new CommandLoggedIn(l_oServer, "MeinKeks", "EMehl", () => DateTime.Now);
LoginStateChangedEventArgs l_oEventArgs = null;
l_oCmd.LoginStateChanged += (sender, eventargs) => l_oEventArgs = eventargs;
l_oCmd.DoLogout().Wait();
l_oServer.Received().DoAuthoutAsync();
Assert.That(l_oEventArgs, Is.Not.Null);
}
/// <summary> Verifies, that logout leads to expected call on copri server. </summary>
[Test]
public void TestDoLogout_Exception()
{
var l_oServer = Substitute.For<ICopriServer>();
l_oServer.When(x => x.DoAuthoutAsync()).Throw(new System.Exception("Sometheing went wrong."));
var l_oCmd = new CommandLoggedIn(l_oServer, "MeinKeks", "EMehl", () => DateTime.Now);
LoginStateChangedEventArgs l_oEventArgs = null;
l_oCmd.LoginStateChanged += (sender, eventargs) => l_oEventArgs = eventargs;
Assert.Throws<AggregateException>(() => l_oCmd.DoLogout().Wait());
l_oServer.Received().DoAuthoutAsync();
Assert.That(l_oEventArgs, Is.Null);
}
}
}