Пример #1
0
        public async void ReplaceAsync_Works_DataMatches(
            OrganizationUser postOrgUser,
            OrganizationUser replaceOrgUser,
            User user,
            Organization org,
            OrganizationUserCompare equalityComparer,
            List <EfRepo.OrganizationUserRepository> suts,
            List <EfRepo.UserRepository> efUserRepos,
            List <EfRepo.OrganizationRepository> efOrgRepos,
            SqlRepo.OrganizationUserRepository sqlOrgUserRepo,
            SqlRepo.UserRepository sqlUserRepo,
            SqlRepo.OrganizationRepository sqlOrgRepo
            )
        {
            var savedOrgUsers = new List <OrganizationUser>();

            foreach (var sut in suts)
            {
                var i          = suts.IndexOf(sut);
                var postEfUser = await efUserRepos[i].CreateAsync(user);
                var postEfOrg  = await efOrgRepos[i].CreateAsync(org);
                sut.ClearChangeTracking();

                postOrgUser.UserId         = replaceOrgUser.UserId = postEfUser.Id;
                postOrgUser.OrganizationId = replaceOrgUser.OrganizationId = postEfOrg.Id;
                var postEfOrgUser = await sut.CreateAsync(postOrgUser);

                sut.ClearChangeTracking();

                replaceOrgUser.Id = postOrgUser.Id;
                await sut.ReplaceAsync(replaceOrgUser);

                sut.ClearChangeTracking();

                var replacedOrganizationUser = await sut.GetByIdAsync(replaceOrgUser.Id);

                savedOrgUsers.Add(replacedOrganizationUser);
            }

            var postSqlUser = await sqlUserRepo.CreateAsync(user);

            var postSqlOrg = await sqlOrgRepo.CreateAsync(org);

            postOrgUser.UserId         = replaceOrgUser.UserId = postSqlUser.Id;
            postOrgUser.OrganizationId = replaceOrgUser.OrganizationId = postSqlOrg.Id;
            var postSqlOrgUser = await sqlOrgUserRepo.CreateAsync(postOrgUser);

            replaceOrgUser.Id = postSqlOrgUser.Id;
            await sqlOrgUserRepo.ReplaceAsync(replaceOrgUser);

            var replacedSqlUser = await sqlOrgUserRepo.GetByIdAsync(replaceOrgUser.Id);

            var distinctItems = savedOrgUsers.Distinct(equalityComparer);

            Assert.True(!distinctItems.Skip(1).Any());
        }
Пример #2
0
        public async void DeleteAsync_Works_DataMatches(OrganizationUser orgUser, User user, Organization org,
                                                        OrganizationUserCompare equalityComparer, List <EfRepo.OrganizationUserRepository> suts,
                                                        List <EfRepo.UserRepository> efUserRepos, List <EfRepo.OrganizationRepository> efOrgRepos,
                                                        SqlRepo.OrganizationUserRepository sqlOrgUserRepo, SqlRepo.UserRepository sqlUserRepo,
                                                        SqlRepo.OrganizationRepository sqlOrgRepo)
        {
            foreach (var sut in suts)
            {
                var i          = suts.IndexOf(sut);
                var postEfUser = await efUserRepos[i].CreateAsync(user);
                var postEfOrg  = await efOrgRepos[i].CreateAsync(org);
                sut.ClearChangeTracking();

                orgUser.UserId         = postEfUser.Id;
                orgUser.OrganizationId = postEfOrg.Id;
                var postEfOrgUser = await sut.CreateAsync(orgUser);

                sut.ClearChangeTracking();

                var savedEfOrgUser = await sut.GetByIdAsync(postEfOrgUser.Id);

                Assert.True(savedEfOrgUser != null);
                sut.ClearChangeTracking();

                await sut.DeleteAsync(savedEfOrgUser);

                sut.ClearChangeTracking();

                savedEfOrgUser = await sut.GetByIdAsync(savedEfOrgUser.Id);

                Assert.True(savedEfOrgUser == null);
            }

            var postSqlUser = await sqlUserRepo.CreateAsync(user);

            var postSqlOrg = await sqlOrgRepo.CreateAsync(org);

            orgUser.UserId         = postSqlUser.Id;
            orgUser.OrganizationId = postSqlOrg.Id;
            var postSqlOrgUser = await sqlOrgUserRepo.CreateAsync(orgUser);

            var savedSqlOrgUser = await sqlOrgUserRepo.GetByIdAsync(postSqlOrgUser.Id);

            Assert.True(savedSqlOrgUser != null);

            await sqlOrgUserRepo.DeleteAsync(postSqlOrgUser);

            savedSqlOrgUser = await sqlOrgUserRepo.GetByIdAsync(postSqlOrgUser.Id);

            Assert.True(savedSqlOrgUser == null);
        }
Пример #3
0
        [EfPolicyApplicableToUserInlineAutoData(OrganizationUserType.User, false, OrganizationUserStatusType.Invited, false, true, true, false)]        // User not minStatus

        public async void GetManyByTypeApplicableToUser_Works_DataMatches(
            // Inline data
            OrganizationUserType userType,
            bool canManagePolicies,
            OrganizationUserStatusType orgUserStatus,
            bool includeInvited,
            bool policyEnabled,
            bool policySameType,
            bool isProvider,

            // Auto data - models
            Policy policy,
            User user,
            Organization organization,
            OrganizationUser orgUser,
            Provider provider,
            ProviderOrganization providerOrganization,
            ProviderUser providerUser,
            PolicyCompareIncludingOrganization equalityComparer,

            // Auto data - EF repos
            List <EfRepo.PolicyRepository> suts,
            List <EfRepo.UserRepository> efUserRepository,
            List <EfRepo.OrganizationRepository> efOrganizationRepository,
            List <EfRepo.OrganizationUserRepository> efOrganizationUserRepository,
            List <EfRepo.ProviderRepository> efProviderRepository,
            List <EfRepo.ProviderOrganizationRepository> efProviderOrganizationRepository,
            List <EfRepo.ProviderUserRepository> efProviderUserRepository,

            // Auto data - SQL repos
            SqlRepo.PolicyRepository sqlPolicyRepo,
            SqlRepo.UserRepository sqlUserRepo,
            SqlRepo.OrganizationRepository sqlOrganizationRepo,
            SqlRepo.ProviderRepository sqlProviderRepo,
            SqlRepo.OrganizationUserRepository sqlOrganizationUserRepo,
            SqlRepo.ProviderOrganizationRepository sqlProviderOrganizationRepo,
            SqlRepo.ProviderUserRepository sqlProviderUserRepo
            )
        {
            // Combine EF and SQL repos into one list per type
            var policyRepos = suts.ToList <IPolicyRepository>();

            policyRepos.Add(sqlPolicyRepo);
            var userRepos = efUserRepository.ToList <IUserRepository>();

            userRepos.Add(sqlUserRepo);
            var orgRepos = efOrganizationRepository.ToList <IOrganizationRepository>();

            orgRepos.Add(sqlOrganizationRepo);
            var orgUserRepos = efOrganizationUserRepository.ToList <IOrganizationUserRepository>();

            orgUserRepos.Add(sqlOrganizationUserRepo);
            var providerRepos = efProviderRepository.ToList <IProviderRepository>();

            providerRepos.Add(sqlProviderRepo);
            var providerOrgRepos = efProviderOrganizationRepository.ToList <IProviderOrganizationRepository>();

            providerOrgRepos.Add(sqlProviderOrganizationRepo);
            var providerUserRepos = efProviderUserRepository.ToList <IProviderUserRepository>();

            providerUserRepos.Add(sqlProviderUserRepo);

            // Arrange data
            var savedPolicyType   = PolicyType.SingleOrg;
            var queriedPolicyType = policySameType ? savedPolicyType : PolicyType.DisableSend;

            orgUser.Type   = userType;
            orgUser.Status = orgUserStatus;
            var permissionsData = new Permissions {
                ManagePolicies = canManagePolicies
            };

            orgUser.Permissions = JsonSerializer.Serialize(permissionsData, new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            });

            policy.Enabled = policyEnabled;
            policy.Type    = savedPolicyType;

            var results = new List <Policy>();

            foreach (var policyRepo in policyRepos)
            {
                var i = policyRepos.IndexOf(policyRepo);

                // Seed database
                var savedUser = await userRepos[i].CreateAsync(user);
                var savedOrg  = await orgRepos[i].CreateAsync(organization);

                // Invited orgUsers are not associated with an account yet, so they are identified by Email not UserId
                if (orgUserStatus == OrganizationUserStatusType.Invited)
                {
                    orgUser.Email  = savedUser.Email;
                    orgUser.UserId = null;
                }
                else
                {
                    orgUser.UserId = savedUser.Id;
                }

                orgUser.OrganizationId = savedOrg.Id;
                await orgUserRepos[i].CreateAsync(orgUser);

                if (isProvider)
                {
                    var savedProvider = await providerRepos[i].CreateAsync(provider);

                    providerOrganization.OrganizationId = savedOrg.Id;
                    providerOrganization.ProviderId     = savedProvider.Id;
                    await providerOrgRepos[i].CreateAsync(providerOrganization);

                    providerUser.UserId     = savedUser.Id;
                    providerUser.ProviderId = savedProvider.Id;
                    await providerUserRepos[i].CreateAsync(providerUser);
                }

                policy.OrganizationId = savedOrg.Id;
                await policyRepo.CreateAsync(policy);

                if (suts.Contains(policyRepo))
                {
                    (policyRepo as EfRepo.BaseEntityFrameworkRepository).ClearChangeTracking();
                }

                var minStatus = includeInvited ? OrganizationUserStatusType.Invited : OrganizationUserStatusType.Accepted;

                // Act
                var result = await policyRepo.GetManyByTypeApplicableToUserIdAsync(savedUser.Id, queriedPolicyType, minStatus);

                results.Add(result.FirstOrDefault());
            }

            // Assert
            var distinctItems = results.Distinct(equalityComparer);

            Assert.True(results.All(r => r == null) ||
                        !distinctItems.Skip(1).Any());
        }