Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Net;
using MaiChartManager.Controllers.Tools;
using MaiChartManager.Platform;
using MaiChartManager.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MaiChartManager.Tests.Controllers.Tools;

[Collection("Static settings")]
public sealed class ResourceJunctionControllerTests
{
[Fact]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: 测试 LocalWriteActionDoesNotRequireAnAuthenticationHeader 缺少 Linux 平台守卫,在 Linux 构建上会失败。控制器 SelectResourceJunctionSourceif (!OperatingSystem.IsWindows()) return Ok(CreateUnsupportedOverview()) 之前不会调用文件夹选择器,因此 Linux 上 PickFolderCalls 为 0 而非断言的 1。建议按 LinuxUnsupportedStatusDoesNotOpenFolderPicker 的方式补上 if (!OperatingSystem.IsWindows()) return; 守卫或把该测试限定为仅 Windows 运行。

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At MaiChartManager.Tests/Controllers/Tools/ResourceJunctionControllerTests.cs, line 13:

<comment>测试 `LocalWriteActionDoesNotRequireAnAuthenticationHeader` 缺少 Linux 平台守卫,在 Linux 构建上会失败。控制器 `SelectResourceJunctionSource` 在 `if (!OperatingSystem.IsWindows()) return Ok(CreateUnsupportedOverview())` 之前不会调用文件夹选择器,因此 Linux 上 `PickFolderCalls` 为 0 而非断言的 1。建议按 `LinuxUnsupportedStatusDoesNotOpenFolderPicker` 的方式补上 `if (!OperatingSystem.IsWindows()) return;` 守卫或把该测试限定为仅 Windows 运行。</comment>

<file context>
@@ -0,0 +1,91 @@
+[Collection("Static settings")]
+public sealed class ResourceJunctionControllerTests
+{
+    [Fact]
+    public void StatusRejectsRemoteRequests()
+    {
</file context>

public void StatusRejectsRemoteRequests()
{
var controller = CreateController(IPAddress.Parse("192.0.2.1"));

var result = controller.GetResourceJunctionStatus();

var status = Assert.IsType<StatusCodeResult>(result.Result);
Assert.Equal(StatusCodes.Status403Forbidden, status.StatusCode);
}

[Fact]
public void WriteActionRejectsRemoteRequestsBeforeUsingDesktopCapabilities()
{
var dialogService = new RecordingDialogService();
var controller = CreateController(IPAddress.Parse("192.0.2.1"), dialogService);
var result = controller.SelectResourceJunctionSource();

var status = Assert.IsType<StatusCodeResult>(result.Result);
Assert.Equal(StatusCodes.Status403Forbidden, status.StatusCode);
Assert.Equal(0, dialogService.PickFolderCalls);
}

[Fact]
public void LocalWriteActionDoesNotRequireAnAuthenticationHeader()
{
var dialogService = new RecordingDialogService();
var controller = CreateController(IPAddress.Loopback, dialogService);

var result = controller.SelectResourceJunctionSource();

Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(1, dialogService.PickFolderCalls);
}

[Fact]
public void LinuxUnsupportedStatusDoesNotOpenFolderPicker()
{
if (OperatingSystem.IsWindows()) return;
var dialogService = new RecordingDialogService();
var controller = CreateController(IPAddress.Loopback, dialogService);

var result = controller.SelectResourceJunctionSource();

Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(0, dialogService.PickFolderCalls);
}

private static ResourceJunctionController CreateController(
IPAddress remoteAddress,
RecordingDialogService? dialogService = null)
{
StaticSettings.Config = new Config();
var context = new DefaultHttpContext();
context.Connection.RemoteIpAddress = remoteAddress;
return new ResourceJunctionController(
new ResourceJunctionService(() => string.Empty, () => []),
dialogService ?? new RecordingDialogService())
{
ControllerContext = new ControllerContext { HttpContext = context },
};
}

private sealed class RecordingDialogService : IDesktopDialogService
{
public int PickFolderCalls { get; private set; }

public string? PickFolder(string? title = null)
{
PickFolderCalls++;
return null;
}

public string? PickFile(string? title = null, string? filter = null) => null;
public bool Confirm(string message, string title, bool defaultResult = false) => defaultResult;
public void ShowError(string message, string title) { }
}

}
4 changes: 3 additions & 1 deletion MaiChartManager.Tests/MaiChartManager.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<IsLinuxBuild Condition="$(Configuration.StartsWith('Linux'))">true</IsLinuxBuild>
<TargetFramework Condition="'$(IsLinuxBuild)' != 'true'">net10.0-windows10.0.17763.0</TargetFramework>
<TargetFramework Condition="'$(IsLinuxBuild)' == 'true'">net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand Down
Loading