diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs index ccbbfc13..4434870e 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs @@ -56,7 +56,10 @@ namespace Bootstrap.Admin.Controllers.Api { // 判断 Cron 表达式 if (string.IsNullOrEmpty(widget.CronExpression)) return false; - if (_tasks.Any(t => t.Equals(widget.Name, StringComparison.OrdinalIgnoreCase))) return false; + + // 系统内置任务禁止更改 + // 演示模式下禁止删除内置任务 + if (DictHelper.RetrieveSystemModel() && _tasks.Any(t => t.Equals(widget.Name, StringComparison.OrdinalIgnoreCase))) return false; // 加载任务执行体 // 此处可以扩展为任意 DLL 中的任意继承 ITask 接口的实体类 diff --git a/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs b/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs index 160c86b5..826f16d5 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/TasksTest.cs @@ -33,5 +33,64 @@ namespace Bootstrap.Admin.Api var recv = await Client.PutAsJsonAsync("/api/Tasks/SQL日志?operType=run", ""); Assert.True(recv); } + + [Fact] + public async void Post_Ok() + { + var widget = new TaskWidget(); + + // widget Cron 表达式为 ”“ + var resp = await Client.PostAsJsonAsync("/api/Tasks", widget); + Assert.False(resp); + + // task executor 不合法 + widget.CronExpression = Longbow.Tasks.Cron.Secondly(5); + widget.TaskExecutorName = "UnitTest-Widget"; + resp = await Client.PostAsJsonAsync("/api/Tasks", widget); + Assert.False(resp); + + widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor"; + widget.Name = "UnitTest-Task"; + resp = await Client.PostAsJsonAsync("/api/Tasks", widget); + Assert.True(resp); + + // Delete + resp = await Client.DeleteAsJsonAsync, bool>("/api/Tasks", new string[] { widget.Name }); + Assert.True(resp); + } + } + + [Collection("SystemModel")] + public class TasksSystemModelTest + { + private HttpClient client; + + public TasksSystemModelTest(BASystemModelWebHost factory) + { + client = factory.CreateClient("/api/Tasks"); + } + + [Fact] + public async void Post_Ok() + { + var widget = new TaskWidget(); + widget.CronExpression = Longbow.Tasks.Cron.Secondly(5); + widget.Name = "单次任务"; + widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor"; + + // 演示模式下禁止移除系统内置任务 + var resp = await client.PostAsJsonAsync("/api/Tasks", widget); + Assert.False(resp); + + resp = await client.DeleteAsJsonAsync, bool>("/api/Tasks", new string[] { widget.Name }); + Assert.False(resp); + + widget.Name = "Test-Widget"; + resp = await client.PostAsJsonAsync("/api/Tasks", widget); + Assert.True(resp); + + resp = await client.DeleteAsJsonAsync, bool>("/api/Tasks", new string[] { widget.Name }); + Assert.True(resp); + } } }