| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Testing.Shared.Multitenancy; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// A tenant accessor for tests that construct tenant-aware services directly. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// Defaults to the default tenant, which is what an untenanted test expects. The stores it feeds also |
| | | 10 | | /// admit tenant-agnostic and unassigned records, so a test that never sets a tenant on its fixtures keeps |
| | | 11 | | /// seeing them. |
| | | 12 | | /// </remarks> |
| | 0 | 13 | | public sealed class TestTenantAccessor(string tenantId = Tenant.DefaultTenantId) : ITenantAccessor |
| | | 14 | | { |
| | | 15 | | /// <summary>A shared instance scoped to the default tenant.</summary> |
| | 0 | 16 | | public static ITenantAccessor Default { get; } = new TestTenantAccessor(); |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | 0 | 19 | | public string TenantId { get; private set; } = tenantId; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | 0 | 22 | | public Tenant? Tenant { get; private set; } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public IDisposable PushContext(Tenant? tenant) |
| | | 26 | | { |
| | 0 | 27 | | var previousTenant = Tenant; |
| | 0 | 28 | | var previousTenantId = TenantId; |
| | | 29 | | |
| | 0 | 30 | | Tenant = tenant; |
| | 0 | 31 | | TenantId = tenant?.Id ?? Tenant.DefaultTenantId; |
| | | 32 | | |
| | 0 | 33 | | return new Restore(() => |
| | 0 | 34 | | { |
| | 0 | 35 | | Tenant = previousTenant; |
| | 0 | 36 | | TenantId = previousTenantId; |
| | 0 | 37 | | }); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | private sealed class Restore(Action onDispose) : IDisposable |
| | | 41 | | { |
| | 0 | 42 | | public void Dispose() => onDispose(); |
| | | 43 | | } |
| | | 44 | | } |