PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Dwolla.Checkout.Tests/ValidationTests/DwollaCheckoutRequestValidatorTests.cs

http://github.com/bchavez/Dwolla
C# | 154 lines | 129 code | 24 blank | 1 comment | 0 complexity | 6190069f7cb87c2cfe3b103a3a71445b MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Dwolla.Checkout.Validators;
  4. using FluentAssertions;
  5. using FluentValidation;
  6. using FluentValidation.Attributes;
  7. using FluentValidation.TestHelper;
  8. using NUnit.Framework;
  9. namespace Dwolla.Checkout.Tests.ValidationTests
  10. {
  11. [TestFixture]
  12. public class DwollaCheckoutRequestValidatorTests
  13. {
  14. private DwollaCheckoutRequestValidator validator;
  15. [SetUp]
  16. public void BeforeEachTest()
  17. {
  18. validator = new DwollaCheckoutRequestValidator();
  19. }
  20. [Test]
  21. public void valid_key()
  22. {
  23. validator.ShouldNotHaveValidationErrorFor( cr => cr.ClientId, "abcdefg" );
  24. validator.ShouldHaveValidationErrorFor( cr => cr.ClientId, null as string );
  25. validator.ShouldHaveValidationErrorFor( cr => cr.ClientId, string.Empty );
  26. }
  27. [Test]
  28. public void valid_secret()
  29. {
  30. validator.ShouldNotHaveValidationErrorFor( cr => cr.ClientSecret, "abcdefg" );
  31. validator.ShouldHaveValidationErrorFor( cr => cr.ClientSecret, null as string );
  32. validator.ShouldHaveValidationErrorFor( cr => cr.ClientSecret, string.Empty );
  33. }
  34. [Test]
  35. public void callback_url_validation()
  36. {
  37. validator.ShouldNotHaveValidationErrorFor( cr => cr.Callback, new Uri( "http://www.example.com/callback" ) );
  38. validator.ShouldHaveValidationErrorFor( cr => cr.Callback, new Uri( "/callback", UriKind.Relative ) );
  39. }
  40. [Test]
  41. public void redirect_url_validation()
  42. {
  43. validator.ShouldNotHaveValidationErrorFor( cr => cr.Redirect, new Uri( "http://www.example.com/redirect" ) );
  44. validator.ShouldHaveValidationErrorFor( cr => cr.Redirect, new Uri( "/redirect", UriKind.Relative ) );
  45. }
  46. [Test]
  47. public void orderid_validation()
  48. {
  49. validator.ShouldNotHaveValidationErrorFor( cr => cr.OrderId, "A1B2C3" );
  50. validator.ShouldNotHaveValidationErrorFor( cr => cr.OrderId, null as string );
  51. validator.ShouldHaveValidationErrorFor( cr => cr.ClientSecret, "" );
  52. }
  53. [Test]
  54. public void allow_guest_checkout_validation()
  55. {
  56. var cr = new DwollaCheckoutRequest()
  57. {
  58. AllowGuestCheckout = true,
  59. AllowFundingSources = false
  60. };
  61. validator.ShouldHaveValidationErrorFor( x => x.AllowFundingSources, cr );
  62. cr.AllowGuestCheckout = true;
  63. cr.AllowFundingSources = true;
  64. validator.ShouldNotHaveValidationErrorFor( x => x.AllowFundingSources, cr );
  65. cr.AllowGuestCheckout = false;
  66. cr.AllowFundingSources = false;
  67. validator.ShouldNotHaveValidationErrorFor( x => x.AllowFundingSources, cr );
  68. }
  69. [Test]
  70. public void setting_additional_funding_sources_requires_allow_funding_sources_to_be_true()
  71. {
  72. var cr = new DwollaCheckoutRequest()
  73. {
  74. AllowFundingSources = false,
  75. AdditionalFundingSources = FundingSource.Banks | FundingSource.Credit
  76. };
  77. validator.ShouldHaveValidationErrorFor(x => x.AllowFundingSources, cr);
  78. cr.AllowFundingSources = true;
  79. validator.ShouldNotHaveValidationErrorFor(x => x.AllowFundingSources, cr);
  80. }
  81. [Test]
  82. public void purchase_order_validation()
  83. {
  84. validator.ShouldHaveValidationErrorFor( cr => cr.PurchaseOrder, null as DwollaPurchaseOrder );
  85. }
  86. [Test]
  87. public void should_have_child_validator_on_purcahse_order_property()
  88. {
  89. validator.ShouldHaveChildValidator( cr => cr.PurchaseOrder, typeof(DwollaPurchaseOrderValidator) );
  90. }
  91. [Test]
  92. public void invalid_order_item_should_cause_checkout_request_to_fail_validation()
  93. {
  94. var badRequest = new DwollaCheckoutRequest
  95. {
  96. ClientId = "abcdefg",
  97. ClientSecret = "abcdefg",
  98. OrderId = "MyOrderId1",
  99. PurchaseOrder = new DwollaPurchaseOrder
  100. {
  101. OrderItems =
  102. {
  103. new DwollaOrderItem("Candy Bar",-25.00m, 0)
  104. {
  105. Description = "Expensive Candy Bar",
  106. }
  107. },
  108. DestinationId = "",
  109. },
  110. };
  111. var results = new AttributedValidatorFactory().GetValidator<DwollaCheckoutRequest>()
  112. .Validate( badRequest );
  113. results.IsValid.Should().BeFalse();
  114. //check customized error message descriptions too.
  115. results.Errors.Select( x =>
  116. {
  117. Console.WriteLine( (object)x );
  118. return x.ErrorMessage;
  119. }
  120. ).SequenceEqual( new[]
  121. {
  122. "'PurchaseOrder.DestinationId' should not be empty.",
  123. "The 'OrderItem.Price' for 'Candy Bar' must be greater than or equal to $0.00.",
  124. "The 'OrderItem.Quantity' for 'Candy Bar' must be greater than or equal to 1.",
  125. "'PurchaseOrder.Total' must be greater than or equal to '1.00'."
  126. } )
  127. .Should().BeTrue();
  128. }
  129. }
  130. }