Skip to content

Commit

Permalink
Remove ClientProfileItem from ClientProfileService
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Nov 27, 2024
1 parent 12aad33 commit 700bade
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 214 deletions.
1 change: 1 addition & 0 deletions Tests/VpnHood.Test/Tests/AccessTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace VpnHood.Test.Tests;
[TestClass]
public class AccessTest : TestBase
{

[TestMethod]
public async Task AFoo()
{
Expand Down
20 changes: 10 additions & 10 deletions Tests/VpnHood.Test/Tests/ClientAppTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ public async Task ExcludeDomains()

// connect
var token = TestHelper.CreateAccessToken(server);
var clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
await app.Connect(clientProfileItem.ClientProfileId, diagnose: true);
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
await app.Connect(clientProfile.ClientProfileId, diagnose: true);
await TestHelper.WaitForAppState(app, AppConnectionState.Connected);

// text include
Expand All @@ -560,8 +560,8 @@ public async Task Premium_token_must_create_premium_session()
var token = TestHelper.CreateAccessToken(server);

await using var app = TestHelper.CreateClientApp();
var clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
await app.Connect(clientProfileItem.ClientProfileId, diagnose: true);
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
await app.Connect(clientProfile.ClientProfileId, diagnose: true);


Assert.IsTrue(app.State.ClientProfile?.IsPremiumAccount);
Expand All @@ -581,22 +581,22 @@ public async Task ServerLocation_must_reset_to_default_for_no_server_error()

// Create App
await using var clientApp = TestHelper.CreateClientApp();
var clientProfileItem = clientApp.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientApp.ClientProfileService.Update(clientProfileItem.ClientProfileId, new ClientProfileUpdateParams {
var clientProfile = clientApp.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientApp.ClientProfileService.Update(clientProfile.ClientProfileId, new ClientProfileUpdateParams {
SelectedLocation = "FR/Paris"
});

// Connect
try {
await clientApp.Connect(clientProfileItem.ClientProfileId);
await clientApp.Connect(clientProfile.ClientProfileId);
Assert.Fail("SessionException was expected.");
}
catch (SessionException ex) {
Assert.AreEqual(SessionErrorCode.NoServerAvailable, ex.SessionResponse.ErrorCode);
}

// reload clientProfileItem
clientProfileItem = clientApp.ClientProfileService.Get(clientProfileItem.ClientProfileId);
Assert.IsTrue(clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.IsAuto);
// reload clientProfile
clientProfile = clientApp.ClientProfileService.Get(clientProfile.ClientProfileId);
Assert.IsTrue(clientProfile.ToInfo().SelectedLocationInfo?.IsAuto);
}
}
104 changes: 52 additions & 52 deletions Tests/VpnHood.Test/Tests/ClientProfileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task BuiltIn_AccessKeys_RemoveOldKeys()
Assert.AreEqual(tokens2[0].TokenId, clientProfiles[0].Token.TokenId);
Assert.AreEqual(tokens2[1].TokenId, clientProfiles[1].Token.TokenId);
foreach (var clientProfile in clientProfiles)
Assert.IsTrue(clientProfile.ClientProfile.IsBuiltIn);
Assert.IsTrue(clientProfile.ToInfo().IsBuiltIn);
}

[TestMethod]
Expand Down Expand Up @@ -114,8 +114,8 @@ public async Task ClientPolicy()

// test free US client
app.UpdateCurrentCountry("US");
var clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
var clientProfileInfo = clientProfileItem.ClientProfileInfo;
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
var clientProfileInfo = clientProfile.ToInfo();

// default (*/*)
var location = clientProfileInfo.LocationInfos.Single(x => x.ServerLocation == "*/*");
Expand Down Expand Up @@ -146,7 +146,7 @@ public async Task ClientPolicy()

// (US/*) no free for CA clients
app.UpdateCurrentCountry("CA");
clientProfileInfo = app.ClientProfileService.Get(clientProfileInfo.ClientProfileId).ClientProfileInfo;
clientProfileInfo = app.ClientProfileService.Get(clientProfileInfo.ClientProfileId).ToInfo();
location = clientProfileInfo.LocationInfos.Single(x => x.ServerLocation == "US/*");
Assert.IsFalse(location.Options.HasFree);
Assert.IsTrue(location.Options.HasPremium);
Expand All @@ -157,8 +157,8 @@ public async Task ClientPolicy()

// create premium token
token.IsPublic = false;
clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientProfileInfo = clientProfileItem.ClientProfileInfo;
clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientProfileInfo = clientProfile.ToInfo();
location = clientProfileInfo.LocationInfos.Single(x => x.ServerLocation == "FR/*");
Assert.IsFalse(location.Options.HasFree);
Assert.IsTrue(location.Options.HasPremium);
Expand All @@ -179,9 +179,9 @@ public async Task Crud()
// *** TEST ***: AddAccessKey should add a clientProfile
var token1 = CreateToken();
token1.ServerToken.ServerLocations = ["us", "us/california"];
var clientProfile1 = app.ClientProfileService.ImportAccessKey(token1.ToAccessKey());
var clientProfile = app.ClientProfileService.ImportAccessKey(token1.ToAccessKey());
Assert.IsNotNull(app.ClientProfileService.FindByTokenId(token1.TokenId), "ClientProfile is not added");
Assert.AreEqual(token1.TokenId, clientProfile1.Token.TokenId,
Assert.AreEqual(token1.TokenId, clientProfile.Token.TokenId,
"invalid tokenId has been assigned to clientProfile");

// ************
Expand Down Expand Up @@ -216,18 +216,18 @@ public async Task Crud()
IsPremiumLocationSelected = true,
SelectedLocation = "us/california"
};
app.ClientProfileService.Update(clientProfile1.ClientProfileId, updateParams);
var clientProfile = app.ClientProfileService.Get(clientProfile1.ClientProfileId);
Assert.AreEqual(updateParams.ClientProfileName.Value, clientProfile.BaseInfo.ClientProfileName);
Assert.AreEqual(updateParams.IsFavorite.Value, clientProfile.ClientProfile.IsFavorite);
Assert.AreEqual(updateParams.CustomData.Value, clientProfile.ClientProfile.CustomData);
Assert.AreEqual(updateParams.IsPremiumLocationSelected.Value, clientProfile.ClientProfile.IsPremiumLocationSelected);
Assert.AreEqual(updateParams.SelectedLocation.Value, clientProfile.ClientProfile.SelectedLocation);
app.ClientProfileService.Update(clientProfile.ClientProfileId, updateParams);
clientProfile = app.ClientProfileService.Get(clientProfile.ClientProfileId);
Assert.AreEqual(updateParams.ClientProfileName.Value, clientProfile.ClientProfileName);
Assert.AreEqual(updateParams.IsFavorite.Value, clientProfile.IsFavorite);
Assert.AreEqual(updateParams.CustomData.Value, clientProfile.CustomData);
Assert.AreEqual(updateParams.IsPremiumLocationSelected.Value, clientProfile.IsPremiumLocationSelected);
Assert.AreEqual(updateParams.SelectedLocation.Value, clientProfile.SelectedLocation);

// ************
// *** TEST ***: RemoveClientProfile
app.ClientProfileService.Remove(clientProfile1.ClientProfileId);
Assert.IsNull(app.ClientProfileService.FindById(clientProfile1.ClientProfileId),
app.ClientProfileService.Remove(clientProfile.ClientProfileId);
Assert.IsNull(app.ClientProfileService.FindById(clientProfile.ClientProfileId),
"ClientProfile has not been removed!");
}

Expand Down Expand Up @@ -267,38 +267,38 @@ public async Task Default_ServerLocation()
token.ServerToken.ServerLocations = null;

// if there is no server location, it should be null
var clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
Assert.IsNull(clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.ServerLocation);
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey()).ToInfo();
Assert.IsNull(clientProfile.SelectedLocationInfo?.ServerLocation);

// if there is no server location, it should be null
token.ServerToken.ServerLocations = [];
clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
Assert.IsNull(clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.ServerLocation);
clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey()).ToInfo();
Assert.IsNull(clientProfile.SelectedLocationInfo?.ServerLocation);

// if no server location is set, it should return the first server location
token.ServerToken.ServerLocations = ["US/California"];
clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
Assert.AreEqual("US/California", clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.ServerLocation);
clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey()).ToInfo();
Assert.AreEqual("US/California", clientProfile.SelectedLocationInfo?.ServerLocation);

// if null server location is set, it should return the first server location
app.ClientProfileService.Update(clientProfileItem.ClientProfileId,
app.ClientProfileService.Update(clientProfile.ClientProfileId,
new ClientProfileUpdateParams { SelectedLocation = null });
Assert.AreEqual("US/California", clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.ServerLocation);
Assert.AreEqual("US/California", clientProfile.SelectedLocationInfo?.ServerLocation);

// if wrong server location is set for one location, it should return the first server location
app.ClientProfileService.Update(clientProfileItem.ClientProfileId,
app.ClientProfileService.Update(clientProfile.ClientProfileId,
new ClientProfileUpdateParams { SelectedLocation = "US/Cal_Wrong" });
Assert.AreEqual("US/California", clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.ServerLocation);
Assert.AreEqual("US/California", clientProfile.SelectedLocationInfo?.ServerLocation);

// if no server location is set for two location, it should return auto
token.ServerToken.ServerLocations = ["US/California", "FR/Paris"];
clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
Assert.IsTrue(ServerLocationInfo.IsAutoLocation(clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.ServerLocation));
clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey()).ToInfo();
Assert.IsTrue(ServerLocationInfo.IsAutoLocation(clientProfile.SelectedLocationInfo?.ServerLocation));

// if wrong server location is set for two location, it should return auto
token.ServerToken.ServerLocations = ["US/California", "FR/Paris"];
Assert.IsTrue(ServerLocationInfo.IsAutoLocation(clientProfileItem.ClientProfileInfo.SelectedLocationInfo?.ServerLocation));
app.ClientProfileService.Update(clientProfileItem.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/Cal_Wrong" });
Assert.IsTrue(ServerLocationInfo.IsAutoLocation(clientProfile.SelectedLocationInfo?.ServerLocation));
app.ClientProfileService.Update(clientProfile.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/Cal_Wrong" });
}


Expand All @@ -311,30 +311,30 @@ public async Task Update_server_location()
var token = CreateToken();
token.ServerToken.ServerLocations = ["US/texas [#tag1]", "US/california [#tag1 #tag2]"];

var clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
app.UserSettings.ClientProfileId = clientProfileItem.BaseInfo.ClientProfileId;
var clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
app.UserSettings.ClientProfileId = clientProfile.ClientProfileId;

app.ClientProfileService.Update(clientProfileItem.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/*" });
Assert.AreEqual("US/*", app.State.ClientServerLocationInfo?.ServerLocation);
CollectionAssert.AreEquivalent(new[] { "#tag1", "~#tag2" }, app.State.ClientServerLocationInfo?.Tags);
app.ClientProfileService.Update(clientProfile.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/*" });
Assert.AreEqual("US/*", app.State.ClientProfile?.SelectedLocationInfo?.ServerLocation);
CollectionAssert.AreEquivalent(new[] { "#tag1", "~#tag2" }, app.State.ClientProfile?.SelectedLocationInfo?.Tags);

app.ClientProfileService.Update(clientProfileItem.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/california" });
CollectionAssert.AreEquivalent(new[] { "#tag1", "#tag2" }, app.State.ClientServerLocationInfo?.Tags);
app.ClientProfileService.Update(clientProfile.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/california" });
CollectionAssert.AreEquivalent(new[] { "#tag1", "#tag2" }, app.State.ClientProfile?.SelectedLocationInfo?.Tags);

app.ClientProfileService.Update(clientProfileItem.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/texas" });
CollectionAssert.AreEquivalent(new[] { "#tag1", }, app.State.ClientServerLocationInfo?.Tags);
app.ClientProfileService.Update(clientProfile.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "US/texas" });
CollectionAssert.AreEquivalent(new[] { "#tag1", }, app.State.ClientProfile?.SelectedLocationInfo?.Tags);

// test three regin
token = CreateToken();
token.ServerToken.ServerLocations = ["US/texas", "US/california [#z1 #z2]", "FR/paris [#p1 #p2]"];
clientProfileItem = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
app.UserSettings.ClientProfileId = clientProfileItem.BaseInfo.ClientProfileId;
app.ClientProfileService.Update(clientProfileItem.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "FR/paris" });
CollectionAssert.AreEquivalent(new[] { "#p1", "#p2" }, app.State.ClientServerLocationInfo?.Tags);
clientProfile = app.ClientProfileService.ImportAccessKey(token.ToAccessKey());
app.UserSettings.ClientProfileId = clientProfile.ClientProfileId;
app.ClientProfileService.Update(clientProfile.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "FR/paris" });
CollectionAssert.AreEquivalent(new[] { "#p1", "#p2" }, app.State.ClientProfile?.SelectedLocationInfo?.Tags);

app.ClientProfileService.Update(clientProfileItem.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "*/*" });
app.ClientProfileService.Update(clientProfile.ClientProfileId, new ClientProfileUpdateParams { SelectedLocation = "*/*" });
app.Settings.Save();
CollectionAssert.AreEquivalent(new[] { "~#p1", "~#p2", "~#z1", "~#z2" }, app.State.ClientServerLocationInfo?.Tags);
CollectionAssert.AreEquivalent(new[] { "~#p1", "~#p2", "~#z1", "~#z2" }, app.State.ClientProfile?.SelectedLocationInfo?.Tags);
}

[TestMethod]
Expand All @@ -345,8 +345,8 @@ public async Task ServerLocations()
// test two region in a same country
var token = CreateToken();
token.ServerToken.ServerLocations = ["US", "US/california"];
var clientProfileItem = app1.ClientProfileService.ImportAccessKey(token.ToAccessKey());
var clientProfileInfo = clientProfileItem.ClientProfileInfo;
var clientProfile = app1.ClientProfileService.ImportAccessKey(token.ToAccessKey());
var clientProfileInfo = clientProfile.ToInfo();
var serverLocations = clientProfileInfo.LocationInfos.Select(x => x.ServerLocation).ToArray();
var i = 0;
Assert.AreEqual("US/*", serverLocations[i++]);
Expand All @@ -360,8 +360,8 @@ public async Task ServerLocations()
// test multiple countries
token = CreateToken();
token.ServerToken.ServerLocations = ["US", "US/california", "uk"];
clientProfileItem = app1.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientProfileInfo = clientProfileItem.ClientProfileInfo;
clientProfile = app1.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientProfileInfo = clientProfile.ToInfo();
serverLocations = clientProfileInfo.LocationInfos.Select(x => x.ServerLocation).ToArray();
i = 0;
Assert.AreEqual("*/*", serverLocations[i++]);
Expand All @@ -375,8 +375,8 @@ public async Task ServerLocations()
// test multiple countries
token = CreateToken();
token.ServerToken.ServerLocations = ["us/virgina", "us/california", "uk/england [#pr]", "uk/region2"];
clientProfileItem = app1.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientProfileInfo = clientProfileItem.ClientProfileInfo;
clientProfile = app1.ClientProfileService.ImportAccessKey(token.ToAccessKey());
clientProfileInfo = clientProfile.ToInfo();
serverLocations = clientProfileInfo.LocationInfos.Select(x => x.ServerLocation).ToArray();
i = 0;
Assert.AreEqual("*/*", serverLocations[i++]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Android.Content.Res;
using Android.Runtime;
using Android.Views;
using VpnHood.Client.App.ClientProfiles;
using VpnHood.Client.Device;
using VpnHood.Client.Device.Droid;
using VpnHood.Client.Device.Droid.ActivityEvents;
Expand Down Expand Up @@ -93,7 +94,7 @@ private bool ProcessIntent(Intent? intent)
protected void ImportAccessKey(string accessKey)
{
var profiles = VpnHoodApp.Instance.ClientProfileService.List();
var profileInfo = VpnHoodApp.Instance.ClientProfileService.ImportAccessKey(accessKey).BaseInfo;
var profileInfo = VpnHoodApp.Instance.ClientProfileService.ImportAccessKey(accessKey).ToInfo();
VpnHoodApp.Instance.UserSettings.ClientProfileId = profileInfo.ClientProfileId;

var isNew = profiles.Any(x => x.ClientProfileId == profileInfo.ClientProfileId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void Update(bool force = false)

// connection status
// Set subtitle
var activeProfileName = _vpnHoodApp.CurrentClientProfileItem?.BaseInfo.ClientProfileName;
var activeProfileName = _vpnHoodApp.CurrentClientProfileInfo?.ClientProfileName;
_notificationBuilder.SetContentTitle(activeProfileName);
_notificationBuilder.SetSubText(connectionState == AppConnectionState.Connected
? $"{connectionState}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void Refresh()
if (OperatingSystem.IsAndroidVersionAtLeast(30))
QsTile.StateDescription = VpnHoodApp.Instance.ConnectionState.ToString();

var currentProfileInfo = VpnHoodApp.Instance.CurrentClientProfileItem?.BaseInfo;
var currentProfileInfo = VpnHoodApp.Instance.CurrentClientProfileInfo;
if (currentProfileInfo != null && !VpnHoodApp.Instance.IsIdle) {
QsTile.Label = currentProfileInfo.ClientProfileName;
QsTile.State = VpnHoodApp.Instance.ConnectionState ==
Expand Down
3 changes: 2 additions & 1 deletion VpnHood.Client.App.WebServer/Controllers/AppController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EmbedIO;
using EmbedIO.Routing;
using EmbedIO.WebApi;
using VpnHood.Client.App.ClientProfiles;
using VpnHood.Client.App.Settings;
using VpnHood.Client.App.WebServer.Api;
using VpnHood.Client.Device;
Expand Down Expand Up @@ -31,7 +32,7 @@ public Task<AppConfig> GetConfig()
var ret = new AppConfig {
Features = App.Features,
Settings = App.Settings,
ClientProfileInfos = App.ClientProfileService.List().Select(x => x.ClientProfileInfo).ToArray(),
ClientProfileInfos = App.ClientProfileService.List().Select(x => x.ToInfo()).ToArray(),
State = App.State,
AvailableCultureInfos = App.Services.CultureProvider.AvailableCultures
.Select(x => new UiCultureInfo(x))
Expand Down
Loading

0 comments on commit 700bade

Please sign in to comment.