public async Task Then_The_Help_Url_Is_Built_From_Config_If_Feature_Enabled_And_Show_Demand_Is_Returned( int standardCode, GetCourseResult response, LocationCookieItem locationCookieItem, [Frozen] Mock <IMediator> mediator, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Frozen] Mock <IOptions <FindApprenticeshipTrainingWeb> > config, [Greedy] CoursesController controller) { //Arrange config.Object.Value.EmployerDemandFeatureToggle = true; response.ShowEmployerDemand = true; mediator .Setup(x => x.Send( It.Is <GetCourseQuery>(c => c.CourseId.Equals(standardCode)), It.IsAny <CancellationToken>())) .ReturnsAsync(response); //Act var actual = await controller.CourseDetail(standardCode, "") as ViewResult; //Assert Assert.IsNotNull(actual); var actualModel = actual.Model as CourseViewModel; Assert.IsNotNull(actualModel); actualModel.HelpFindingCourseUrl.Should().Be($"{config.Object.Value.EmployerDemandUrl}/registerdemand/course/{actualModel.Id}/enter-apprenticeship-details"); }
public async Task Then_The_Help_Url_Set_If_Feature_Disabled( int standardCode, GetCourseResult response, LocationCookieItem locationCookieItem, [Frozen] Mock <IMediator> mediator, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Frozen] Mock <IOptions <FindApprenticeshipTrainingWeb> > config, [Greedy] CoursesController controller) { //Arrange config.Object.Value.EmployerDemandFeatureToggle = false; mediator .Setup(x => x.Send( It.Is <GetCourseQuery>(c => c.CourseId.Equals(standardCode)), It.IsAny <CancellationToken>())) .ReturnsAsync(response); //Act var actual = await controller.CourseDetail(standardCode, "") as ViewResult; //Assert Assert.IsNotNull(actual); var actualModel = actual.Model as CourseViewModel; Assert.IsNotNull(actualModel); actualModel.HelpFindingCourseUrl.Should().Be("https://help.apprenticeships.education.gov.uk/hc/en-gb#contact-us"); }
public async Task Then_The_Location_Cookie_Is_Checked_And_Added_To_Model( GetCoursesRequest request, GetCoursesResult response, LocationCookieItem cookieItem, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > locationCookieService, [Frozen] Mock <IMediator> mediator, [Greedy] CoursesController controller) { //Arrange request.Location = string.Empty; mediator.Setup(x => x.Send(It.Is <GetCoursesQuery>(c => c.Keyword.Equals(request.Keyword) && c.RouteIds.Equals(request.Sectors) && c.Levels.Equals(request.Levels)), It.IsAny <CancellationToken>())) .ReturnsAsync(response); locationCookieService.Setup(x => x.Get(Constants.LocationCookieName)).Returns(cookieItem); //Act var actual = await controller.Courses(request); //Assert Assert.IsNotNull(actual); var actualResult = actual as ViewResult; Assert.IsNotNull(actualResult); var actualModel = actualResult.Model as CoursesViewModel; Assert.IsNotNull(actualModel); actualModel.Location.Should().Be(cookieItem.Name); }
public async Task Then_If_No_Course_Is_Returned_Redirected_To_Page_Not_Found( int standardCode, GetCourseResult response, LocationCookieItem locationCookieItem, [Frozen] Mock <IMediator> mediator, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Greedy] CoursesController controller) { //Arrange cookieStorageService .Setup(x => x.Get(Constants.LocationCookieName)) .Returns(locationCookieItem); mediator .Setup(x => x.Send(It.Is <GetCourseQuery>(c => c.CourseId.Equals(standardCode)), It.IsAny <CancellationToken>())) .ReturnsAsync(new GetCourseResult()); //Act var actual = await controller.CourseDetail(standardCode, ""); //Assert Assert.IsNotNull(actual); var actualResult = actual as RedirectToRouteResult; Assert.IsNotNull(actualResult); actualResult.RouteName.Should().Be(RouteNames.Error404); }
public async Task Then_If_There_Is_Location_Stored_In_Cookie_And_No_Location_In_Query_It_Is_Used_For_Results_And_Cookie_Updated( int providerId, int courseId, LocationCookieItem location, GetCourseProviderResult response, [Frozen] Mock <IMediator> mediator, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Greedy] CoursesController controller) { //Arrange response.Course.StandardDates.LastDateStarts = DateTime.UtcNow.AddDays(5); cookieStorageService.Setup(x => x.Get(Constants.LocationCookieName)).Returns(location); mediator.Setup(x => x.Send(It.Is <GetCourseProviderQuery>(c => c.ProviderId.Equals(providerId) && c.CourseId.Equals(courseId) && c.Location.Equals(location.Name) && c.Lat.Equals(location.Lat) && c.Lon.Equals(location.Lon) ), It.IsAny <CancellationToken>())) .ReturnsAsync(response); //Act var actual = await controller.CourseProviderDetail(courseId, providerId, "", "", ""); //Assert Assert.IsNotNull(actual); var actualResult = actual as ViewResult; Assert.IsNotNull(actualResult); var actualModel = actualResult.Model as CourseProviderViewModel; Assert.IsNotNull(actualModel); cookieStorageService.Verify(x => x.Update(Constants.LocationCookieName, It.Is <LocationCookieItem>(c => c.Name.Equals(response.Location)), 2)); }
private void UpdateLocationCookie(LocationCookieItem location) { if (!string.IsNullOrEmpty(location.Name) && location.Lat != 0 && location.Lon != 0) { _locationCookieStorageService.Update(Constants.LocationCookieName, location, 2); } }
public async Task And_The_Location_Cookie_Exists_Then_Location_Is_Added_To_Shortlist( CreateShortlistItemRequest request, ShortlistCookieItem shortlistCookie, LocationCookieItem locationCookieItem, [Frozen] Mock <ICookieStorageService <ShortlistCookieItem> > mockShortlistCookieService, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > mockLocationCookieService, [Frozen] Mock <IMediator> mockMediator, [Greedy] ShortlistController controller) { //Arrange mockShortlistCookieService .Setup(service => service.Get(Constants.ShortlistCookieName)) .Returns(shortlistCookie); mockLocationCookieService.Setup(x => x.Get(Constants.LocationCookieName)) .Returns(locationCookieItem); request.RouteName = string.Empty; //Act var actual = await controller.CreateShortlistItem(request) as AcceptedResult; //Assert actual.Should().NotBeNull(); mockMediator.Verify(x => x.Send(It.Is <CreateShortlistItemForUserCommand>(c => c.ShortlistUserId.Equals(shortlistCookie.ShortlistUserId) && c.Lat.Equals(locationCookieItem.Lat) && c.Lon.Equals(locationCookieItem.Lon) && c.LocationDescription.Equals(locationCookieItem.Name) && c.Ukprn.Equals(request.Ukprn) && c.TrainingCode.Equals(request.TrainingCode) ), It.IsAny <CancellationToken>()), Times.Once); }
public async Task And_If_ProviderName_Is_In_The_Request_Is_Encoded_Using_The_Protector( CreateShortlistItemRequest request, ShortlistCookieItem shortlistCookie, LocationCookieItem locationCookieItem, [Frozen] Mock <ICookieStorageService <ShortlistCookieItem> > mockShortlistCookieService, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > mockLocationCookieService, [Frozen] Mock <IDataProtector> protector, [Frozen] Mock <IDataProtectionProvider> provider, [Greedy] ShortlistController controller) { //Arrange mockShortlistCookieService .Setup(service => service.Get(Constants.ShortlistCookieName)) .Returns(shortlistCookie); mockLocationCookieService.Setup(x => x.Get(Constants.LocationCookieName)) .Returns(locationCookieItem); request.RouteName = RouteNames.CourseProviders; //Act await controller.CreateShortlistItem(request); //Assert protector.Verify(c => c.Protect(It.Is <byte[]>( x => x[0].Equals(Encoding.UTF8.GetBytes($"{request.ProviderName}")[0]))), Times.Once); }
public async Task And_If_There_Is_A_Route_Name_Then_It_Is_Redirected(CreateShortlistItemRequest request, ShortlistCookieItem shortlistCookie, LocationCookieItem locationCookieItem, [Frozen] Mock <ICookieStorageService <ShortlistCookieItem> > mockShortlistCookieService, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > mockLocationCookieService, [Frozen] Mock <IMediator> mockMediator, [Greedy] ShortlistController controller) { //Arrange mockShortlistCookieService .Setup(service => service.Get(Constants.ShortlistCookieName)) .Returns(shortlistCookie); mockLocationCookieService.Setup(x => x.Get(Constants.LocationCookieName)) .Returns(locationCookieItem); request.RouteName = RouteNames.CourseProviders; //Act var actual = await controller.CreateShortlistItem(request) as RedirectToRouteResult; //Assert actual.Should().NotBeNull(); actual.RouteName.Should().Be(request.RouteName); actual.RouteValues.Should().ContainKey("id"); actual.RouteValues["id"].Should().Be(request.TrainingCode); actual.RouteValues.Should().ContainKey("providerId"); actual.RouteValues["providerId"].Should().Be(request.Ukprn); }
private ActionExecutingContext SetupContextAndCookieLocations(CoursesController controller, string location, LocationCookieItem cookieLocation, Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, string providerId = "", string data = "") { cookieStorageService.Setup(x => x.Get(Constants.LocationCookieName)) .Returns(cookieLocation); var httpContext = new DefaultHttpContext(); var routeData = new RouteData(); var queryString = ""; if (!string.IsNullOrEmpty(location)) { queryString += $"?location={location}"; } if (!string.IsNullOrEmpty(providerId)) { routeData.Values.Add("providerId", providerId); if (!string.IsNullOrEmpty(data)) { if (string.IsNullOrEmpty(queryString)) { queryString += $"?data={data}"; } else { queryString += $"&data={data}"; } } } if (!string.IsNullOrEmpty(queryString)) { httpContext.Request.QueryString = new QueryString(queryString); } controller.ControllerContext = new ControllerContext { HttpContext = httpContext }; var actionContext = new ActionContext( httpContext, routeData, Mock.Of <ActionDescriptor>(), new ModelStateDictionary() ); return(new ActionExecutingContext( actionContext, new List <IFilterMetadata>(), new Dictionary <string, object>(), controller )); }
Then_If_No_Location_In_Context_And_Location_In_Cookie_Adds_The_Location_From_The_Cookie_To_The_ViewBag( LocationCookieItem location, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Greedy] CoursesController controller, GoogleAnalyticsFilter filter) { //Arrange var context = SetupContextAndCookieLocations(controller, null, location, cookieStorageService); //Act await filter.OnActionExecutionAsync(context, Mock.Of <ActionExecutionDelegate>()); //Assert var viewBag = controller.ViewBag.GaData as GaData; Assert.IsNotNull(viewBag); Assert.AreEqual(location.Name, viewBag.Location); }
public async Task Then_The_Query_Is_Sent_And_Data_Retrieved_And_View_Shown( int standardCode, GetCourseResult response, LocationCookieItem locationCookieItem, ShortlistCookieItem shortlistCookieItem, [Frozen] Mock <IMediator> mediator, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Frozen] Mock <ICookieStorageService <ShortlistCookieItem> > shortlistStorageService, [Greedy] CoursesController controller) { //Arrange response.ShowEmployerDemand = true; cookieStorageService .Setup(x => x.Get(Constants.LocationCookieName)) .Returns(locationCookieItem); shortlistStorageService .Setup(x => x.Get(Constants.ShortlistCookieName)) .Returns(shortlistCookieItem); mediator .Setup(x => x.Send(It.Is <GetCourseQuery>(c => c.CourseId.Equals(standardCode) && c.ShortlistUserId.Equals(shortlistCookieItem.ShortlistUserId) && c.Lat.Equals(locationCookieItem.Lat) && c.Lon.Equals(locationCookieItem.Lon) ), It.IsAny <CancellationToken>())) .ReturnsAsync(response); //Act var actual = await controller.CourseDetail(standardCode, ""); //Assert Assert.IsNotNull(actual); var actualResult = actual as ViewResult; Assert.IsNotNull(actualResult); var actualModel = actualResult.Model as CourseViewModel; Assert.IsNotNull(actualModel); actualModel.TotalProvidersCount.Should().Be(response.ProvidersCount.TotalProviders); actualModel.ProvidersAtLocationCount.Should().Be(response.ProvidersCount.ProvidersAtLocation); actualModel.LocationName.Should().Be(locationCookieItem.Name); actualModel.ShortlistItemCount.Should().Be(response.ShortlistItemCount); }
public async Task Then_If_There_Is_Location_Stored_In_Cookie_It_Is_Used_For_Results_And_Cookie_Updated( GetCourseProvidersRequest request, LocationCookieItem location, GetCourseProvidersResult response, [Frozen] Mock <IMediator> mediator, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Greedy] CoursesController controller) { //Arrange response.Course.StandardDates.LastDateStarts = DateTime.UtcNow.AddDays(5); request.Location = string.Empty; cookieStorageService .Setup(x => x.Get(Constants.LocationCookieName)) .Returns(location); mediator .Setup(x => x.Send( It.Is <GetCourseProvidersQuery>(c => c.CourseId.Equals(request.Id) && c.Location.Equals(location.Name) && c.Lon.Equals(location.Lon) && c.Lat.Equals(location.Lat) ), It.IsAny <CancellationToken>())) .ReturnsAsync(response); //Act var actual = await controller.CourseProviders(request) as ViewResult; //Assert Assert.IsNotNull(actual); var actualModel = actual.Model as CourseProvidersViewModel; Assert.IsNotNull(actualModel); actualModel.Providers.Should().BeEquivalentTo(response.Providers.Select(provider => (ProviderViewModel)provider)); actualModel.HasLocation.Should().BeTrue(); cookieStorageService.Verify(x => x.Update(Constants.LocationCookieName, It.Is <LocationCookieItem>(c => c.Name.Equals(response.Location) && c.Lat.Equals(response.LocationGeoPoint.FirstOrDefault()) && c.Lon.Equals(response.LocationGeoPoint.LastOrDefault()) ) , 2)); }
public async Task Then_If_There_Is_A_Location_Cookie_The_Lat_Lon_And_Name_Are_Passed_To_The_Query( int standardCode, GetCourseResult response, LocationCookieItem locationCookieItem, [Frozen] Mock <IMediator> mediator, [Frozen] Mock <ICookieStorageService <LocationCookieItem> > cookieStorageService, [Frozen] Mock <ICookieStorageService <ShortlistCookieItem> > shortlistStorageService, [Greedy] CoursesController controller) { //Arrange cookieStorageService.Setup(x => x.Get(Constants.LocationCookieName)) .Returns(locationCookieItem); shortlistStorageService .Setup(x => x.Get(Constants.ShortlistCookieName)) .Returns((ShortlistCookieItem)null); mediator.Setup(x => x.Send(It.Is <GetCourseQuery>(c => c.CourseId.Equals(standardCode) && c.Lat.Equals(locationCookieItem.Lat) && c.Lon.Equals(locationCookieItem.Lon) && c.LocationName.Equals(locationCookieItem.Name) && c.ShortlistUserId == null ), It.IsAny <CancellationToken>())) .ReturnsAsync(response); //Act var actual = await controller.CourseDetail(standardCode, ""); //Assert Assert.IsNotNull(actual); var actualResult = actual as ViewResult; Assert.IsNotNull(actualResult); var actualModel = actualResult.Model as CourseViewModel; Assert.IsNotNull(actualModel); }
public async Task <IActionResult> CourseProviderDetail(int id, int providerId, string location, string removed, string added) { try { var locationItem = CheckLocation(location); var shortlistItem = _shortlistCookieService.Get(Constants.ShortlistCookieName); var result = await _mediator.Send(new GetCourseProviderQuery { ProviderId = providerId, CourseId = id, Location = locationItem?.Name ?? "", Lat = locationItem?.Lat ?? 0, Lon = locationItem?.Lon ?? 0, ShortlistUserId = shortlistItem?.ShortlistUserId }); var cookieResult = new LocationCookieItem { Name = result.Location, Lat = result.LocationGeoPoint?.FirstOrDefault() ?? 0, Lon = result.LocationGeoPoint?.LastOrDefault() ?? 0 }; UpdateLocationCookie(cookieResult); if (result.Course == null) { return(RedirectToRoute(RouteNames.Error404)); } var viewModel = (CourseProviderViewModel)result; viewModel.Location = cookieResult.Name; var providersRequestCookie = _courseProvidersCookieStorageService.Get(Constants.ProvidersCookieName); if (providersRequestCookie != default) { if (id != providersRequestCookie.Id) { providersRequestCookie.Id = id; providersRequestCookie.DeliveryModes = new List <DeliveryModeType>(); providersRequestCookie.ProviderRatings = new List <ProviderRating>(); } providersRequestCookie.Location = result?.Location; viewModel.GetCourseProvidersRequest = providersRequestCookie.ToDictionary(); } if (viewModel.Course.AfterLastStartDate) { return(RedirectToRoute(RouteNames.CourseDetails, new { Id = id })); } var removedProviderFromShortlist = string.IsNullOrEmpty(removed) ? "" : HttpUtility.HtmlDecode(GetEncodedProviderName(removed)); var addedProviderToShortlist = string.IsNullOrEmpty(added) ? "" : HttpUtility.HtmlDecode(GetEncodedProviderName(added)); viewModel.BannerUpdateMessage = GetProvidersBannerUpdateMessage(removedProviderFromShortlist, addedProviderToShortlist); viewModel.HelpFindingCourseUrl = BuildHelpFindingCourseUrl(viewModel.Course.Id, EntryPoint.ProviderDetail); return(View(viewModel)); } catch (Exception e) { _logger.LogError(e, e.Message); return(RedirectToRoute(RouteNames.Error500)); } }
public async Task <IActionResult> CourseProviders(GetCourseProvidersRequest request) { try { var location = CheckLocation(request.Location); var shortlistItem = _shortlistCookieService.Get(Constants.ShortlistCookieName); var result = await _mediator.Send(new GetCourseProvidersQuery { CourseId = request.Id, Location = location?.Name ?? "", Lat = location?.Lat ?? 0, Lon = location?.Lon ?? 0, DeliveryModes = request.DeliveryModes.Select(type => (Domain.Courses.DeliveryModeType)type), ProviderRatings = request.ProviderRatings.Select(rating => (Domain.Courses.ProviderRating)rating), ShortlistUserId = shortlistItem?.ShortlistUserId }); var cookieResult = new LocationCookieItem { Name = result.Location, Lat = result.LocationGeoPoint?.FirstOrDefault() ?? 0, Lon = result.LocationGeoPoint?.LastOrDefault() ?? 0 }; UpdateLocationCookie(cookieResult); if (result.Course == null) { return(RedirectToRoute(RouteNames.Error404)); } var providerList = result.Providers.ToList(); var providers = result.Providers .ToDictionary(provider => provider.ProviderId, provider => WebEncoders.Base64UrlEncode(_providerDataProtector.Protect( System.Text.Encoding.UTF8.GetBytes($"{providerList.IndexOf(provider) + 1}|{result.TotalFiltered}")))); _courseProvidersCookieStorageService.Update(Constants.ProvidersCookieName, request, 2); var courseProvidersViewModel = new CourseProvidersViewModel(request, result, providers); if (courseProvidersViewModel.Course.AfterLastStartDate) { return(RedirectToRoute(RouteNames.CourseDetails, new { request.Id })); } var removedProviderFromShortlist = string.IsNullOrEmpty(request.Removed) ? "" : HttpUtility.HtmlDecode(GetEncodedProviderName(request.Removed)); var addedProviderToShortlist = string.IsNullOrEmpty(request.Added) ? "" : HttpUtility.HtmlDecode(GetEncodedProviderName(request.Added)); courseProvidersViewModel.BannerUpdateMessage = GetProvidersBannerUpdateMessage(removedProviderFromShortlist, addedProviderToShortlist); courseProvidersViewModel.HelpFindingCourseUrl = BuildHelpFindingCourseUrl(courseProvidersViewModel.Course.Id, EntryPoint.Providers); return(View(courseProvidersViewModel)); } catch (Exception e) { _logger.LogError(e, e.Message); return(RedirectToRoute(RouteNames.Error500)); } }