예제 #1
0
        private static async Task <AccountEntity> InsertAccountEntityAsync(RivaIdentityDbContext context, Guid initialRoleId)
        {
            var accountId     = AuthUserOptions.UserId;
            var accountEntity = new AccountEntity
            {
                Id            = accountId,
                Email         = AuthUserOptions.Email,
                Confirmed     = true,
                PasswordHash  = "PasswordHash",
                SecurityStamp = Guid.NewGuid(),
                Created       = DateTimeOffset.UtcNow,
                Roles         = new List <AccountRoleEntity>
                {
                    new AccountRoleEntity
                    {
                        AccountId = accountId,
                        RoleId    = initialRoleId
                    }
                }
            };

            context.Accounts.Add(accountEntity);
            await context.SaveChangesAsync();

            return(accountEntity);
        }
예제 #2
0
        public IntegrationTestFixture()
        {
            _administratorFactory = new AdministratorWebApplicationFactory("RivaIdentityWebAppIntegrationTestsAdministratorDb");
            _userFactory          = new UserWebApplicationFactory("RivaIdentityWebAppIntegrationTestsAccountDb");
            _anonymousFactory     = new AnonymousWebApplicationFactory("RivaIdentityWebAppIntegrationTestsAnonymousDb");

            AdministratorHttpClient = _administratorFactory
                                      .WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                                      .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            UserHttpClient = _userFactory
                             .WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                             .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            AnonymousHttpClient = _anonymousFactory
                                  .WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                                  .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            AdministratorDbContext = _administratorFactory.DbContext;
            AccountDbContext       = _userFactory.DbContext;
            AnonymousDbContext     = _anonymousFactory.DbContext;
        }
예제 #3
0
        private static async Task <AccountEntity> InsertAccountEntityAsync(RivaIdentityDbContext context, string token)
        {
            var accountEntity = new AccountEntity
            {
                Id            = Guid.NewGuid(),
                Email         = "*****@*****.**",
                Confirmed     = false,
                PasswordHash  = "PasswordHash",
                SecurityStamp = Guid.NewGuid(),
                Created       = DateTimeOffset.UtcNow,
                Tokens        = new List <TokenEntity>
                {
                    new TokenEntity
                    {
                        Value   = token,
                        Id      = Guid.NewGuid(),
                        Issued  = DateTimeOffset.UtcNow.AddHours(-1),
                        Expires = DateTimeOffset.UtcNow.AddDays(1),
                        Type    = TokenTypeEnumeration.AccountConfirmation.ConvertToEnum()
                    }
                }
            };

            context.Accounts.Add(accountEntity);
            await context.SaveChangesAsync();

            return(accountEntity);
        }
예제 #4
0
 public AccountRepository(RivaIdentityDbContext context, IMapper mapper,
                          IOrderByExpressionCreator <AccountEntity> orderByExpressionCreator)
 {
     _context = context;
     _dbSet   = context.Set <AccountEntity>();
     _mapper  = mapper;
     _orderByExpressionCreator = orderByExpressionCreator;
 }
예제 #5
0
 public RoleRepositoryTest(RoleRepositoryTestFixture fixture)
 {
     _context    = fixture.Context;
     _mapperMock = fixture.MapperMock;
     _repository = fixture.Repository;
     _role       = fixture.Role;
     _fixture    = fixture;
 }
예제 #6
0
        public DatabaseFixture()
        {
            var options = new DbContextOptionsBuilder <RivaIdentityDbContext>()
                          .UseInMemoryDatabase("RivaIdentityDatabaseIntegrationTestsDb").Options;

            Context = new RivaIdentityDbContext(options);
            Context.ClearDatabase();
        }
예제 #7
0
 public AccountDataConsistencyService(RivaIdentityDbContext rivaIdentityDbContext, PersistedGrantDbContext persistedGrantDbContext,
                                      IAccountRepository accountRepository, IPersistedGrantRepository persistedGrantRepository)
 {
     _rivaIdentityDbContext    = rivaIdentityDbContext;
     _persistedGrantDbContext  = persistedGrantDbContext;
     _accountRepository        = accountRepository;
     _persistedGrantRepository = persistedGrantRepository;
 }
예제 #8
0
 public AccountRepositoryTest(AccountRepositoryTestFixture fixture)
 {
     _context    = fixture.Context;
     _mapperMock = fixture.MapperMock;
     _orderByExpressionCreatorMock = fixture.OrderByExpressionCreatorMock;
     _repository = fixture.Repository;
     _account    = fixture.Account;
     _fixture    = fixture;
 }
예제 #9
0
        private static async Task InsertRoleEntityAsync(RivaIdentityDbContext context)
        {
            var roleEntity = new RoleEntity
            {
                Id         = Guid.NewGuid(),
                Name       = "GetRolesIntegrationTest",
                RowVersion = new byte[] { 1, 2, 4, 8, 16, 64 }
            };

            context.Roles.Add(roleEntity);
            await context.SaveChangesAsync();
        }
예제 #10
0
        private static async Task <RoleEntity> InsertRoleEntityAsync(RivaIdentityDbContext context)
        {
            var roleEntity = new RoleEntity
            {
                Id         = Guid.NewGuid(),
                Name       = "DeleteRoleIntegrationTest",
                RowVersion = new byte[] { 0, 0, 0, 0, 0, 0, 70, 81 }
            };

            context.Roles.Add(roleEntity);
            await context.SaveChangesAsync();

            return(roleEntity);
        }
예제 #11
0
        private static async Task <RoleEntity> InsertRoleEntityToUpdateAsync(RivaIdentityDbContext context)
        {
            var roleEntity = new RoleEntity
            {
                Id         = Guid.NewGuid(),
                Name       = "UpdateAccountIntegrationTestRoleToUpdate",
                RowVersion = new byte[] { 1, 2, 4, 8, 16, 32, 64, 128 }
            };

            context.Roles.Add(roleEntity);
            await context.SaveChangesAsync();

            return(roleEntity);
        }
예제 #12
0
        private static async Task <string> PrepareExpectedResponseAsync(RivaIdentityDbContext context, string roleName)
        {
            var roleEntity = await context.Roles.SingleOrDefaultAsync(x => x.Name.Equals(roleName));

            var getRoleResponse = new GetRoleResponse(roleEntity.Id, roleEntity.RowVersion, roleEntity.Name);
            var settings        = new JsonSerializerSettings
            {
                Formatting       = Formatting.Indented,
                ContractResolver = new DefaultTestPlatformContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            return(JsonConvert.SerializeObject(getRoleResponse, settings));
        }
예제 #13
0
        private static async Task InsertUserRoleEntityIfNotExistsAsync(RivaIdentityDbContext context)
        {
            var roleEntity = await context.Roles.SingleOrDefaultAsync(x => x.Name.Equals(DefaultRoleEnumeration.User.DisplayName));

            if (roleEntity is null)
            {
                roleEntity = new RoleEntity
                {
                    Id         = Guid.NewGuid(),
                    Name       = DefaultRoleEnumeration.User.DisplayName,
                    RowVersion = new byte[] { 1, 2, 4, 8, 16, 32, 64 }
                };
                context.Roles.Add(roleEntity);
                await context.SaveChangesAsync();
            }
        }
예제 #14
0
        private static async Task <AccountEntity> InsertAccountEntityAsync(RivaIdentityDbContext context)
        {
            var accountEntity = new AccountEntity
            {
                Id            = AuthUserOptions.UserId,
                Email         = AuthUserOptions.Email,
                Confirmed     = true,
                SecurityStamp = Guid.NewGuid(),
                Created       = DateTimeOffset.UtcNow
            };

            context.Accounts.Add(accountEntity);
            await context.SaveChangesAsync();

            return(accountEntity);
        }
예제 #15
0
        private static async Task <AccountEntity> InsertAccountEntityAsync(RivaIdentityDbContext context)
        {
            var accountEntity = new AccountEntity
            {
                Id            = Guid.NewGuid(),
                Email         = "*****@*****.**",
                Confirmed     = true,
                PasswordHash  = "PasswordHash",
                SecurityStamp = Guid.NewGuid(),
                Created       = DateTimeOffset.UtcNow
            };

            context.Accounts.Add(accountEntity);
            await context.SaveChangesAsync();

            return(accountEntity);
        }
예제 #16
0
        private static async Task <string> PrepareExpectedResponseAsync(RivaIdentityDbContext context)
        {
            var roleEntities = await context.Roles.ToListAsync();

            var getRoleResponses   = roleEntities.Select(x => new GetRoleResponse(x.Id, x.RowVersion, x.Name));
            var collectionResponse = new CollectionResponse <GetRoleResponse>(roleEntities.Count, getRoleResponses);
            var settings           = new JsonSerializerSettings
            {
                Formatting       = Formatting.Indented,
                ContractResolver = new DefaultTestPlatformContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            return(JsonConvert.SerializeObject(collectionResponse, settings));
        }
예제 #17
0
        private static async Task <AccountEntity> InsertAccountEntityAsync(RivaIdentityDbContext context, string password)
        {
            var passwordService = new PasswordService();
            var accountEntity   = new AccountEntity
            {
                Id            = AuthUserOptions.UserId,
                Email         = AuthUserOptions.Email,
                Confirmed     = true,
                PasswordHash  = passwordService.HashPassword(password),
                SecurityStamp = Guid.NewGuid(),
                Created       = DateTimeOffset.UtcNow
            };

            context.Accounts.Add(accountEntity);
            await context.SaveChangesAsync();

            return(accountEntity);
        }
예제 #18
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureTestServices(services =>
            {
                var descriptor = services.SingleOrDefault(x => x.ServiceType == typeof(DbContextOptions <RivaIdentityDbContext>));
                if (descriptor != null)
                {
                    services.Remove(descriptor);
                }

                descriptor = services.SingleOrDefault(x => x.ServiceType == typeof(IIntegrationEventBus));
                if (descriptor != null)
                {
                    services.Remove(descriptor);
                }

                descriptor = services.SingleOrDefault(x => x.ServiceType == typeof(IPersistedGrantRepository));
                if (descriptor != null)
                {
                    services.Remove(descriptor);
                }

                descriptor = services.SingleOrDefault(x => x.ServiceType == typeof(IAccountDataConsistencyService));
                if (descriptor != null)
                {
                    services.Remove(descriptor);
                }

                services
                .AddDbContext <RivaIdentityDbContext>((options, context) =>
                                                      context.UseInMemoryDatabase(_databaseName));

                var sp             = services.BuildServiceProvider();
                _serviceScope      = sp.CreateScope();
                var scopedServices = _serviceScope.ServiceProvider;
                DbContext          = scopedServices.GetRequiredService <RivaIdentityDbContext>();
                DbContext.Database.EnsureCreated();
                DbContext.ClearDatabase();

                services.AddSingleton <IIntegrationEventBus, IntegrationEventBusStub>();
                services.AddScoped <IPersistedGrantRepository, PersistedGrantRepositoryStub>();
                services.AddScoped <IAccountDataConsistencyService, AccountDataConsistencyServiceStub>();
            });
        }
예제 #19
0
        private static async Task <AccountEntity> InsertAccountEntityAsync(RivaIdentityDbContext context)
        {
            var accountId     = AuthUserOptions.UserId;
            var accountEntity = new AccountEntity
            {
                Id            = AuthUserOptions.UserId,
                Email         = AuthUserOptions.Email,
                Confirmed     = true,
                PasswordHash  = "PasswordHash",
                SecurityStamp = Guid.NewGuid(),
                Created       = DateTimeOffset.UtcNow,
                Roles         = new List <AccountRoleEntity>
                {
                    new AccountRoleEntity
                    {
                        AccountId = accountId,
                        RoleId    = Guid.NewGuid()
                    }
                },
                Tokens = new List <TokenEntity>
                {
                    new TokenEntity
                    {
                        Id        = Guid.NewGuid(),
                        Issued    = DateTimeOffset.UtcNow,
                        Expires   = DateTimeOffset.UtcNow.AddDays(1),
                        Type      = TokenTypeEnumeration.AccountConfirmation.ConvertToEnum(),
                        Value     = "123456",
                        AccountId = accountId
                    }
                }
            };

            context.Accounts.Add(accountEntity);
            await context.SaveChangesAsync();

            return(accountEntity);
        }
예제 #20
0
        private static async Task <string> PrepareExpectedResponseAsync(GetAccountsRequest getAccountsRequest, RivaIdentityDbContext context)
        {
            var accountEntities = await context.Accounts
                                  .Where(x => x.Email.ToLower().StartsWith(getAccountsRequest.Email.ToLower()) && x.Confirmed == getAccountsRequest.Confirmed)
                                  .OrderBy(x => x.Email)
                                  .Skip(getAccountsRequest.PageSize.Value * (getAccountsRequest.Page.Value - 1))
                                  .Take(getAccountsRequest.PageSize.Value)
                                  .ToListAsync();

            var totalCount = await context.Accounts
                             .LongCountAsync(x => x.Email.ToLower().StartsWith(getAccountsRequest.Email.ToLower()) && x.Confirmed == getAccountsRequest.Confirmed);

            var getAccountsCollectionItemResponses = accountEntities.Select(x =>
                                                                            new GetAccountsCollectionItemResponse(x.Id, x.Email, x.Confirmed, x.Created,
                                                                                                                  !string.IsNullOrWhiteSpace(x.PasswordHash), x.LastLogin));
            var collectionResponse = new CollectionResponse <GetAccountsCollectionItemResponse>(totalCount, getAccountsCollectionItemResponses);
            var settings           = new JsonSerializerSettings
            {
                Formatting       = Formatting.Indented,
                ContractResolver = new DefaultTestPlatformContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                },
                Converters = new List <JsonConverter> {
                    new StringEnumConverter()
                }
            };

            return(JsonConvert.SerializeObject(collectionResponse, settings));
        }
예제 #21
0
 public RoleRepository(RivaIdentityDbContext context, IMapper mapper)
 {
     _context = context;
     _mapper  = mapper;
     _dbSet   = context.Set <RoleEntity>();
 }
예제 #22
0
        private static void RemoveAccounts(RivaIdentityDbContext context)
        {
            var accounts = context.Accounts.ToList();

            context.Accounts.RemoveRange(accounts);
        }
예제 #23
0
        private static void RemoveRoles(RivaIdentityDbContext context)
        {
            var roles = context.Roles.ToList();

            context.Roles.RemoveRange(roles);
        }
예제 #24
0
 public static void ClearDatabase(this RivaIdentityDbContext context)
 {
     RemoveAccounts(context);
     RemoveRoles(context);
     context.SaveChanges();
 }