示例#1
0
        public async Task Handle_ProductionEnvironmentAndExistingStripeCustomer_NewStripeCustomerCreated()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync(new EnvironmentSetupOptions ()
            {
                EnvironmentName = Environments.Production
            });

            var customerService = environment.ServiceProvider.GetRequiredService <CustomerService>();

            var fakeIdentityName = Guid.NewGuid().ToString();
            var fakeIdentity     = TestClaimsPrincipalFactory.CreateWithIdentityName(fakeIdentityName);

            //Act
            var createdUser = await environment.Mediator.Send(new CreateUserForIdentityCommand(fakeIdentity));

            var createdStripeCustomer = await customerService.GetAsync(createdUser.StripeCustomerId);

            //Assert
            try
            {
                Assert.IsNotNull(createdStripeCustomer);
                Assert.AreEqual(fakeIdentityName + "@example.com", createdStripeCustomer.Email);
                Assert.AreEqual(createdUser.Id.ToString(), createdStripeCustomer.Metadata["UserId"]);
            }
            finally
            {
                await customerService.DeleteAsync(createdStripeCustomer.Id);
            }
        }
        public async Task Handle_NoPaymentMethodsPresent_DefaultPaymentMethodChangedToAddedMethod()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var user = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-identity-name")));

            var paymentMethodService = environment
                                       .ServiceProvider
                                       .GetRequiredService <PaymentMethodService>();

            var newPaymentMethod = await CreatePaymentMethodAsync(paymentMethodService);

            //Act
            await environment.Mediator.Send(
                new SetActivePaymentMethodForUserCommand(
                    user,
                    newPaymentMethod.Id));

            //Assert
            var stripeCustomer = await environment
                                 .ServiceProvider
                                 .GetRequiredService <CustomerService>()
                                 .GetAsync(user.StripeCustomerId);

            Assert.AreEqual(stripeCustomer.InvoiceSettings.DefaultPaymentMethodId, newPaymentMethod.Id);
        }
        public async Task Handle_MultipleUsersExist_MatchingUserIsReturned()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var existingUserIdentityName = "identity-name";

            await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-dummy-user")));

            var existingUser = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName(existingUserIdentityName)));

            await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-other-dummy-user")));

            //Act
            var returnedUser = await environment.Mediator.Send(
                new GetUserByIdentityNameQuery(existingUserIdentityName));

            //Assert
            Assert.IsNotNull(returnedUser);
            Assert.AreNotEqual(default, existingUser.Id);
示例#4
0
        public async Task Handle_StripeExceptionThrown_NothingIsCommitted()
        {
            //Arrange
            var fakeIdentityName = Guid.NewGuid().ToString();
            var fakeIdentity     = TestClaimsPrincipalFactory.CreateWithIdentityName(fakeIdentityName);

            await using var environment = await IntegrationTestEnvironment.CreateAsync(new EnvironmentSetupOptions ()
            {
                IocConfiguration = services => services.AddSingleton(provider =>
                {
                    var partiallyFakeCustomerService = Substitute.ForPartsOf <CustomerService>(
                        provider.GetRequiredService <IStripeClient>());
                    partiallyFakeCustomerService
                    .CreateAsync(Arg.Any <CustomerCreateOptions>())
                    .Throws(new TestException());

                    return(partiallyFakeCustomerService);
                })
            });

            //Act
            var exception = await Assert.ThrowsExceptionAsync <TestException>(
                async() => await environment.Mediator.Send(new CreateUserForIdentityCommand(fakeIdentity)));

            //Assert
            var createdUser = await GetUserByIdentityNameAsync(environment, fakeIdentityName);

            Assert.IsNotNull(exception);
            Assert.IsNull(createdUser);
        }
示例#5
0
        public async Task Handle_ProperArgumentsGiven_FullServerProvisioningFlowIsRunAndProperInstanceIsCreated()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync(
                            new EnvironmentSetupOptions ()
            {
                IocConfiguration = FakeOutMinimalLightsailFeaturesForFullProvisioning
            });

            var provisioningService = environment
                                      .ServiceProvider
                                      .GetRequiredService <IProvisioningService>();

            var paymentMethodService = environment
                                       .ServiceProvider
                                       .GetRequiredService <PaymentMethodService>();

            var user = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-identity-name")));

            var paymentMethod = await CreatePaymentMethodAsync(paymentMethodService);

            await environment.Mediator.Send(
                new SetActivePaymentMethodForUserCommand(
                    user,
                    paymentMethod.Id));

            //Act
            await environment.Mediator.Send(
                new ProvisionInstanceForUserCommand(
                    new Plan(
                        "nano_2_0",
                        1337,
                        new Bundle()
            {
                Price = 1337,
                BundleId = "nano_2_0"
            },
                        Array.Empty <PullDogPlan>()),
                    user));

            await provisioningService.ProcessPendingJobsAsync();

            //Assert
            await environment.WithFreshDataContext(async dataContext =>
            {
                var provisionedInstance = await dataContext
                                          .Instances
                                          .Include(x => x.Cluster)
                                          .ThenInclude(x => x.User)
                                          .SingleAsync();

                Assert.IsTrue(provisionedInstance.IsProvisioned);
                Assert.AreEqual(provisionedInstance.Cluster.UserId, user.Id);
            });
        }
示例#6
0
        public async Task Handle_UserFound_ReturnsUser()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var existingUser = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-identity-name")));

            //Act
            var returnedUser = await environment.Mediator.Send(
                new GetUserByIdQuery(existingUser.Id));

            //Assert
            Assert.IsNotNull(returnedUser);
            Assert.AreNotEqual(default, existingUser.Id);
示例#7
0
        public async Task Handle_JobServiceThrowsError_NothingIsCommitted()
        {
            //Arrange
            var fakeProvisioningService = Substitute.For <IProvisioningService>();

            fakeProvisioningService
            .ScheduleJobAsync(Arg.Any <IProvisioningStateFlow>())
            .Throws(new TestException());

            await using var environment = await IntegrationTestEnvironment.CreateAsync(new EnvironmentSetupOptions ()
            {
                IocConfiguration = services =>
                {
                    services.AddSingleton(fakeProvisioningService);
                }
            });

            var user = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-identity-name")));

            //Act
            var exception = await Assert.ThrowsExceptionAsync <TestException>(async() =>
                                                                              await environment.Mediator.Send(
                                                                                  new ProvisionInstanceForUserCommand(
                                                                                      new Plan(
                                                                                          "nano_2_0",
                                                                                          1337,
                                                                                          new Bundle()
            {
                Price    = 1337,
                BundleId = "nano_2_0"
            },
                                                                                          Array.Empty <PullDogPlan>()),
                                                                                      user)));

            //Assert
            await environment.WithFreshDataContext(async dataContext =>
            {
                var addedInstance = await dataContext
                                    .Instances
                                    .SingleOrDefaultAsync();

                Assert.IsNotNull(exception);
                Assert.IsNull(addedInstance);
            });
        }
示例#8
0
        public async Task Handle_NoPaymentMethodPresentOnStripeCustomer_NullReturned()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var createdUser = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName($"some-identity-name-{Guid.NewGuid()}")));

            //Act
            var activePaymentMethod = await environment.Mediator.Send(
                new GetActivePaymentMethodForUserQuery(
                    createdUser));

            //Assert
            Assert.IsNull(activePaymentMethod);
        }
        public async Task Handle_ExistingIdentityNameGiven_ExistingUserIsReturned()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var existingUserIdentityName = "identity-name";
            var existingUser             = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName(existingUserIdentityName)));

            //Act
            var returnedUser = await environment.Mediator.Send(
                new EnsureUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName(existingUserIdentityName)));

            //Assert
            Assert.IsNotNull(returnedUser);
            Assert.AreNotEqual(default, existingUser.Id);
示例#10
0
        public async Task Handle_IdentityNameGiven_NewUserIsAddedToDatabase()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var fakeIdentityName = Guid.NewGuid().ToString();
            var fakeIdentity     = TestClaimsPrincipalFactory.CreateWithIdentityName(fakeIdentityName);

            //Act
            await environment.Mediator.Send(new CreateUserForIdentityCommand(fakeIdentity));

            //Assert
            var createdUser = await GetUserByIdentityNameAsync(environment, fakeIdentityName);

            Assert.IsNotNull(createdUser);

            Assert.AreEqual(1, createdUser.Identities.Count);
            Assert.AreEqual(fakeIdentityName, createdUser.Identities.Single().Name);
        }
        public async Task Handle_ExistingPaymentMethodsPresent_OldPaymentMethodsRemoved()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var user = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-identity-name")));

            var paymentMethodService = environment
                                       .ServiceProvider
                                       .GetRequiredService <PaymentMethodService>();

            var existingPaymentMethod = await CreatePaymentMethodAsync(paymentMethodService);

            await paymentMethodService.AttachAsync(existingPaymentMethod.Id, new PaymentMethodAttachOptions()
            {
                Customer = user.StripeCustomerId
            });

            var newPaymentMethod = await CreatePaymentMethodAsync(paymentMethodService);

            //Act
            await environment.Mediator.Send(
                new SetActivePaymentMethodForUserCommand(
                    user,
                    newPaymentMethod.Id));

            //Assert
            var refreshedExistingPaymentMethod = await GetPaymentMethodForCustomerAsync(
                paymentMethodService,
                user.StripeCustomerId,
                existingPaymentMethod.Id);

            var refreshedNewPaymentMethod = await GetPaymentMethodForCustomerAsync(
                paymentMethodService,
                user.StripeCustomerId,
                newPaymentMethod.Id);

            Assert.IsNotNull(refreshedNewPaymentMethod);
            Assert.IsNull(refreshedExistingPaymentMethod);
        }
示例#12
0
        public async Task Handle_ProperArgumentsGiven_UnprovisionedInstanceIsAddedToDatabase()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync(new EnvironmentSetupOptions ()
            {
                IocConfiguration = services =>
                {
                    services.AddSingleton(Substitute.For <IProvisioningService>());
                }
            });

            var user = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-identity-name")));

            //Act
            await environment.Mediator.Send(
                new ProvisionInstanceForUserCommand(
                    new Plan(
                        "dummy",
                        1337,
                        new Bundle()
            {
                Price = 1337,
                BundleId = "dummy"
            },
                        Array.Empty <PullDogPlan>()),
                    user));

            //Assert
            await environment.WithFreshDataContext(async dataContext =>
            {
                var addedInstance = await dataContext
                                    .Instances
                                    .SingleAsync();

                Assert.IsNotNull(addedInstance);
                Assert.IsFalse(addedInstance.IsProvisioned);
            });
        }
示例#13
0
        public async Task Handle_SinglePaymentMethodPresentOnStripeCustomer_SinglePaymentMethodReturned()
        {
            //Arrange
            await using var environment = await IntegrationTestEnvironment.CreateAsync();

            var createdUser = await environment.Mediator.Send(
                new CreateUserForIdentityCommand(
                    TestClaimsPrincipalFactory.CreateWithIdentityName("some-identity-name")));

            var createdPaymentMethod = await environment
                                       .ServiceProvider
                                       .GetRequiredService <PaymentMethodService>()
                                       .CreateAsync(new PaymentMethodCreateOptions()
            {
                Card = new PaymentMethodCardCreateOptions()
                {
                    Number   = "4242424242424242",
                    Cvc      = "123",
                    ExpMonth = 10,
                    ExpYear  = 30
                },
                Type = "card"
            });

            await environment.Mediator.Send(
                new SetActivePaymentMethodForUserCommand(
                    createdUser,
                    createdPaymentMethod.Id));

            //Act
            var activePaymentMethod = await environment.Mediator.Send(
                new GetActivePaymentMethodForUserQuery(
                    createdUser));

            //Assert
            Assert.IsNotNull(activePaymentMethod);
        }
示例#14
0
 public static void FakeAuthentication(this ControllerBase controller, string identityName)
 {
     controller.EnsureControllerContext();
     controller.HttpContext.User = TestClaimsPrincipalFactory.CreateWithIdentityName(identityName);
 }