releases/tests/Paca.Tests/Browser/ChromeSitePermissionsCoordinatorTests.cs

91 lines
3.8 KiB
C#

using System;
using System.IO;
using System.Linq;
using Paca.Browser;
using Paca.Browser.Permissions;
using Xunit;
namespace Paca.Tests.Browser;
public class ChromeSitePermissionsCoordinatorTests
{
[Theory]
[InlineData("https://www.youtube.com/watch?v=123", "https://www.youtube.com")]
[InlineData("http://localhost:52100/index.html#hash", "http://localhost:52100")]
[InlineData("https://meet.google.com:443/abc-def-ghi", "https://meet.google.com")]
[InlineData("not a valid url", "")]
public void CanonicalOrigin_Extracts_Scheme_Host_Port_Correctly(string raw, string expected)
{
var origin = ChromeSitePermissionsCoordinator.GetCanonicalOrigin(raw);
Assert.Equal(expected, origin);
}
[Fact]
public void SiteSecurityInfo_Reflects_Https_And_Permissions()
{
var coord = new ChromeSitePermissionsCoordinator();
coord.SetPermission("https://app.slack.com", "Camera", PermissionVerdict.Allow);
coord.SetPermission("https://app.slack.com", "Notifications", PermissionVerdict.Deny);
var infoHttps = coord.GetSiteSecurityInfo("https://app.slack.com/messages/general");
Assert.True(infoHttps.IsSecure);
Assert.Equal("https://app.slack.com", infoHttps.Origin);
Assert.Contains("Camera", infoHttps.AllowedPermissions);
Assert.Contains("Notifications", infoHttps.BlockedPermissions);
var infoHttp = coord.GetSiteSecurityInfo("http://insecure-site.org/login");
Assert.False(infoHttp.IsSecure);
Assert.Equal("Insecure", infoHttp.SecurityStatus);
}
[Fact]
public void GeolocationOverride_Manages_Presets_And_Custom_Coordinates()
{
var coord = new ChromeSitePermissionsCoordinator();
// Default: no override active
Assert.Null(coord.CurrentGeolocationOverride);
// Apply Seoul preset
coord.ApplyGeolocationPreset("Seoul");
Assert.NotNull(coord.CurrentGeolocationOverride);
Assert.Equal("Seoul", coord.CurrentGeolocationOverride.Name);
Assert.Equal(37.5665, Math.Round(coord.CurrentGeolocationOverride.Latitude, 4));
Assert.Equal(126.9780, Math.Round(coord.CurrentGeolocationOverride.Longitude, 4));
// Clear override
coord.ClearGeolocationOverride();
Assert.Null(coord.CurrentGeolocationOverride);
// Custom override
coord.SetCustomGeolocation(35.6762, 139.6503, 10.0, "Tokyo Tower");
Assert.NotNull(coord.CurrentGeolocationOverride);
Assert.Equal(35.6762, coord.CurrentGeolocationOverride.Latitude);
Assert.Equal(139.6503, coord.CurrentGeolocationOverride.Longitude);
Assert.Equal(10.0, coord.CurrentGeolocationOverride.Accuracy);
}
[Fact]
public void Coordinator_Persistence_Saves_And_Restores_Correctly()
{
var tempFile = Path.Combine(Path.GetTempPath(), "paca_perm_coord_" + Guid.NewGuid().ToString("N") + ".json");
try
{
var coord1 = new ChromeSitePermissionsCoordinator();
coord1.SetPermission("https://maps.google.com", "Geolocation", PermissionVerdict.Allow);
coord1.SetPermission("https://meet.google.com", "Microphone", PermissionVerdict.Allow);
coord1.SaveToFile(tempFile);
var coord2 = new ChromeSitePermissionsCoordinator();
coord2.LoadFromFile(tempFile);
Assert.Equal(PermissionVerdict.Allow, coord2.GetPermission("https://maps.google.com", "Geolocation"));
Assert.Equal(PermissionVerdict.Allow, coord2.GetPermission("https://meet.google.com", "Microphone"));
Assert.Equal(PermissionVerdict.Ask, coord2.GetPermission("https://meet.google.com", "Camera"));
}
finally
{
if (File.Exists(tempFile)) File.Delete(tempFile);
}
}
}