Exemplo n.º 1
0
        public async void GetReview_WithDataReturnWithNoErrors_RedirectsToSubmit()
        {
            // Arrange
            DataReturnForSubmission dataReturnForSubmission = new DataReturnForSubmission(
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"),
                new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"),
                2015,
                QuarterType.Q4,
                A.Dummy <IReadOnlyCollection <DataReturnWarning> >(),
                A.Dummy <IReadOnlyCollection <DataReturnError> >(),
                false);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <FetchDataReturnForSubmission> ._))
            .WhenArgumentsMatch(args => args.Get <FetchDataReturnForSubmission>("request").DataReturnUploadId == new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"))
            .Returns(dataReturnForSubmission);

            DataReturnsController controller = GetDummyDataReturnsController(weeeClient);

            // Act
            ActionResult result = await controller.Review(
                new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"),
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"));

            // Assert
            Assert.IsAssignableFrom <RedirectToRouteResult>(result);
            RedirectToRouteResult redirectToRouteResult = result as RedirectToRouteResult;

            Assert.Equal("Submit", redirectToRouteResult.RouteValues["action"]);
            Assert.Equal(new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"), redirectToRouteResult.RouteValues["pcsId"]);
            Assert.Equal(new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"), redirectToRouteResult.RouteValues["dataReturnUploadId"]);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This test ensures that the OnActionExecuting filter doesn't interupt the action when the configuration
        /// has data returns enabled, the specified "pcsId" parameter is for a non-approved scheme and the user
        /// is requesting the "AuthorisationRequired" action. This prevents an infinite loop from occuring.
        /// </summary>
        public void OnActionExecuting_ConfigEnabledAndSpecifiedSchemeIsNotApprovedAndActionIsAuthorisationRequired_DoesNothing()
        {
            // Arrange
            IAppConfiguration configuration = A.Fake <IAppConfiguration>();

            A.CallTo(() => configuration.EnableDataReturns).Returns(true);

            ConfigurationService configurationService = A.Fake <ConfigurationService>();

            A.CallTo(() => configurationService.CurrentConfiguration).Returns(configuration);

            IWeeeClient weeeClient = A.Fake <IWeeeClient>();

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <VerifyOrganisationExists> ._))
            .WhenArgumentsMatch(a => a.Get <VerifyOrganisationExists>("request").OrganisationId == new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9"))
            .Returns(true);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <GetSchemeStatus> ._))
            .WhenArgumentsMatch(a => a.Get <GetSchemeStatus>("request").PcsId == new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9"))
            .Returns(SchemeStatus.Pending);

            DataReturnsController controller = new DataReturnsController(
                () => weeeClient,
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                configurationService);

            // Act
            ActionDescriptor actionDescriptor = A.Fake <ActionDescriptor>();

            A.CallTo(() => actionDescriptor.ActionName == "AuthorisationRequired");

            ActionExecutingContext actionExecutingContext = new ActionExecutingContext();

            actionExecutingContext.ActionParameters          = new Dictionary <string, object>();
            actionExecutingContext.ActionParameters["pcsId"] = new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9");
            actionExecutingContext.ActionDescriptor          = actionDescriptor;

            MethodInfo onActionExecutingMethod = typeof(DataReturnsController).GetMethod(
                "OnActionExecuting",
                BindingFlags.NonPublic | BindingFlags.Instance);

            Action testCode = () =>
            {
                object[] args = new object[] { actionExecutingContext };
                try
                {
                    onActionExecutingMethod.Invoke(controller, args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            };

            // Assert
            Assert.Null(actionExecutingContext.Result);
        }
Exemplo n.º 3
0
        public async void GetSubmit_WithDataReturnWithDifferentOrganisationId_ThrowsAnException()
        {
            // Arrange
            DataReturnForSubmission dataReturnForSubmission = new DataReturnForSubmission(
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"),
                new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"),
                2015,
                QuarterType.Q4,
                A.Dummy <IReadOnlyCollection <DataReturnWarning> >(),
                A.Dummy <IReadOnlyCollection <DataReturnError> >(),
                false);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <FetchDataReturnForSubmission> ._))
            .WhenArgumentsMatch(args => args.Get <FetchDataReturnForSubmission>("request").DataReturnUploadId == new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"))
            .Returns(dataReturnForSubmission);

            DataReturnsController controller = GetDummyDataReturnsController(weeeClient);
            // Act
            Func <Task <ActionResult> > testCode = async() =>
                                                   await controller.Submit(
                new Guid("AA7DA88A-19AF-4130-A24D-45389D97B274"),
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"));

            // Assert
            await Assert.ThrowsAnyAsync <Exception>(testCode);
        }
Exemplo n.º 4
0
        public async void GetSubmit_HappyPath_ReturnsSubmitViewWithPopulatedViewModel()
        {
            // Arrange
            DataReturnForSubmission dataReturnForSubmission = new DataReturnForSubmission(
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"),
                new Guid("AA7DA88A-19AF-4130-A24D-45389D97B274"),
                2015,
                QuarterType.Q4,
                A.Dummy <IReadOnlyCollection <DataReturnWarning> >(),
                A.Dummy <IReadOnlyCollection <DataReturnError> >(),
                false);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <FetchDataReturnForSubmission> ._))
            .WhenArgumentsMatch(args => args.Get <FetchDataReturnForSubmission>("request").DataReturnUploadId == new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"))
            .Returns(dataReturnForSubmission);

            DataReturnsController controller = GetDummyDataReturnsController(weeeClient);

            // Act
            ActionResult result = await controller.Submit(
                new Guid("AA7DA88A-19AF-4130-A24D-45389D97B274"),
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"));

            // Assert
            Assert.IsAssignableFrom <ViewResultBase>(result);
            ViewResultBase viewResult = result as ViewResultBase;

            Assert.True(viewResult.ViewName == string.Empty || viewResult.ViewName == "Submit",
                        "The GET Submit action must return the view called \"Submit\".");

            Assert.IsAssignableFrom <SubmitViewModel>(viewResult.Model);
            SubmitViewModel viewModel = viewResult.Model as SubmitViewModel;

            Assert.Equal(dataReturnForSubmission, viewModel.DataReturn);
        }
Exemplo n.º 5
0
        public async Task GetDownloadEeeWeeeData_Always_CallsApiAndReturnsFileContents()
        {
            // Arrange
            Guid organisationId = new Guid("ADED8BDE-CF03-4696-B972-DDAB9306A6DD");

            FileInfo fileInfo = new FileInfo("Test file.csv", A.Dummy <byte[]>());

            IWeeeClient client = A.Fake <IWeeeClient>();

            A.CallTo(() => client.SendAsync(A <string> ._, A <FetchSummaryCsv> ._))
            .WhenArgumentsMatch(a => a.Get <FetchSummaryCsv>("request").OrganisationId == organisationId &&
                                a.Get <FetchSummaryCsv>("request").ComplianceYear == 2017)
            .Returns(fileInfo);

            DataReturnsController controller = new DataReturnsController(
                () => client,
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                A.Dummy <ConfigurationService>());

            // Act
            ActionResult result = await controller.DownloadEeeWeeeData(organisationId, 2017);

            // Assert
            FileResult fileResult = result as FileResult;

            Assert.NotNull(fileResult);

            Assert.Equal("Test file.csv", fileResult.FileDownloadName);
        }
Exemplo n.º 6
0
        private static DataReturnsController GetDummyDataReturnsController(IWeeeClient weeeClient)
        {
            DataReturnsController controller = new DataReturnsController(
                () => weeeClient,
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                A.Dummy <ConfigurationService>());

            new HttpContextMocker().AttachToController(controller);

            return(controller);
        }
Exemplo n.º 7
0
        private DataReturnsController DataReturnsController()
        {
            configService.CurrentConfiguration.EnableDataReturns = true;
            DataReturnsController controller = new DataReturnsController(
                () => weeeClient,
                A.Fake <IWeeeCache>(),
                A.Fake <BreadcrumbService>(),
                A.Fake <CsvWriterFactory>(),
                mapper,
                configService);

            new HttpContextMocker().AttachToController(controller);

            return(controller);
        }
Exemplo n.º 8
0
        private DataReturnsController DataReturnsController(object viewModel)
        {
            DataReturnsController controller = DataReturnsController();

            // Mimic the behaviour of the model binder which is responsible for Validating the Model
            var validationContext = new ValidationContext(viewModel, null, null);
            var validationResults = new List <ValidationResult>();

            Validator.TryValidateObject(viewModel, validationContext, validationResults, true);
            foreach (var validationResult in validationResults)
            {
                controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
            }

            return(controller);
        }
Exemplo n.º 9
0
        public async void PostUpload_WithDataReturnWithErrors_RedirectsToReview()
        {
            // Arrange
            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <ProcessDataReturnXmlFile> ._))
            .Returns(new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"));

            DataReturnError error = new DataReturnError("Test Error");

            DataReturnForSubmission dataReturnForSubmission = new DataReturnForSubmission(
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"),
                new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"),
                2015,
                QuarterType.Q4,
                A.Dummy <IReadOnlyCollection <DataReturnWarning> >(),
                new List <DataReturnError>()
            {
                error
            },
                false);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <FetchDataReturnForSubmission> ._))
            .WhenArgumentsMatch(args => args.Get <FetchDataReturnForSubmission>("request").DataReturnUploadId == new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"))
            .Returns(dataReturnForSubmission);

            DataReturnsController controller = GetDummyDataReturnsController(weeeClient);

            PCSFileUploadViewModel viewModel = new PCSFileUploadViewModel();

            var controllerContext = A.Fake <HttpContextBase>();

            controller.ControllerContext = new ControllerContext(controllerContext, new RouteData(), controller);
            var request = A.Fake <HttpRequestBase>();

            A.CallTo(() => controllerContext.Request).Returns(request);

            // Act
            ActionResult result = await controller.Upload(new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"), viewModel);

            // Assert
            Assert.IsAssignableFrom <RedirectToRouteResult>(result);
            RedirectToRouteResult redirectToRouteResult = result as RedirectToRouteResult;

            Assert.Equal("Review", redirectToRouteResult.RouteValues["action"]);
            Assert.Equal(new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"), redirectToRouteResult.RouteValues["pcsId"]);
            Assert.Equal(new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"), redirectToRouteResult.RouteValues["dataReturnUploadId"]);
        }
Exemplo n.º 10
0
        public void OnActionExecuting_ConfigEnabledAndSpecifiedPcsIdNotAGuid_ThrowsArgumentException()
        {
            // Arrange
            IAppConfiguration configuration = A.Fake <IAppConfiguration>();

            A.CallTo(() => configuration.EnableDataReturns).Returns(true);

            ConfigurationService configurationService = A.Fake <ConfigurationService>();

            A.CallTo(() => configurationService.CurrentConfiguration).Returns(configuration);

            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy <IWeeeClient>(),
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                configurationService);

            // Act
            ActionExecutingContext actionExecutingContext = new ActionExecutingContext();

            actionExecutingContext.ActionParameters          = new Dictionary <string, object>();
            actionExecutingContext.ActionParameters["pcsId"] = 12345;

            MethodInfo onActionExecutingMethod = typeof(DataReturnsController).GetMethod(
                "OnActionExecuting",
                BindingFlags.NonPublic | BindingFlags.Instance);

            Action testCode = () =>
            {
                object[] args = new object[] { actionExecutingContext };
                try
                {
                    onActionExecutingMethod.Invoke(controller, args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            };

            // Assert
            Assert.Throws <ArgumentException>(testCode);
        }
Exemplo n.º 11
0
        public async void PostSubmit_HappyPath_RedirectsToSuccessfulSubmission()
        {
            // Arrange
            DataReturnsController controller = GetDummyDataReturnsController(weeeClient);

            // Act
            ActionResult result = await controller.Submit(
                new Guid("AA7DA88A-19AF-4130-A24D-45389D97B274"),
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"),
                new SubmitViewModel());

            // Assert
            Assert.IsAssignableFrom <RedirectToRouteResult>(result);
            RedirectToRouteResult redirectToRouteResult = result as RedirectToRouteResult;

            Assert.Equal("SuccessfulSubmission", redirectToRouteResult.RouteValues["action"]);
            Assert.Equal(new Guid("AA7DA88A-19AF-4130-A24D-45389D97B274"), redirectToRouteResult.RouteValues["pcsId"]);
        }
Exemplo n.º 12
0
        public async void GetSuccessfulSubmission_HappyPath_ReturnsSuccessfulSubmissionView()
        {
            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <GetUploadInfoByDataReturnUploadId> ._))
            .Returns(new DataReturnUploadInfo
            {
                Year    = 2016,
                Quarter = QuarterType.Q1
            });

            DataReturnsController controller = GetDummyDataReturnsController(weeeClient);

            ActionResult result = await controller.SuccessfulSubmission(Guid.NewGuid(), Guid.NewGuid());

            Assert.IsAssignableFrom <ViewResultBase>(result);
            ViewResultBase viewResult = result as ViewResultBase;

            Assert.True(viewResult.ViewName == string.Empty || viewResult.ViewName == "SuccessfulSubmission");

            Assert.IsAssignableFrom <EA.Weee.Web.Areas.Scheme.ViewModels.DataReturns.SuccessfulSubmissionViewModel>(viewResult.Model);
        }
Exemplo n.º 13
0
        public async Task GetDownloadEeeWeeeData_Always_ReturnsFileWithContentTypeTextCsv()
        {
            // Arrange
            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy <IWeeeClient>(),
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                A.Dummy <ConfigurationService>());

            // Act
            ActionResult result = await controller.DownloadEeeWeeeData(A.Dummy <Guid>(), A.Dummy <int>());

            // Assert
            FileResult fileResult = result as FileResult;

            Assert.NotNull(fileResult);

            Assert.Equal("text/csv", fileResult.ContentType);
        }
Exemplo n.º 14
0
        public async Task GetIndex_Always_ReturnsIndexView()
        {
            // Arrange
            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy <IWeeeClient>(),
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                A.Dummy <ConfigurationService>());

            // Act
            ActionResult result = await controller.Index(A.Dummy <Guid>());

            // Assert
            ViewResult viewResult = result as ViewResult;

            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || string.Equals("Index", viewResult.ViewName, StringComparison.InvariantCultureIgnoreCase));
        }
Exemplo n.º 15
0
        public async void GetReview_HappyPath_ReturnsSubmitViewWithPopulatedViewModel()
        {
            // Arrange
            DataReturnError error = new DataReturnError("Test Error");

            DataReturnForSubmission dataReturnForSubmission = new DataReturnForSubmission(
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"),
                new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"),
                2015,
                QuarterType.Q4,
                A.Dummy <IReadOnlyCollection <DataReturnWarning> >(),
                new List <DataReturnError>()
            {
                error
            },
                false);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <FetchDataReturnForSubmission> ._))
            .WhenArgumentsMatch(args => args.Get <FetchDataReturnForSubmission>("request").DataReturnUploadId == new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"))
            .Returns(dataReturnForSubmission);

            DataReturnsController controller = GetDummyDataReturnsController(weeeClient);

            // Act
            ActionResult result = await controller.Review(
                new Guid("DDE08793-D655-4CDD-A87A-083307C1AA66"),
                new Guid("06FFB265-46D3-4CE3-805A-A81F1B11622A"));

            // Assert
            Assert.IsAssignableFrom <ViewResultBase>(result);
            ViewResultBase viewResult = result as ViewResultBase;

            Assert.True(viewResult.ViewName == "Submit",
                        "The GET Review action must return the view called \"Submit\" when the data return has no errors.");

            Assert.IsAssignableFrom <SubmitViewModel>(viewResult.Model);
            SubmitViewModel viewModel = viewResult.Model as SubmitViewModel;

            Assert.Equal(dataReturnForSubmission, viewModel.DataReturn);
        }
Exemplo n.º 16
0
        public async Task GetIndex_CallsApiToFetchSummary_AndPopulatesViewModel()
        {
            IWeeeClient client = A.Fake <IWeeeClient>();

            List <int> complianceYearsForScheme = new List <int>()
            {
                2016, 2017, 2018
            };

            A.CallTo(() => client.SendAsync(A <string> ._, A <FetchDataReturnComplianceYearsForScheme> ._))
            .WhenArgumentsMatch(a => a.Get <FetchDataReturnComplianceYearsForScheme>("request").PcsId == new Guid("BA7F772F-626D-4CBD-8D50-50A7B852A9AC"))
            .Returns(complianceYearsForScheme);

            // Arrange
            DataReturnsController controller = new DataReturnsController(
                () => client,
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                A.Dummy <ConfigurationService>());

            // Act
            ActionResult result = await controller.Index(new Guid("BA7F772F-626D-4CBD-8D50-50A7B852A9AC"));

            // Assert
            ViewResult viewResult = result as ViewResult;

            Assert.NotNull(viewResult);

            IndexViewModel viewModel = viewResult.Model as IndexViewModel;

            Assert.NotNull(viewModel);

            Assert.Equal(new Guid("BA7F772F-626D-4CBD-8D50-50A7B852A9AC"), viewModel.OrganisationId);
            Assert.Collection(viewModel.ComplianceYears,
                              r1 => Assert.Equal(2016, r1),
                              r2 => Assert.Equal(2017, r2),
                              r3 => Assert.Equal(2018, r3));
        }
Exemplo n.º 17
0
        public void OnActionExecuting_ConfigDisabled_ThrowsException()
        {
            // Arrange
            IAppConfiguration configuration = A.Fake <IAppConfiguration>();

            A.CallTo(() => configuration.EnableDataReturns).Returns(false);

            ConfigurationService configurationService = A.Fake <ConfigurationService>();

            A.CallTo(() => configurationService.CurrentConfiguration).Returns(configuration);

            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy <IWeeeClient>(),
                A.Dummy <IWeeeCache>(),
                A.Dummy <BreadcrumbService>(),
                A.Dummy <CsvWriterFactory>(),
                A.Dummy <IMapper>(),
                configurationService);

            // Act
            MethodInfo onActionExecutingMethod = typeof(DataReturnsController).GetMethod(
                "OnActionExecuting",
                BindingFlags.NonPublic | BindingFlags.Instance);

            Action testCode = () =>
            {
                object[] args = new object[] { A.Dummy <ActionExecutingContext>() };
                try
                {
                    onActionExecutingMethod.Invoke(controller, args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            };

            // Assert
            Assert.Throws <InvalidOperationException>(testCode);
        }
        public void OnActionExecuting_ConfigDisabled_ThrowsException()
        {
            // Arrange
            IAppConfiguration configuration = A.Fake<IAppConfiguration>();
            A.CallTo(() => configuration.EnableDataReturns).Returns(false);
            
            ConfigurationService configurationService = A.Fake<ConfigurationService>();
            A.CallTo(() => configurationService.CurrentConfiguration).Returns(configuration);

            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy<IWeeeClient>(),
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                configurationService);

            // Act           
            MethodInfo onActionExecutingMethod = typeof(DataReturnsController).GetMethod(
                "OnActionExecuting",
                BindingFlags.NonPublic | BindingFlags.Instance);

            Action testCode = () =>
            {
                object[] args = new object[] { A.Dummy<ActionExecutingContext>() };
                try
                {
                    onActionExecutingMethod.Invoke(controller, args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            };

            // Assert
            Assert.Throws<InvalidOperationException>(testCode);
        }
        public async Task GetDownloadEeeWeeeData_Always_ReturnsFileWithContentTypeTextCsv()
        {
            // Arrange
            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy<IWeeeClient>(),
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                A.Dummy<ConfigurationService>());

            // Act
            ActionResult result = await controller.DownloadEeeWeeeData(A.Dummy<Guid>(), A.Dummy<int>());

            // Assert
            FileResult fileResult = result as FileResult;
            Assert.NotNull(fileResult);

            Assert.Equal("text/csv", fileResult.ContentType);
        }
        private static DataReturnsController GetDummyDataReturnsController(IWeeeClient weeeClient)
        {
            DataReturnsController controller = new DataReturnsController(
                () => weeeClient,
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                A.Dummy<ConfigurationService>());

            new HttpContextMocker().AttachToController(controller);

            return controller;
        }
        public void OnActionExecuting_ConfigEnabledAndSpecifiedPcsIdDoesNotExist_ThrowsArgumentException()
        {
            // Arrange
            IAppConfiguration configuration = A.Fake<IAppConfiguration>();
            A.CallTo(() => configuration.EnableDataReturns).Returns(true);

            ConfigurationService configurationService = A.Fake<ConfigurationService>();
            A.CallTo(() => configurationService.CurrentConfiguration).Returns(configuration);

            IWeeeClient weeeClient = A.Fake<IWeeeClient>();
            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<VerifyOrganisationExists>._))
                .WhenArgumentsMatch(a => a.Get<VerifyOrganisationExists>("request").OrganisationId == new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9"))
                .Returns(false);

            DataReturnsController controller = new DataReturnsController(
                () => weeeClient,
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                configurationService);

            // Act
            ActionExecutingContext actionExecutingContext = new ActionExecutingContext();
            actionExecutingContext.ActionParameters = new Dictionary<string, object>();
            actionExecutingContext.ActionParameters["pcsId"] = new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9");

            MethodInfo onActionExecutingMethod = typeof(DataReturnsController).GetMethod(
                "OnActionExecuting",
                BindingFlags.NonPublic | BindingFlags.Instance);

            Action testCode = () =>
            {
                object[] args = new object[] { actionExecutingContext };
                try
                {
                    onActionExecutingMethod.Invoke(controller, args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
        }
            };

            // Assert
            Assert.Throws<ArgumentException>(testCode);
        }
        /// <summary>
        /// This test ensures that the OnActionExecuting filter redirects the user to the "AuthorisationRequired" action
        /// when the configuration has data returns enabled, the specified "pcsId" parameter is for a non-approved scheme and the user
        /// is not already requesting the "AuthorizationRequired" action.
        /// </summary>
        public void OnActionExecuting_ConfigEnabledAndSpecifiedSchemeIsNotApprovedAndActionIsNotAuthorisationRequired_RedirectsToAuthorisationRequired()
        {
            // Arrange
            IAppConfiguration configuration = A.Fake<IAppConfiguration>();
            A.CallTo(() => configuration.EnableDataReturns).Returns(true);

            ConfigurationService configurationService = A.Fake<ConfigurationService>();
            A.CallTo(() => configurationService.CurrentConfiguration).Returns(configuration);

            IWeeeClient weeeClient = A.Fake<IWeeeClient>();
            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<VerifyOrganisationExists>._))
                .WhenArgumentsMatch(a => a.Get<VerifyOrganisationExists>("request").OrganisationId == new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9"))
                .Returns(true);

            A.CallTo(() => weeeClient.SendAsync(A<string>._, A<GetSchemeStatus>._))
                .WhenArgumentsMatch(a => a.Get<GetSchemeStatus>("request").PcsId == new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9"))
                .Returns(SchemeStatus.Pending);

            DataReturnsController controller = new DataReturnsController(
                () => weeeClient,
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                configurationService);

            // Act
            ActionDescriptor actionDescriptor = A.Fake<ActionDescriptor>();
            A.CallTo(() => actionDescriptor.ActionName == "SomeOtherAction");

            ActionExecutingContext actionExecutingContext = new ActionExecutingContext();
            actionExecutingContext.ActionParameters = new Dictionary<string, object>();
            actionExecutingContext.ActionParameters["pcsId"] = new Guid("51254A73-D885-4F9A-BC47-2787CB1416B9");
            actionExecutingContext.ActionDescriptor = actionDescriptor;

            MethodInfo onActionExecutingMethod = typeof(DataReturnsController).GetMethod(
                "OnActionExecuting",
                BindingFlags.NonPublic | BindingFlags.Instance);

            Action testCode = () =>
            {
                object[] args = new object[] { actionExecutingContext };
                try
                {
                    onActionExecutingMethod.Invoke(controller, args);
        }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            };

            // Assert
            RedirectToRouteResult redirectResult = actionExecutingContext.Result as RedirectToRouteResult;
            Assert.NotNull(redirectResult);

            Assert.Equal("AuthorisationRequired", redirectResult.RouteValues["action"]);
        }
        public async Task GetIndex_Always_ReturnsIndexView()
        {
            // Arrange
            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy<IWeeeClient>(),
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                A.Dummy<ConfigurationService>());

            // Act
            ActionResult result = await controller.Index(A.Dummy<Guid>());

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            Assert.True(string.IsNullOrEmpty(viewResult.ViewName) || string.Equals("Index", viewResult.ViewName, StringComparison.InvariantCultureIgnoreCase));
        }
        public async Task GetIndex_CallsApiToFetchSummary_AndPopulatesViewModel()
        {
            IWeeeClient client = A.Fake<IWeeeClient>();

            List<int> complianceYearsForScheme = new List<int>() { 2016, 2017, 2018 };

            A.CallTo(() => client.SendAsync(A<string>._, A<FetchDataReturnComplianceYearsForScheme>._))
                .WhenArgumentsMatch(a => a.Get<FetchDataReturnComplianceYearsForScheme>("request").PcsId == new Guid("BA7F772F-626D-4CBD-8D50-50A7B852A9AC"))
                .Returns(complianceYearsForScheme);

            // Arrange
            DataReturnsController controller = new DataReturnsController(
                () => client,
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                A.Dummy<ConfigurationService>());

            // Act
            ActionResult result = await controller.Index(new Guid("BA7F772F-626D-4CBD-8D50-50A7B852A9AC"));

            // Assert
            ViewResult viewResult = result as ViewResult;
            Assert.NotNull(viewResult);

            IndexViewModel viewModel = viewResult.Model as IndexViewModel;
            Assert.NotNull(viewModel);

            Assert.Equal(new Guid("BA7F772F-626D-4CBD-8D50-50A7B852A9AC"), viewModel.OrganisationId);
            Assert.Collection(viewModel.ComplianceYears,
                r1 => Assert.Equal(2016, r1),
                r2 => Assert.Equal(2017, r2),
                r3 => Assert.Equal(2018, r3));
        }
        public void OnActionExecuting_ConfigEnabledAndPcsIdNotSpecified_ThrowsArgumentException()
        {
            // Arrange
            IAppConfiguration configuration = A.Fake<IAppConfiguration>();
            A.CallTo(() => configuration.EnableDataReturns).Returns(true);

            ConfigurationService configurationService = A.Fake<ConfigurationService>();
            A.CallTo(() => configurationService.CurrentConfiguration).Returns(configuration);

            DataReturnsController controller = new DataReturnsController(
                () => A.Dummy<IWeeeClient>(),
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                configurationService);

            // Act
            ActionExecutingContext actionExecutingContext = new ActionExecutingContext();
            actionExecutingContext.ActionParameters = new Dictionary<string, object>();

            MethodInfo onActionExecutingMethod = typeof(DataReturnsController).GetMethod(
                "OnActionExecuting",
                BindingFlags.NonPublic | BindingFlags.Instance);

            Action testCode = () =>
            {
                object[] args = new object[] { actionExecutingContext };
                try
                {
                    onActionExecutingMethod.Invoke(controller, args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            };

            // Assert
            Assert.Throws<ArgumentException>(testCode);
        }
        private DataReturnsController DataReturnsController()
        {
            configService.CurrentConfiguration.EnableDataReturns = true;
            DataReturnsController controller = new DataReturnsController(
                 () => weeeClient,
                 A.Fake<IWeeeCache>(),
                 A.Fake<BreadcrumbService>(),
                 A.Fake<CsvWriterFactory>(),
                 mapper,
                 configService);

            new HttpContextMocker().AttachToController(controller);

            return controller;
        }
        public async Task GetDownloadEeeWeeeData_Always_CallsApiAndReturnsFileContents()
        {
            // Arrange
            Guid organisationId = new Guid("ADED8BDE-CF03-4696-B972-DDAB9306A6DD");

            FileInfo fileInfo = new FileInfo("Test file.csv", A.Dummy<byte[]>());

            IWeeeClient client = A.Fake<IWeeeClient>();
            A.CallTo(() => client.SendAsync(A<string>._, A<FetchSummaryCsv>._))
                .WhenArgumentsMatch(a => a.Get<FetchSummaryCsv>("request").OrganisationId == organisationId
                    && a.Get<FetchSummaryCsv>("request").ComplianceYear == 2017)
                .Returns(fileInfo);

            DataReturnsController controller = new DataReturnsController(
                () => client,
                A.Dummy<IWeeeCache>(),
                A.Dummy<BreadcrumbService>(),
                A.Dummy<CsvWriterFactory>(),
                A.Dummy<IMapper>(),
                A.Dummy<ConfigurationService>());

            // Act
            ActionResult result = await controller.DownloadEeeWeeeData(organisationId, 2017);

            // Assert
            FileResult fileResult = result as FileResult;
            Assert.NotNull(fileResult);

            Assert.Equal("Test file.csv", fileResult.FileDownloadName);
        }