2023-02-22 14:03:35 +01:00
using System ;
2022-08-30 15:42:25 +02:00
using NSubstitute ;
using NUnit.Framework ;
using Plugin.BLE.Abstractions.Contracts ;
2024-04-09 12:53:23 +02:00
using SharedBusinessLogic.Tests.Framework.Repository ;
using ShareeBike.Model ;
using ShareeBike.Model.Connector ;
using ShareeBike.Model.Device ;
using ShareeBike.Model.Services.CopriApi.ServerUris ;
using ShareeBike.Model.User.Account ;
using ShareeBike.Repository ;
using ShareeBike.Services ;
using ShareeBike.Services.BluetoothLock ;
using ShareeBike.Services.Geolocation ;
using ShareeBike.Services.Permissions ;
2021-06-26 20:57:55 +02:00
2024-04-09 12:53:23 +02:00
namespace SharedBusinessLogic.Tests.Fixtures.UseCases.Logout
2021-06-26 20:57:55 +02:00
{
2022-09-06 16:08:19 +02:00
[TestFixture]
2024-04-09 12:53:23 +02:00
public class TestShareeBikeApp
2022-09-06 16:08:19 +02:00
{
[Test]
public void Logout ( )
{
var accountStore = Substitute . For < IStore > ( ) ;
var locksService = Substitute . For < ILocksService > ( ) ;
var device = Substitute . For < ISmartDevice > ( ) ;
var specialFolder = Substitute . For < ISpecialFolder > ( ) ;
var permissions = Substitute . For < ILocationPermission > ( ) ;
var account = Substitute . For < IAccount > ( ) ;
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
accountStore . Load ( ) . Returns ( account ) ;
account . Mail . Returns ( "javaminister@gmail.com" ) ;
account . Pwd . Returns ( "*********" ) ;
account . SessionCookie . Returns ( "6103_112e96b36ba33de245943c5ffaf369cd_" ) ;
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
// No user logged in is initial state to verify.
2024-04-09 12:53:23 +02:00
var l_oShareeBikeApp = new ShareeBikeApp (
new ShareeBike . Model . Settings . Settings (
2022-09-06 16:08:19 +02:00
activeLockService : locksService . GetType ( ) . FullName ,
activeGeolocationService : "NotRelevantActiveGeoloactionServiceName" ,
2024-04-09 12:53:23 +02:00
activeUri : new Uri ( CopriServerUriList . ShareeBike_DEVEL ) ) ,
2022-09-06 16:08:19 +02:00
accountStore ,
isConnectedFunc : ( ) = > true ,
connectorFactory : ( isConnected , uri , sessionCookie , mail , expiresAfter ) = > string . IsNullOrEmpty ( sessionCookie )
2023-02-22 14:03:35 +01:00
? new ConnectorCache ( new AppContextInfo ( "MyMerchId" , "MyApp" , new Version ( 1 , 2 ) ) , null /*UI language */ , sessionCookie , mail , server : new CopriCallsMemory001 ( ) )
: new ConnectorCache ( new AppContextInfo ( "MyMerchId" , "MyApp" , new Version ( 1 , 2 ) ) , null /*UI language */ , sessionCookie , mail , server : new CopriCallsMemory001 ( sessionCookie ) ) ,
2022-09-06 16:08:19 +02:00
merchantId : "MyMerchId" ,
bluetoothService : Substitute . For < IBluetoothLE > ( ) ,
locationPermissionsService : permissions ,
2023-04-05 15:02:10 +02:00
locationServicesContainer : Substitute . For < IServicesContainer < IGeolocationService > > ( ) ,
2022-09-06 16:08:19 +02:00
locksService : locksService , // Cipher
device : device ,
specialFolder : specialFolder ,
cipher : null ,
theme : null ,
currentVersion : new Version ( 3 , 2 , 0 , 115 ) ,
lastVersion : new Version ( 3 , 0 , 173 ) ) ; // Current app version. Must be larger or equal 3.0.173 to
2021-06-26 20:57:55 +02:00
2024-04-09 12:53:23 +02:00
Assert . That ( l_oShareeBikeApp . ActiveUser . IsLoggedIn , Is . True ) ;
2022-09-06 16:08:19 +02:00
// There are 6 bikes available and 2, one reserved and one rented by javaminsiter.
2024-04-09 12:53:23 +02:00
Assert . That (
l_oShareeBikeApp . GetConnector ( true ) . Query . GetBikesAsync ( ) . Result . Response . Count , Is . EqualTo ( 10 ) ,
2022-09-06 16:08:19 +02:00
"Sum of bikes is 6 occupied plus 2 occupied." ) ;
2024-04-09 12:53:23 +02:00
Assert . That ( l_oShareeBikeApp . GetConnector ( true ) . Query . GetBikesOccupiedAsync ( ) . Result . Response . Count , Is . EqualTo ( 2 ) ,
2022-09-06 16:08:19 +02:00
"Javaminster occupies 2 bikes." ) ;
2024-04-09 12:53:23 +02:00
Assert . That ( l_oShareeBikeApp . GetConnector ( true ) . Command . SessionCookie , Is . EqualTo ( "6103_112e96b36ba33de245943c5ffaf369cd_" ) ) ;
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
// Log user out.
2024-04-09 12:53:23 +02:00
l_oShareeBikeApp . GetConnector ( true ) . Command . DoLogout ( ) . Wait ( ) ;
l_oShareeBikeApp . ActiveUser . Logout ( ) ;
2021-06-26 20:57:55 +02:00
2024-04-09 12:53:23 +02:00
Assert . That ( l_oShareeBikeApp . ActiveUser . IsLoggedIn , Is . False ) ;
Assert . That (
l_oShareeBikeApp . GetConnector ( true ) . Query . GetBikesAsync ( ) . Result . Response . Count , Is . EqualTo ( 8 ) ,
2022-09-06 16:08:19 +02:00
"Sum of bikes is 6 occupied, no one occupied because no user is logged in" ) ;
2024-04-09 12:53:23 +02:00
Assert . That (
l_oShareeBikeApp . GetConnector ( true ) . Query . GetBikesOccupiedAsync ( ) . Result . Response . Count , Is . EqualTo ( 0 ) ,
2022-09-06 16:08:19 +02:00
"If no user is logged in no occupied bikes are shown." ) ;
2024-04-09 12:53:23 +02:00
Assert . That ( l_oShareeBikeApp . GetConnector ( true ) . Command . SessionCookie , Is . Null ) ;
2022-09-06 16:08:19 +02:00
}
}
2021-06-26 20:57:55 +02:00
}