|
|
|
|
@@ -151,6 +151,9 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_applicationCacheService);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Core.Billing.Payment.Models.BillingAddress CreateTestBillingAddress() =>
|
|
|
|
|
new() { Country = "US", PostalCode = "12345" };
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task Run_UserNotPremium_ReturnsBadRequest(User user)
|
|
|
|
|
{
|
|
|
|
|
@@ -158,7 +161,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
user.Premium = false;
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT1);
|
|
|
|
|
@@ -174,7 +177,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
user.GatewaySubscriptionId = null;
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT1);
|
|
|
|
|
@@ -190,7 +193,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
user.GatewaySubscriptionId = "";
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT1);
|
|
|
|
|
@@ -245,7 +248,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
@@ -320,7 +323,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Families Org", "encrypted-key", PlanType.FamiliesAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Families Org", "encrypted-key", PlanType.FamiliesAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
@@ -383,7 +386,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
@@ -453,7 +456,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
@@ -520,7 +523,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
@@ -589,7 +592,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
@@ -636,12 +639,12 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
_pricingClient.ListPremiumPlans().Returns(mockPremiumPlans);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT1);
|
|
|
|
|
var badRequest = result.AsT1;
|
|
|
|
|
Assert.Equal("Premium subscription item not found.", badRequest.Response);
|
|
|
|
|
Assert.Equal("Premium subscription password manager item not found.", badRequest.Response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
@@ -697,7 +700,7 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var testStartTime = DateTime.UtcNow;
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually);
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
var testEndTime = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
@@ -714,4 +717,110 @@ public class UpgradePremiumToOrganizationCommandTests
|
|
|
|
|
Assert.True(trialEndDateTime.Value >= testStartTime.AddDays(7));
|
|
|
|
|
Assert.True(trialEndDateTime.Value <= testEndTime.AddDays(7));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task Run_UpdatesCustomerBillingAddress(User user)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
user.Premium = true;
|
|
|
|
|
user.GatewaySubscriptionId = "sub_123";
|
|
|
|
|
user.GatewayCustomerId = "cus_123";
|
|
|
|
|
|
|
|
|
|
var mockSubscription = new Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "sub_123",
|
|
|
|
|
Items = new StripeList<SubscriptionItem>
|
|
|
|
|
{
|
|
|
|
|
Data = new List<SubscriptionItem>
|
|
|
|
|
{
|
|
|
|
|
new SubscriptionItem
|
|
|
|
|
{
|
|
|
|
|
Id = "si_premium",
|
|
|
|
|
Price = new Price { Id = "premium-annually" }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Metadata = new Dictionary<string, string>()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var mockPremiumPlans = CreateTestPremiumPlansList();
|
|
|
|
|
var mockPlan = CreateTestPlan(PlanType.TeamsAnnually, stripeSeatPlanId: "teams-seat-annually");
|
|
|
|
|
|
|
|
|
|
_stripeAdapter.GetSubscriptionAsync("sub_123").Returns(mockSubscription);
|
|
|
|
|
_pricingClient.ListPremiumPlans().Returns(mockPremiumPlans);
|
|
|
|
|
_pricingClient.GetPlanOrThrow(PlanType.TeamsAnnually).Returns(mockPlan);
|
|
|
|
|
_stripeAdapter.UpdateSubscriptionAsync(Arg.Any<string>(), Arg.Any<SubscriptionUpdateOptions>()).Returns(mockSubscription);
|
|
|
|
|
_stripeAdapter.UpdateCustomerAsync(Arg.Any<string>(), Arg.Any<CustomerUpdateOptions>()).Returns(Task.FromResult(new Customer()));
|
|
|
|
|
_organizationRepository.CreateAsync(Arg.Any<Organization>()).Returns(callInfo => Task.FromResult(callInfo.Arg<Organization>()));
|
|
|
|
|
_organizationApiKeyRepository.CreateAsync(Arg.Any<OrganizationApiKey>()).Returns(callInfo => Task.FromResult(callInfo.Arg<OrganizationApiKey>()));
|
|
|
|
|
_organizationUserRepository.CreateAsync(Arg.Any<OrganizationUser>()).Returns(callInfo => Task.FromResult(callInfo.Arg<OrganizationUser>()));
|
|
|
|
|
_applicationCacheService.UpsertOrganizationAbilityAsync(Arg.Any<Organization>()).Returns(Task.CompletedTask);
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
var billingAddress = new Core.Billing.Payment.Models.BillingAddress { Country = "US", PostalCode = "12345" };
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, billingAddress);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
|
|
|
|
|
await _stripeAdapter.Received(1).UpdateCustomerAsync(
|
|
|
|
|
"cus_123",
|
|
|
|
|
Arg.Is<CustomerUpdateOptions>(opts =>
|
|
|
|
|
opts.Address.Country == "US" &&
|
|
|
|
|
opts.Address.PostalCode == "12345"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task Run_EnablesAutomaticTaxOnSubscription(User user)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
user.Premium = true;
|
|
|
|
|
user.GatewaySubscriptionId = "sub_123";
|
|
|
|
|
user.GatewayCustomerId = "cus_123";
|
|
|
|
|
|
|
|
|
|
var mockSubscription = new Subscription
|
|
|
|
|
{
|
|
|
|
|
Id = "sub_123",
|
|
|
|
|
Items = new StripeList<SubscriptionItem>
|
|
|
|
|
{
|
|
|
|
|
Data = new List<SubscriptionItem>
|
|
|
|
|
{
|
|
|
|
|
new SubscriptionItem
|
|
|
|
|
{
|
|
|
|
|
Id = "si_premium",
|
|
|
|
|
Price = new Price { Id = "premium-annually" }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Metadata = new Dictionary<string, string>()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var mockPremiumPlans = CreateTestPremiumPlansList();
|
|
|
|
|
var mockPlan = CreateTestPlan(PlanType.TeamsAnnually, stripeSeatPlanId: "teams-seat-annually");
|
|
|
|
|
|
|
|
|
|
_stripeAdapter.GetSubscriptionAsync("sub_123").Returns(mockSubscription);
|
|
|
|
|
_pricingClient.ListPremiumPlans().Returns(mockPremiumPlans);
|
|
|
|
|
_pricingClient.GetPlanOrThrow(PlanType.TeamsAnnually).Returns(mockPlan);
|
|
|
|
|
_stripeAdapter.UpdateSubscriptionAsync(Arg.Any<string>(), Arg.Any<SubscriptionUpdateOptions>()).Returns(mockSubscription);
|
|
|
|
|
_stripeAdapter.UpdateCustomerAsync(Arg.Any<string>(), Arg.Any<CustomerUpdateOptions>()).Returns(Task.FromResult(new Customer()));
|
|
|
|
|
_organizationRepository.CreateAsync(Arg.Any<Organization>()).Returns(callInfo => Task.FromResult(callInfo.Arg<Organization>()));
|
|
|
|
|
_organizationApiKeyRepository.CreateAsync(Arg.Any<OrganizationApiKey>()).Returns(callInfo => Task.FromResult(callInfo.Arg<OrganizationApiKey>()));
|
|
|
|
|
_organizationUserRepository.CreateAsync(Arg.Any<OrganizationUser>()).Returns(callInfo => Task.FromResult(callInfo.Arg<OrganizationUser>()));
|
|
|
|
|
_applicationCacheService.UpsertOrganizationAbilityAsync(Arg.Any<Organization>()).Returns(Task.CompletedTask);
|
|
|
|
|
_userService.SaveUserAsync(user).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _command.Run(user, "My Organization", "encrypted-key", PlanType.TeamsAnnually, CreateTestBillingAddress());
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.True(result.IsT0);
|
|
|
|
|
|
|
|
|
|
await _stripeAdapter.Received(1).UpdateSubscriptionAsync(
|
|
|
|
|
"sub_123",
|
|
|
|
|
Arg.Is<SubscriptionUpdateOptions>(opts =>
|
|
|
|
|
opts.AutomaticTax != null &&
|
|
|
|
|
opts.AutomaticTax.Enabled == true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|