From c76dc3fc4567b8d82bc56cdb5630549d0f4c68d7 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 24 May 2023 21:42:26 +0200 Subject: [PATCH] Rule events. --- .../TestSuite.ApiTests/AnonymousTests.cs | 4 +- .../TestSuite.ApiTests/AppClientsTests.cs | 6 +- .../AppContributorsTests.cs | 8 +- .../TestSuite.ApiTests/AppLanguagesTests.cs | 10 +- .../TestSuite.ApiTests/AppWorkflowsTests.cs | 19 ++- .../TestSuite.ApiTests/AssetTests.cs | 6 +- .../TestSuite.ApiTests/BackupTests.cs | 2 +- .../TestSuite.ApiTests/ContentUpdateTests.cs | 2 +- .../TestSuite.ApiTests/RuleEventsTests.cs | 119 ++++++++++++++++++ .../TestSuite.ApiTests/RuleRunnerTests.cs | 14 +-- .../TestSuite/TestSuite.ApiTests/RuleTests.cs | 7 +- .../Fixtures/ClientFixture.cs | 4 +- 12 files changed, 159 insertions(+), 42 deletions(-) create mode 100644 tools/TestSuite/TestSuite.ApiTests/RuleEventsTests.cs diff --git a/tools/TestSuite/TestSuite.ApiTests/AnonymousTests.cs b/tools/TestSuite/TestSuite.ApiTests/AnonymousTests.cs index 56752f4e4..240bd98eb 100644 --- a/tools/TestSuite/TestSuite.ApiTests/AnonymousTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/AnonymousTests.cs @@ -46,7 +46,7 @@ public class AnonymousTests : IClassFixture // STEP 3: Check anonymous permission. - var url = $"{_.Client.Options.Url}api/apps/{appName}/settings"; + var url = $"{_.Client.Options.Url}api/apps/{app.Options.AppName}/settings"; using (var httpClient = new HttpClient()) { @@ -89,7 +89,7 @@ public class AnonymousTests : IClassFixture // STEP 3: Create a content. - var url = $"{_.Client.Options.Url}api/content/{appName}/my-content"; + var url = $"{_.Client.Options.Url}api/content/{app.Options.AppName}/my-content"; using (var httpClient = new HttpClient()) { diff --git a/tools/TestSuite/TestSuite.ApiTests/AppClientsTests.cs b/tools/TestSuite/TestSuite.ApiTests/AppClientsTests.cs index f2080f8a8..c0418f677 100644 --- a/tools/TestSuite/TestSuite.ApiTests/AppClientsTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/AppClientsTests.cs @@ -33,7 +33,7 @@ public sealed class AppClientsTests : IClassFixture public async Task Should_create_client() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Create client. @@ -51,7 +51,7 @@ public sealed class AppClientsTests : IClassFixture public async Task Should_update_client() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 0: Create client. @@ -86,7 +86,7 @@ public sealed class AppClientsTests : IClassFixture public async Task Should_delete_client() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 0: Create client. diff --git a/tools/TestSuite/TestSuite.ApiTests/AppContributorsTests.cs b/tools/TestSuite/TestSuite.ApiTests/AppContributorsTests.cs index efdfb6882..9227ba7ae 100644 --- a/tools/TestSuite/TestSuite.ApiTests/AppContributorsTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/AppContributorsTests.cs @@ -31,7 +31,7 @@ public sealed class AppContributorsTests : IClassFixture public async Task Should_not_invite_contributor_if_flag_is_false() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Do not invite contributors when flag is false. @@ -52,7 +52,7 @@ public sealed class AppContributorsTests : IClassFixture public async Task Should_invite_contributor() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Assign contributor. @@ -70,7 +70,7 @@ public sealed class AppContributorsTests : IClassFixture public async Task Should_update_contributor() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Assign contributor. @@ -95,7 +95,7 @@ public sealed class AppContributorsTests : IClassFixture public async Task Should_remove_contributor() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Assign contributor. diff --git a/tools/TestSuite/TestSuite.ApiTests/AppLanguagesTests.cs b/tools/TestSuite/TestSuite.ApiTests/AppLanguagesTests.cs index b78db4963..83d26aac3 100644 --- a/tools/TestSuite/TestSuite.ApiTests/AppLanguagesTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/AppLanguagesTests.cs @@ -30,7 +30,7 @@ public sealed class AppLanguagesTests : IClassFixture public async Task Should_add_language() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Add languages. @@ -48,7 +48,7 @@ public sealed class AppLanguagesTests : IClassFixture public async Task Should_add_custom_language() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Add languages. @@ -66,7 +66,7 @@ public sealed class AppLanguagesTests : IClassFixture public async Task Should_update_language() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Add languages. @@ -97,7 +97,7 @@ public sealed class AppLanguagesTests : IClassFixture public async Task Should_update_master_language() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Add languages. @@ -146,7 +146,7 @@ public sealed class AppLanguagesTests : IClassFixture public async Task Should_delete_language() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Add languages. diff --git a/tools/TestSuite/TestSuite.ApiTests/AppWorkflowsTests.cs b/tools/TestSuite/TestSuite.ApiTests/AppWorkflowsTests.cs index 716003649..95707a045 100644 --- a/tools/TestSuite/TestSuite.ApiTests/AppWorkflowsTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/AppWorkflowsTests.cs @@ -17,8 +17,7 @@ namespace TestSuite.ApiTests; [UsesVerify] public sealed class AppWorkflowsTests : IClassFixture { - private readonly string appName = Guid.NewGuid().ToString(); - private readonly string name = Guid.NewGuid().ToString(); + private readonly string workflowName = Guid.NewGuid().ToString(); public ClientFixture _ { get; } @@ -31,7 +30,7 @@ public sealed class AppWorkflowsTests : IClassFixture public async Task Should_create_workflow() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Create workflow. @@ -48,7 +47,7 @@ public sealed class AppWorkflowsTests : IClassFixture public async Task Should_update_workflow() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 0: Create workflow. @@ -70,11 +69,11 @@ public sealed class AppWorkflowsTests : IClassFixture }, ["Published"] = new WorkflowStepDto(), }, - Name = name + Name = workflowName }; var workflows_2 = await app.Apps.PutWorkflowAsync(workflow.Id, updateRequest); - var workflow_2 = workflows_2.Items.Find(x => x.Name == name); + var workflow_2 = workflows_2.Items.Find(x => x.Name == workflowName); Assert.NotNull(workflow_2); Assert.NotNull(workflow_2.Name); @@ -87,7 +86,7 @@ public sealed class AppWorkflowsTests : IClassFixture public async Task Should_delete_workflow() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 0: Create workflow. @@ -97,7 +96,7 @@ public sealed class AppWorkflowsTests : IClassFixture // STEP 1: Delete workflow. var workflows_2 = await app.Apps.DeleteWorkflowAsync(workflow.Id); - Assert.DoesNotContain(workflows_2.Items, x => x.Name == name); + Assert.DoesNotContain(workflows_2.Items, x => x.Name == workflowName); await Verify(workflows_2); } @@ -106,11 +105,11 @@ public sealed class AppWorkflowsTests : IClassFixture { var createRequest = new AddWorkflowDto { - Name = name + Name = workflowName }; var workflows = await app.Apps.PostWorkflowAsync(createRequest); - var workflow = workflows.Items.Find(x => x.Name == name); + var workflow = workflows.Items.Find(x => x.Name == workflowName); return workflow; } diff --git a/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs b/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs index f74ec4828..ac7e15135 100644 --- a/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/AssetTests.cs @@ -462,10 +462,8 @@ public class AssetTests : IClassFixture [Fact] public async Task Should_protect_asset_with_script() { - var appName = Guid.NewGuid().ToString(); - // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Create folder. @@ -667,7 +665,7 @@ public class AssetTests : IClassFixture Filter = "isDeleted eq true" }); - Assert.Equal(!permanent, deleted.Items.Any(x => x.Id == asset.Id)); + Assert.Equal(!permanent, deleted.Items.Exists(x => x.Id == asset.Id)); } [Theory] diff --git a/tools/TestSuite/TestSuite.ApiTests/BackupTests.cs b/tools/TestSuite/TestSuite.ApiTests/BackupTests.cs index 8af50d121..da0519223 100644 --- a/tools/TestSuite/TestSuite.ApiTests/BackupTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/BackupTests.cs @@ -144,7 +144,7 @@ public class BackupTests : IClassFixture // Create a workflow var workflowRequest = new AddWorkflowDto { - Name = appName + Name = "workflow" }; await app.Apps.PostWorkflowAsync(workflowRequest); diff --git a/tools/TestSuite/TestSuite.ApiTests/ContentUpdateTests.cs b/tools/TestSuite/TestSuite.ApiTests/ContentUpdateTests.cs index 90456fd8a..a1b59f5eb 100644 --- a/tools/TestSuite/TestSuite.ApiTests/ContentUpdateTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/ContentUpdateTests.cs @@ -501,7 +501,7 @@ public class ContentUpdateTests : IClassFixture var deleted = await _.Contents.GetAsync(q, QueryContext.Default.Unpublished(true)); - Assert.Equal(strategy is Strategies.Deletion.SingleSoft or Strategies.Deletion.BulkSoft, deleted.Items.Any(x => x.Id == content.Id)); + Assert.Equal(strategy is Strategies.Deletion.SingleSoft or Strategies.Deletion.BulkSoft, deleted.Items.Exists(x => x.Id == content.Id)); } [Theory] diff --git a/tools/TestSuite/TestSuite.ApiTests/RuleEventsTests.cs b/tools/TestSuite/TestSuite.ApiTests/RuleEventsTests.cs new file mode 100644 index 000000000..7b1c6ef49 --- /dev/null +++ b/tools/TestSuite/TestSuite.ApiTests/RuleEventsTests.cs @@ -0,0 +1,119 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using Squidex.ClientLibrary; +using Squidex.ClientLibrary.Management; +using TestSuite.Fixtures; + +#pragma warning disable SA1300 // Element should begin with upper-case letter +#pragma warning disable SA1507 // Code should not contain multiple blank lines in a row + +namespace TestSuite.ApiTests; + +public class RuleEventsTests : IClassFixture +{ + public ClientFixture _ { get; } + + public RuleEventsTests(ClientFixture fixture) + { + _ = fixture; + } + + [Fact] + public async Task Should_cancel_event() + { + // STEP 0: Create app. + var (app, rule) = await CreateAppAndRuleAsync(); + + + // STEP 1: Get events. + var events_0 = await app.Rules.GetEventsAsync(rule.Id); + + Assert.Single(events_0.Items); + Assert.Single(events_0.Items, x => x.NextAttempt != null); + + + // STEP 2: Cancel event. + await app.Rules.DeleteEventAsync(events_0.Items[0].Id); + + var events_1 = await app.Rules.GetEventsAsync(rule.Id); + + Assert.Single(events_1.Items); + Assert.Single(events_1.Items, x => x.NextAttempt == null); + } + + [Fact] + public async Task Should_cancel_event_by_rule() + { + // STEP 0: Create app. + var (app, rule) = await CreateAppAndRuleAsync(); + + + // STEP 1: Get events. + var events_0 = await app.Rules.GetEventsAsync(rule.Id); + + Assert.Single(events_0.Items); + Assert.Single(events_0.Items, x => x.NextAttempt != null); + + + // STEP 2: Cancel event. + await app.Rules.DeleteRuleEventsAsync(rule.Id); + + var events_1 = await app.Rules.GetEventsAsync(rule.Id); + + Assert.Single(events_1.Items); + Assert.Single(events_1.Items, x => x.NextAttempt == null); + } + + [Fact] + public async Task Should_cancel_event_by_app() + { + // STEP 0: Create app. + var (app, rule) = await CreateAppAndRuleAsync(); + + + // STEP 1: Get events. + var events_0 = await app.Rules.GetEventsAsync(rule.Id); + + Assert.Single(events_0.Items); + Assert.Single(events_0.Items, x => x.NextAttempt != null); + + + // STEP 2: Cancel event. + await app.Rules.DeleteEventsAsync(); + + var events_1 = await app.Rules.GetEventsAsync(rule.Id); + + Assert.Single(events_1.Items); + Assert.Single(events_1.Items, x => x.NextAttempt == null); + } + + private async Task<(ISquidexClient App, RuleDto)> CreateAppAndRuleAsync() + { + var appName = Guid.NewGuid().ToString(); + + var (app, _) = await _.PostAppAsync(); + + var createRule = new CreateRuleDto + { + Action = new WebhookRuleActionDto + { + Method = WebhookMethod.POST, + Payload = null, + PayloadType = null, + Url = new Uri("http://squidex.io") + }, + Trigger = new ManualRuleTriggerDto(), + }; + + var rule = await app.Rules.PostRuleAsync(createRule); + + await app.Rules.TriggerRuleAsync(rule.Id); + + return (app, rule); + } +} diff --git a/tools/TestSuite/TestSuite.ApiTests/RuleRunnerTests.cs b/tools/TestSuite/TestSuite.ApiTests/RuleRunnerTests.cs index 4cc1262f6..bf9fab3f3 100644 --- a/tools/TestSuite/TestSuite.ApiTests/RuleRunnerTests.cs +++ b/tools/TestSuite/TestSuite.ApiTests/RuleRunnerTests.cs @@ -37,7 +37,7 @@ public class RuleRunnerTests : IClassFixture, IClassFixture, IClassFixture, IClassFixture, IClassFixture, IClassFixture, IClassFixture, IClassFixture { - private readonly string appName = Guid.NewGuid().ToString(); private readonly string ruleName = Guid.NewGuid().ToString(); public ClientFixture _ { get; } @@ -30,7 +29,7 @@ public class RuleTests : IClassFixture public async Task Should_create_rule() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Create rule. @@ -60,7 +59,7 @@ public class RuleTests : IClassFixture public async Task Should_update_rule() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Create rule. @@ -99,7 +98,7 @@ public class RuleTests : IClassFixture public async Task Should_delete_rule() { // STEP 0: Create app. - var (app, _) = await _.PostAppAsync(appName); + var (app, _) = await _.PostAppAsync(); // STEP 1: Create rule. diff --git a/tools/TestSuite/TestSuite.Shared/Fixtures/ClientFixture.cs b/tools/TestSuite/TestSuite.Shared/Fixtures/ClientFixture.cs index 2a78a3761..662de4679 100644 --- a/tools/TestSuite/TestSuite.Shared/Fixtures/ClientFixture.cs +++ b/tools/TestSuite/TestSuite.Shared/Fixtures/ClientFixture.cs @@ -40,8 +40,10 @@ public class ClientFixture : IAsyncLifetime VerifierSettings.IgnoreMembersWithType(); } - public Task<(ISquidexClient, AppDto)> PostAppAsync(string name) + public Task<(ISquidexClient, AppDto)> PostAppAsync(string name = null) { + name ??= Guid.NewGuid().ToString(); + var createRequest = new CreateAppDto { Name = name