using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using NUnit.Framework;
using TINK.Model.Connector;
using TINK.Repository;
using TINK.Repository.Exception;
using TINK.Repository.Response;
namespace TestTINKLib.Fixtures.ObjectTests.Connector
{
[TestFixture]
public class TestCommandLoggedIn
{
/// Verifies, that logout leads to expected call on copri server.
[Test]
public void TestDoLogout()
{
var l_oServer = Substitute.For();
l_oServer.DoAuthoutAsync().Returns(Task.Run(() => JsonConvert.DeserializeObject("{ \"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.IsNotNull(l_oEventArgs);
}
/// Verifies, that logout leads to expected call on copri server.
[Test]
public void TestDoLogout_AuthcookieNotDefined()
{
var l_oServer = Substitute.For();
l_oServer.When(x => x.DoAuthoutAsync()).Throw(new AuthcookieNotDefinedException("Testing action", JsonConvert.DeserializeObject(@"{ ""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.IsNotNull(l_oEventArgs);
}
/// Verifies, that logout leads to expected call on copri server.
[Test]
public void TestDoLogout_Exception()
{
var l_oServer = Substitute.For();
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(() => l_oCmd.DoLogout().Wait());
l_oServer.Received().DoAuthoutAsync();
Assert.IsNull(l_oEventArgs);
}
}
}