예제 #1
0
        public async Task <IActionResult> UpcomingBirthdays(int withinDays)
        {
            IRefreshableToken token = await _tokenHelperFactory.GetTokenAsync <IRefreshableToken>(TokenType.MicrosoftGraphToken, HttpContext);

            if (token == null)
            {
                return(Unauthorized());
            }

            IGetContactWithBirthdayCollectionQuery query = new GetContactWithBirthdayCollectionQuery
            {
                BirthdayWithinDays = withinDays,
                TokenType          = token.TokenType,
                AccessToken        = token.AccessToken,
                RefreshToken       = token.RefreshToken,
                Expires            = token.Expires
            };
            IEnumerable <IContact> contacts = await _queryBus.QueryAsync <IGetContactWithBirthdayCollectionQuery, IEnumerable <IContact> >(query);

            List <ContactWithUpcomingBirthdayViewModel> contactWithUpcomingBirthdayViewModels = contacts.AsParallel()
                                                                                                .Where(contact => contact.Birthday.HasValue)
                                                                                                .Select(contact => _homeViewModelConverter.Convert <IContact, ContactWithUpcomingBirthdayViewModel>(contact))
                                                                                                .OrderBy(contactWithUpcomingBirthdayViewModel => contactWithUpcomingBirthdayViewModel.UpcomingBirthday)
                                                                                                .ThenBy(contactWithUpcomingBirthdayViewModel => contactWithUpcomingBirthdayViewModel.DisplayName)
                                                                                                .ToList();

            return(PartialView("_UpcomingBirthdayCollectionPartial", contactWithUpcomingBirthdayViewModels));
        }
        public async Task GetTokenAsync_WhenCalledWithSupportedTokenType_AssertGetTokenAsyncWasCalledOnTokenHelperForTokenType()
        {
            TokenType tokenType = _fixture.Create <TokenType>();
            Mock <ITokenHelper <IRefreshableToken> > tokenHelperMock = BuildTokenHelperMock <IRefreshableToken>(tokenType);
            ITokenHelperFactory sut = CreateSut(new[] { tokenHelperMock.Object });

            HttpContext httpContext = CreateHttpContext();
            await sut.GetTokenAsync <IRefreshableToken>(tokenType, httpContext);

            tokenHelperMock.Verify(m => m.GetTokenAsync(It.Is <HttpContext>(value => value == httpContext)), Times.Once);
        }
        public async Task GetTokenAsync_WhenCalledWithSupportedTokenType_ReturnTokenFromGetTokenAsyncOnTokenHelperForTokenType()
        {
            TokenType         tokenType                  = _fixture.Create <TokenType>();
            IRefreshableToken refreshableToken           = _fixture.BuildRefreshableTokenMock().Object;
            ITokenHelper <IRefreshableToken> tokenHelper = BuildTokenHelperMock(tokenType, refreshableToken).Object;
            ITokenHelperFactory sut = CreateSut(new[] { tokenHelper });

            HttpContext       httpContext = CreateHttpContext();
            IRefreshableToken result      = await sut.GetTokenAsync <IRefreshableToken>(tokenType, httpContext);

            Assert.That(result, Is.EqualTo(refreshableToken));
        }
        private Task <IRefreshableToken> GetRefreshableTokenAsync(TokenType tokenType, HttpContext httpContext)
        {
            NullGuard.NotNull(httpContext, nameof(httpContext));

            switch (tokenType)
            {
            case TokenType.MicrosoftGraphToken:
                return(_tokenHelperFactory.GetTokenAsync <IRefreshableToken>(tokenType, httpContext));

            default:
                return(Task.Run(() => (IRefreshableToken)null));
            }
        }
        public void GetTokenAsync_WhenCalledWithSupportedTokenTypeButUnsupportedGenericToken_ThrowsNotSupportedException()
        {
            TokenType             tokenType   = _fixture.Create <TokenType>();
            ITokenHelper <IToken> tokenHelper = BuildTokenHelperMock <IToken>(tokenType).Object;
            ITokenHelperFactory   sut         = CreateSut(new[] { tokenHelper });

            NotSupportedException result = Assert.ThrowsAsync <NotSupportedException>(async() => await sut.GetTokenAsync <IRefreshableToken>(tokenType, CreateHttpContext()));

            Assert.That(result.Message, Is.EqualTo($"The helper logic for the token type '{tokenType}' does not support the generic type 'IRefreshableToken' within the method 'GetTokenAsync'."));
        }
        public void GetTokenAsync_WhenCalledWithUnsupportedTokenType_ThrowsNotSupportedException()
        {
            ITokenHelperFactory sut = CreateSut();

            TokenType             tokenType = _fixture.Create <TokenType>();
            NotSupportedException result    = Assert.ThrowsAsync <NotSupportedException>(async() => await sut.GetTokenAsync <IRefreshableToken>(tokenType, CreateHttpContext()));

            Assert.That(result.Message, Is.EqualTo($"The token type '{tokenType}' is not supported within the method 'GetTokenAsync'."));
        }
        public void GetTokenAsync_WhenHttpContextIsNull_ThrowsArgumentNullException()
        {
            ITokenHelperFactory sut = CreateSut();

            ArgumentNullException result = Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.GetTokenAsync <IRefreshableToken>(_fixture.Create <TokenType>(), null));

            Assert.That(result.ParamName, Is.EqualTo("httpContext"));
        }
예제 #8
0
        public async Task <IActionResult> StartLoadingContacts(string filter = null, string externalIdentifier = null)
        {
            if (await _tokenHelperFactory.GetTokenAsync <IRefreshableToken>(TokenType.MicrosoftGraphToken, HttpContext) == null)
            {
                return(Unauthorized());
            }

            ContactOptionsViewModel contactOptionsViewModel = new ContactOptionsViewModel
            {
                Filter             = string.IsNullOrWhiteSpace(filter) ? null : filter,
                ExternalIdentifier = string.IsNullOrWhiteSpace(externalIdentifier) ? null : externalIdentifier
            };

            return(PartialView("_LoadingContactsPartial", contactOptionsViewModel));
        }