Esempio n. 1
0
        public void ArticleClient_constructor_test()
        {
            Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object);

            httpClientFactory.Setup(p => p.Create()).Returns(() =>
            {
                return(new HttpClient());
            });

            Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>();

            baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter()));

            Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>();

            clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType);
            clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1));
            clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test");
            clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1");
            clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1");

            ArticleClient target = new ArticleClient(clientConfiguration.Object, baasicClientFactory.Object);

            target.Should().NotBeNull();
            target.Configuration.Should().NotBeNull();
        }
Esempio n. 2
0
        public void ArticleClient_UpdateAsync_test()
        {
            #region Setup

            var handler = new Mock <HttpMessageHandler>();
            handler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns((HttpRequestMessage request, CancellationToken cancellationToken) =>
            {
                HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                if (request.RequestUri.ToString().EndsWith("article"))
                {
                    Article article = new JsonFormatter().Deserialize <Article>(request.Content.ReadAsStreamAsync().Result);

                    if (article == null)
                    {
                        httpResponseMessage         = new HttpResponseMessage(HttpStatusCode.NotFound);
                        httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(""));
                    }
                    else
                    {
                        httpResponseMessage         = new HttpResponseMessage(HttpStatusCode.OK);
                        httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(new Article()
                        {
                            Slug = "Slug"
                        }));
                    }
                }
                return(Task.FromResult(httpResponseMessage));
            });

            Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object);
            httpClientFactory.Setup(p => p.Create()).Returns(() =>
            {
                return(new HttpClient(handler.Object));
            });

            Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>();
            baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter()));

            Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>();
            clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType);
            clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1));
            clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test");
            clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1");
            clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1");

            ArticleClient target = new ArticleClient(clientConfiguration.Object, baasicClientFactory.Object);

            target.Should().NotBeNull();

            #endregion Setup

            target.UpdateAsync(null).Result.Should().BeNull();
            var expected = target.UpdateAsync(new Article()
            {
                Id = Guid.NewGuid(), Slug = "Slug"
            }).Result;
            expected.Should().NotBeNull();
            expected.Slug.Should().Be("Slug");
        }
Esempio n. 3
0
        public void ArticleClient_AddTagToArticleAsync_Tag_test()
        {
            #region Setup

            Guid   articleId = Guid.NewGuid();
            string tag       = "tag";

            var handler = new Mock <HttpMessageHandler>();
            handler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns((HttpRequestMessage request, CancellationToken cancellationToken) =>
            {
                HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                string url = articleId.ToString() + "/tag/" + tag;
                if (!request.RequestUri.ToString().Contains(url))
                {
                    httpResponseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
                }
                else if (request.RequestUri.ToString().EndsWith(url))
                {
                    httpResponseMessage         = new HttpResponseMessage(HttpStatusCode.OK);
                    httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(new ArticleTagEntry()
                    {
                        Id = Guid.NewGuid(), ArticleId = articleId, TagId = Guid.NewGuid()
                    }));
                }
                return(Task.FromResult(httpResponseMessage));
            });

            Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object);
            httpClientFactory.Setup(p => p.Create()).Returns(() =>
            {
                return(new HttpClient(handler.Object));
            });

            Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>();
            baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter()));

            Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>();
            clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType);
            clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1));
            clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test");
            clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1");
            clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1");

            ArticleClient target = new ArticleClient(clientConfiguration.Object, baasicClientFactory.Object);

            target.Should().NotBeNull();

            #endregion Setup

            target.AddTagToArticleAsync(Guid.Empty, "").Result.Should().BeNull();

            target.AddTagToArticleAsync(Guid.Empty, tag).Result.Should().BeNull();

            var expected = target.AddTagToArticleAsync(articleId, tag).Result;
            expected.Should().NotBeNull();
            expected.ArticleId.Should().Be(articleId);
        }
Esempio n. 4
0
        public async Task GetByIdAsync()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await client.GetByIdAsync(1);

            await apiClient.Received().ExecuteGetAsync <Article>("articles/1");
        }
Esempio n. 5
0
        public async Task UpdateAsync_ArticleIdZero_Throw()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(async() => await client.UpdateAsync(0, new UpdateArticle()));

            await apiClient.DidNotReceive().ExecutePostAsync <object, Article>("articles/0", Arg.Any <object>());
        }
Esempio n. 6
0
        public async Task GetByPathAsync_EmptyUsername_Throw()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await client.GetByPathAsync(string.Empty, "test"));

            await apiClient.DidNotReceive().ExecuteGetAsync <Article>(Arg.Any <string>());
        }
Esempio n. 7
0
        public async Task UpdateAsync()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await client.UpdateAsync(1, new UpdateArticle());

            await apiClient.Received().ExecutePutAsync <object, Article>("articles/1", Arg.Any <object>());
        }
Esempio n. 8
0
        public async Task CreateAsync_RequestNull_Throw()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await client.CreateAsync(null));

            await apiClient.DidNotReceive().ExecutePostAsync <object, Article>("articles", Arg.Any <object>());
        }
Esempio n. 9
0
        public async Task GetMyPublishedAsync()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await client.GetMyPublishedAsync();

            await apiClient.Received().ExecutePaginationGetAsync <MyArticle>("articles/me/published", Arg.Any <PageQueryOption>());
        }
Esempio n. 10
0
        public async Task GetByIdAsync_ArticleIdZero_Throw()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(async() => await client.GetByIdAsync(0));

            await apiClient.DidNotReceive().ExecuteGetAsync <Article>(Arg.Any <string>());
        }
Esempio n. 11
0
        public void ArticleClient_PublishAsync_test()
        {
            #region Setup

            var handler = new Mock <HttpMessageHandler>();
            handler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns((HttpRequestMessage request, CancellationToken cancellationToken) =>
            {
                HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                if (request.RequestUri.ToString().EndsWith("publish/NA"))
                {
                    httpResponseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
                }
                else if (request.RequestUri.ToString().EndsWith("publish/Slug"))
                {
                    httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
                }
                return(Task.FromResult(httpResponseMessage));
            });

            Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object);
            httpClientFactory.Setup(p => p.Create()).Returns(() =>
            {
                return(new HttpClient(handler.Object));
            });

            Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>();
            baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter()));

            Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>();
            clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType);
            clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1));
            clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test");
            clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1");
            clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1");

            ArticleClient target = new ArticleClient(clientConfiguration.Object, baasicClientFactory.Object);

            target.Should().NotBeNull();

            #endregion Setup

            target.PublishAsync("NA", new ArticleOptions {
                ArticleUrl = "NA"
            }).Result.Should().BeFalse();
            target.PublishAsync("Slug", new ArticleOptions {
                ArticleUrl = "Slug"
            }).Result.Should().BeTrue();
        }
Esempio n. 12
0
        public async Task GetAsync_TopZero_Throw()
        {
            var apiClient = Substitute.For <IApiConnection>();
            var client    = new ArticleClient(apiClient);

            await client.GetAsync();

            await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(async() => await client.GetAsync(option =>
            {
                option.Top = 0;
            }));

            await apiClient.DidNotReceive().ExecutePaginationGetAsync <Article>("articles", new ArticleQueryOption
            {
                Top = 0
            });
        }
Esempio n. 13
0
        /// <summary>
        /// API client
        /// </summary>
        /// <remarks>
        /// See the <a href="https://docs.dev.to/api/#section/Authentication/api_key">Authentication</a> for more information
        /// </remarks>
        /// <param name="apiUrl">API connection url</param>
        /// <param name="token">API key</param>
        public DevToClient(string apiUrl, string token)
        {
            var restClient = new RestClient(apiUrl);

            restClient.AddDefaultHeader("api-key", token);
            restClient.AddDefaultHeader("User-Agent", "DevToAPI-client-dotnet");
            var apiConnection = new ApiConnection(restClient);

            AdminConfigurations = new AdminConfigurationClient(apiConnection);
            Articles            = new ArticleClient(apiConnection);
            Comments            = new CommentClient(apiConnection);
            Followers           = new FollowerClient(apiConnection);
            Follows             = new FollowClient(apiConnection);
            Listings            = new ListingClient(apiConnection);
            Organizations       = new OrganizationClient(apiConnection);
            PodcastEpisodes     = new PodcastEpisodeClient(apiConnection);
            ReadingLists        = new ReadingListClient(apiConnection);
            Tags          = new TagClient(apiConnection);
            Users         = new UserClient(apiConnection);
            Videos        = new VideoClient(apiConnection);
            Webhooks      = new WebhookClient(apiConnection);
            ProfileImages = new ProfileImageClient(apiConnection);
        }
Esempio n. 14
0
        public void ArticleClient_GetTagEntriesAsync_find_test()
        {
            #region Setup

            Guid articleId  = Guid.NewGuid();
            Guid tagId      = Guid.NewGuid();
            Guid tagEntryId = Guid.NewGuid();

            var handler = new Mock <HttpMessageHandler>();
            handler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns((HttpRequestMessage request, CancellationToken cancellationToken) =>
            {
                HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                if (request.RequestUri.ToString().EndsWith("NA"))
                {
                    httpResponseMessage         = new HttpResponseMessage(HttpStatusCode.NotFound);
                    httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(new CollectionModelBase <ArticleTagEntry>()));
                }
                else if (request.RequestUri.ToString().EndsWith("Tag"))
                {
                    httpResponseMessage         = new HttpResponseMessage(HttpStatusCode.OK);
                    httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(new CollectionModelBase <ArticleTagEntry>()
                    {
                        Item = new List <ArticleTagEntry>()
                        {
                            new ArticleTagEntry()
                            {
                                Id = tagEntryId, ArticleId = articleId, TagId = tagId
                            }
                        }
                    }));
                }
                return(Task.FromResult(httpResponseMessage));
            });

            Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object);
            httpClientFactory.Setup(p => p.Create()).Returns(() =>
            {
                return(new HttpClient(handler.Object));
            });

            Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>();
            baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter()));

            Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>();
            clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType);
            clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1));
            clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test");
            clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1");
            clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1");

            ArticleClient target = new ArticleClient(clientConfiguration.Object, baasicClientFactory.Object);

            target.Should().NotBeNull();

            #endregion Setup

            var expected = target.FindTagEntriesAsync(articleId, "NA", 1, 10, "", "").Result;
            expected.Should().NotBeNull();
            expected.Item.Should().NotBeNull();
            expected.Item.Count.Should().Be(0);

            expected = target.FindTagEntriesAsync(articleId, "Tag", 1, 10, "", "").Result;
            expected.Should().NotBeNull();
            expected.Item.Should().NotBeNull();
            expected.Item.First().TagId.Should().Be(tagId);
        }