-
-
Notifications
You must be signed in to change notification settings - Fork 86
RG-T89 Fixes #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RG-T89 Fixes #493
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Tests/Resgrid.Tests/Services/DeleteRepositorySchemaTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| using System; | ||
| using System.IO; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Resgrid.Tests.Services | ||
| { | ||
| [TestFixture] | ||
| public class DeleteRepositorySchemaTests | ||
| { | ||
| private static string RepositorySource() | ||
| { | ||
| var directory = new DirectoryInfo(TestContext.CurrentContext.TestDirectory); | ||
| while (directory != null && !File.Exists(Path.Combine(directory.FullName, "Resgrid.sln"))) | ||
| directory = directory.Parent; | ||
|
|
||
| directory.Should().NotBeNull("the tests must be able to find the repository root"); | ||
| var path = Path.Combine(directory!.FullName, "Repositories", | ||
| "Resgrid.Repositories.DataRepository", "DeleteRepository.cs"); | ||
| return File.ReadAllText(path); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Department_delete_uses_the_current_active_department_storage() | ||
| { | ||
| RepositorySource().Should().NotContain("[dbo].[ActiveDepartments]", | ||
| "active-department state is stored on DepartmentMembers.IsActive"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Global_user_data_is_deleted_only_after_the_last_membership_is_removed() | ||
| { | ||
| var source = RepositorySource(); | ||
| const string deleteMembership = | ||
| "DELETE FROM [dbo].[DepartmentMembers] WHERE UserId = @UserId AND DepartmentId = @DepartmentId"; | ||
| const string noMembershipsRemain = | ||
| "IF (SELECT COUNT(*) FROM DepartmentMembers WHERE UserId = @UserId) = 0"; | ||
|
|
||
| var deleteMembershipIndex = source.IndexOf(deleteMembership, StringComparison.Ordinal); | ||
| var remainingMembershipCheckIndex = source.IndexOf(noMembershipsRemain, StringComparison.Ordinal); | ||
|
|
||
| deleteMembershipIndex.Should().BeGreaterThan(0); | ||
| remainingMembershipCheckIndex.Should().BeGreaterThan(deleteMembershipIndex, | ||
| "the current department membership must be removed before checking for another membership"); | ||
| source.Should().NotContain( | ||
| "IF (SELECT COUNT(*) FROM DepartmentMembers WHERE UserId = @UserId) = 1"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Managing_user_keeps_other_department_memberships_and_account() | ||
| { | ||
| var source = RepositorySource(); | ||
| const string deleteTargetMembership = | ||
| "DELETE FROM [dbo].[DepartmentMembers] WHERE UserId = @ManagingUserId AND DepartmentId = @DepartmentId"; | ||
| const string noMembershipsRemain = | ||
| "IF (SELECT COUNT(*) FROM DepartmentMembers WHERE UserId = @ManagingUserId) = 0"; | ||
| const string deleteAccount = | ||
| "DELETE FROM [dbo].[AspNetUsers] WHERE Id = @ManagingUserId"; | ||
|
|
||
| var deleteTargetMembershipIndex = source.IndexOf(deleteTargetMembership, StringComparison.Ordinal); | ||
| var remainingMembershipCheckIndex = source.IndexOf(noMembershipsRemain, StringComparison.Ordinal); | ||
| var deleteAccountIndex = source.IndexOf(deleteAccount, StringComparison.Ordinal); | ||
|
|
||
| deleteTargetMembershipIndex.Should().BeGreaterThan(0); | ||
| remainingMembershipCheckIndex.Should().BeGreaterThan(deleteTargetMembershipIndex); | ||
| deleteAccountIndex.Should().BeGreaterThan(remainingMembershipCheckIndex, | ||
| "the managing user's account must be deleted only when no memberships remain"); | ||
| source.Should().NotMatchRegex( | ||
| @"DELETE FROM \[dbo\]\.\[DepartmentMembers\] WHERE UserId = @ManagingUserId\s*(?:\r?\n|$)", | ||
| "memberships in other departments must be preserved"); | ||
| } | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
Tests/Resgrid.Tests/Services/DepartmentMemberSensitiveDataRepositorySchemaTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using NUnit.Framework; | ||
| using Resgrid.Config; | ||
| using Resgrid.Model.Repositories.Connection; | ||
| using Resgrid.Model.Repositories.Queries; | ||
| using Resgrid.Repositories.DataRepository; | ||
| using Resgrid.Repositories.DataRepository.Configs; | ||
| using Resgrid.Repositories.DataRepository.Servers.SqlServer; | ||
|
|
||
| namespace Resgrid.Tests.Services | ||
| { | ||
| [TestFixture] | ||
| public class DepartmentMemberSensitiveDataRepositorySchemaTests | ||
| { | ||
| [Test] | ||
| [TestCase(DatabaseTypes.SqlServer, true)] | ||
| [TestCase(DatabaseTypes.SqlServer, false)] | ||
| [TestCase(DatabaseTypes.Postgres, true)] | ||
| [TestCase(DatabaseTypes.Postgres, false)] | ||
| public async Task Outstanding_legacy_profile_query_matches_the_deployed_schema( | ||
| DatabaseTypes databaseType, bool legacyIdentificationNumberExists) | ||
| { | ||
| var originalDatabaseType = DataConfig.DatabaseType; | ||
| DataConfig.DatabaseType = databaseType; | ||
|
|
||
| try | ||
| { | ||
| var executedSql = new List<string>(); | ||
| var connection = new CapturingConnection(executedSql, | ||
| scalarResult: legacyIdentificationNumberExists ? 1L : 0L); | ||
| var unitOfWork = new Mock<IUnitOfWork>(); | ||
| unitOfWork.Setup(x => x.Connection).Returns(connection); | ||
| unitOfWork.Setup(x => x.CreateOrGetConnection()).Returns(connection); | ||
|
|
||
| SqlConfiguration configuration = databaseType == DatabaseTypes.Postgres | ||
| ? new PostgreSqlConfiguration() | ||
| : new SqlServerConfiguration(); | ||
|
|
||
| var repository = new DepartmentMemberSensitiveDataRepository( | ||
| Mock.Of<IConnectionProvider>(), configuration, unitOfWork.Object, Mock.Of<IQueryFactory>()); | ||
|
|
||
| await repository.GetDepartmentIdsWithOutstandingLegacyProfileDataAsync(); | ||
|
|
||
| executedSql.Should().HaveCount(2); | ||
| executedSql[0].Should().ContainEquivalentOf("information_schema.columns"); | ||
| executedSql[1].Should().ContainEquivalentOf("homeaddressid"); | ||
| executedSql[1].Should().ContainEquivalentOf("mailingaddressid"); | ||
|
|
||
| var identificationNumberReference = databaseType == DatabaseTypes.Postgres | ||
| ? "up.identificationnumber" | ||
| : "up.[IdentificationNumber]"; | ||
|
|
||
| if (legacyIdentificationNumberExists) | ||
| executedSql[1].Should().ContainEquivalentOf(identificationNumberReference); | ||
| else | ||
| executedSql[1].Should().NotContainEquivalentOf(identificationNumberReference); | ||
| } | ||
| finally | ||
| { | ||
| DataConfig.DatabaseType = originalDatabaseType; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.