Exemplo n.º 1
0
        public async Task Handle_NoUserFoundInDatabaseAndUserIdGiven_CreatesGroupForUserId()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Any <EnsureAmazonGroupWithNameCommand>())
            .Returns(new Group()
            {
                GroupName = "some-group"
            });

            var fakeAmazonIdentityManagementService = Substitute.For <IAmazonIdentityManagementService>();

            fakeAmazonIdentityManagementService
            .CreateAccessKeyAsync(Arg.Any <CreateAccessKeyRequest>())
            .Returns(new CreateAccessKeyResponse()
            {
                AccessKey = new AccessKey()
            });

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

            var user = new Dogger.Domain.Models.User()
            {
                StripeCustomerId = "dummy"
            };
            await environment.WithFreshDataContext(async dataContext =>
            {
                await dataContext.Users.AddAsync(user);
            });

            //Act
            var amazonUser = await environment.Mediator.Send(new EnsureAmazonUserWithNameCommand("some-name")
            {
                UserId = user.Id
            });

            //Assert
            Assert.IsNotNull(amazonUser);

            await fakeAmazonIdentityManagementService
            .Received(1)
            .AddUserToGroupAsync(Arg.Is <AddUserToGroupRequest>(args =>
                                                                args.GroupName == "some-group" &&
                                                                args.UserName == "some-name"));
        }
Exemplo n.º 2
0
        public async Task Handle_ErrorOccuredDuringCreationOfGroup_DeletesCreatedAmazonUserAccessKey()
        {
            //Arrange
            var fakeMediator = Substitute.For <IMediator>();

            fakeMediator
            .Send(Arg.Any <EnsureAmazonGroupWithNameCommand>())
            .Throws(new TestException());

            var fakeAmazonIdentityManagementService = Substitute.For <IAmazonIdentityManagementService>();

            fakeAmazonIdentityManagementService
            .CreateAccessKeyAsync(Arg.Any <CreateAccessKeyRequest>())
            .Returns(new CreateAccessKeyResponse()
            {
                AccessKey = new AccessKey()
            });

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

            var user = new Dogger.Domain.Models.User()
            {
                StripeCustomerId = "dummy"
            };
            await environment.WithFreshDataContext(async dataContext =>
            {
                await dataContext.Users.AddAsync(user);
            });

            //Act
            var exception = await Assert.ThrowsExceptionAsync <TestException>(async() =>
                                                                              await environment.Mediator.Send(new EnsureAmazonUserWithNameCommand("some-name")
            {
                UserId = user.Id
            }));

            //Assert
            Assert.IsNotNull(exception);

            await fakeAmazonIdentityManagementService
            .Received(1)
            .DeleteAccessKeyAsync(Arg.Is <DeleteAccessKeyRequest>(args => args.AccessKeyId == "some-name"));
        }
        public async Task Handle_ProvisionedNonFreeInstancePresent_SubscriptionIsUpdated()
        {
            //Arrange
            var fakeInstanceId = Guid.NewGuid();

            var fakeAmazonLightsailClient = Substitute.For <IAmazonLightsail>();

            fakeAmazonLightsailClient
            .DeleteInstanceAsync(
                Arg.Any <DeleteInstanceRequest>(),
                default)
            .Returns(new DeleteInstanceResponse()
            {
                Operations = new List <Operation>()
            });

            var fakeLightsailOperationService = Substitute.For <ILightsailOperationService>();

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

            var stripeCustomerService = environment.ServiceProvider.GetRequiredService <CustomerService>();
            var customer = await stripeCustomerService.CreateAsync(new CustomerCreateOptions()
            {
                Email = "*****@*****.**"
            });

            var stripePaymentMethodService = environment.ServiceProvider.GetRequiredService <PaymentMethodService>();
            var paymentMethod = await stripePaymentMethodService.AttachAsync("pm_card_visa", new PaymentMethodAttachOptions()
            {
                Customer = customer.Id
            });

            var stripeSubscriptionService = environment.ServiceProvider.GetRequiredService <SubscriptionService>();
            var subscription = await stripeSubscriptionService.CreateAsync(new SubscriptionCreateOptions()
            {
                Customer             = customer.Id,
                DefaultPaymentMethod = paymentMethod.Id,
                Items = new List <SubscriptionItemOptions>()
                {
                    new SubscriptionItemOptions()
                    {
                        Plan = "nano_2_0"
                    }
                },
                Metadata = new Dictionary <string, string>()
                {
                    {
                        "InstanceId", fakeInstanceId.ToString()
                    }
                }
            });

            var clusterId = Guid.NewGuid();
            var user      = new User()
            {
                StripeCustomerId     = customer.Id,
                StripeSubscriptionId = subscription.Id
            };

            var cluster = new Cluster()
            {
                Id     = clusterId,
                User   = user,
                UserId = user.Id
            };

            user.Clusters.Add(cluster);

            var instance = new Instance()
            {
                Id            = fakeInstanceId,
                Name          = "some-instance-name",
                PlanId        = "dummy",
                Cluster       = cluster,
                ClusterId     = cluster.Id,
                IsProvisioned = true
            };

            cluster.Instances.Add(instance);

            await environment.WithFreshDataContext(async dataContext =>
            {
                await dataContext.Clusters.AddAsync(cluster);
                await dataContext.Instances.AddAsync(instance);
                await dataContext.Users.AddAsync(user);
            });

            //Act
            await environment.Mediator.Send(
                new DeleteInstanceByNameCommand("some-instance-name"),
                default);

            //Assert
            var refreshedSubscription = await stripeSubscriptionService.GetAsync(subscription.Id);

            Assert.AreNotEqual(refreshedSubscription.Status, subscription.Status);
            Assert.AreEqual("canceled", refreshedSubscription.Status);
        }