| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.ExternalAuthentication.Permissions; |
| | | 4 | | using Elsa.ExternalAuthentication.Services; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | | 7 | | |
| | | 8 | | namespace Elsa.ExternalAuthentication.Endpoints.Connections; |
| | | 9 | | |
| | 21 | 10 | | internal sealed class TestConnection(IServiceProvider services, ITenantAccessor tenantAccessor) : ElsaEndpointWithoutReq |
| | | 11 | | { |
| | | 12 | | public override void Configure() |
| | | 13 | | { |
| | 21 | 14 | | Post("/external-authentication/connections/{connectionId}/test"); |
| | 21 | 15 | | ConfigurePermissions(ExternalAuthenticationPermissions.ConnectionsTest); |
| | 21 | 16 | | } |
| | | 17 | | |
| | | 18 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 19 | | { |
| | 0 | 20 | | if (!ConnectionEndpointSupport.TryGetExpectedRevision(HttpContext, out var revision)) |
| | | 21 | | { |
| | 0 | 22 | | await ConnectionEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status428PreconditionRequired, "prec |
| | 0 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | var tests = services.GetService<ConnectionTestService>(); |
| | 0 | 27 | | if (tests is null) |
| | | 28 | | { |
| | 0 | 29 | | await ConnectionEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status503ServiceUnavailable, "test_u |
| | 0 | 30 | | return; |
| | | 31 | | } |
| | 0 | 32 | | var result = await tests.TestAsync(Route<string>("connectionId")!, revision, tenantAccessor.TenantId, User, canc |
| | | 33 | | switch (result) |
| | | 34 | | { |
| | | 35 | | case ConnectionTestOperationResult.Completed(var observation): |
| | 0 | 36 | | await HttpContext.Response.WriteAsJsonAsync(new ConnectionTestResponse( |
| | 0 | 37 | | observation.Status.ToString().ToLowerInvariant(), observation.ObservedAt, observation.TestedMaterial |
| | 0 | 38 | | observation.Category, observation.Summary, observation.Warnings, observation.Duration, observation.C |
| | 0 | 39 | | return; |
| | | 40 | | case ConnectionTestOperationResult.PreconditionFailed(var currentRevision): |
| | 0 | 41 | | await ConnectionEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status412PreconditionFailed, "pr |
| | 0 | 42 | | return; |
| | | 43 | | case ConnectionTestOperationResult.NotFound: |
| | 0 | 44 | | await ConnectionEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status404NotFound, "not_found", |
| | 0 | 45 | | return; |
| | | 46 | | default: |
| | 0 | 47 | | await ConnectionEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status400BadRequest, "test_unava |
| | 0 | 48 | | return; |
| | | 49 | | } |
| | 0 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | internal sealed record ConnectionTestResponse(string Status, DateTimeOffset ObservedAt, string TestedMaterialRevision, s |