public async Task AddSingleUserToSingleRole()
        {
            var testContextBuilder = TestContextBuilder.CreateDefault(builder => builder
                                                                      .AddDatabase()
                                                                      .SeedDefaults()
                                                                      .SeedDataset(Datasets.Dataset1));

            await using var testContext = await testContextBuilder.BuildAsync();

            var command = new AddUsersToRolesCommand
            {
                UserNames = new[] { "JohnDoe", },
                RoleNames = new[] { nameof(RoleEnum.Administators) },
            };

            var commandContext = new CommandContext(command, testContext.Services);

            var handler = ActivatorUtilities.CreateInstance <AddUsersToRolesCommandHandler>(testContext.Services);

            await handler.HandleAsync(command, commandContext, default);

            await using (var dbContext = testContext.CreateReadOnlyDbContext())
            {
                var user = await dbContext.Users
                           .Include(user => user.Roles).ThenInclude(userRole => userRole.Role)
                           .FirstAsync(user => user.UserName == command.UserNames[0]);

                Assert.Equal(1, user.Roles?.Count);
                Assert.Contains(user.Roles, userRole => userRole.Role.RoleName == command.RoleNames[0]);
            }
        }
        public async Task FilterByNamePagedAndSorted()
        {
            var testContextBuilder = TestContextBuilder.CreateDefault(builder => builder
                                                                      .AddDatabase()
                                                                      .SeedDefaults()
                                                                      .SeedDataset(Datasets.Dataset1));

            await using var testContext = await testContextBuilder.BuildAsync();

            var query = new ListUsersQuery
            {
                // TODO: SQLite doesn't respect case insensitivity when matching parts of strings
                UserNamePattern = "Doe",
                PageSize        = 1,
                OrderColumns    = new[] { "+UserName" }
            };

            var queryContext = new QueryContext(query, testContext.Services);

            var handler = ActivatorUtilities.CreateInstance <ListUsersQueryHandler>(testContext.Services);

            var result = await handler.HandleAsync(query, queryContext, default);

            Assert.Equal(1, result.Items?.Length);
            Assert.Equal(2, result.TotalItemCount);

            Assert.Contains(result.Items !, item => item.UserId == 12 && item.UserName == "JaneDoe");
        }
예제 #3
0
        public async Task ContextPublishesToAdditionalModuleAsync()
        {
            var context = TestContextBuilder.Create <InMemoryEventStore, TestModule>(TestModule.Initialize, OtherModule.Initialize);
            var id      = new AggregateId(Guid.NewGuid());
            await context.WhenAsync(new TestCommand(id));

            context.Then(events => Assert.True(events.OfType <OtherEvent>().Happened()));
        }
예제 #4
0
        public void GroupTree_IsCreated()
        {
            var context  = new TestContextBuilder().WithGroupsAndUsers().Build();
            var allGroup = context.Groups.First();

            Assert.IsNotNull(allGroup);
            Assert.AreEqual(3, allGroup.Groups.Count());
        }
예제 #5
0
        public void Calendars_Created()
        {
            var context = new TestContextBuilder()
                          .WithGroupsAndUsers()
                          .WithCalendarTypes()
                          .WithCalendars()
                          .Build();

            Assert.AreEqual(5, context.Calendars.Count());
            Assert.AreEqual(3, context.CalendarTypes.Count());
        }
예제 #6
0
        public OnTimeActionReportShould()
        {
            var builder = new TestContextBuilder();

            builder.AddDateProvider();
            items = new List <PlannerItemModel>();
            plannerItemDataServiceMock = new Mock <IPlannerItemDataService>();
            plannerItemDataServiceMock.Setup(o => o.LoadOverdueItems()).ReturnsAsync(items);
            ctx = builder.Build();
            ctx.Services.AddScoped <IPlannerItemComponentService>(o => Mock.Of <IPlannerItemComponentService>());
            ctx.Services.AddScoped <IPlannerItemDataService>(o => plannerItemDataServiceMock.Object);
        }
예제 #7
0
        private void SetupContext(TestContext ctx)
        {
            var testContextBuilder = new TestContextBuilder(ctx);

            testContextBuilder.AddCommon();
            var dataServiceMock = Mock.Of <IPlannerItemDataService>(o => o.LoadItems(It.IsAny <DateTime>(), It.IsAny <DateTime>()) == Task.FromResult <IEnumerable <PlannerItemModel> >(Array.Empty <PlannerItemModel>()));

            ctx.Services.AddScoped <IPlannerItemDataService>(o => dataServiceMock);
            state = new CalendarState();
            var calendarServiceMock = new Mock <ICalendarService>();

            calendarServiceMock.Setup(c => c.State).Returns(state);
            ctx.Services.AddScoped <ICalendarService>(o => calendarServiceMock.Object);
        }
예제 #8
0
        public async Task FlowAsync()
        {
            var context = TestContextBuilder.Create <InMemoryEventStore, SalesModule>(
                SalesModule.Initialize,
                PaymentModule.Initialize,
                WarehouseModule.Initialize,
                ShippingModule.Initialize
                );

            await context.WhenAsync(new Sales.PlaceOrder());

            context.Then((events, table) => {
                testOutput.WriteLine(table);
                Assert.True(events.AsEnvelopes().Happened <OrderCompleted>());
            });
        }
예제 #9
0
 public StateChangeGameTestsWeb(ITestOutputHelper outputHelper)
 {
     this.context = TestContextBuilder.Create <InMemoryEventStore, GameModule>((store, pub) =>
     {
         this.helper = outputHelper;
         var module  = GameModule.Initialize(store, new InMemorySnapshotStore(), pub);
         hostBuilder = RPS.Web.Program.CreateHostBuilder(Array.Empty <string>())
                       .ConfigureWebHost(webHost => webHost
                                         .UseTestServer()
                                         .ConfigureTestServices(services =>
                                                                services
                                                                .AddSingleton(module)
                                                                .Remove(services.SingleOrDefault(x => x.ImplementationType == typeof(ChangeFeedHostedService)))
                                                                ));
         return(module);
     });
 }
예제 #10
0
        public async Task SeedDefaultsAndCsvData()
        {
            var testContextBuilder = new TestContextBuilder();

            testContextBuilder.AddDatabase()
            .SeedDefaults()
            .SeedDataset(Datasets.Dataset1);

            await using (var testContext = await testContextBuilder.BuildAsync())
            {
                await using (var dbContext = testContext.CreateReadOnlyDbContext())
                {
                    Assert.True(await dbContext.Roles.CountAsync() > 0);

                    // ...
                }
            }
        }
예제 #11
0
        public void RenderCorrectly()
        {
            // Arrange
            using var ctx = new TestContext();
            var builder = new TestContextBuilder(ctx);

            builder.AddAuth();

            // Act
            var cut = ctx.RenderComponent <LoginForm>();

            // Assert
            cut.MarkupMatches(@"<div class='box'>
                <h1 class='title is-5'>Welcome back,</h1>
                <h2 class='subtitle is-5 has-text-grey-light'>sign in to continue.</h2>
                <form>
                    <div class='field '>
                        <label class='label'>Email Address</label>
                        <div class='control'>
                            <input name='Email' type = 'text' class='input  valid'>
                        </div>
                        <p class='help '></p>
                        </div>
                    <div class='field '>
                        <label class='label'>Password</label>
                        <div class='control'>
                            <input name='Password' type = 'password' class='input  valid'>  
                        </div>
                        <p class='help '></p>
                    </div>
                    <div class='field has-text-centered has-padding-top-20'>
                        <div class='control'>        
                            <button type = 'submit' class='button has-width-200 is-success'>
                                Sign In
                            </button>
                            <div class='has-padding-10'>or</div>
                            <button type = 'button' class='button has-width-200 is-outlined is-primary'>
                                Sign Up
                            </button>
                        </div>
                    </div>
                </form>
            </div>");
        }
예제 #12
0
        public async Task SeedDefaults()
        {
            var testContextBuilder = new TestContextBuilder();

            testContextBuilder.AddDatabase()
            .SeedDefaults();

            await using (var testContext = await testContextBuilder.BuildAsync())
            {
                await using (var dbContext = testContext.CreateReadOnlyDbContext())
                {
                    Assert.True(await dbContext.Roles.CountAsync() > 0);

                    var admin = await dbContext.Users
                                .Include(user => user.Profile)
                                .SingleOrDefaultAsync(user => user.UserName == ApplicationConstants.BuiltInRootUserName);

                    Assert.NotNull(admin);
                    Assert.NotNull(admin.Profile);
                }
            }
        }
예제 #13
0
        public async Task TestCaseGotDedicatedDbInstance2()
        {
            var testContextBuilder = new TestContextBuilder();

            testContextBuilder.AddDatabase();

            await using (var testContext = await testContextBuilder.BuildAsync())
            {
                await using (var dbContext = testContext.CreateWritableDbContext())
                {
                    dbContext.Roles.Add(new Role {
                        RoleName = "Y"
                    });
                    await dbContext.SaveChangesAsync();
                }

                await using (var dbContext = testContext.CreateReadOnlyDbContext())
                {
                    Assert.Equal(1, await dbContext.Roles.CountAsync(role => role.RoleName == "Y"));
                    Assert.Equal(1, await dbContext.Roles.CountAsync());
                }
            }
        }
        public async Task QueryDataAnnotationsValidatorInterceptorTest()
        {
            var testContextBuilder = TestContextBuilder.CreateDefault(builder => builder
                                                                      .AddServices(services =>
            {
                services.AddSingleton <IHostApplicationLifetime>(NullHostApplicationLifetime.Instance);
                services.AddServiceLayer(new OptionsProvider(builder.CreateDataAccessOptions()));
            })
                                                                      .AddDatabase(addDataAccessServices: false));

            await using var testContext = await testContextBuilder.BuildAsync();

            var query = new GetUserQuery {
            };

            var queryDispatcher = testContext.Services.GetRequiredService <IQueryDispatcher>();

            var ex = await Assert.ThrowsAsync <ServiceErrorException>(
                async() => await queryDispatcher.DispatchAsync(query, default));

            Assert.Equal(ServiceErrorCode.ParamNotSpecified, ex.ErrorCode);
            Assert.Equal(new[] { nameof(query.Identifier) }, ex.Args);
        }
        public async Task CommandDataAnnotationsValidatorInterceptorTest()
        {
            var testContextBuilder = TestContextBuilder.CreateDefault(builder => builder
                                                                      .AddServices(services =>
            {
                services.AddSingleton <IHostApplicationLifetime>(NullHostApplicationLifetime.Instance);
                services.AddServiceLayer(new OptionsProvider(builder.CreateDataAccessOptions()));
            })
                                                                      .AddDatabase(addDataAccessServices: false));

            await using var testContext = await testContextBuilder.BuildAsync();

            var command = new AddUsersToRolesCommand {
                UserNames = new string[] { }, RoleNames = new string[] { "x" }
            };

            var commandDispatcher = testContext.Services.GetRequiredService <ICommandDispatcher>();

            var ex = await Assert.ThrowsAsync <ServiceErrorException>(
                async() => await commandDispatcher.DispatchAsync(command, default));

            Assert.Equal(ServiceErrorCode.ParamNotValid, ex.ErrorCode);
            Assert.Equal(new[] { nameof(command.UserNames) }, ex.Args);
        }
예제 #16
0
        public async Task NoParams()
        {
            var testContextBuilder = TestContextBuilder.CreateDefault(builder => builder
                                                                      .AddDatabase()
                                                                      .SeedDefaults()
                                                                      .SeedDataset(Datasets.Dataset1));

            await using var testContext = await testContextBuilder.BuildAsync();

            var query = new ListUsersQuery {
            };

            var queryContext = new QueryContext(query, testContext.Services);

            var handler = ActivatorUtilities.CreateInstance <ListUsersQueryHandler>(testContext.Services);

            var result = await handler.HandleAsync(query, queryContext, default);

            Assert.Equal(3, result.Items?.Length);

            Assert.Contains(result.Items !, item => item.UserId == 1 && item.UserName == ApplicationConstants.BuiltInRootUserName);
            Assert.Contains(result.Items !, item => item.UserId == 11 && item.UserName == "JohnDoe");
            Assert.Contains(result.Items !, item => item.UserId == 12 && item.UserName == "JaneDoe");
        }
예제 #17
0
        public void Events_Exist()
        {
            var context = new TestContextBuilder().Full().Build();

            Assert.IsTrue(context.Events.Any());
        }
예제 #18
0
        public void UserTree_ContainsWaddles()
        {
            var context = new TestContextBuilder().WithGroupsAndUsers().Build();

            Assert.IsNotNull(context.Users.FirstOrDefault(x => x.Appeal == "Waddles"));
        }