public async Task Handle_RaisesException_WhenOrganizationUserExists()
        {
            var username = DataProvider.Get <string>();
            var role     = DataProvider.Get <string>();

            DateTimeDbTestExtensions.SetUtcNowToRandomDate();


            var testParams = Endpoint.ArrangeOnSqlSession(AssemblySetupFixture.EndpointTestContainer,
                                                          s =>
            {
                var org = new OrganizationBuilder().BuildAndPersist(s);
                new TestObjectBuilder <OrganizationUser>()
                .SetArgument(o => o.Username, username)
                .SetArgument(o => o.Role, role)
                .SetArgument(o => o.Organization, org)
                .BuildAndPersist(s);
                return(new
                {
                    Cmd = new CreateOrganizationUserCommand(username, role, org)
                });
            });

            await Endpoint.ActRaisesException <CreateOrganizationUserCommandHandler>(AssemblySetupFixture.EndpointTestContainer,
                                                                                     (h, ctx) => h.Handle(testParams.Cmd, ctx));

            Endpoint.AssertOnSqlSessionThat(AssemblySetupFixture.EndpointTestContainer,
                                            s =>
            {
                var organizationUser = s.Query <OrganizationUser>().Single();
                organizationUser.Should().NotBeNull();
            });
        }
        public async Task Handle_RaisesException_WhenInvalidOrganization()
        {
            var username = DataProvider.Get <string>();
            var role     = DataProvider.Get <string>();

            DateTimeDbTestExtensions.SetUtcNowToRandomDate();


            var testParams = Endpoint.ArrangeOnSqlSession(AssemblySetupFixture.EndpointTestContainer,
                                                          s =>
            {
                var org = new OrganizationBuilder().Build();    //not persisted
                return(new
                {
                    Cmd = new CreateOrganizationUserCommand(username, role, org)
                });
            });


            await Endpoint.ActRaisesException <CreateOrganizationUserCommandHandler>(AssemblySetupFixture.EndpointTestContainer,
                                                                                     (h, ctx) => h.Handle(testParams.Cmd, ctx));

            Endpoint.AssertOnSqlSessionThat(AssemblySetupFixture.EndpointTestContainer,
                                            s =>
            {
                var organizationUser = s.Query <OrganizationUser>().SingleOrDefault();
                organizationUser.Should().BeNull();
            });
        }