|
| 1 | +using BackendConfiguration.Pn.Services.UserPropertyAccess; |
| 2 | +using Microting.eForm.Infrastructure.Constants; |
| 3 | +using Microting.EformBackendConfigurationBase.Infrastructure.Data.Entities; |
| 4 | + |
| 5 | +namespace BackendConfiguration.Pn.Integration.Test.UserPropertyAccess; |
| 6 | + |
| 7 | +[Parallelizable(ParallelScope.Fixtures)] |
| 8 | +[TestFixture] |
| 9 | +public class BackendConfigurationUserPropertyAccessTests : TestBaseSetup |
| 10 | +{ |
| 11 | + [SetUp] |
| 12 | + public async Task ClearPropertyWorkers() |
| 13 | + { |
| 14 | + BackendConfigurationPnDbContext!.PropertyWorkers.RemoveRange( |
| 15 | + BackendConfigurationPnDbContext.PropertyWorkers); |
| 16 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 17 | + |
| 18 | + BackendConfigurationPnDbContext.Properties.RemoveRange( |
| 19 | + BackendConfigurationPnDbContext.Properties); |
| 20 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 21 | + } |
| 22 | + |
| 23 | + [Test] |
| 24 | + public async Task GetAccessiblePropertyIdsAsync_returns_only_properties_where_site_has_active_property_worker() |
| 25 | + { |
| 26 | + BackendConfigurationPnDbContext!.Properties.Add(new Property { Id = 100 }); |
| 27 | + BackendConfigurationPnDbContext.Properties.Add(new Property { Id = 101 }); |
| 28 | + BackendConfigurationPnDbContext.Properties.Add(new Property { Id = 102 }); |
| 29 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 30 | + |
| 31 | + BackendConfigurationPnDbContext.PropertyWorkers.Add(new PropertyWorker { WorkerId = 7, PropertyId = 100, WorkflowState = Constants.WorkflowStates.Created }); |
| 32 | + BackendConfigurationPnDbContext.PropertyWorkers.Add(new PropertyWorker { WorkerId = 7, PropertyId = 101, WorkflowState = Constants.WorkflowStates.Removed }); |
| 33 | + BackendConfigurationPnDbContext.PropertyWorkers.Add(new PropertyWorker { WorkerId = 99, PropertyId = 102, WorkflowState = Constants.WorkflowStates.Created }); |
| 34 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 35 | + |
| 36 | + var sut = new BackendConfigurationUserPropertyAccess(BackendConfigurationPnDbContext); |
| 37 | + |
| 38 | + var ids = await sut.GetAccessiblePropertyIdsAsync(7); |
| 39 | + |
| 40 | + Assert.That(ids, Is.EquivalentTo(new[] { 100 })); |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public async Task GetAccessiblePropertyIdsAsync_returns_empty_for_unknown_site() |
| 45 | + { |
| 46 | + BackendConfigurationPnDbContext!.Properties.Add(new Property { Id = 100 }); |
| 47 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 48 | + |
| 49 | + BackendConfigurationPnDbContext.PropertyWorkers.Add(new PropertyWorker { WorkerId = 7, PropertyId = 100, WorkflowState = Constants.WorkflowStates.Created }); |
| 50 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 51 | + |
| 52 | + var sut = new BackendConfigurationUserPropertyAccess(BackendConfigurationPnDbContext); |
| 53 | + |
| 54 | + var ids = await sut.GetAccessiblePropertyIdsAsync(42); |
| 55 | + |
| 56 | + Assert.That(ids, Is.Empty); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public async Task HasAccessAsync_true_when_active_property_worker_exists() |
| 61 | + { |
| 62 | + BackendConfigurationPnDbContext!.Properties.Add(new Property { Id = 100 }); |
| 63 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 64 | + |
| 65 | + BackendConfigurationPnDbContext.PropertyWorkers.Add(new PropertyWorker { WorkerId = 7, PropertyId = 100, WorkflowState = Constants.WorkflowStates.Created }); |
| 66 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 67 | + |
| 68 | + var sut = new BackendConfigurationUserPropertyAccess(BackendConfigurationPnDbContext); |
| 69 | + |
| 70 | + Assert.That(await sut.HasAccessAsync(7, 100), Is.True); |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + public async Task HasAccessAsync_false_for_removed_property_worker() |
| 75 | + { |
| 76 | + BackendConfigurationPnDbContext!.Properties.Add(new Property { Id = 100 }); |
| 77 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 78 | + |
| 79 | + BackendConfigurationPnDbContext.PropertyWorkers.Add(new PropertyWorker { WorkerId = 7, PropertyId = 100, WorkflowState = Constants.WorkflowStates.Removed }); |
| 80 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 81 | + |
| 82 | + var sut = new BackendConfigurationUserPropertyAccess(BackendConfigurationPnDbContext); |
| 83 | + |
| 84 | + Assert.That(await sut.HasAccessAsync(7, 100), Is.False); |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public async Task HasAccessAsync_false_for_different_property() |
| 89 | + { |
| 90 | + BackendConfigurationPnDbContext!.Properties.Add(new Property { Id = 100 }); |
| 91 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 92 | + |
| 93 | + BackendConfigurationPnDbContext.PropertyWorkers.Add(new PropertyWorker { WorkerId = 7, PropertyId = 100, WorkflowState = Constants.WorkflowStates.Created }); |
| 94 | + await BackendConfigurationPnDbContext.SaveChangesAsync(); |
| 95 | + |
| 96 | + var sut = new BackendConfigurationUserPropertyAccess(BackendConfigurationPnDbContext); |
| 97 | + |
| 98 | + Assert.That(await sut.HasAccessAsync(7, 999), Is.False); |
| 99 | + } |
| 100 | +} |
0 commit comments