public async Task WhenUserIsNotConfirmedTheUserRecordIsDeleted()
            {
                //Arange
                User testUser = new User("TestsUser")
                {
                    Key = Key++, UnconfirmedEmailAddress = "*****@*****.**"
                };
                var testableService      = new DeleteAccountTestService(testUser);
                var deleteAccountService = testableService.GetDeleteAccountService();

                //Act
                var status = await deleteAccountService.DeleteAccountAsync(userToBeDeleted : testUser,
                                                                           userToExecuteTheDelete : testUser,
                                                                           commitAsTransaction : false,
                                                                           signature : testUser.Username,
                                                                           orphanPackagePolicy : AccountDeletionOrphanPackagePolicy.UnlistOrphans);

                //Assert
                Assert.True(status.Success);
                Assert.Null(testableService.User);
                Assert.Single(testableService.AuditService.Records);
                Assert.Equal(1, testableService.AuditService.Records.Count);
                var deleteAccountAuditRecord = testableService.AuditService.Records[0] as DeleteAccountAuditRecord;

                Assert.NotNull(deleteAccountAuditRecord);
                Assert.Equal(testUser.Username, deleteAccountAuditRecord.AdminUsername);
                Assert.Equal(testUser.Username, deleteAccountAuditRecord.Username);
                Assert.Equal(DeleteAccountAuditRecord.ActionStatus.Success, deleteAccountAuditRecord.Status);
            }
            public async Task DeleteHappyUser()
            {
                //Arange
                PackageRegistration registration = null;
                var testUser             = CreateTestData(ref registration);
                var testableService      = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService = testableService.GetDeleteAccountService();

                //Act
                var signature = "Hello";
                await deleteAccountService.
                DeleteGalleryUserAccountAsync(userToBeDeleted : testUser,
                                              admin : testUser,
                                              signature : signature,
                                              unlistOrphanPackages : true,
                                              commitAsTransaction : false);

                Assert.Equal <int>(0, registration.Owners.Count());
                Assert.Equal <int>(0, testUser.SecurityPolicies.Count());
                Assert.Equal <int>(0, testUser.ReservedNamespaces.Count());
                Assert.Equal <bool>(false, registration.Packages.ElementAt(0).Listed);
                Assert.Null(testUser.EmailAddress);
                Assert.Equal <int>(1, testableService.DeletedAccounts.Count());
                Assert.Equal <string>(signature, testableService.DeletedAccounts.ElementAt(0).Signature);
                Assert.Equal <int>(1, testableService.SupportRequests.Count);
                Assert.Equal(1, testableService.AuditService.Records.Count);
                var deleteRecord = testableService.AuditService.Records[0] as DeleteAccountAuditRecord;

                Assert.True(deleteRecord != null);
            }
            public async Task WhenAccountIsOrganizationMember_DoesNotDelete()
            {
                // Arrange
                var fakes                = new Fakes();
                var account              = fakes.OrganizationCollaborator;
                var testableService      = new DeleteAccountTestService(account, fakes.Package);
                var deleteAccountService = testableService.GetDeleteAccountService();

                // Act
                var result = await deleteAccountService.DeleteGalleryUserAccountAsync(
                    account,
                    fakes.Admin,
                    "signature",
                    unlistOrphanPackages : true,
                    commitAsTransaction : false);

                // Assert
                Assert.False(result.Success);

                var expected = string.Format(CultureInfo.CurrentCulture,
                                             Strings.AccountDelete_OrganizationMemberDeleteNotImplemented,
                                             account.Username);

                Assert.Equal(expected, result.Description);
            }
            public async Task DeleteUserThatOwnsOrphanRegistrationWillCleanTheRegistration(bool multipleOwners)
            {
                // Arrange
                PackageRegistration registration = null;
                var testUser = CreateTestUserWithRegistration(ref registration);
                var newOwner = new User("newOwner");

                newOwner.EmailAddress = "*****@*****.**";
                if (multipleOwners)
                {
                    registration.Owners.Add(newOwner);
                }

                var testableService      = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService = testableService.GetDeleteAccountService(isPackageOrphaned: true);

                // Act
                await deleteAccountService.DeleteAccountAsync(
                    userToBeDeleted : testUser,
                    userToExecuteTheDelete : testUser,
                    orphanPackagePolicy : AccountDeletionOrphanPackagePolicy.UnlistOrphans);

                // Assert
                if (multipleOwners)
                {
                    Assert.Contains <User>(newOwner, registration.Owners);
                    Assert.Equal(1, registration.Owners.Count());
                }
                else
                {
                    Assert.Empty(registration.Owners);
                }
            }
            public async Task NullAdmin()
            {
                //Arange
                PackageRegistration registration = null;
                var testUser             = CreateTestData(ref registration);
                var testableService      = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService = testableService.GetDeleteAccountService();

                //Assert
                await Assert.ThrowsAsync <ArgumentNullException>(() => deleteAccountService.DeleteGalleryUserAccountAsync(new User("TestUser"), null, "Signature", unlistOrphanPackages: true, commitAsTransaction: false));
            }
            public async Task DeleteHappyUser()
            {
                // Arrange
                PackageRegistration registration = null;
                var testUser = CreateTestData(ref registration);
                var testUserOrganizations = testUser.Organizations.ToList();
                var testableService       = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService  = testableService.GetDeleteAccountService();

                // Act
                var signature = "Hello";
                await deleteAccountService.
                DeleteAccountAsync(userToBeDeleted : testUser,
                                   userToExecuteTheDelete : testUser,
                                   commitAsTransaction : false,
                                   signature : signature,
                                   orphanPackagePolicy : AccountDeletionOrphanPackagePolicy.UnlistOrphans);

                Assert.Empty(registration.Owners);
                Assert.Empty(testUser.SecurityPolicies);
                Assert.Empty(testUser.ReservedNamespaces);
                Assert.False(registration.Packages.ElementAt(0).Listed);
                Assert.Null(testUser.EmailAddress);
                Assert.Single(testableService.DeletedAccounts);
                Assert.Equal(signature, testableService.DeletedAccounts.ElementAt(0).Signature);
                Assert.Single(testableService.SupportRequests);
                Assert.Empty(testableService.PackageOwnerRequests);
                Assert.True(testableService.HasDeletedOwnerScope);
                Assert.Equal(1, testableService.AuditService.Records.Count());
                Assert.Null(testUser.OrganizationMigrationRequest);
                Assert.Empty(testUser.OrganizationMigrationRequests);
                Assert.Empty(testUser.OrganizationRequests);

                Assert.Empty(testUser.Organizations);
                foreach (var testUserOrganization in testUserOrganizations)
                {
                    var notDeletedMembers = testUserOrganization.Organization.Members.Where(m => m.Member != testUser);
                    if (!notDeletedMembers.Any())
                    {
                        Assert.Contains(testUserOrganization.Organization, testableService.DeletedUsers);
                    }
                    else
                    {
                        Assert.Contains(notDeletedMembers, m => m.IsAdmin);
                    }
                }

                var deleteRecord = testableService.AuditService.Records[0] as DeleteAccountAuditRecord;

                Assert.True(deleteRecord != null);
            }
예제 #7
0
            public async Task NullAdmin(bool isPackageOrphaned)
            {
                // Arrange
                PackageRegistration registration = null;
                var testUser             = CreateTestUser(ref registration);
                var testableService      = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService = testableService.GetDeleteAccountService(isPackageOrphaned);

                // Assert
                await Assert.ThrowsAsync <ArgumentNullException>(() => deleteAccountService.DeleteAccountAsync(
                                                                     new User("TestUser")
                {
                    Key = Key++
                },
                                                                     null,
                                                                     orphanPackagePolicy: AccountDeletionOrphanPackagePolicy.UnlistOrphans));
            }
            public async Task NullUser()
            {
                // Arrange
                PackageRegistration registration = null;
                var testUser             = CreateTestData(ref registration);
                var testableService      = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService = testableService.GetDeleteAccountService();

                // Assert
                await Assert.ThrowsAsync <ArgumentNullException>(() => deleteAccountService.DeleteAccountAsync(
                                                                     null,
                                                                     new User("AdminUser")
                {
                    Key = Key++
                },
                                                                     commitAsTransaction: false,
                                                                     orphanPackagePolicy: AccountDeletionOrphanPackagePolicy.UnlistOrphans));
            }
예제 #9
0
            public async Task DeleteDeletedUser(bool isPackageOrphaned)
            {
                // Arrange
                PackageRegistration registration = null;
                var testUser = CreateTestUser(ref registration);

                testUser.IsDeleted = true;
                var testableService      = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService = testableService.GetDeleteAccountService(isPackageOrphaned);

                // Act
                var result = await deleteAccountService.DeleteAccountAsync(
                    userToBeDeleted : testUser,
                    userToExecuteTheDelete : testUser,
                    orphanPackagePolicy : AccountDeletionOrphanPackagePolicy.UnlistOrphans);

                string expected = $"The account '{testUser.Username}' was already deleted. No action was performed.";

                Assert.Equal(expected, result.Description);
            }
예제 #10
0
            public async Task FailsWhenFeatureFlagsRemovalFails()
            {
                // Arrange
                var testUser = new User("TestsUser")
                {
                    Key = Key++
                };
                var testableService      = new DeleteAccountTestService(testUser);
                var deleteAccountService = testableService.GetDeleteAccountService(
                    isPackageOrphaned: false,
                    isFeatureFlagsRemovalSuccessful: false);

                // Act
                var result = await deleteAccountService.DeleteAccountAsync(
                    userToBeDeleted : testUser,
                    userToExecuteTheDelete : testUser);

                // Assert
                Assert.False(result.Success);
                Assert.Equal("TestsUser", result.AccountName);
                Assert.Contains("An exception was encountered while trying to delete the account 'TestsUser'", result.Description);
            }
            public async Task DeleteDeletedUser()
            {
                // Arrange
                PackageRegistration registration = null;
                var testUser = CreateTestData(ref registration);

                testUser.IsDeleted = true;
                var testableService      = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService = testableService.GetDeleteAccountService();

                // Act
                var signature = "Hello";
                var result    = await deleteAccountService.
                                DeleteAccountAsync(userToBeDeleted : testUser,
                                                   userToExecuteTheDelete : testUser,
                                                   commitAsTransaction : false,
                                                   signature : signature,
                                                   orphanPackagePolicy : AccountDeletionOrphanPackagePolicy.UnlistOrphans);

                string expected = $"The account:{testUser.Username} was already deleted. No action was performed.";

                Assert.Equal(expected, result.Description);
            }
            public async Task DeleteUnconfirmedOrganization()
            {
                // Arrange
                var member = new User("testUser")
                {
                    Key = Key++
                };
                var organization = new Organization("testOrganization")
                {
                    Key = Key++
                };

                var membership = new Membership()
                {
                    Organization = organization, Member = member
                };

                member.Organizations.Add(membership);
                organization.Members.Add(membership);

                var requestedMember = new User("testRequestedMember")
                {
                    Key = Key++
                };
                var memberRequest = new MembershipRequest()
                {
                    Organization = organization, NewMember = requestedMember
                };

                requestedMember.OrganizationRequests.Add(memberRequest);
                organization.MemberRequests.Add(memberRequest);

                PackageRegistration registration = new PackageRegistration();

                registration.Owners.Add(organization);

                Package p = new Package()
                {
                    Description = "TestPackage",
                    Key         = 1
                };

                p.PackageRegistration = registration;
                registration.Packages.Add(p);

                var testableService      = new DeleteAccountTestService(organization, registration);
                var deleteAccountService = testableService.GetDeleteAccountService();

                // Act
                var status = await deleteAccountService.
                             DeleteAccountAsync(
                    organization,
                    member,
                    commitAsTransaction : false,
                    orphanPackagePolicy : AccountDeletionOrphanPackagePolicy.KeepOrphans);

                // Assert
                Assert.True(status.Success);
                Assert.Null(testableService.User);
                Assert.Empty(registration.Owners);
                Assert.Empty(organization.SecurityPolicies);
                Assert.Empty(organization.ReservedNamespaces);
                Assert.Empty(testableService.DeletedAccounts);
                Assert.Single(testableService.SupportRequests);
                Assert.Empty(testableService.PackageOwnerRequests);
                Assert.True(testableService.HasDeletedOwnerScope);
                Assert.Single(testableService.AuditService.Records);

                var deleteRecord = testableService.AuditService.Records[0] as DeleteAccountAuditRecord;

                Assert.True(deleteRecord != null);
            }
            public async Task DeleteOrganization(bool isPackageOrphaned, AccountDeletionOrphanPackagePolicy orphanPolicy)
            {
                // Arrange
                var member = new User("testUser")
                {
                    Key = Key++
                };
                var organization = new Organization("testOrganization")
                {
                    Key          = Key++,
                    EmailAddress = "*****@*****.**"
                };

                var membership = new Membership()
                {
                    Organization = organization, Member = member
                };

                member.Organizations.Add(membership);
                organization.Members.Add(membership);

                var requestedMember = new User("testRequestedMember")
                {
                    Key = Key++
                };
                var memberRequest = new MembershipRequest()
                {
                    Organization = organization, NewMember = requestedMember
                };

                requestedMember.OrganizationRequests.Add(memberRequest);
                organization.MemberRequests.Add(memberRequest);

                PackageRegistration registration = new PackageRegistration();

                registration.Owners.Add(organization);

                Package p = new Package()
                {
                    Description = "TestPackage",
                    Key         = 1
                };

                p.PackageRegistration = registration;
                registration.Packages.Add(p);

                var testableService      = new DeleteAccountTestService(organization, registration);
                var deleteAccountService = testableService.GetDeleteAccountService(isPackageOrphaned);

                // Act
                var status = await deleteAccountService.
                             DeleteAccountAsync(
                    organization,
                    member,
                    commitAsTransaction : false,
                    orphanPackagePolicy : orphanPolicy);

                // Assert
                if (orphanPolicy == AccountDeletionOrphanPackagePolicy.DoNotAllowOrphans && isPackageOrphaned)
                {
                    Assert.False(status.Success);
                    Assert.Equal(organization.Confirmed, organization.EmailAddress != null);
                    Assert.True(registration.Owners.Any(o => o.MatchesUser(organization)));
                    Assert.NotEmpty(organization.SecurityPolicies);
                    Assert.Empty(testableService.DeletedAccounts);
                    Assert.NotEmpty(testableService.PackageOwnerRequests);
                    Assert.Empty(testableService.AuditService.Records);
                    Assert.False(testableService.HasDeletedOwnerScope);
                    Assert.Empty(testableService.AuditService.Records);
                }
                else
                {
                    Assert.True(status.Success);
                    Assert.Null(organization.EmailAddress);
                    Assert.Equal(
                        orphanPolicy == AccountDeletionOrphanPackagePolicy.UnlistOrphans && isPackageOrphaned,
                        !registration.Packages.Single().Listed);
                    Assert.False(registration.Owners.Any(o => o.MatchesUser(organization)));
                    Assert.Empty(organization.SecurityPolicies);
                    Assert.Single(testableService.DeletedAccounts);
                    Assert.Empty(testableService.PackageOwnerRequests);
                    Assert.Single(testableService.AuditService.Records);
                    Assert.True(testableService.HasDeletedOwnerScope);

                    var deleteRecord = testableService.AuditService.Records[0] as DeleteAccountAuditRecord;
                    Assert.True(deleteRecord != null);
                }

                // Reserved namespaces and support requests are deleted before the request fails due to orphaned packages.
                // Because we are not committing as a transaction in these tests, they remain deleted.
                // In production, they would not be deleted because the transaction they were deleted in would fail.
                Assert.Empty(organization.ReservedNamespaces);
                Assert.Single(testableService.SupportRequests);
            }
            public async Task DeleteHappyUser(bool isPackageOrphaned, AccountDeletionOrphanPackagePolicy orphanPolicy)
            {
                // Arrange
                PackageRegistration registration = null;
                var testUser = CreateTestUser(ref registration);
                var testUserOrganizations = testUser.Organizations.ToList();
                var testableService       = new DeleteAccountTestService(testUser, registration);
                var deleteAccountService  = testableService.GetDeleteAccountService(isPackageOrphaned);

                // Act
                await deleteAccountService.
                DeleteAccountAsync(userToBeDeleted : testUser,
                                   userToExecuteTheDelete : testUser,
                                   commitAsTransaction : false,
                                   orphanPackagePolicy : orphanPolicy);


                if (orphanPolicy == AccountDeletionOrphanPackagePolicy.DoNotAllowOrphans && isPackageOrphaned)
                {
                    Assert.True(registration.Owners.Any(o => o.MatchesUser(testUser)));
                    Assert.NotEmpty(testUser.SecurityPolicies);
                    Assert.True(registration.Packages.Single().Listed);
                    Assert.NotNull(testUser.EmailAddress);
                    Assert.Empty(testableService.DeletedAccounts);
                    Assert.NotEmpty(testableService.PackageOwnerRequests);
                    Assert.False(testableService.HasDeletedOwnerScope);
                    Assert.Empty(testableService.AuditService.Records);
                    Assert.NotNull(testUser.OrganizationMigrationRequest);
                    Assert.NotEmpty(testUser.OrganizationMigrationRequests);
                    Assert.NotEmpty(testUser.OrganizationRequests);
                    Assert.NotEmpty(testUser.Organizations);
                }
                else
                {
                    Assert.False(registration.Owners.Any(o => o.MatchesUser(testUser)));
                    Assert.Empty(testUser.SecurityPolicies);
                    Assert.Equal(
                        orphanPolicy == AccountDeletionOrphanPackagePolicy.UnlistOrphans && isPackageOrphaned,
                        !registration.Packages.Single().Listed);
                    Assert.Null(testUser.EmailAddress);
                    Assert.Single(testableService.DeletedAccounts);
                    Assert.Empty(testableService.PackageOwnerRequests);
                    Assert.True(testableService.HasDeletedOwnerScope);
                    Assert.Single(testableService.AuditService.Records);
                    Assert.Null(testUser.OrganizationMigrationRequest);
                    Assert.Empty(testUser.OrganizationMigrationRequests);
                    Assert.Empty(testUser.OrganizationRequests);

                    Assert.Empty(testUser.Organizations);
                    foreach (var testUserOrganization in testUserOrganizations)
                    {
                        var notDeletedMembers = testUserOrganization.Organization.Members.Where(m => m.Member != testUser);
                        if (notDeletedMembers.Any())
                        {
                            // If an organization that the deleted user was a part of had other members, it should have at least one admin.
                            Assert.Contains(notDeletedMembers, m => m.IsAdmin);
                        }
                        else
                        {
                            // If an organization that the deleted user was a part of had no other members, it should have been deleted.
                            Assert.Contains(testUserOrganization.Organization, testableService.DeletedUsers);
                        }
                    }

                    var deleteRecord = testableService.AuditService.Records[0] as DeleteAccountAuditRecord;
                    Assert.True(deleteRecord != null);
                }

                // Reserved namespaces and support requests are deleted before the request fails due to orphaned packages.
                // Because we are not committing as a transaction in these tests, they remain deleted.
                // In production, they would not be deleted because the transaction they were deleted in would fail.
                Assert.Single(testableService.SupportRequests);
                Assert.Empty(testUser.ReservedNamespaces);
            }