public void ActionCreateShouldReturnCorrectViewModels()
        {
            var jobPostMockService = new Mock <IJobPostsService>();

            jobPostMockService.Setup(
                x => x.GetJobPost(1))
            .Returns(new JobPost()
            {
                Id = 1, Title = "TestTitle"
            });

            var controller = new ReportController(jobPostMockService.Object, null);
            var result     = controller.Create(1, "test");

            Assert.IsType <ViewResult>(result);
            var viewResult = result as ViewResult;

            Assert.IsType <ReportViewModel>(viewResult.Model);
            var viewModel = viewResult.Model as ReportViewModel;

            Assert.Equal(1, viewModel.PostId);
            Assert.Equal("TestTitle", viewModel.PostTitle);

            jobPostMockService.Verify(x => x.GetJobPost(1), Times.Once);
        }
        public async void ReportController_Create_ValidViewModel_CallsNewReport()
        {
            // Given
            // Create View Model
            var rvm = new ReportViewModel()
            {
                Latitude       = 45.00,
                Longitude      = 45.00,
                AdditionalInfo = "Testing"
            };

            // Adjust Mocked Components
            var _svc = new Mock <IReportService>();

            _svc.Setup(m => m.NewReport(45.00, 45.00, "Testing"))
            .Returns(Task.FromResult(new Report()));
            var _Controller = new ReportController(_db, _svc.Object);

            _Controller.TempData =
                new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());

            // When
            var Page = await _Controller.Create(rvm);

            // Then
            _svc.Verify(r => r.NewReport(45.00, 45.00, "Testing"), Times.Once());
        }
        public void ReportController_Create_ReturnsView()
        {
            // When
            var Page = _Controller.Create();

            // Then
            Assert.IsType <ViewResult>(Page);
        }
        public void CreateTest()
        {
            PartialViewResult rv = (PartialViewResult)_controller.Create();

            Assert.IsInstanceOfType(rv.Model, typeof(ReportVM));

            ReportVM vm = rv.Model as ReportVM;
            Report   v  = new Report();

            v.Temperature = 36;
            vm.Entity     = v;
            _controller.Create(vm);

            using (var context = new DataContext(_seed, DBTypeEnum.Memory))
            {
                var data = context.Set <Report>().FirstOrDefault();

                Assert.AreEqual(data.Temperature, 36);
            }
        }
        public async void ReportController_Create_InValidViewModel_ReturnsView()
        {
            // Given
            // Create Invalid View Model
            var rvm = new ReportViewModel()
            {
                Latitude       = 45.00,
                Longitude      = 45.00,
                AdditionalInfo = "Test"
            };

            // Adjust Mocked Components
            var _svc = new Mock <IReportService>();

            _svc.Setup(m => m.NewReport(45.00, 45.00, "Testing"))
            .Returns(Task.FromResult(new Report()));
            var _Controller = new ReportController(_db, _svc.Object);

            _Controller.TempData =
                new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());

            // When
            _Controller.ModelState.AddModelError("Latitude", "Latitude is Required");
            var Page = await _Controller.Create(rvm) as ViewResult;

            // Then
            // Ensue New Report was never called
            _svc.Verify(r => r.NewReport(45.00, 45.00, "Testing"), Times.Never());

            // Ensure The View Model is returned
            var PageModel = (ReportViewModel)Page.ViewData.Model;

            Assert.Equal(45.00, PageModel.Latitude);
            Assert.Equal(45.00, PageModel.Longitude);
            Assert.Equal("Test", PageModel.AdditionalInfo);
        }
        public async void Integration_CreateAReport_Success()
        {
            /* Given */
            _ReportController.TempData =
                new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());

            // User Accounts Added By Seed
            _Seeder.Seed();

            /* When */
            // Open New Report Page
            var CreatePage = _ReportController.Create();

            Assert.IsType <ViewResult>(CreatePage);

            // Fill ViewModel
            var FilledViewModel = new ReportViewModel()
            {
                Latitude       = 54.717805,
                Longitude      = -6.226443,
                AdditionalInfo = "Report from Antrim Castle Gardens",
            };

            // Submit
            var Result = (RedirectToActionResult)await _ReportController.Create(FilledViewModel);

            // Then
            Assert.Equal("Home", Result.ControllerName);
            Assert.Equal("Index", Result.ActionName);

            // Controller Calls Service, Service Should Get 3 Word Address
            // Should Add To Database, and have Created at time
            var CreatedReport = _db.Reports.FirstOrDefault(r =>
                                                           (r.Latitude == 54.717805) && (r.Longitude == -6.226443));

            Assert.NotNull(CreatedReport);
            Assert.NotNull(CreatedReport.ThreeWordAddress);
            Assert.Equal("cares.sand.pacifist", CreatedReport.ThreeWordAddress);
            Assert.Equal(DateTime.Now, CreatedReport.CreatedAt, TimeSpan.FromSeconds(10.00));

            // Should Call Notify Function
            var client = new HttpClient();

            client.BaseAddress = new Uri("https://api.testmail.app/api/json");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            var Query = client.BaseAddress +
                        "?apikey=" + GetKey.TestmailAPIKey() + "&namespace=9h5an";

            TestMailResponse EmailResponse = null;

            HttpResponseMessage response = await client.GetAsync(Query);

            if (response.IsSuccessStatusCode)
            {
                EmailResponse = await response.Content.ReadAsAsync <TestMailResponse>();
            }

            string ExpectedMessage =
                "A new Report has been created in Non-City Area. The Three word" +
                " address of the report is: cares.sand.pacifist. To view on a map" +
                " open:\n https://what3words.com/cares.sand.pacifist\n This" +
                " report was created at " +
                DateTime.Now.ToString() +
                ", and the following" +
                " was the additional information provided: Report from Antrim" +
                " Castle Gardens\n\n Thank you! \n This message was auto-" +
                "generated by HelpingHand & sent via MailGun. \nIf you want to" +
                " stop recieving these messages, please turn off notifications" +
                " in your account settings.\n";

            // Pattern Below Matches Return Strings using RegEx to ignore the
            // Time as this varies from report being sent to now.
            var    input   = ExpectedMessage;
            string pattern =
                @"\d+[/]\d+[/]\d+\s\d+[:]\d+[:]\d+";

            String[] ExpectedElements =
                System.Text.RegularExpressions.Regex.Split(ExpectedMessage, pattern);

            bool EmailFound = false;


            for (int i = 0; i < EmailResponse.emails.Length && !EmailFound; i++)
            {
                var      Email            = EmailResponse.emails[i];
                String[] ReturnedElements =
                    System.Text.RegularExpressions.Regex.Split(Email.text, pattern);
                if ((ExpectedElements[0] == ReturnedElements[0]) &&
                    (ExpectedElements[1] == ReturnedElements[1]))
                {
                    EmailFound = true;
                }
            }

            // Then
            Assert.True(EmailFound);
        }