<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Web;
using CRP.Controllers;
using CRP.Controllers.Filter;
using CRP.Controllers.Helpers;
using CRP.Controllers.ViewModels;
using CRP.Core.Abstractions;
using CRP.Core.Domain;
using CRP.Core.Resources;
using CRP.Tests.Core.Extensions;
using CRP.Tests.Core.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcContrib.Attributes;
using MvcContrib.TestHelper;
using Rhino.Mocks;
using UCDArch.Core.PersistanceSupport;
using UCDArch.Testing;
using UCDArch.Web.Attributes;

namespace CRP.Tests.Controllers
{
    /// <summary>
    /// Transaction Controller Tests 
    /// </summary>
    [TestClass]
    public class TransactionControllerTests : ControllerTestBase<TransactionController>
    {
        private readonly Type _controllerClass = typeof(TransactionController);
        protected IRepositoryWithTypedId<OpenIdUser, string> OpenIdUserRepository { get; set; }
        public INotificationProvider NotificationProvider { get; set; }
        protected List<Transaction> Transactions { get; set; }
        protected IRepository<Transaction> TransactionRepository { get; set; }
        protected List<Item> Items { get; set; }
        protected IRepository<Item> ItemRepository { get; set; }
        protected List<Unit> Units { get; set; }
        protected IRepository<Unit> UnitRepository { get; set; }
        protected List<DisplayProfile> DisplayProfiles { get; set; }
        protected IRepository<DisplayProfile> DisplayProfileRepository { get; set; }
        protected List<OpenIdUser> OpenIdUsers { get; set; }
        protected List<QuestionSet> QuestionSets { get; set; }
        protected IRepository<QuestionSet> QuestionSetRepository { get; set; }
        protected List<Question> Questions { get; set; }
        protected IRepository<Question> QuestionRepository { get; set; }
        protected List<Coupon> Coupons { get; set; }
        protected IRepository<Coupon> CouponRepository { get; set; }

        #region Init
        public TransactionControllerTests()
        {
            Transactions = new List<Transaction>();
            TransactionRepository = FakeRepository<Transaction>();
            Controller.Repository.Expect(a => a.OfType<Transaction>()).Return(TransactionRepository).Repeat.Any();
            
            Items = new List<Item>();
            ItemRepository = FakeRepository<Item>();
            Controller.Repository.Expect(a => a.OfType<Item>()).Return(ItemRepository).Repeat.Any();

            Units = new List<Unit>();
            UnitRepository = FakeRepository<Unit>();
            Controller.Repository.Expect(a => a.OfType<Unit>()).Return(UnitRepository).Repeat.Any();

            DisplayProfiles = new List<DisplayProfile>();
            DisplayProfileRepository = FakeRepository<DisplayProfile>();
            Controller.Repository.Expect(a => a.OfType<DisplayProfile>()).Return(DisplayProfileRepository).Repeat.Any();

            QuestionSets = new List<QuestionSet>();
            QuestionSetRepository = FakeRepository<QuestionSet>();
            Controller.Repository.Expect(a => a.OfType<QuestionSet>()).Return(QuestionSetRepository).Repeat.Any();
        
            Questions = new List<Question>();
            QuestionRepository = FakeRepository<Question>();
            Controller.Repository.Expect(a => a.OfType<Question>()).Return(QuestionRepository).Repeat.Any();

            Coupons = new List<Coupon>();
            CouponRepository = FakeRepository<Coupon>();
            Controller.Repository.Expect(a => a.OfType<Coupon>()).Return(CouponRepository).Repeat.Any();
        
            OpenIdUsers = new List<OpenIdUser>();
            //OpenIdUserRepository = FakeRepository<OpenIdUser>();
            //Controller.Repository.Expect(a => a.OfType<OpenIdUser>()).Return(OpenIdUserRepository).Repeat.Any();
        }
        /// <summary>
        /// Registers the routes.
        /// </summary>
        protected override void RegisterRoutes()
        {
            new RouteConfigurator().RegisterRoutes();
        }

        /// <summary>
        /// Setups the controller.
        /// </summary>
        protected override void SetupController()
        {
            OpenIdUserRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<OpenIdUser, string>>();
            NotificationProvider = MockRepository.GenerateStub<INotificationProvider>();
            Controller = new TestControllerBuilder().CreateController<TransactionController>(OpenIdUserRepository, NotificationProvider);
        }

        #endregion Init

        #region Route Tests


        /// <summary>
        /// Tests the checkout get mapping.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetMapping()
        {
            "~/Transaction/Checkout/5".ShouldMapTo<TransactionController>(a => a.Checkout(5));
        }

        /// <summary>
        /// Tests the checkout post mapping.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostMapping()
        {
            "~/Transaction/Checkout/5".ShouldMapTo<TransactionController>(a => a.Checkout(5,1, null, "test", string.Empty, string.Empty , new QuestionAnswerParameter[1],new QuestionAnswerParameter[1], true ), true);
        }

        /// <summary>
        /// Tests the confirmation get mapping.
        /// </summary>
        [TestMethod]
        public void TestConfirmationGetMapping()
        {
            "~/Transaction/Confirmation/5".ShouldMapTo<TransactionController>(a => a.Confirmation(5));
        }

        /// <summary>
        /// Tests the payment success mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentSuccessMapping()
        {
            "~/Transaction/PaymentSuccess/5".ShouldMapTo<TransactionController>(a => a.PaymentSuccess("12","12"), true);
        }

        /// <summary>
        /// Tests the payment error mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentErrorMapping()
        {
            "~/Transaction/PaymentError/5".ShouldMapTo<TransactionController>(a => a.PaymentError("12", "12"), true);
        }

        /// <summary>
        /// Tests the payment cancel mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentCancelMapping()
        {
            "~/Transaction/PaymentCancel/5".ShouldMapTo<TransactionController>(a => a.PaymentCancel("12", "12"), true);
        }

        /// <summary>
        /// Tests the edit get mapping.
        /// </summary>
        [TestMethod]
        public void TestEditGetMapping()
        {
            "~/Transaction/Edit/5".ShouldMapTo<TransactionController>(a => a.Edit(5));
        }

        /// <summary>
        /// Tests the edit post mapping.
        /// </summary>
        [TestMethod]
        public void TestEditPostMapping()
        {
            "~/Transaction/Edit/".ShouldMapTo<TransactionController>(a => a.Edit(new Transaction()), true);
        }

        /// <summary>
        /// Tests the payment result mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentResultMapping()
        {
            "~/Transaction/PaymentResult/".ShouldMapTo<TransactionController>(a => a.PaymentResult(new PaymentResultParameters()), true);
        }

        /// <summary>
        /// Tests the lookup get mapping.
        /// </summary>
        [TestMethod]
        public void TestLookupGetMapping()
        {
            "~/Transaction/Lookup".ShouldMapTo<TransactionController>(a => a.Lookup());
        }

        /// <summary>
        /// Tests the lookup post mapping.
        /// </summary>
        [TestMethod]
        public void TestLookupPostMapping()
        {
            "~/Transaction/Lookup/test".ShouldMapTo<TransactionController>(a => a.Lookup("order", "email"), true);
        }
        #endregion Route Tests

        #region Checkout Get Tests

        /// <summary>
        /// Tests the checkout get redirects to home controller if item is not found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetRedirectsToHomeControllerIfItemIsNotFound()
        {
            #region Arrange
            SetupDataForTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(1)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            Assert.AreEqual("Item not found.", Controller.Message);
            #endregion Assert		
        }
        
        /// <summary>
        /// Tests the checkout get returns view when item is found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetReturnsViewWhenItemIsFound()
        {
            #region Arrange
            SetupDataForTests();
            #endregion Arrange

            #region Act
            var result = Controller.Checkout(2)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            #endregion Assert		
        }


        /// <summary>
        /// Tests the checkout get will populate contact information if open id found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetWillPopulateContactInformationIfOpenIdFound()
        {
            #region Arrange
            SetupDataForTests();
            SetupDataForPopulateItemTransactionAnswer();
            #endregion Arrange

            #region Act
            var result = Controller.Checkout(2)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(9, result.Answers.Count());
            Assert.AreEqual("FirstName", result.Answers.ElementAt(0).Answer);
            Assert.AreEqual("LastName", result.Answers.ElementAt(1).Answer);
            Assert.AreEqual("Address1", result.Answers.ElementAt(2).Answer);
            Assert.AreEqual("Address2", result.Answers.ElementAt(3).Answer);
            Assert.AreEqual("City", result.Answers.ElementAt(4).Answer);
            Assert.AreEqual("CA", result.Answers.ElementAt(5).Answer);
            Assert.AreEqual("95616", result.Answers.ElementAt(6).Answer);
            Assert.AreEqual("555 555 5555", result.Answers.ElementAt(7).Answer);
            Assert.AreEqual("<EMAIL>", result.Answers.ElementAt(8).Answer);
            #endregion Assert		
        }

        /// <summary>
        /// Tests the checkout get will throw exception when populate contact information if A different question name is found.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void TestCheckoutGetWillThrowExceptionWhenPopulateContactInformationIfADifferentQuestionNameIsFound()
        {
            #region Arrange
            SetupDataForTests();
            SetupDataForPopulateItemTransactionAnswer();
            Questions[1].Name = "NotValid"; //This is not a contact Information Question. It should not be in the data. 
            #endregion Arrange

            #region Act
            Controller.Checkout(2);
            #endregion Act
        }
        
        /// <summary>
        /// Tests the checkout get will default quantity to one.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetWillDefaultQuantityToOne()
        {
            #region Arrange
            SetupDataForTests();
            #endregion Arrange

            #region Act
            var result = Controller.Checkout(2)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Quantity);
            #endregion Assert		
        }
        

        #endregion Checkout Get Tests

        #region Checkout Post Tests

        /// <summary>
        /// Tests the checkout post redirects to home controller if item not found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemNotFound()
        {
            #region Arrange
            SetupDataForTests();
            SetupDataForPopulateItemTransactionAnswer();
            #endregion Arrange

            #region Act
            Controller.Checkout(1, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item not found.", Controller.Message);
            #endregion Assert		
        }


        /// <summary>
        /// Tests the checkout post redirects to home controller if item is not available1.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemIsNotAvailable1()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            Items[1].Quantity = 10;
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item is not available.", Controller.Message);
            #endregion Assert		
        }

        /// <summary>
        /// Tests the checkout post redirects to home controller if item is not available2.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemIsNotAvailable2()
        {
            #region Arrange
            var fakeDate = SetupDataForCheckoutTests();
            Items[1].Expiration = fakeDate.AddDays(-1);
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item is not available.", Controller.Message);
            #endregion Assert
        }

        /// <summary>
        /// Tests the checkout post redirects to home controller if item is not available3.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemIsNotAvailable3()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            Items[1].Available = false;
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item is not available.", Controller.Message);
            #endregion Assert
        }


        /// <summary>
        /// Tests the checkout with minimal valid data saves.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithValidDataSaves()
        {
            #region Arrange
            SetupDataForCheckoutTests();            
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<TransactionController>(a => a.Confirmation(1));
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            #endregion Assert		
        }

        /// <summary>
        /// Tests the checkout with false recaptch does not save.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithFalseRecaptchDoesNotSave()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, false)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.IsNull(Controller.Message);
            Controller.ModelState.AssertErrorsAre("Captcha values are not valid.");
            #endregion Assert
        }

        /// <summary>
        /// Tests the checkout with donation creates child transaction.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithDonationCreatesChildTransaction()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, 25.01m, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<TransactionController>(a => a.Confirmation(1));
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            var args = (Transaction)TransactionRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything))[0][0];
            Assert.IsNotNull(args);
            Assert.AreEqual(20.00m, args.Amount);
            Assert.AreEqual(1, args.ChildTransactions.Count);
            Assert.AreEqual(25.01m, args.ChildTransactions.ElementAt(0).Amount);
            Assert.IsTrue(args.ChildTransactions.ElementAt(0).Donation);
            #endregion Assert
        }

        /// <summary>
        /// Tests the checkout with valid data and credit card saves.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithValidDataAndCreditCardSaves()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, StaticValues.CreditCard, string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<TransactionController>(a => a.Confirmation(1));
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            #endregion Assert
        }

        //test credit card
        //test check
        //test not a credit card or check
        //test coupon variations
        //test amount is calculated properly
        //test transaction answers
        //test quantity answers
        //test donation creates another transaction
        //test restricted key
        //test inventory exists

        
        #endregion Checkout Post Tests

        #region Total Tests


        /// <summary>
        /// Tests the total to string format.
        /// Used for the MD5 hash for touchNet.
        /// </summary><commit_msg>WIP -- TransactionController tests. (Updated by TFS to TFS Connector, server: http://1x:8080/, id 6760. )<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Web;
using CRP.Controllers;
using CRP.Controllers.Filter;
using CRP.Controllers.Helpers;
using CRP.Controllers.ViewModels;
using CRP.Core.Abstractions;
using CRP.Core.Domain;
using CRP.Core.Resources;
using CRP.Tests.Core.Extensions;
using CRP.Tests.Core.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcContrib.Attributes;
using MvcContrib.TestHelper;
using Rhino.Mocks;
using UCDArch.Core.PersistanceSupport;
using UCDArch.Testing;
using UCDArch.Web.Attributes;

namespace CRP.Tests.Controllers
{
    /// <summary>
    /// Transaction Controller Tests 
    /// </summary>
    [TestClass]
    public class TransactionControllerTests : ControllerTestBase<TransactionController>
    {
        private readonly Type _controllerClass = typeof(TransactionController);
        protected IRepositoryWithTypedId<OpenIdUser, string> OpenIdUserRepository { get; set; }
        public INotificationProvider NotificationProvider { get; set; }
        protected List<Transaction> Transactions { get; set; }
        protected IRepository<Transaction> TransactionRepository { get; set; }
        protected List<Item> Items { get; set; }
        protected IRepository<Item> ItemRepository { get; set; }
        protected List<Unit> Units { get; set; }
        protected IRepository<Unit> UnitRepository { get; set; }
        protected List<DisplayProfile> DisplayProfiles { get; set; }
        protected IRepository<DisplayProfile> DisplayProfileRepository { get; set; }
        protected List<OpenIdUser> OpenIdUsers { get; set; }
        protected List<QuestionSet> QuestionSets { get; set; }
        protected IRepository<QuestionSet> QuestionSetRepository { get; set; }
        protected List<Question> Questions { get; set; }
        protected IRepository<Question> QuestionRepository { get; set; }
        protected List<Coupon> Coupons { get; set; }
        protected IRepository<Coupon> CouponRepository { get; set; }

        #region Init
        public TransactionControllerTests()
        {
            Transactions = new List<Transaction>();
            TransactionRepository = FakeRepository<Transaction>();
            Controller.Repository.Expect(a => a.OfType<Transaction>()).Return(TransactionRepository).Repeat.Any();
            
            Items = new List<Item>();
            ItemRepository = FakeRepository<Item>();
            Controller.Repository.Expect(a => a.OfType<Item>()).Return(ItemRepository).Repeat.Any();

            Units = new List<Unit>();
            UnitRepository = FakeRepository<Unit>();
            Controller.Repository.Expect(a => a.OfType<Unit>()).Return(UnitRepository).Repeat.Any();

            DisplayProfiles = new List<DisplayProfile>();
            DisplayProfileRepository = FakeRepository<DisplayProfile>();
            Controller.Repository.Expect(a => a.OfType<DisplayProfile>()).Return(DisplayProfileRepository).Repeat.Any();

            QuestionSets = new List<QuestionSet>();
            QuestionSetRepository = FakeRepository<QuestionSet>();
            Controller.Repository.Expect(a => a.OfType<QuestionSet>()).Return(QuestionSetRepository).Repeat.Any();
        
            Questions = new List<Question>();
            QuestionRepository = FakeRepository<Question>();
            Controller.Repository.Expect(a => a.OfType<Question>()).Return(QuestionRepository).Repeat.Any();

            Coupons = new List<Coupon>();
            CouponRepository = FakeRepository<Coupon>();
            Controller.Repository.Expect(a => a.OfType<Coupon>()).Return(CouponRepository).Repeat.Any();
        
            OpenIdUsers = new List<OpenIdUser>();
            //OpenIdUserRepository = FakeRepository<OpenIdUser>();
            //Controller.Repository.Expect(a => a.OfType<OpenIdUser>()).Return(OpenIdUserRepository).Repeat.Any();
        }
        /// <summary>
        /// Registers the routes.
        /// </summary>
        protected override void RegisterRoutes()
        {
            new RouteConfigurator().RegisterRoutes();
        }

        /// <summary>
        /// Setups the controller.
        /// </summary>
        protected override void SetupController()
        {
            OpenIdUserRepository = MockRepository.GenerateStub<IRepositoryWithTypedId<OpenIdUser, string>>();
            NotificationProvider = MockRepository.GenerateStub<INotificationProvider>();
            Controller = new TestControllerBuilder().CreateController<TransactionController>(OpenIdUserRepository, NotificationProvider);
        }

        #endregion Init

        #region Route Tests


        /// <summary>
        /// Tests the checkout get mapping.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetMapping()
        {
            "~/Transaction/Checkout/5".ShouldMapTo<TransactionController>(a => a.Checkout(5));
        }

        /// <summary>
        /// Tests the checkout post mapping.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostMapping()
        {
            "~/Transaction/Checkout/5".ShouldMapTo<TransactionController>(a => a.Checkout(5,1, null, "test", string.Empty, string.Empty , new QuestionAnswerParameter[1],new QuestionAnswerParameter[1], true ), true);
        }

        /// <summary>
        /// Tests the confirmation get mapping.
        /// </summary>
        [TestMethod]
        public void TestConfirmationGetMapping()
        {
            "~/Transaction/Confirmation/5".ShouldMapTo<TransactionController>(a => a.Confirmation(5));
        }

        /// <summary>
        /// Tests the payment success mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentSuccessMapping()
        {
            "~/Transaction/PaymentSuccess/5".ShouldMapTo<TransactionController>(a => a.PaymentSuccess("12","12"), true);
        }

        /// <summary>
        /// Tests the payment error mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentErrorMapping()
        {
            "~/Transaction/PaymentError/5".ShouldMapTo<TransactionController>(a => a.PaymentError("12", "12"), true);
        }

        /// <summary>
        /// Tests the payment cancel mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentCancelMapping()
        {
            "~/Transaction/PaymentCancel/5".ShouldMapTo<TransactionController>(a => a.PaymentCancel("12", "12"), true);
        }

        /// <summary>
        /// Tests the edit get mapping.
        /// </summary>
        [TestMethod]
        public void TestEditGetMapping()
        {
            "~/Transaction/Edit/5".ShouldMapTo<TransactionController>(a => a.Edit(5));
        }

        /// <summary>
        /// Tests the edit post mapping.
        /// </summary>
        [TestMethod]
        public void TestEditPostMapping()
        {
            "~/Transaction/Edit/".ShouldMapTo<TransactionController>(a => a.Edit(new Transaction()), true);
        }

        /// <summary>
        /// Tests the payment result mapping.
        /// </summary>
        [TestMethod]
        public void TestPaymentResultMapping()
        {
            "~/Transaction/PaymentResult/".ShouldMapTo<TransactionController>(a => a.PaymentResult(new PaymentResultParameters()), true);
        }

        /// <summary>
        /// Tests the lookup get mapping.
        /// </summary>
        [TestMethod]
        public void TestLookupGetMapping()
        {
            "~/Transaction/Lookup".ShouldMapTo<TransactionController>(a => a.Lookup());
        }

        /// <summary>
        /// Tests the lookup post mapping.
        /// </summary>
        [TestMethod]
        public void TestLookupPostMapping()
        {
            "~/Transaction/Lookup/test".ShouldMapTo<TransactionController>(a => a.Lookup("order", "email"), true);
        }
        #endregion Route Tests

        #region Checkout Get Tests

        /// <summary>
        /// Tests the checkout get redirects to home controller if item is not found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetRedirectsToHomeControllerIfItemIsNotFound()
        {
            #region Arrange
            SetupDataForTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(1)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            Assert.AreEqual("Item not found.", Controller.Message);
            #endregion Assert		
        }
        
        /// <summary>
        /// Tests the checkout get returns view when item is found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetReturnsViewWhenItemIsFound()
        {
            #region Arrange
            SetupDataForTests();
            #endregion Arrange

            #region Act
            var result = Controller.Checkout(2)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            #endregion Assert		
        }


        /// <summary>
        /// Tests the checkout get will populate contact information if open id found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetWillPopulateContactInformationIfOpenIdFound()
        {
            #region Arrange
            SetupDataForTests();
            SetupDataForPopulateItemTransactionAnswer();
            #endregion Arrange

            #region Act
            var result = Controller.Checkout(2)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(9, result.Answers.Count());
            Assert.AreEqual("FirstName", result.Answers.ElementAt(0).Answer);
            Assert.AreEqual("LastName", result.Answers.ElementAt(1).Answer);
            Assert.AreEqual("Address1", result.Answers.ElementAt(2).Answer);
            Assert.AreEqual("Address2", result.Answers.ElementAt(3).Answer);
            Assert.AreEqual("City", result.Answers.ElementAt(4).Answer);
            Assert.AreEqual("CA", result.Answers.ElementAt(5).Answer);
            Assert.AreEqual("95616", result.Answers.ElementAt(6).Answer);
            Assert.AreEqual("555 555 5555", result.Answers.ElementAt(7).Answer);
            Assert.AreEqual("<EMAIL>", result.Answers.ElementAt(8).Answer);
            #endregion Assert		
        }

        /// <summary>
        /// Tests the checkout get will throw exception when populate contact information if A different question name is found.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void TestCheckoutGetWillThrowExceptionWhenPopulateContactInformationIfADifferentQuestionNameIsFound()
        {
            #region Arrange
            SetupDataForTests();
            SetupDataForPopulateItemTransactionAnswer();
            Questions[1].Name = "NotValid"; //This is not a contact Information Question. It should not be in the data. 
            #endregion Arrange

            #region Act
            Controller.Checkout(2);
            #endregion Act
        }
        
        /// <summary>
        /// Tests the checkout get will default quantity to one.
        /// </summary>
        [TestMethod]
        public void TestCheckoutGetWillDefaultQuantityToOne()
        {
            #region Arrange
            SetupDataForTests();
            #endregion Arrange

            #region Act
            var result = Controller.Checkout(2)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Quantity);
            #endregion Assert		
        }
        

        #endregion Checkout Get Tests

        #region Checkout Post Tests

        /// <summary>
        /// Tests the checkout post redirects to home controller if item not found.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemNotFound()
        {
            #region Arrange
            SetupDataForTests();
            SetupDataForPopulateItemTransactionAnswer();
            #endregion Arrange

            #region Act
            Controller.Checkout(1, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item not found.", Controller.Message);
            #endregion Assert		
        }


        /// <summary>
        /// Tests the checkout post redirects to home controller if item is not available1.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemIsNotAvailable1()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            Items[1].Quantity = 10;
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item is not available.", Controller.Message);
            #endregion Assert		
        }

        /// <summary>
        /// Tests the checkout post redirects to home controller if item is not available2.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemIsNotAvailable2()
        {
            #region Arrange
            var fakeDate = SetupDataForCheckoutTests();
            Items[1].Expiration = fakeDate.AddDays(-1);
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item is not available.", Controller.Message);
            #endregion Assert
        }

        /// <summary>
        /// Tests the checkout post redirects to home controller if item is not available3.
        /// </summary>
        [TestMethod]
        public void TestCheckoutPostRedirectsToHomeControllerIfItemIsNotAvailable3()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            Items[1].Available = false;
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<HomeController>(a => a.Index());
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.AreEqual("Item is not available.", Controller.Message);
            #endregion Assert
        }


        /// <summary>
        /// Tests the checkout with minimal valid data saves.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithValidDataSaves()
        {
            #region Arrange
            SetupDataForCheckoutTests();            
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<TransactionController>(a => a.Confirmation(1));
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            #endregion Assert		
        }

        /// <summary>
        /// Tests the checkout with false recaptch does not save.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithFalseRecaptchDoesNotSave()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, "Check", string.Empty, string.Empty, null, null, false)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.IsNull(Controller.Message);
            Controller.ModelState.AssertErrorsAre("Captcha values are not valid.");
            #endregion Assert
        }

        /// <summary>
        /// Tests the checkout with donation creates child transaction.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithDonationCreatesChildTransaction()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, 25.01m, "Check", string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<TransactionController>(a => a.Confirmation(1));
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            var args = (Transaction)TransactionRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything))[0][0];
            Assert.IsNotNull(args);
            Assert.AreEqual(20.00m, args.Amount);
            Assert.AreEqual(1, args.ChildTransactions.Count);
            Assert.AreEqual(25.01m, args.ChildTransactions.ElementAt(0).Amount);
            Assert.IsTrue(args.ChildTransactions.ElementAt(0).Donation);
            #endregion Assert
        }

        /// <summary>
        /// Tests the checkout with valid data and credit card saves.
        /// </summary>
        [TestMethod]
        public void TestCheckoutWithValidDataAndCreditCardSaves()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, StaticValues.CreditCard, string.Empty, string.Empty, null, null, true)
                .AssertActionRedirect()
                .ToAction<TransactionController>(a => a.Confirmation(1));
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            #endregion Assert
        }

        [TestMethod]
        public void TestCheckoutWithCheckOrCreditNotSpecifiedDoesNotSave()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 1, null, string.Empty, string.Empty, string.Empty, null, null, true)
                .AssertViewRendered()
                .WithViewData<ItemDetailViewModel>();
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            Assert.IsNull(Controller.Message);
            Controller.ModelState.AssertErrorsAre("PaymentType: Payment type was not selected.");
            #endregion Assert
        }

        #region Coupon Tests
        [TestMethod]
        public void TestCheckoutWithValidDataAndCouponSaves()
        {
            #region Arrange
            SetupDataForCheckoutTests();
            Items[1].CostPerItem = 20m;
            Coupons[1].Item = Items[1];
            Coupons[1].Code = "COUPON";
            Coupons[1].IsActive = true;
            Coupons[1].DiscountAmount = 5.01m;
            Coupons[1].Unlimited = true;
            Coupons[1].MaxQuantity = 2;
            Coupons[1].Email = string.Empty;
            #endregion Arrange

            #region Act
            Controller.Checkout(2, 3, null, StaticValues.CreditCard, string.Empty, "COUPON", null, null, true)
                .AssertActionRedirect()
                .ToAction<TransactionController>(a => a.Confirmation(1));
            #endregion Act

            #region Assert
            TransactionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything));
            var args = (Transaction)TransactionRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Transaction>.Is.Anything))[0][0];
            Assert.IsNotNull(args);
            Assert.AreEqual(49.98m, args.Amount);
            #endregion Assert
        }
        
        #endregion Coupon Tests


        //test coupon variations
        //test amount is calculated properly
        //test transaction answers
        //test quantity answers
        //test donation creates another transaction
        //test restricted key
        //test inventory exists

        
        #endregion Checkout Post Tests

        #region Total Tests


        /// <summary>
        /// Tests the total to string format.
        /// Used for the MD5 hash for touchNet.
        /// </summary>
{'fraction_non_alphanumeric': 0.10534961788443682, 'fraction_numerical': 0.0060233792824322075, 'mean_word_length': 2.0391433326097967, 'pattern_counts': {'":': 0, '<': 276, '<?xml version=': 0, '>': 352, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6882986', 'n_tokens_mistral': 10549, 'n_tokens_neox': 10223, 'n_words': 2289}