示例#1
0
        public void ItemEqualityChecks()
        {
            PocketItem item1 = new PocketItem()
            {
                ID = "12872"
            };
            PocketItem item2 = new PocketItem()
            {
                ID = "12872"
            };
            PocketItem item3 = new PocketItem()
            {
                ID = "12800"
            };
            PocketArticle article = new PocketArticle();

            Assert.True(item1.Equals(item2));
            Assert.False(item1.Equals(item3));
            Assert.False(item1.Equals(null));

            Assert.True(item1 == item2);
            Assert.False(item1 == item3);
            Assert.False(item1 == null);

            Assert.False(item1 != item2);
            Assert.True(item1 != item3);
            Assert.True(item1 != null);

            Assert.False(item1.Equals(article));

            Assert.True(new List <PocketItem>()
            {
                item1
            }.IndexOf(item2) > -1);
        }
        public void Setup()
        {
            _article = new PocketArticle(ArticleId, ArticleUrl, ArticleTitle);

            _mockService = new Mock <IPocketService>();
            _mockService.Setup(s => s.GenerateAuthUrlAsync(It.IsAny <Uri>(), It.IsAny <Uri>())).Returns(Task.FromResult(PocketUrl));

            _mockUrlHelper = new Mock <IUrlHelper>();
            _mockUrlHelper.Setup(s => s.Action(It.Is <UrlActionContext>(c => c.Action == "CompleteAuth"))).Returns(ActionUrl.ToString());
            _mockUrlHelper.Setup(s => s.Action(It.Is <UrlActionContext>(c => c.Action == "DeleteArticleAsync"))).Returns(DeleteUrl);
            _mockUrlHelper.Setup(s => s.Action(It.Is <UrlActionContext>(c => c.Action == "ArchiveArticleAsync"))).Returns(ArchiveUrl);

            _account = new PocketAccount {
                RedirectUrl = CallerUrl, AccessToken = PocketAccessToken
            };
            _mockService.Setup(s => s.UpgradeRequestTokenAsync(AccessToken)).ReturnsAsync(UrlWithAccessToken);
            _mockService.Setup(s => s.GetNextArticleAsync(AccessToken)).ReturnsAsync(_article);

            _controller = new PocketController(Mock.Of <ILogger <PocketController> >(), _mockService.Object)
            {
                Url = _mockUrlHelper.Object,
                // Set a HttpContext so that the Request property is not null when tests are running
                ControllerContext = { HttpContext = new DefaultHttpContext() }
            };
        }
 public static Article Map(PocketArticle pocketArticle)
 {
     return(new Article()
     {
         Title = pocketArticle.Title,
         Link = pocketArticle.Uri.ToString()
     });
 }
        public static ReaderItem ToReaderItem(this PocketArticle article)
        {
            var result = new ReaderItem
            {
                Id          = article.ID ?? string.Empty,
                Source      = SourceProvider.Pocket,
                Author      = article.Authors == null ? string.Empty : string.Join(", ", article.Authors) ?? string.Empty,
                Title       = article.Title ?? string.Empty,
                Url         = article.Uri.AbsoluteUri ?? string.Empty,
                Excerpt     = string.Empty,
                Text        = article.Content.StripHtmlTags() ?? string.Empty,
                CreatedDate = article.PublishedTime ?? DateTime.Now
            };

            result.WordCount = string.IsNullOrEmpty(result.Text) ? 0 : result.Text.Split(new[] { ' ' }).Length;
            return(result);
        }
        public void Setup()
        {
            _accountId         = Guid.NewGuid();
            _emailUserId       = Guid.NewGuid();
            _code              = new PocketRequestCode(BaseUri, RedirectUri, RequestCode);
            _accessTokenResult = new PocketAccessToken(PocketUserName, PocketAccessToken);
            _account           = new PocketAccount
            {
                Id           = _accountId,
                RequestToken = RequestCode,
                AccessToken  = PocketAccessToken,
                RedirectUrl  = CallerCallbackUrl.ToString()
            };
            _article = new PocketArticle(ArticleId, ArticleUrl, ArticleTitle);


            _mockRepo = new Mock <IPocketAccountsRepository>();
            _mockRepo.Setup(r => r.InsertAsync(It.IsAny <PocketAccount>())).ReturnsAsync((PocketAccount account) => {
                account.Id = _accountId;
                return(account);
            });
            _mockRepo.Setup(r => r.FindByIdAsync(_accountId)).ReturnsAsync(_account);

            _mockClient = new Mock <IPocketClient>();
            _mockClient.Setup(c => c.CreateRequestCodeAsync(It.IsAny <Uri>())).ReturnsAsync(_code);
            _mockClient.Setup(c => c.CreateAccessTokenAsync(RequestCode)).ReturnsAsync(_accessTokenResult);
            _mockClient.Setup(c => c.GetRandomArticleAsync(PocketAccessToken, 200)).ReturnsAsync(_article);

            _mockDataProtector = new Mock <IDataProtector>();
            _mockDataProtector.Setup(p => p.Protect(It.IsAny <byte[]>())).Returns(AccessTokenBytes);
            _mockDataProtector.Setup(p => p.Unprotect(It.IsAny <byte[]>())).Returns(Encoding.UTF8.GetBytes(_accountId.ToString()));

            _mockDataProtectionProvider = new Mock <IDataProtectionProvider>();
            _mockDataProtectionProvider.Setup(p => p.CreateProtector(It.IsAny <string>())).Returns(_mockDataProtector.Object);

            _service = new PocketService(_mockRepo.Object, _mockClient.Object, _mockDataProtectionProvider.Object);
        }
示例#6
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var list          = new List <IPocketArticle>();
            var pocketArticle = new PocketArticle();

            while (reader.Read())
            {
                if (reader.Value != null)
                {
                    string[] strArray = reader.Path.Split('.');

                    switch (strArray[strArray.Count() - 1])
                    {
                    case "item_id":
                        pocketArticle.ItemId = reader.ReadAsString();
                        break;

                    case "resolved_id":
                        pocketArticle.ResolvedId = reader.ReadAsString();
                        break;

                    case "given_url":
                        pocketArticle.GivenUrl = reader.ReadAsString();
                        break;

                    case "given_title":
                        pocketArticle.GivenTitle = reader.ReadAsString();
                        break;

                    case "favorite":
                        pocketArticle.Favorite = reader.ReadAsString();
                        break;

                    case "status":
                        pocketArticle.Status = reader.ReadAsString();
                        break;

                    case "time_added":
                        pocketArticle.TimeAdded = reader.ReadAsString();
                        break;

                    case "time_updated":
                        pocketArticle.TimeUpdated = reader.ReadAsString();
                        break;

                    case "time_read":
                        pocketArticle.TimeRead = reader.ReadAsString();
                        break;

                    case "time_favorited":
                        pocketArticle.TimeFavorited = reader.ReadAsString();
                        break;

                    case "sort_id":
                        pocketArticle.SortId = reader.ReadAsInt32().GetValueOrDefault();
                        break;

                    case "resolved_title":
                        pocketArticle.ResolvedTitle = reader.ReadAsString();
                        break;

                    case "resolved_url":
                        pocketArticle.ResolvedUrl = reader.ReadAsString();
                        break;

                    case "excerpt":
                        pocketArticle.Excerpt = reader.ReadAsString();
                        break;

                    case "is_article":
                        pocketArticle.IsArticle = reader.ReadAsString();
                        break;

                    case "is_index":
                        pocketArticle.IsIndex = reader.ReadAsString();
                        break;

                    case "has_video":
                        pocketArticle.HasVideo = reader.ReadAsString();
                        break;

                    case "has_image":
                        pocketArticle.HasImage = reader.ReadAsString();
                        break;

                    case "word_count":
                        pocketArticle.WordCount = reader.ReadAsString();
                        break;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(pocketArticle.ItemId))
                    {
                        list.Add(pocketArticle);
                    }
                    pocketArticle = new PocketArticle();
                }
            }
            return(list);
        }