Skip to content

Commit

Permalink
Merge pull request #77 from EdLichtman/develop
Browse files Browse the repository at this point in the history
Note: This introduces a breaking change, Chargify id is now long
  • Loading branch information
kfrancis authored Apr 1, 2019
2 parents 435715d + 53fc1cf commit 19f5d8a
Show file tree
Hide file tree
Showing 15 changed files with 586 additions and 74 deletions.
1 change: 1 addition & 0 deletions Source/Chargify.NET/ChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5309,6 +5309,7 @@ public IBillingManagementInfo GetManagementLink(int chargifyId)
throw;
}
}

#endregion

#region Invoices
Expand Down
24 changes: 24 additions & 0 deletions Source/ChargifyDotNet.Tests/Base/ChargifyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,29 @@ internal void SetJson(bool useJson)
_chargify.UseJSON = useJson;
}
}

internal int GetRandomNegativeInt()
{
return Math.Abs(Guid.NewGuid().GetHashCode()) * -1;
}
internal void ValidateRun(Func<bool> validation, string customFailureMessage = null)
{//To prevent "multiple asserts" in a single test class this masks the
//idea of having multiple asserts and allows us to verify all data is valid before running
if (!validation())
Assert.Fail(customFailureMessage ?? "The test setup resulted in invalid test data. Please resolve any issues before continuing");
}

internal void AssertTheFollowingThrowsException(Action runAttempt, Action<Exception> runAssertions)
{
try
{
runAttempt();
Assert.Fail("Attempt should have thrown an error but did not");
}
catch (Exception e)
{
runAssertions(e);
}
}
}
}
159 changes: 158 additions & 1 deletion Source/ChargifyDotNet.Tests/CustomerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,164 @@ public void Customer_CreateCustomer()
Assert.IsTrue(createdCustomer.ShippingCountry == customer.ShippingCountry);
Assert.IsTrue(createdCustomer.TaxExempt);

// Can't cleanup, Chargify doesn't support customer deletions
Chargify.DeleteCustomer(createdCustomer.ChargifyID);
}

[TestMethod]
public void Can_revoke_billing_portal_access()
{
// Arrange
string referenceID = Guid.NewGuid().ToString();
var customer = new Customer()
{
FirstName = Faker.Name.FirstName(),
LastName = Faker.Name.LastName(),
Email = Faker.Internet.Email(),
Phone = Faker.Phone.PhoneNumber(),
Organization = Faker.Company.CompanyName(),
SystemID = referenceID,
ShippingAddress = Faker.Address.StreetAddress(false),
ShippingAddress2 = Faker.Address.SecondaryAddress(),
ShippingCity = Faker.Address.City(),
ShippingState = Faker.Address.StateAbbr(),
ShippingZip = Faker.Address.ZipCode(),
ShippingCountry = "US",
TaxExempt = true
};

// Act
var createdCustomer = Chargify.CreateCustomer(customer);
Chargify.RevokeBillingPortalAccess(createdCustomer.ChargifyID);
// Assert

try
{
Chargify.GetManagementLink(createdCustomer.ChargifyID);
Assert.Fail("Error was expected, but not received");
}
catch (ChargifyException chEx)
{
Assert.IsNotNull(chEx.ErrorMessages);
Assert.AreEqual(1, chEx.ErrorMessages.Count);
Assert.IsTrue(chEx.ErrorMessages.Any(e => e.Message.Contains("Billing Portal")), $"Found '{string.Join(", ", chEx.ErrorMessages.Select(x => x.Message))}'");
//todo: Need to run test to find out the exact error message
}

Chargify.DeleteCustomer(createdCustomer.ChargifyID);
}

[TestMethod]
public void Can_enable_billing_portal_access()
{
// Arrange
string referenceID = Guid.NewGuid().ToString();
var customer = new Customer()
{
FirstName = Faker.Name.FirstName(),
LastName = Faker.Name.LastName(),
Email = Faker.Internet.Email(),
Phone = Faker.Phone.PhoneNumber(),
Organization = Faker.Company.CompanyName(),
SystemID = referenceID,
ShippingAddress = Faker.Address.StreetAddress(false),
ShippingAddress2 = Faker.Address.SecondaryAddress(),
ShippingCity = Faker.Address.City(),
ShippingState = Faker.Address.StateAbbr(),
ShippingZip = Faker.Address.ZipCode(),
ShippingCountry = "US",
TaxExempt = true
};

// Act
var createdCustomer = Chargify.CreateCustomer(customer);
Chargify.RevokeBillingPortalAccess(createdCustomer.ChargifyID);
Chargify.EnableBillingPortalAccess(createdCustomer.ChargifyID);

// Assert
var managementLink = Chargify.GetManagementLink(createdCustomer.ChargifyID);
Assert.IsNotNull(managementLink);


Chargify.DeleteCustomer(createdCustomer.ChargifyID);
}


[TestMethod]
public void Can_revoke_billing_portal_access_by_system_id()
{
// Arrange
string referenceID = Guid.NewGuid().ToString();
var customer = new Customer()
{
FirstName = Faker.Name.FirstName(),
LastName = Faker.Name.LastName(),
Email = Faker.Internet.Email(),
Phone = Faker.Phone.PhoneNumber(),
Organization = Faker.Company.CompanyName(),
SystemID = referenceID,
ShippingAddress = Faker.Address.StreetAddress(false),
ShippingAddress2 = Faker.Address.SecondaryAddress(),
ShippingCity = Faker.Address.City(),
ShippingState = Faker.Address.StateAbbr(),
ShippingZip = Faker.Address.ZipCode(),
ShippingCountry = "US",
TaxExempt = true
};

// Act
var createdCustomer = Chargify.CreateCustomer(customer);
Chargify.RevokeBillingPortalAccess(createdCustomer.SystemID);
// Assert

try
{
Chargify.GetManagementLink(createdCustomer.ChargifyID);
Assert.Fail("Error was expected, but not received");
}
catch (ChargifyException chEx)
{
Assert.IsNotNull(chEx.ErrorMessages);
Assert.AreEqual(1, chEx.ErrorMessages.Count);
Assert.IsTrue(chEx.ErrorMessages.Any(e => e.Message.Contains("Billing Portal")));
//todo: Need to run test to find out the exact error message
}

Chargify.DeleteCustomer(createdCustomer.ChargifyID);
}

[TestMethod]
public void Can_enable_billing_portal_access_by_system_Id()
{
// Arrange
string referenceID = Guid.NewGuid().ToString();
var customer = new Customer()
{
FirstName = Faker.Name.FirstName(),
LastName = Faker.Name.LastName(),
Email = Faker.Internet.Email(),
Phone = Faker.Phone.PhoneNumber(),
Organization = Faker.Company.CompanyName(),
SystemID = referenceID,
ShippingAddress = Faker.Address.StreetAddress(false),
ShippingAddress2 = Faker.Address.SecondaryAddress(),
ShippingCity = Faker.Address.City(),
ShippingState = Faker.Address.StateAbbr(),
ShippingZip = Faker.Address.ZipCode(),
ShippingCountry = "US",
TaxExempt = true
};

// Act
var createdCustomer = Chargify.CreateCustomer(customer);
Chargify.RevokeBillingPortalAccess(createdCustomer.ChargifyID);
Chargify.EnableBillingPortalAccess(createdCustomer.SystemID);

// Assert
var managementLink = Chargify.GetManagementLink(createdCustomer.ChargifyID);
Assert.IsNotNull(managementLink);


Chargify.DeleteCustomer(createdCustomer.ChargifyID);
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion Source/ChargifyDotNet.Tests/MetafieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void Metadata_Can_Read_Specific_Subscription()
Assert.IsTrue(result.TotalCount != int.MinValue);
Assert.IsTrue(result.TotalPages != int.MinValue);
Assert.AreEqual(result.TotalCount, result.Metadata.Count);
Assert.AreEqual(3, result.Metadata.Where(m => !string.IsNullOrEmpty(m.Name)).Count());
Assert.IsTrue(result.Metadata.Where(m => !string.IsNullOrEmpty(m.Name)).Count() > 0);

TestContext.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result));

Expand Down
2 changes: 1 addition & 1 deletion Source/ChargifyDotNet.Tests/SiteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Startup()
productHandle = productList.FirstOrDefault().Value.Handle;
}

var customerId = int.MinValue;
var customerId = long.MinValue;
var customerList = Chargify.GetCustomerList();
string referenceID = Guid.NewGuid().ToString();
Customer customer = null;
Expand Down
86 changes: 84 additions & 2 deletions Source/ChargifyDotNet.Tests/SubscriptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,30 @@ public void Subscription_Can_EditProduct_NoDelay()
Assert.IsNotNull(result);
Assert.AreEqual(otherProduct.Handle, result.Product.Handle);
}
[TestMethod]
public void Subscription_Can_Be_Purged()
{
// Arrange
var trialingProduct = Chargify.GetProductList().Values.FirstOrDefault(p => p.TrialInterval > 0);
var referenceId = Guid.NewGuid().ToString();
var expMonth = DateTime.Now.AddMonths(1).Month;
var expYear = DateTime.Now.AddMonths(12).Year;

var newCustomer = new CustomerAttributes(Faker.Name.FirstName(), Faker.Name.LastName(), Faker.Internet.Email(), Faker.Phone.PhoneNumber(), Faker.Company.CompanyName(), referenceId);
var newPaymentInfo = GetTestPaymentMethod(newCustomer);
var createdSubscription = Chargify.CreateSubscription(trialingProduct.Handle, newCustomer, newPaymentInfo);
Assert.IsNotNull(createdSubscription);

Chargify.PurgeSubscription(createdSubscription.SubscriptionID);
var purgedSubscription = Chargify.Find<Subscription>(createdSubscription.SubscriptionID);

Assert.IsNull(purgedSubscription);
}
[TestMethod]
public void Subscription_Can_Reactivate_Without_Trial()
{
// Arrange
var trialingProduct = Chargify.GetProductList().Values.FirstOrDefault(p => p.TrialInterval > 0);
var trialingProduct = Chargify.GetProductList().Values.FirstOrDefault(p => p.TrialInterval > 0 && !string.IsNullOrEmpty(p.Handle));
var referenceId = Guid.NewGuid().ToString();
var expMonth = DateTime.Now.AddMonths(1).Month;
var expYear = DateTime.Now.AddMonths(12).Year;
Expand Down Expand Up @@ -411,7 +429,7 @@ public void Subscription_Can_Reactivate_Without_Trial()
public void Subscription_Can_Reactivate_With_Trial()
{
// Arrange
var trialingProduct = Chargify.GetProductList().Values.FirstOrDefault(p => p.TrialInterval > 0);
var trialingProduct = Chargify.GetProductList().Values.FirstOrDefault(p => p.TrialInterval > 0 && !string.IsNullOrEmpty(p.Handle));
var referenceId = Guid.NewGuid().ToString();
var expMonth = DateTime.Now.AddMonths(1).Month;
var expYear = DateTime.Now.AddMonths(12).Year;
Expand Down Expand Up @@ -744,6 +762,70 @@ public void Subscription_UpdateBillingDate()
Assert.IsTrue(billingDate == restoredSubscription.NextAssessmentAt);
}

[TestMethod]
public void Can_create_delayed_cancel()
{
var existingSubscription = Chargify.GetSubscriptionList().Values.FirstOrDefault(s => s.State == SubscriptionState.Active && s.PaymentProfile != null && s.PaymentProfile.Id > 0) as Subscription;
ValidateRun(() => existingSubscription != null, "No applicable subscription found.");
ValidateRun(() => existingSubscription.PaymentProfile.Id > 0, "No payment profile found");

var newSubscription = Chargify.CreateSubscription(existingSubscription.Product.Handle, existingSubscription.Customer.ToCustomerAttributes(), DateTime.MinValue, existingSubscription.PaymentProfile.Id);
ValidateRun(() => newSubscription != null, "No new subscription was created. Cannot test cancellation");

var updatedSubscription = Chargify.UpdateDelayedCancelForSubscription(newSubscription.SubscriptionID, true, "Testing Delayed Cancel");

Assert.IsTrue(updatedSubscription.CancelAtEndOfPeriod);
}

[TestMethod]
public void Can_undo_delayed_cancel()
{
var existingSubscription = Chargify.GetSubscriptionList().Values.FirstOrDefault(s => s.State == SubscriptionState.Active && s.PaymentProfile != null && s.PaymentProfile.Id > 0) as Subscription;
ValidateRun(() => existingSubscription != null, "No applicable subscription found.");
ValidateRun(() => existingSubscription.PaymentProfile.Id > 0, "No payment profile found");

var newSubscription = Chargify.CreateSubscription(existingSubscription.Product.Handle, existingSubscription.Customer.ToCustomerAttributes(), DateTime.MinValue, existingSubscription.PaymentProfile.Id);
ValidateRun(() => newSubscription != null, "No new subscription was created. Cannot test cancellation");

var cancelledSubscription = Chargify.UpdateDelayedCancelForSubscription(newSubscription.SubscriptionID, true, "Testing Delayed Cancel");
ValidateRun(() => cancelledSubscription.CancelAtEndOfPeriod, "Subscription is not cancelled at end of period. No opportunity to test uncancel");

var updatedSubscription = Chargify.UpdateDelayedCancelForSubscription(cancelledSubscription.SubscriptionID,
false, "Testing Undo Delayed Cancel");

Assert.IsFalse(updatedSubscription.CancelAtEndOfPeriod);
}

[TestMethod]
public void Chargify_exception_is_thrown_when_setting_delayed_cancel_of_invalid_subscription_to_true()
{
AssertTheFollowingThrowsException(() =>
{
Chargify.UpdateDelayedCancelForSubscription(GetRandomNegativeInt(), true,
"No subscription exists by this number");
},
e =>
{
var exception = (ChargifyException)e;
Assert.AreEqual(exception.ErrorMessages.First(), "Subscription not found");
});
}

[TestMethod]
public void Chargify_exception_is_thrown_when_setting_delayed_cancel_of_invalid_subscription_to_false()
{
AssertTheFollowingThrowsException(() =>
{
Chargify.UpdateDelayedCancelForSubscription(GetRandomNegativeInt(), false,
"No subscription exists by this number");
},
e =>
{
var exception = (ChargifyException)e;
Assert.AreEqual(exception.ErrorMessages.First(), "Subscription not found");
});
}

[TestMethod, Ignore]
public void Subscription_Update()
{
Expand Down
Loading

0 comments on commit 19f5d8a

Please sign in to comment.