PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/source/Dwolla.Checkout.Tests/RedirectDeseralizationTest.cs

http://github.com/bchavez/Dwolla
C# | 96 lines | 79 code | 17 blank | 0 comment | 0 complexity | 9c033380282039b2622844f0ee468830 MD5 | raw file
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Web;
  4. using System.Web.Mvc;
  5. using System.Web.Routing;
  6. using FluentAssertions;
  7. using NUnit.Framework;
  8. using Moq;
  9. using Newtonsoft.Json;
  10. using ObjectDumper;
  11. namespace Dwolla.Checkout.Tests
  12. {
  13. public class TestController : Controller
  14. {
  15. [HttpGet]
  16. public void DwollaRedirect(DwollaRedirect redirect)
  17. {
  18. }
  19. }
  20. [TestFixture]
  21. public class RedirectDeseralizationTest
  22. {
  23. private void RegisterRoutes(RouteCollection routes)
  24. {
  25. routes.MapRoute(
  26. name: "Default",
  27. url: "{controller}/{action}/{id}",
  28. defaults: new
  29. {
  30. controller = "Home",
  31. action = "Index",
  32. id = UrlParameter.Optional
  33. }
  34. );
  35. }
  36. private T GetModelFor<T>(string url)
  37. {
  38. RouteTable.Routes.Clear();
  39. RegisterRoutes(RouteTable.Routes);
  40. var httpContext = MvcMockHelpers.MockHttpContext(url);
  41. var routeData = RouteTable.Routes.GetRouteData(httpContext);
  42. var controller = new TestController();
  43. controller.SetMockControllerContext(httpContext, routeData, RouteTable.Routes);
  44. var queryStringValueProvider = new QueryStringValueProvider(controller.ControllerContext);
  45. var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(T));
  46. var bindingContext = new ModelBindingContext
  47. {
  48. ModelName = null,
  49. ValueProvider = queryStringValueProvider,
  50. ModelMetadata = modelMetadata
  51. };
  52. var binder = ModelBinders.Binders.GetBinder(typeof(DwollaRedirect));
  53. return (T)binder.BindModel(controller.ControllerContext, bindingContext);
  54. }
  55. [Test]
  56. public void SuccessRedirect()
  57. {
  58. var successRedirect =
  59. "~/Test/DwollaRedirect?signature=c50e4b2a4c50ad795c3ee31370aec1a563565aa4&orderId=&amount=0.01&checkoutId=f32b1e55-9612-4b6d-90f9-1c1519e588da&status=Completed&clearingDate=8/28/2012%203:17:18%20PM&transaction=1312616&postback=success";
  60. var redirect = GetModelFor<DwollaRedirect>(successRedirect);
  61. redirect.Signature.Should().Be("c50e4b2a4c50ad795c3ee31370aec1a563565aa4");
  62. redirect.OrderId.Should().BeNull();
  63. redirect.Amount.Should().Be(0.01m);
  64. redirect.CheckoutId.Should().Be("f32b1e55-9612-4b6d-90f9-1c1519e588da");
  65. redirect.Status.Should().Be(DwollaStatus.Completed);
  66. redirect.Transaction.Should().Be(1312616);
  67. redirect.Postback.Should().Be(DwollaPostbackStatus.Success);
  68. }
  69. [Test]
  70. public void FailureRedirect()
  71. {
  72. var failureRedirect =
  73. "~/Test/DwollaRedirect?checkoutId=694e6bcb-349f-451d-bf5d-8aa16530c960&error=failure&error_description=There+are+insufficient+funds+for+this+transaction";
  74. var redirect = GetModelFor<DwollaRedirect>(failureRedirect);
  75. redirect.CheckoutId.Should().Be("694e6bcb-349f-451d-bf5d-8aa16530c960");
  76. redirect.Error_Description.Should().Be("There+are+insufficient+funds+for+this+transaction");
  77. }
  78. }
  79. }