sharee.bike-App/SharedBusinessLogic.Tests/ViewModel/Account/TestAccountPageViewModel.cs

265 lines
13 KiB
C#
Raw Normal View History

2022-10-17 18:45:38 +02:00
using System;
2021-07-12 21:31:46 +02:00
using System.Collections.Generic;
using System.Threading.Tasks;
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.Model.Device;
using SharedBusinessLogic.Tests.Framework.Model.Services.Geolocation;
using SharedBusinessLogic.Tests.Framework.Model.User.Account;
using SharedBusinessLogic.Tests.Framework.Repository;
using SharedBusinessLogic.Tests.Framework.Services.BluetoothLock;
using ShareeBike.Model;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Model.Settings;
using ShareeBike.Repository;
using ShareeBike.Repository.Exception;
using ShareeBike.Services;
using ShareeBike.Services.Geolocation;
using ShareeBike.Services.Permissions;
using ShareeBike.View;
using ShareeBike.ViewModel.Account;
using ShareeBike.ViewModel.Map;
using ShareeBike.ViewModel.Settings;
using static ShareeBike.Repository.CopriCallsMemory;
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
namespace SharedBusinessLogic.Tests.Fixtures.ObjectTests.Account
2021-07-12 21:31:46 +02:00
{
2022-09-06 16:08:19 +02:00
public class TestAccountPageViewModel
{
[Test]
public void TestConstruct_NotLoggedIn()
{
const string MERCH_ID = "MyMerchId";
2022-08-30 15:42:25 +02:00
2024-04-09 12:53:23 +02:00
var shareeBikeApp = new ShareeBikeApp(
new ShareeBike.Model.Settings.Settings(
new GroupFilterMapPage(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.Off } }),
new GroupFilterSettings(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.On } }),
2022-10-17 18:45:38 +02:00
new StartupSettings(),
2022-09-06 16:08:19 +02:00
new Uri("https://shareeapp-primary.copri-bike.de/APIjsonserver"),
2024-04-09 12:53:23 +02:00
new ShareeBike.Settings.PollingParameters(new TimeSpan(10000), true),
2022-09-06 16:08:19 +02:00
Serilog.Events.LogEventLevel.Error,
activeLockService: typeof(LocksServiceMock).FullName,
activeGeolocationService: typeof(GeolocationMock).FullName),
new StoreMock(),
isConnectedFunc: () => true,
2023-02-22 14:03:35 +01:00
connectorFactory: (isConnected, uri, sessionCookie, mail, expiresAfter) => new ConnectorCache(new AppContextInfo(MERCH_ID, "MyApp", new Version(1, 2)), null /*UI language */, sessionCookie, mail, server: new CopriCallsMemory(MERCH_ID, SampleSets.Set2, 1, sessionCookie)),
2022-09-06 16:08:19 +02:00
merchantId: MERCH_ID,
bluetoothService: Substitute.For<IBluetoothLE>(),
locationPermissionsService: Substitute.For<ILocationPermission>(),
2023-04-05 15:02:10 +02:00
locationServicesContainer: Substitute.For<IServicesContainer<IGeolocationService>>(),
2022-09-06 16:08:19 +02:00
locksService: new LocksServiceMock(), // Cipher
device: new DeviceMock(),
specialFolder: new SpecialFolderMock(),
cipher: null,
theme: null,
postAction: (d, obj) => d(obj),
currentVersion: new Version(3, 2, 0, 115), // Current app version
lastVersion: new Version(3, 0, 173), // Current app version. Must be larger or equal 3.0.173 to
whatsNewShownInVersion: null); // Whats new page was never shown.
2021-07-12 21:31:46 +02:00
2023-04-05 15:02:10 +02:00
var viewService = Substitute.For<IViewService>();
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
var settingsPageViewModel = new AccountPageViewModel(
2024-04-09 12:53:23 +02:00
shareeBikeApp,
2022-09-06 16:08:19 +02:00
(uri) => { },
viewService);
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(settingsPageViewModel.LoggedInInfo, Is.EqualTo("No user logged in."));
Assert.That(settingsPageViewModel.IsBookingStateInfoVisible, Is.False, "No user logged in.");
Assert.That(settingsPageViewModel.BookingStateInfo, Is.EqualTo(string.Empty));
2022-09-06 16:08:19 +02:00
}
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public async Task TestConstruct()
{
const string MERCH_ID = "MyMerchId";
2022-08-30 15:42:25 +02:00
2024-04-09 12:53:23 +02:00
var shareeBikeApp = new ShareeBikeApp(
new ShareeBike.Model.Settings.Settings(
new GroupFilterMapPage(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.Off } }),
new GroupFilterSettings(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.On } }),
2022-10-17 18:45:38 +02:00
new StartupSettings(),
2022-09-06 16:08:19 +02:00
new Uri("https://shareeapp-primary.copri-bike.de/APIjsonserver"),
2024-04-09 12:53:23 +02:00
new ShareeBike.Settings.PollingParameters(new TimeSpan(10000), true),
2022-09-06 16:08:19 +02:00
Serilog.Events.LogEventLevel.Error,
activeLockService: typeof(LocksServiceMock).FullName,
activeGeolocationService: typeof(GeolocationMock).FullName),
2024-04-09 12:53:23 +02:00
new StoreMock(new ShareeBike.Model.User.Account.Account("a@b", "123456789", false, "UnknownCookie", new List<string> { "ShareeBike" })),
2022-09-06 16:08:19 +02:00
isConnectedFunc: () => true,
2023-02-22 14:03:35 +01:00
connectorFactory: (isConnected, uri, sessionCookie, mail, expiresAfter) => new ConnectorCache(new AppContextInfo(MERCH_ID, "MyApp", new Version(1, 2)), null /*UI language */, sessionCookie, mail, server: new CopriCallsMemory(MERCH_ID, SampleSets.Set2, 1, sessionCookie)),
2022-09-06 16:08:19 +02:00
merchantId: MERCH_ID,
bluetoothService: Substitute.For<IBluetoothLE>(),
locationPermissionsService: Substitute.For<ILocationPermission>(),
2023-04-05 15:02:10 +02:00
locationServicesContainer: Substitute.For<IServicesContainer<IGeolocationService>>(),
2022-09-06 16:08:19 +02:00
locksService: new LocksServiceMock(), // Cipher
device: new DeviceMock(),
specialFolder: new SpecialFolderMock(),
cipher: null,
theme: null,
postAction: (d, obj) => d(obj),
currentVersion: new Version(3, 2, 0, 115), // Current app version
lastVersion: new Version(3, 0, 173), // Current app version. Must be larger or equal 3.0.173 to
whatsNewShownInVersion: null); // Whats new page was never shown.
2021-07-12 21:31:46 +02:00
2023-04-05 15:02:10 +02:00
var viewService = Substitute.For<IViewService>();
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
var settingsPageViewModel = new AccountPageViewModel(
2024-04-09 12:53:23 +02:00
shareeBikeApp,
2022-09-06 16:08:19 +02:00
(uri) => { },
viewService);
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
await settingsPageViewModel.OnAppearing();
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(settingsPageViewModel.LoggedInInfo, Is.EqualTo("Logged in as a@b."));
Assert.That(settingsPageViewModel.IsBookingStateInfoVisible, Is.False, "A user is logged but no bikes requested/ booked.");
Assert.That(settingsPageViewModel.BookingStateInfo, Is.EqualTo(string.Empty));
2022-09-06 16:08:19 +02:00
}
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public async Task TestConstruct_TwoBikes()
{
2024-04-09 12:53:23 +02:00
var shareeBikeApp = new ShareeBikeApp(
new ShareeBike.Model.Settings.Settings(
new GroupFilterMapPage(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.Off } }),
new GroupFilterSettings(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.On } }),
2022-10-17 18:45:38 +02:00
new StartupSettings(),
2022-09-06 16:08:19 +02:00
new Uri("https://shareeapp-primary.copri-bike.de/APIjsonserver"),
2024-04-09 12:53:23 +02:00
new ShareeBike.Settings.PollingParameters(new TimeSpan(10000), true),
2022-09-06 16:08:19 +02:00
Serilog.Events.LogEventLevel.Error,
activeLockService: typeof(LocksServiceMock).FullName,
activeGeolocationService: typeof(GeolocationMock).FullName),
2024-04-09 12:53:23 +02:00
new StoreMock(new ShareeBike.Model.User.Account.Account("a@b", "123456789", false, "6103_112e96b36ba33de245943c5ffaf369cd_", new List<string> { "ShareeBike" })),
2022-09-06 16:08:19 +02:00
isConnectedFunc: () => true,
2023-02-22 14:03:35 +01:00
connectorFactory: (isConnected, uri, sessionCookie, mail, expiresAfter) => 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: Substitute.For<ILocationPermission>(),
2023-04-05 15:02:10 +02:00
locationServicesContainer: Substitute.For<IServicesContainer<IGeolocationService>>(),
2022-09-06 16:08:19 +02:00
locksService: new LocksServiceMock(), // Cipher
device: new DeviceMock(),
specialFolder: new SpecialFolderMock(),
cipher: null,
theme: null,
postAction: (d, obj) => d(obj),
currentVersion: new Version(3, 2, 0, 115), // Current app version
lastVersion: new Version(3, 0, 173), // Current app version. Must be larger or equal 3.0.173 to
whatsNewShownInVersion: null); // Whats new page was never shown.
2021-07-12 21:31:46 +02:00
2023-04-05 15:02:10 +02:00
var viewService = Substitute.For<IViewService>();
2022-09-06 16:08:19 +02:00
var settingsPageViewModel = new AccountPageViewModel(
2024-04-09 12:53:23 +02:00
shareeBikeApp,
2022-09-06 16:08:19 +02:00
(uri) => { },
viewService);
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
await settingsPageViewModel.OnAppearing();
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(settingsPageViewModel.LoggedInInfo, Is.EqualTo("Logged in as a@b."));
Assert.That(settingsPageViewModel.IsBookingStateInfoVisible, Is.True, "A user is logged but no bikes requested/ booked.");
Assert.That(settingsPageViewModel.BookingStateInfo, Is.EqualTo("Currently 2 bikes reserved/ booked."));
2022-09-06 16:08:19 +02:00
}
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public async Task TestConstruct_TwoBikes_Offline()
{
2024-04-09 12:53:23 +02:00
var shareeBikeApp = new ShareeBikeApp(
new ShareeBike.Model.Settings.Settings(
new GroupFilterMapPage(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.Off } }),
new GroupFilterSettings(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.On } }),
2022-10-17 18:45:38 +02:00
new StartupSettings(),
2022-09-06 16:08:19 +02:00
new Uri("https://shareeapp-primary.copri-bike.de/APIjsonserver"),
2024-04-09 12:53:23 +02:00
new ShareeBike.Settings.PollingParameters(new TimeSpan(10000), true),
2022-09-06 16:08:19 +02:00
Serilog.Events.LogEventLevel.Error,
activeLockService: typeof(LocksServiceMock).FullName,
activeGeolocationService: typeof(GeolocationMock).FullName),
2024-04-09 12:53:23 +02:00
new StoreMock(new ShareeBike.Model.User.Account.Account("a@b", "123456789", false, "6103_112e96b36ba33de245943c5ffaf369cd_", new List<string> { "ShareeBike" })),
2022-09-06 16:08:19 +02:00
isConnectedFunc: () => false,
2023-02-22 14:03:35 +01:00
connectorFactory: (isConnected, uri, sessionCookie, mail, expiresAfter) => 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: Substitute.For<ILocationPermission>(),
2023-04-05 15:02:10 +02:00
locationServicesContainer: Substitute.For<IServicesContainer<IGeolocationService>>(),
2022-09-06 16:08:19 +02:00
locksService: new LocksServiceMock(), // Cipher
device: new DeviceMock(),
specialFolder: new SpecialFolderMock(),
cipher: null, // Offline
theme: null,
postAction: (d, obj) => d(obj),
currentVersion: new Version(3, 2, 0, 115), // Current app version
lastVersion: new Version(3, 0, 173), // Current app version. Must be larger or equal 3.0.173 to
whatsNewShownInVersion: null); // Whats new page was never shown.
2021-07-12 21:31:46 +02:00
2023-04-05 15:02:10 +02:00
var viewService = Substitute.For<IViewService>();
2022-09-06 16:08:19 +02:00
var settingsPageViewModel = new AccountPageViewModel(
2024-04-09 12:53:23 +02:00
shareeBikeApp,
2022-09-06 16:08:19 +02:00
(uri) => { },
viewService);
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
await settingsPageViewModel.OnAppearing();
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(settingsPageViewModel.LoggedInInfo, Is.EqualTo("Logged in as a@b."));
Assert.That(settingsPageViewModel.IsBookingStateInfoVisible, Is.True, "A user is logged but no bikes requested/ booked.");
Assert.That(settingsPageViewModel.BookingStateInfo, Is.EqualTo("Currently 2 bikes reserved/ booked. Connectivity: Offline."));
2022-09-06 16:08:19 +02:00
}
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
[Test]
public async Task TestConstruct_TwoBikes_WebConnectCommunicationError()
{
2024-04-09 12:53:23 +02:00
var shareeBikeApp = new ShareeBikeApp(
new ShareeBike.Model.Settings.Settings(
new GroupFilterMapPage(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.Off } }),
new GroupFilterSettings(new Dictionary<string, FilterState> { { "ShareeBike", FilterState.On }, { "Citybike", FilterState.On } }),
2022-10-17 18:45:38 +02:00
new StartupSettings(),
2022-09-06 16:08:19 +02:00
new Uri("https://shareeapp-primary.copri-bike.de/APIjsonserver"),
2024-04-09 12:53:23 +02:00
new ShareeBike.Settings.PollingParameters(new TimeSpan(10000), true),
2022-09-06 16:08:19 +02:00
Serilog.Events.LogEventLevel.Error,
activeLockService: typeof(LocksServiceMock).FullName,
activeGeolocationService: typeof(GeolocationMock).FullName),
2024-04-09 12:53:23 +02:00
new StoreMock(new ShareeBike.Model.User.Account.Account("a@b", "123456789", false, "6103_112e96b36ba33de245943c5ffaf369cd_", new List<string> { "ShareeBike" })),
2022-09-06 16:08:19 +02:00
isConnectedFunc: () => false,
2024-04-09 12:53:23 +02:00
connectorFactory: (isConnected, uri, sessionCookie, mail, expiresAfter) => new ShareeBike.Model.Connector.Connector(
2022-09-06 16:08:19 +02:00
uri,
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
sessionCookie,
mail,
server: new CopriProviderHttps(
uri,
2024-04-09 12:53:23 +02:00
ShareeBikeApp.MerchantId,
2022-09-06 16:08:19 +02:00
new AppContextInfo("oiF2kahH", "sharee.bike.test", new Version(3, 0, 267)),
null /*UI language */,
sessionCookie: sessionCookie,
cacheServer: new CopriCallsCacheMemory001(sessionCookie: sessionCookie),
2023-08-31 12:20:06 +02:00
httpsServer: new ExceptionServer((msg) => new WebConnectFailureException(msg, new Exception("Source exception."))))),
2022-09-06 16:08:19 +02:00
merchantId: "MyMerchId",
bluetoothService: Substitute.For<IBluetoothLE>(),
locationPermissionsService: Substitute.For<ILocationPermission>(),
2023-04-05 15:02:10 +02:00
locationServicesContainer: Substitute.For<IServicesContainer<IGeolocationService>>(),
2022-09-06 16:08:19 +02:00
locksService: new LocksServiceMock(), // Cipher
device: new DeviceMock(),
specialFolder: new SpecialFolderMock(),
cipher: null, // Offline
theme: null,
postAction: (d, obj) => d(obj),
currentVersion: new Version(3, 2, 0, 115), // Current app version
lastVersion: new Version(3, 0, 173), // Current app version. Must be larger or equal 3.0.173 to
whatsNewShownInVersion: null); // Whats new page was never shown.
2021-07-12 21:31:46 +02:00
2023-04-05 15:02:10 +02:00
var viewService = Substitute.For<IViewService>();
2022-09-06 16:08:19 +02:00
var settingsPageViewModel = new AccountPageViewModel(
2024-04-09 12:53:23 +02:00
shareeBikeApp,
2022-09-06 16:08:19 +02:00
(uri) => { },
viewService);
2021-07-12 21:31:46 +02:00
2022-09-06 16:08:19 +02:00
await settingsPageViewModel.OnAppearing();
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(settingsPageViewModel.LoggedInInfo, Is.EqualTo("Logged in as a@b.")); // CopriCallsCacheMemory(SampleSets.Set2,
Assert.That(settingsPageViewModel.IsBookingStateInfoVisible, Is.True, "A user is logged but no bikes requested/ booked.");
Assert.That(settingsPageViewModel.BookingStateInfo, Is.EqualTo("Currently 2 bikes reserved/ booked. Connectivity: Offline."));
2022-09-06 16:08:19 +02:00
}
}
2021-07-12 21:31:46 +02:00
}