Exemplo n.º 1
0
        public async Task JobProfileServiceRefreshSegmentReturnsNewMarkupWhenSuccess()
        {
            // arrange
            var refreshJobProfileSegmentModel = A.Fake <RefreshJobProfileSegment>();
            var existingJobProfileModel       = A.Fake <JobProfileModel>();
            var existingModel = new SegmentModel
            {
                Segment = Data.JobProfileSegment.Overview,
                Markup  = new HtmlString("This is existing markup"),
                Json    = "This is existing json",
            };

            existingJobProfileModel.Segments = new List <SegmentModel>
            {
                existingModel,
            };

            var jobProfileModel      = A.Fake <JobProfileModel>();
            var existingSegmentModel = A.Dummy <SegmentModel>();
            var segmentModel         = A.Dummy <SegmentModel>();

            segmentModel.Json   = "This is new Json";
            segmentModel.Markup = new HtmlString("This is new markup");

            var offlineModel = new OfflineSegmentModel
            {
                OfflineMarkup = new HtmlString("This is offline markup"),
                OfflineJson   = "This is offline json",
            };

            var expectedResult = HttpStatusCode.OK;

            A.CallTo(() => repository.GetAsync(A <Expression <Func <JobProfileModel, bool> > > .Ignored)).Returns(existingJobProfileModel);
            A.CallTo(() => segmentService.RefreshSegmentAsync(refreshJobProfileSegmentModel)).Returns(segmentModel);
            A.CallTo(() => segmentService.GetOfflineSegment(refreshJobProfileSegmentModel.Segment)).Returns(offlineModel);
            A.CallTo(() => repository.UpsertAsync(A <JobProfileModel> .Ignored)).Returns(HttpStatusCode.OK);

            // act
            var result = await jobProfileService.RefreshSegmentsAsync(refreshJobProfileSegmentModel).ConfigureAwait(false);

            // assert
            A.CallTo(() => repository.GetAsync(A <Expression <Func <JobProfileModel, bool> > > .Ignored)).MustHaveHappenedOnceExactly();
            A.CallTo(() => segmentService.RefreshSegmentAsync(refreshJobProfileSegmentModel)).MustHaveHappenedOnceExactly();
            A.CallTo(() => repository.UpsertAsync(A <JobProfileModel> .That.Matches(m =>
                                                                                    m.Segments[0].Markup.Value == segmentModel.Markup.Value &&
                                                                                    m.Segments[0].Json == segmentModel.Json))).MustHaveHappenedOnceExactly();

            result.Should().Be(expectedResult);
        }
        public async Task BodyReturnsOfflineMarkupWhenNonCriticalSegmentsHaveNoMarkup(List <SegmentModel> segments)
        {
            // Arrange
            var controller    = BuildProfileController(MediaTypeNames.Application.Json);
            var bodyViewModel = new BodyViewModel
            {
                CanonicalName = FakeArticleName,
                Segments      = segments,
            };

            var offlineSegmentModel = new OfflineSegmentModel
            {
                OfflineMarkup = new HtmlString("<h1>Some offline markup for this non-critical segment</h1>"),
            };

            A.CallTo(() => FakeJobProfileService.GetByNameAsync(A <string> .Ignored)).Returns(A.Fake <JobProfileModel>());
            A.CallTo(() => FakeJobProfileService.GetByAlternativeNameAsync(A <string> .Ignored)).Returns((JobProfileModel)null);
            A.CallTo(() => FakeMapper.Map <BodyViewModel>(A <JobProfileModel> .Ignored)).Returns(bodyViewModel);
            A.CallTo(() => FakeSegmentService.GetOfflineSegment(A <JobProfileSegment> .Ignored)).Returns(offlineSegmentModel);
            A.CallTo(() => FakeRedirectionSecurityService.IsValidHost(A <Uri> .Ignored)).Returns(true);

            // Act
            var result = await controller.Body(FakeArticleName).ConfigureAwait(false);

            // Assert
            var okObjectResult     = result as OkObjectResult;
            var resultViewModel    = Assert.IsAssignableFrom <BodyViewModel>(okObjectResult?.Value);
            var nonCriticalSegment = segments.FirstOrDefault(s => s.Segment != JobProfileSegment.Overview && s.Segment != JobProfileSegment.HowToBecome && s.Segment != JobProfileSegment.WhatItTakes);
            var resultSegmentModel = resultViewModel.Segments.FirstOrDefault(s => s.Segment == nonCriticalSegment?.Segment);

            Assert.Equal((int)HttpStatusCode.OK, okObjectResult.StatusCode);
            A.CallTo(() => FakeSegmentService.GetOfflineSegment(A <JobProfileSegment> .Ignored)).MustHaveHappenedOnceExactly();
            Assert.Equal(offlineSegmentModel.OfflineMarkup.Value, resultSegmentModel?.Markup.Value);

            controller.Dispose();
        }