<commit_before>﻿using System;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using NLog;
using NUnit.Framework;
using SFA.DAS.EAS.Domain;
using SFA.DAS.EAS.Domain.Interfaces;
using SFA.DAS.EAS.Domain.Models.Account;
using SFA.DAS.EAS.Domain.Models.EmployerAgreement;
using SFA.DAS.EAS.Domain.Models.Organisation;
using SFA.DAS.EAS.Web.Authentication;
using SFA.DAS.EAS.Web.Controllers;
using SFA.DAS.EAS.Web.Models;
using SFA.DAS.EAS.Web.Orchestrators;
using SFA.DAS.EAS.Web.ViewModels;

namespace SFA.DAS.EAS.Web.UnitTests.Controllers.EmployerAccountControllerTests
{
    class WhenICreateAnAccount : ControllerTestBase
    {
        private EmployerAccountController _employerAccountController;
        private Mock<EmployerAccountOrchestrator> _orchestrator;
        private Mock<IOwinWrapper> _owinWrapper;
        private Mock<IFeatureToggle> _featureToggle;
        private Mock<IMultiVariantTestingService> _userViewTestingService;
        private const string ExpectedRedirectUrl = "http://redirect.local.test";
        private EmployerAccountData _accountData;
        private OrchestratorResponse<EmployerAgreementViewModel> _response;
        private Mock<ICookieStorageService<FlashMessageViewModel>> _flashMessage;
        private const string HashedAccountId = "ABC123";

        [SetUp]
        public void Arrange()
        {
            base.Arrange(ExpectedRedirectUrl);

            _orchestrator = new Mock<EmployerAccountOrchestrator>();

            _owinWrapper = new Mock<IOwinWrapper>();
            _featureToggle = new Mock<IFeatureToggle>();
            _userViewTestingService = new Mock<IMultiVariantTestingService>();
            var logger = new Mock<ILogger>();
            _flashMessage = new Mock<ICookieStorageService<FlashMessageViewModel>>();

            _employerAccountController = new EmployerAccountController(
               _owinWrapper.Object, _orchestrator.Object, _featureToggle.Object, _userViewTestingService.Object, logger.Object, _flashMessage.Object)
            {
                ControllerContext = _controllerContext.Object,
                Url = new UrlHelper(new RequestContext(_httpContext.Object, new RouteData()), _routes)
            };

            _accountData = new EmployerAccountData
            {
                OrganisationName = "<NAME>",
                EmployerRefName = "Scheme 1",
                OrganisationReferenceNumber = "1244454",
                OrganisationRegisteredAddress = "1, Test Street",
                OrganisationDateOfInception = DateTime.Now.AddYears(-10),
                OrganisationStatus = "active",
                PayeReference = "123/ABC",
                RefreshToken = "<PASSWORD>",
                AccessToken = "<PASSWORD>",
                EmpRefNotFound = true,
                OrganisationType = OrganisationType.Charities,
                Sector = "Public"
            };

            _orchestrator.Setup(x => x.GetCookieData(It.IsAny<HttpContextBase>()))
                       .Returns(_accountData);

            _response = new OrchestratorResponse<EmployerAgreementViewModel>()
            {
                Data = new EmployerAgreementViewModel
                {
                    EmployerAgreement = new EmployerAgreementView
                    {
                        HashedAccountId = HashedAccountId
                    }
                },
                Status = HttpStatusCode.OK
            };

            _orchestrator.Setup(x => x.CreateAccount(It.IsAny<CreateAccountViewModel>(), It.IsAny<HttpContextBase>()))
                .ReturnsAsync(_response);
        }

        [Test]
        public async Task ThenIShouldGoBackToTheEmployerTeamPage()
        {
            //Act
            var result = await _employerAccountController.CreateAccount() as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["Action"]);
            Assert.AreEqual("EmployerTeam", result.RouteValues["Controller"]);
        }

        [Test]
        public async Task ThenIShouldGetBackTheAccountId()
        {
            //Act
            var result = await _employerAccountController.CreateAccount() as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HashedAccountId, result.RouteValues["HashedAccountId"]);
        }
        
        [Test]
        public async Task ThenTheParamtersArePassedFromTheCookieWhenCreatingTheAccount()
        {
            //Act
            await _employerAccountController.CreateAccount();

            //Assert
            _orchestrator.Verify(x => x.CreateAccount(It.Is<CreateAccountViewModel>(
                c =>
                    c.OrganisationStatus.Equals(_accountData.OrganisationStatus) &&
                    c.OrganisationName.Equals(_accountData.OrganisationName) &&
                    c.RefreshToken.Equals(_accountData.RefreshToken) &&
                    c.OrganisationDateOfInception.Equals(_accountData.OrganisationDateOfInception) &&
                    c.OrganisationAddress.Equals(_accountData.OrganisationRegisteredAddress) &&
                    c.AccessToken.Equals(_accountData.AccessToken) &&
                    c.PayeReference.Equals(_accountData.PayeReference) &&
                    c.EmployerRefName.Equals(_accountData.EmployerRefName) &&
                    c.Sector.Equals(_accountData.Sector) &&
                    c.OrganisationReferenceNumber.Equals(_accountData.OrganisationReferenceNumber)
                ), It.IsAny<HttpContextBase>()));
        }

        [Test]
        public async Task ThenIfTheAccountIsSucessfullyCreatedThenTheFlashMessageIsAddedToCookieStorage()
        {
            //Act
            await _employerAccountController.CreateAccount();

            //Assert
            _flashMessage.Verify(x=>x.Create(It.Is<FlashMessageViewModel>(c=>c.Headline.Equals("Account created") && c.Severity.Equals(FlashMessageSeverityLevel.Complete)),"sfa-das-employerapprenticeshipsservice-flashmessage",1),Times.Once);
        }



        [Test]
        public async Task ThenIfTheAccountIsSucessfullyCreatedThenTheOrganisationTypeIsAddedToFlashMessageCookie()
        {
            //Act
            await _employerAccountController.CreateAccount();

            //Assert
            _flashMessage.Verify(x => x.Create(It.Is<FlashMessageViewModel>(c => c.HiddenFlashMessageInformation.Equals("Charities")), "sfa-das-employerapprenticeshipsservice-flashmessage", 1), Times.Once);

        }
    }
}<commit_msg>Fix to test for new success message class<commit_after>﻿using System;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using NLog;
using NUnit.Framework;
using SFA.DAS.EAS.Domain;
using SFA.DAS.EAS.Domain.Interfaces;
using SFA.DAS.EAS.Domain.Models.Account;
using SFA.DAS.EAS.Domain.Models.EmployerAgreement;
using SFA.DAS.EAS.Domain.Models.Organisation;
using SFA.DAS.EAS.Web.Authentication;
using SFA.DAS.EAS.Web.Controllers;
using SFA.DAS.EAS.Web.Models;
using SFA.DAS.EAS.Web.Orchestrators;
using SFA.DAS.EAS.Web.ViewModels;

namespace SFA.DAS.EAS.Web.UnitTests.Controllers.EmployerAccountControllerTests
{
    class WhenICreateAnAccount : ControllerTestBase
    {
        private EmployerAccountController _employerAccountController;
        private Mock<EmployerAccountOrchestrator> _orchestrator;
        private Mock<IOwinWrapper> _owinWrapper;
        private Mock<IFeatureToggle> _featureToggle;
        private Mock<IMultiVariantTestingService> _userViewTestingService;
        private const string ExpectedRedirectUrl = "http://redirect.local.test";
        private EmployerAccountData _accountData;
        private OrchestratorResponse<EmployerAgreementViewModel> _response;
        private Mock<ICookieStorageService<FlashMessageViewModel>> _flashMessage;
        private const string HashedAccountId = "ABC123";

        [SetUp]
        public void Arrange()
        {
            base.Arrange(ExpectedRedirectUrl);

            _orchestrator = new Mock<EmployerAccountOrchestrator>();

            _owinWrapper = new Mock<IOwinWrapper>();
            _featureToggle = new Mock<IFeatureToggle>();
            _userViewTestingService = new Mock<IMultiVariantTestingService>();
            var logger = new Mock<ILogger>();
            _flashMessage = new Mock<ICookieStorageService<FlashMessageViewModel>>();

            _employerAccountController = new EmployerAccountController(
               _owinWrapper.Object, _orchestrator.Object, _featureToggle.Object, _userViewTestingService.Object, logger.Object, _flashMessage.Object)
            {
                ControllerContext = _controllerContext.Object,
                Url = new UrlHelper(new RequestContext(_httpContext.Object, new RouteData()), _routes)
            };

            _accountData = new EmployerAccountData
            {
                OrganisationName = "<NAME>",
                EmployerRefName = "Scheme 1",
                OrganisationReferenceNumber = "1244454",
                OrganisationRegisteredAddress = "1, Test Street",
                OrganisationDateOfInception = DateTime.Now.AddYears(-10),
                OrganisationStatus = "active",
                PayeReference = "123/ABC",
                RefreshToken = "<PASSWORD>",
                AccessToken = "<PASSWORD>",
                EmpRefNotFound = true,
                OrganisationType = OrganisationType.Charities,
                Sector = "Public"
            };

            _orchestrator.Setup(x => x.GetCookieData(It.IsAny<HttpContextBase>()))
                       .Returns(_accountData);

            _response = new OrchestratorResponse<EmployerAgreementViewModel>()
            {
                Data = new EmployerAgreementViewModel
                {
                    EmployerAgreement = new EmployerAgreementView
                    {
                        HashedAccountId = HashedAccountId
                    }
                },
                Status = HttpStatusCode.OK
            };

            _orchestrator.Setup(x => x.CreateAccount(It.IsAny<CreateAccountViewModel>(), It.IsAny<HttpContextBase>()))
                .ReturnsAsync(_response);
        }

        [Test]
        public async Task ThenIShouldGoBackToTheEmployerTeamPage()
        {
            //Act
            var result = await _employerAccountController.CreateAccount() as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["Action"]);
            Assert.AreEqual("EmployerTeam", result.RouteValues["Controller"]);
        }

        [Test]
        public async Task ThenIShouldGetBackTheAccountId()
        {
            //Act
            var result = await _employerAccountController.CreateAccount() as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(HashedAccountId, result.RouteValues["HashedAccountId"]);
        }
        
        [Test]
        public async Task ThenTheParamtersArePassedFromTheCookieWhenCreatingTheAccount()
        {
            //Act
            await _employerAccountController.CreateAccount();

            //Assert
            _orchestrator.Verify(x => x.CreateAccount(It.Is<CreateAccountViewModel>(
                c =>
                    c.OrganisationStatus.Equals(_accountData.OrganisationStatus) &&
                    c.OrganisationName.Equals(_accountData.OrganisationName) &&
                    c.RefreshToken.Equals(_accountData.RefreshToken) &&
                    c.OrganisationDateOfInception.Equals(_accountData.OrganisationDateOfInception) &&
                    c.OrganisationAddress.Equals(_accountData.OrganisationRegisteredAddress) &&
                    c.AccessToken.Equals(_accountData.AccessToken) &&
                    c.PayeReference.Equals(_accountData.PayeReference) &&
                    c.EmployerRefName.Equals(_accountData.EmployerRefName) &&
                    c.Sector.Equals(_accountData.Sector) &&
                    c.OrganisationReferenceNumber.Equals(_accountData.OrganisationReferenceNumber)
                ), It.IsAny<HttpContextBase>()));
        }

        [Test]
        public async Task ThenIfTheAccountIsSucessfullyCreatedThenTheFlashMessageIsAddedToCookieStorage()
        {
            //Act
            await _employerAccountController.CreateAccount();

            //Assert
            _flashMessage.Verify(x=>x.Create(It.Is<FlashMessageViewModel>(c=>c.Headline.Equals("Account created") && c.Severity.Equals(FlashMessageSeverityLevel.Success)),"sfa-das-employerapprenticeshipsservice-flashmessage",1),Times.Once);
        }



        [Test]
        public async Task ThenIfTheAccountIsSucessfullyCreatedThenTheOrganisationTypeIsAddedToFlashMessageCookie()
        {
            //Act
            await _employerAccountController.CreateAccount();

            //Assert
            _flashMessage.Verify(x => x.Create(It.Is<FlashMessageViewModel>(c => c.HiddenFlashMessageInformation.Equals("Charities")), "sfa-das-employerapprenticeshipsservice-flashmessage", 1), Times.Once);

        }
    }
}
{'fraction_non_alphanumeric': 0.08844100172971348, 'fraction_numerical': 0.0028577874708580883, 'mean_word_length': 2.3785569105691056, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 69, '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': '5223311', 'n_tokens_mistral': 3602, 'n_tokens_neox': 3398, 'n_words': 513}