Exemplo n.º 1
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndHttpContextWasNotReturned_AssertToBase64StringWasNotCalledOnContentHelperWithValueString()
        {
            ICookieHelper sut = CreateSut(hasHttpContext: false);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            _contentHelperMock.Verify(m => m.ToBase64String(It.IsAny <string>()), Times.Never);
        }
Exemplo n.º 2
0
        public void ToCookie_WhenCalledWithDashboardViewModel_AssertHttpContextWasCalledOnHttpContextAccessor()
        {
            ICookieHelper sut = CreateSut();

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            _httpContextAccessorMock.Verify(m => m.HttpContext, Times.Exactly(2));
        }
Exemplo n.º 3
0
        public void ToCookie_WhenCalledWithDashboardSettingsViewModelAndResponseCookiesWasReturned_AssertToBase64StringWasCalledOnContentHelperWithDashboardSettingsViewModel()
        {
            ICookieHelper sut = CreateSut();

            DashboardSettingsViewModel dashboardSettingsViewModel = BuildDashboardSettingsViewModel(_random);

            sut.ToCookie(dashboardSettingsViewModel);

            _contentHelperMock.Verify(m => m.ToBase64String(It.Is <DashboardSettingsViewModel>(value => value == dashboardSettingsViewModel)), Times.Once);
        }
Exemplo n.º 4
0
        public void ToCookie_WhenCalledWithDashboardSettingsViewModelAndHttpContextWasReturned_AssertRequestWasCalledOnReturnedHttpContext()
        {
            Mock <HttpContext> httpContextMock = BuildHttpContextMock();
            ICookieHelper      sut             = CreateSut(httpContext: httpContextMock.Object);

            DashboardSettingsViewModel dashboardSettingsViewModel = BuildDashboardSettingsViewModel(_random);

            sut.ToCookie(dashboardSettingsViewModel);

            httpContextMock.Verify(m => m.Request, Times.Once);
        }
Exemplo n.º 5
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndCacheEntryWasCreated_AssertValueSetterWasCalledOnCreatedCacheEntry()
        {
            string        cookieValueToStore = Guid.NewGuid().ToString("D");
            ICookieHelper sut = CreateSut(cookieValueToStore: cookieValueToStore);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            _cacheEntryMock.VerifySet(m => m.Value = It.Is <byte[]>(value => value != null && string.CompareOrdinal(Convert.ToBase64String(value), BuildBase64String(cookieValueToStore)) == 0), Times.Once);
        }
Exemplo n.º 6
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndHttpContextWasReturnedButHttpResponseWasNotReturned_AssertCreateEntryWasNotCalledOnMemoryCache()
        {
            HttpContext   httpContext = BuildHttpContext(hasHttpResponse: false);
            ICookieHelper sut         = CreateSut(httpContext: httpContext);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            _memoryCacheMock.Verify(m => m.CreateEntry(It.IsAny <object>()), Times.Never);
        }
Exemplo n.º 7
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndHttpContextWasReturnedButHttpResponseWasNotReturned_AssertToByteArrayWasNotCalledOnContentHelperWithDashboardViewModel()
        {
            HttpContext   httpContext = BuildHttpContext(hasHttpResponse: false);
            ICookieHelper sut         = CreateSut(httpContext: httpContext);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            _contentHelperMock.Verify(m => m.ToByteArray(It.IsAny <DashboardViewModel>()), Times.Never);
        }
Exemplo n.º 8
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndCacheEntryWasCreated_AssertAbsoluteExpirationSetterWasCalledOnCreatedCacheEntry()
        {
            ICookieHelper sut = CreateSut();

            DateTime expires = DateTime.Now.AddSeconds(30);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            _cacheEntryMock.VerifySet(m => m.AbsoluteExpiration = It.Is <DateTimeOffset>(value => value >= expires.AddSeconds(-5) && value <= expires.AddSeconds(5)), Times.Once);
        }
Exemplo n.º 9
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndHttpResponseWasReturnedButResponseCookiesWasNotReturned_AssertToBase64StringWasNotCalledOnContentHelperWithValueString()
        {
            HttpResponse  httpResponse = BuildHttpResponse(false);
            HttpContext   httpContext  = BuildHttpContext(httpResponse: httpResponse);
            ICookieHelper sut          = CreateSut(httpContext: httpContext);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            _contentHelperMock.Verify(m => m.ToBase64String(It.IsAny <string>()), Times.Never);
        }
Exemplo n.º 10
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndHttpResponseWasReturned_AssertCookiesWasCalledOnReturnedHttpResponse()
        {
            Mock <HttpResponse> httpResponseMock = BuildHttpResponseMock();
            HttpContext         httpContext      = BuildHttpContext(httpResponse: httpResponseMock.Object);
            ICookieHelper       sut = CreateSut(httpContext: httpContext);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            httpResponseMock.Verify(m => m.Cookies, Times.Once);
        }
Exemplo n.º 11
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndDashboardViewModelWasNotConvertedToByteArray_AssertAppendWasNotCalledOnReturnedResponseCookies()
        {
            Mock <IResponseCookies> responseCookiesMock = BuildResponseCookiesMock();
            HttpResponse            httpResponse        = BuildHttpResponse(responseCookies: responseCookiesMock.Object);
            HttpContext             httpContext         = BuildHttpContext(httpResponse: httpResponse);
            ICookieHelper           sut = CreateSut(false, httpContext: httpContext);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            responseCookiesMock.Verify(m => m.Append(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CookieOptions>()), Times.Never);
        }
Exemplo n.º 12
0
        protected override DashboardSettingsViewModel Build(IDashboardSettings dashboardSettings)
        {
            IRedditAccessToken redditAccessToken = dashboardSettings.RedditAccessToken;

            bool includeNsfwContent = dashboardSettings.IncludeNsfwContent;
            bool onlyNsfwContent    = dashboardSettings.OnlyNsfwContent;

            DashboardSettingsViewModel dashboardSettingsViewModel = new DashboardSettingsViewModel
            {
                NumberOfNews       = dashboardSettings.NumberOfNews,
                UseReddit          = dashboardSettings.UseReddit,
                IncludeNsfwContent = includeNsfwContent ? true : (bool?)null,
                OnlyNsfwContent    = onlyNsfwContent ? true : (bool?)null,
                RedditAccessToken  = redditAccessToken != null?redditAccessToken.ToBase64() : null
            };

            _cookieHelper.ToCookie(dashboardSettingsViewModel);

            return(dashboardSettingsViewModel);
        }
Exemplo n.º 13
0
        public void ToCookie_WhenCalledWithDashboardViewModelAndDashboardViewModelWasConvertedToByteArray_AssertAppendWasCalledOnReturnedResponseCookies()
        {
            bool                    isHttps             = _random.Next(100) > 50;
            HttpRequest             httpRequest         = BuildHttpRequest(isHttps);
            string                  cookieValueToStore  = $"{DashboardViewModel.CookieName}.{Guid.NewGuid().ToString("D")}";
            Mock <IResponseCookies> responseCookiesMock = BuildResponseCookiesMock();
            HttpResponse            httpResponse        = BuildHttpResponse(responseCookies: responseCookiesMock.Object);
            HttpContext             httpContext         = BuildHttpContext(httpRequest: httpRequest, httpResponse: httpResponse);
            ICookieHelper           sut = CreateSut(cookieValueToStore: cookieValueToStore, httpContext: httpContext);

            DateTime expires = DateTime.Now.AddSeconds(30);

            DashboardViewModel dashboardViewModel = BuildDashboardViewModel();

            sut.ToCookie(dashboardViewModel);

            responseCookiesMock.Verify(m => m.Append(
                                           It.Is <string>(value => string.CompareOrdinal(value, DashboardViewModel.CookieName) == 0),
                                           It.Is <string>(value => string.CompareOrdinal(value, BuildBase64String(cookieValueToStore)) == 0),
                                           It.Is <CookieOptions>(value => value != null && value.Expires >= expires.AddSeconds(-5) && value.Expires <= expires.AddSeconds(5) && value.Secure == isHttps && value.SameSite == SameSiteMode.None)),
                                       Times.Once);
        }
Exemplo n.º 14
0
        protected override DashboardViewModel Build(IDashboard dashboard)
        {
            IList <InformationViewModel> informationViewModelCollection = new List <InformationViewModel>();
            IList <SystemErrorViewModel> systemErrorViewModelCollection = new List <SystemErrorViewModel>();
            DashboardSettingsViewModel   dashboardSettingsViewModel     = null;
            ObjectViewModel <IRedditAuthenticatedUser>  objectViewModelForRedditAuthenticatedUser   = null;
            IList <ObjectViewModel <IRedditSubreddit> > objectViewModelForRedditSubredditCollection = new List <ObjectViewModel <IRedditSubreddit> >();
            object syncRoot = new object();

            try
            {
                Task handleInformationViewModelsForNewsTask = HandleAsync <InformationViewModel>(
                    dashboard,
                    m => GenerateViewModelBuilderTaskArrayForCollection(m, n => n.News, _newsToInformationViewModelBuilder),
                    viewModel => AddViewModelToViewModelCollection(viewModel, informationViewModelCollection, syncRoot),
                    exception => HandleException(exception, systemErrorViewModelCollection, syncRoot));
                Task handleSystemErrorViewModelsTask = HandleAsync <SystemErrorViewModel>(
                    dashboard,
                    m => GenerateViewModelBuilderTaskArrayForCollection(m, n => n.SystemErrors, _systemErrorViewModelBuilder),
                    viewModel => AddViewModelToViewModelCollection(viewModel, systemErrorViewModelCollection, syncRoot),
                    exception => HandleException(exception, systemErrorViewModelCollection, syncRoot));
                Task handleDashboardSettingsViewModelTask = HandleAsync <DashboardSettingsViewModel>(
                    dashboard,
                    m => GenerateViewModelBuilderTaskArrayForObject(m, n => n.Settings, _dashboardSettingsViewModelBuilder),
                    viewModel => dashboardSettingsViewModel = viewModel,
                    exception => HandleException(exception, systemErrorViewModelCollection, syncRoot));
                Task handleObjectViewModelForRedditAuthenticatedUserTask = HandleAsync <ObjectViewModel <IRedditAuthenticatedUser> >(
                    dashboard,
                    m => GenerateViewModelBuilderTaskArrayForObject(m, n => n.RedditAuthenticatedUser, _redditAuthenticatedUserToObjectViewModelBuilder),
                    viewModel => objectViewModelForRedditAuthenticatedUser = viewModel,
                    exception => HandleException(exception, systemErrorViewModelCollection, syncRoot));
                Task handleObjectViewModelsForRedditSubredditTask = HandleAsync <ObjectViewModel <IRedditSubreddit> >(
                    dashboard,
                    m => GenerateViewModelBuilderTaskArrayForCollection(m, n => n.RedditSubreddits, _redditSubredditToObjectViewModelBuilder),
                    viewModel => AddViewModelToViewModelCollection(viewModel, objectViewModelForRedditSubredditCollection, syncRoot),
                    exception => HandleException(exception, systemErrorViewModelCollection, syncRoot));
                Task handleInformationViewModelsForRedditLinkTask = HandleAsync <InformationViewModel>(
                    dashboard,
                    m => GenerateViewModelBuilderTaskArrayForCollection(m, n => n.RedditLinks, _redditLinkToInformationViewModelBuilder),
                    viewModel => AddViewModelToViewModelCollection(viewModel, informationViewModelCollection, syncRoot),
                    exception => HandleException(exception, systemErrorViewModelCollection, syncRoot));
                Task.WhenAll(new[] { handleInformationViewModelsForNewsTask, handleSystemErrorViewModelsTask, handleDashboardSettingsViewModelTask, handleObjectViewModelForRedditAuthenticatedUserTask, handleObjectViewModelsForRedditSubredditTask, handleInformationViewModelsForRedditLinkTask })
                .GetAwaiter()
                .GetResult();
            }
            catch (Exception ex)
            {
                HandleException(ex, systemErrorViewModelCollection, syncRoot);
            }

            IEnumerable <ImageViewModel <InformationViewModel> > latestInformationsWithImage = GetLatestInformationsWithImage(informationViewModelCollection, systemErrorViewModelCollection, syncRoot);

            DashboardViewModel dashboardViewModel = new DashboardViewModel
            {
                Informations = informationViewModelCollection.OrderByDescending(informationViewModel => informationViewModel.Timestamp).ToList(),
                LatestInformationsWithImage = latestInformationsWithImage.OrderByDescending(latestInformationWithImage => latestInformationWithImage.ViewModel.Timestamp).ToList(),
                SystemErrors            = systemErrorViewModelCollection.OrderByDescending(systemErrorViewModel => systemErrorViewModel.Timestamp).ToList(),
                Settings                = dashboardSettingsViewModel,
                RedditAuthenticatedUser = objectViewModelForRedditAuthenticatedUser,
                RedditSubreddits        = objectViewModelForRedditSubredditCollection.OrderByDescending(objectViewModelForRedditSubreddit => objectViewModelForRedditSubreddit.Object.Subscribers).ToList()
            };

            dashboardViewModel.ApplyRules(dashboard.Rules, _cookieHelper);

            _cookieHelper.ToCookie(dashboardViewModel);

            return(dashboardViewModel);
        }
Exemplo n.º 15
0
        public void ToCookie_WhenDashboardSettingsViewModelIsNull_ThrowsArgumentNullException()
        {
            ICookieHelper sut = CreateSut();

            sut.ToCookie((DashboardSettingsViewModel)null);
        }