sharee.bike-App/SharedBusinessLogic.Tests/Model/Connector/TestCommandLoggedIn.cs

76 lines
2.5 KiB
C#
Raw Normal View History

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