public async Task LoadContacts_WhenNoTokenWasReturnedFromTokenHelperFactory_ReturnsUnauthorizedResult()
        {
            Controller sut = CreateSut(false);

            IActionResult result = await sut.LoadContacts();

            Assert.That(result, Is.TypeOf <UnauthorizedResult>());
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndExternalIdentifierIsNotNullEmptyOrWhiteSpace_ReturnsPartialViewResultWhereViewDataContainingExternalIdentifier()
        {
            Controller sut = CreateSut();

            PartialViewResult result = (PartialViewResult)await sut.LoadContacts(externalIdentifier : _fixture.Create <string>());

            Assert.That(result.ViewData.ContainsKey("ExternalIdentifier"), Is.True);
        }
        public async Task LoadContacts_WhenNoTokenWasReturnedFromTokenHelperFactory_AssertQueryAsyncWasNotCalledOnQueryBusWithGetMatchingContactCollectionQuery()
        {
            Controller sut = CreateSut(false);

            await sut.LoadContacts();

            _queryBusMock.Verify(m => m.QueryAsync <IGetMatchingContactCollectionQuery, IEnumerable <IContact> >(It.IsAny <IGetMatchingContactCollectionQuery>()), Times.Never);
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndFilterIsWhiteSpace_ReturnsPartialViewResultWhereViewNameIsEqualToContactCollectionPartial()
        {
            Controller sut = CreateSut();

            PartialViewResult result = (PartialViewResult)await sut.LoadContacts(" ");

            Assert.That(result.ViewName, Is.EqualTo("_ContactCollectionPartial"));
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndExternalIdentifierIsWhiteSpace_ReturnsPartialViewResultWhereViewDataDoesNotContainExternalIdentifier()
        {
            Controller sut = CreateSut();

            PartialViewResult result = (PartialViewResult)await sut.LoadContacts(externalIdentifier : " ");

            Assert.That(result.ViewData.ContainsKey("ExternalIdentifier"), Is.False);
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndFilterIsWhiteSpace_ReturnsPartialViewResult()
        {
            Controller sut = CreateSut();

            IActionResult result = await sut.LoadContacts(" ");

            Assert.That(result, Is.TypeOf <PartialViewResult>());
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndExternalIdentifierIsNotNullEmptyOrWhiteSpace_ReturnsPartialViewResultWhereViewDataContainingExternalIdentifierWithValueEqualToExternalIdentifierArgument()
        {
            Controller sut = CreateSut();

            string            externalIdentifier = _fixture.Create <string>();
            PartialViewResult result             = (PartialViewResult)await sut.LoadContacts(externalIdentifier : externalIdentifier);

            Assert.That(result.ViewData["ExternalIdentifier"], Is.EqualTo(externalIdentifier));
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndFilterIsNotNullEmptyOrWhiteSpace_ReturnsPartialViewResult()
        {
            Controller sut = CreateSut();

            string        filter = _fixture.Create <string>();
            IActionResult result = await sut.LoadContacts(filter);

            Assert.That(result, Is.TypeOf <PartialViewResult>());
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndFilterIsNotNullEmptyOrWhiteSpace_AssertQueryAsyncWasNotCalledOnQueryBusWithGetContactCollectionQuery()
        {
            Controller sut = CreateSut();

            string filter = _fixture.Create <string>();
            await sut.LoadContacts(filter);

            _queryBusMock.Verify(m => m.QueryAsync <IGetContactCollectionQuery, IEnumerable <IContact> >(It.IsAny <IGetContactCollectionQuery>()), Times.Never());
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactory_AssertExpiresWasCalledOnTokenFromTokenHelperFactory()
        {
            Mock <IRefreshableToken> refreshableTokenMock = _fixture.BuildRefreshableTokenMock();
            Controller sut = CreateSut(refreshableToken: refreshableTokenMock.Object);

            await sut.LoadContacts();

            refreshableTokenMock.Verify(m => m.Expires, Times.Once);
        }
        public async Task LoadContacts_WhenCalled_AssertGetTokenAsyncWasCalledOnTokenHelperFactory()
        {
            Controller sut = CreateSut();

            await sut.LoadContacts();

            _tokenHelperFactoryMock.Verify(m => m.GetTokenAsync <IRefreshableToken>(
                                               It.Is <TokenType>(value => value == TokenType.MicrosoftGraphToken),
                                               It.IsAny <HttpContext>()),
                                           Times.Once);
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndFilterIsWhiteSpace_AssertQueryAsyncWasCalledOnQueryBusWithGetContactCollectionQuery()
        {
            string            tokenType        = _fixture.Create <string>();
            string            accessToken      = _fixture.Create <string>();
            string            refreshToken     = _fixture.Create <string>();
            DateTime          expires          = DateTime.Now.AddMinutes(_random.Next(5, 60));
            IRefreshableToken refreshableToken = _fixture.BuildRefreshableTokenMock(tokenType, accessToken, refreshToken, expires).Object;
            Controller        sut = CreateSut(refreshableToken: refreshableToken);

            await sut.LoadContacts(" ");

            _queryBusMock.Verify(m => m.QueryAsync <IGetContactCollectionQuery, IEnumerable <IContact> >(It.Is <IGetContactCollectionQuery>(value => value != null && string.CompareOrdinal(value.TokenType, tokenType) == 0 && string.CompareOrdinal(value.AccessToken, accessToken) == 0 && string.CompareOrdinal(value.RefreshToken, refreshToken) == 0 && value.Expires == expires)), Times.Once);
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndFilterIsNotNullEmptyOrWhiteSpace_AssertQueryAsyncWasCalledOnQueryBusWithGetMatchingContactCollectionQuery()
        {
            string            tokenType        = _fixture.Create <string>();
            string            accessToken      = _fixture.Create <string>();
            string            refreshToken     = _fixture.Create <string>();
            DateTime          expires          = DateTime.Now.AddMinutes(_random.Next(5, 60));
            IRefreshableToken refreshableToken = _fixture.BuildRefreshableTokenMock(tokenType, accessToken, refreshToken, expires).Object;
            Controller        sut = CreateSut(refreshableToken: refreshableToken);

            string filter = _fixture.Create <string>();
            await sut.LoadContacts(filter);

            _queryBusMock.Verify(m => m.QueryAsync <IGetMatchingContactCollectionQuery, IEnumerable <IContact> >(It.Is <IGetMatchingContactCollectionQuery>(value => value != null && string.CompareOrdinal(value.TokenType, tokenType) == 0 && string.CompareOrdinal(value.AccessToken, accessToken) == 0 && string.CompareOrdinal(value.RefreshToken, refreshToken) == 0 && value.Expires == expires && string.CompareOrdinal(value.SearchFor, filter) == 0 && value.SearchWithinName && value.SearchWithinMailAddress && value.SearchWithinPrimaryPhone == false && value.SearchWithinSecondaryPhone == false && value.SearchWithinHomePhone && value.SearchWithinMobilePhone)), Times.Once);
        }
        public async Task LoadContacts_WhenTokenWasReturnedFromTokenHelperFactoryAndFilterIsWhiteSpace_ReturnsPartialViewResultWhereModelIsCollectionOfContactInfoViewModel()
        {
            IEnumerable <IContact> contactCollection = _fixture.CreateMany <IContact>(_random.Next(25, 50)).ToList();
            Controller             sut = CreateSut(contactCollection: contactCollection);

            PartialViewResult result = (PartialViewResult)await sut.LoadContacts(" ");

            Assert.That(result.Model, Is.TypeOf <List <ContactInfoViewModel> >());

            List <ContactInfoViewModel> contactInfoViewModelCollection = (List <ContactInfoViewModel>)result.Model;

            Assert.IsNotNull(contactInfoViewModelCollection);
            Assert.IsTrue(contactInfoViewModelCollection.All(contactInfoViewModel => contactCollection.Any(contact => string.CompareOrdinal(contact.ExternalIdentifier, contactInfoViewModel.ExternalIdentifier) == 0)));
        }