using System; using System.Collections.Specialized; using System.Linq; using adMoto.Payments.Core; using adMoto.Payments.Core.Data; using adMoto.Payments.Core.Interfaces; using adMoto.Payments.Test.Fakes; using adMoto.Payments.Web.Controllers; using NUnit.Framework; using MvcContrib.TestHelper; using System.Web.Mvc; namespace adMoto.Payments.Test.Controllers { [TestFixture] // ReSharper disable InconsistentNaming public class eCardControllerTests // ReSharper restore InconsistentNaming { private IRepository _repConfirm; private eCardController CreateController() { _repConfirm = new Repository(new FakeDataContext()); var builder = new TestControllerBuilder(); return builder.CreateController(_repConfirm); } [Test] [Category("Unit")] public void Status_Returns_True_When_Passing_Correct_Request_Data() { var controller = CreateController(); const string dateFormat = "yyyy-MM-dd HH:mm:ss.fff"; var dateTime = DateTime.Now.ToString(dateFormat); var formValues = new NameValueCollection { {"MERCHANTNUMBER", "1"}, {"ORDERNUMBER", "2"}, {"COMMTYPE", "3"}, {"CURRENTSTATE", "4"}, {"PREVIOUSSTATE", "5"}, {"PAYMENTTYPE", "1"}, {"EVENTTYPE", "0"}, {"PAYMENTNUMBER", "1"}, {"APPROVALCODE", "6"}, {"VALIDATIONCODE", "7"}, {"BIN", "8"}, {"AUTHTIME", dateTime}, {"TYPE", "9"}, {"WITHCVC", "10"}}; controller.Request.Form.Add(formValues); var result = controller.Status() as ContentResult; Assert.That(result.Content.Contains("OK")); var paymentConfirmation = _repConfirm.FindAll().First(); Assert.That(paymentConfirmation.MERCHANTNUMBER, Is.EqualTo("1")); Assert.That(paymentConfirmation.ORDERNUMBER, Is.EqualTo(2)); Assert.That(paymentConfirmation.COMMTYPE, Is.EqualTo("3")); Assert.That(paymentConfirmation.CURRENTSTATE, Is.EqualTo("4")); Assert.That(paymentConfirmation.PREVIOUSSTATE, Is.EqualTo("5")); Assert.That(paymentConfirmation.APPROVALCODE, Is.EqualTo("6")); Assert.That(paymentConfirmation.VALIDATIONCODE, Is.EqualTo("7")); Assert.That(paymentConfirmation.BIN, Is.EqualTo("8")); Assert.That(paymentConfirmation.TYPE, Is.EqualTo("9")); Assert.That(paymentConfirmation.WITHCVC, Is.EqualTo("10")); Assert.That(paymentConfirmation.PAYMENTTYPE, Is.True); Assert.That(paymentConfirmation.EVENTTYPE, Is.False); Assert.That(paymentConfirmation.PAYMENTNUMBER, Is.True); Assert.That(paymentConfirmation.AUTHTIME.Value.ToString(dateFormat), Is.EqualTo(dateTime)); } [Test] [Category("Unit")] public void Status_Returns_False_And_Throws_FormatException_When_Passing_OrderNumber_In_Wrong_Format() { var controller = CreateController(); var valueCollection = new NameValueCollection {{"ORDERNUMBER", "fdsf"}}; controller.Request.Form.Add(valueCollection); var result = controller.Status() as ContentResult; Assert.That(_repConfirm.Count(), Is.EqualTo(0)); Assert.That(result.Content.Contains("FALSE")); Assert.That(result.Content.Contains("FormatException")); } [Test] [Category("Unit")] public void Status_Returns_False_And_Throws_FormatException_When_Passing_PaymentType_In_Wrong_Format() { var controller = CreateController(); var valueCollection = new NameValueCollection { { "PAYMENTTYPE", "32hvhsvhv" } }; controller.Request.Form.Add(valueCollection); var result = controller.Status() as ContentResult; Assert.That(_repConfirm.Count(), Is.EqualTo(0)); Assert.That(result.Content.Contains("FALSE")); Assert.That(result.Content.Contains("FormatException")); } [Test] [Category("Unit")] public void Status_Returns_False_And_Throws_FormatException_When_Passing_EventType_In_Wrong_Format() { var controller = CreateController(); var valueCollection = new NameValueCollection { { "EVENTTYPE", "32hvhsvhv" } }; controller.Request.Form.Add(valueCollection); var result = controller.Status() as ContentResult; Assert.That(_repConfirm.Count(), Is.EqualTo(0)); Assert.That(result.Content.Contains("FALSE")); Assert.That(result.Content.Contains("FormatException")); } [Test] [Category("Unit")] public void Status_Returns_False_And_Throws_OverflowException_When_Passing_OrderNumer_Greater_Than_Integer() { var controller = CreateController(); var valueCollection = new NameValueCollection { { "ORDERNUMBER", "12311111111111111" } }; controller.Request.Form.Add(valueCollection); var result = controller.Status() as ContentResult; Assert.That(_repConfirm.Count(), Is.EqualTo(0)); Assert.That(result.Content.Contains("FALSE"), "Response should contain FALSE"); Assert.That(result.Content.Contains("OverflowException"), "Response should contain OverflowException"); } [Test] [Category("Unit")] public void Status_Returns_False_And_Throws_SqlException_When_Passing_ValidationCode_Longer_Than_Three_Characters() { var controller = CreateController(); var valueCollection = new NameValueCollection { { "VALIDATIONCODE", "1234" } }; controller.Request.Form.Add(valueCollection); var result = controller.Status() as ContentResult; Assert.That(_repConfirm.Count(), Is.EqualTo(0), "Should not have any items"); Assert.That(result.Content.Contains("FALSE"), "Response should contain FALSE"); Assert.That(result.Content.Contains("ArgumentException"), "Response should contain SqlException"); } } }