sharee.bike-App/TestShareeLib/Model/Connector/TestCommandLoggedIn.cs

76 lines
2.4 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;
using TINK.Model.Connector;
using TINK.Repository;
using TINK.Repository.Exception;
using TINK.Repository.Response;
namespace TestTINKLib.Fixtures.ObjectTests.Connector
{
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();
2022-09-06 16:08:19 +02:00
Assert.IsNotNull(l_oEventArgs);
}
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();
2022-09-06 16:08:19 +02:00
Assert.IsNotNull(l_oEventArgs);
}
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();
2022-09-06 16:08:19 +02:00
Assert.IsNull(l_oEventArgs);
}
}
2021-07-12 21:31:46 +02:00
}