From bf3fcdf6752a71090f474475803097a93a4172b3 Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Wed, 4 Aug 2021 21:44:21 +0530 Subject: [PATCH] Add url property in Page object --- Src/Notion.Client/Constants.cs | 5 +- Src/Notion.Client/Models/Page/Page.cs | 2 + Test/Notion.UnitTests/ApiTestBase.cs | 52 +++++++++++++++++++ Test/Notion.UnitTests/Notion.UnitTests.csproj | 8 +++ Test/Notion.UnitTests/PagesClientTests.cs | 37 +++++++++++++ ...geObjectShouldHaveUrlPropertyResponse.json | 37 +++++++++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 Test/Notion.UnitTests/ApiTestBase.cs create mode 100644 Test/Notion.UnitTests/PagesClientTests.cs create mode 100644 Test/Notion.UnitTests/data/pages/PageObjectShouldHaveUrlPropertyResponse.json diff --git a/Src/Notion.Client/Constants.cs b/Src/Notion.Client/Constants.cs index 209e3b91..b2a413b4 100644 --- a/Src/Notion.Client/Constants.cs +++ b/Src/Notion.Client/Constants.cs @@ -1,4 +1,7 @@ -namespace Notion.Client +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Notion.UnitTests")] +namespace Notion.Client { internal class Constants { diff --git a/Src/Notion.Client/Models/Page/Page.cs b/Src/Notion.Client/Models/Page/Page.cs index 58f23b34..23f6acf2 100644 --- a/Src/Notion.Client/Models/Page/Page.cs +++ b/Src/Notion.Client/Models/Page/Page.cs @@ -20,5 +20,7 @@ public class Page : IObject public bool IsArchived { get; set; } public IDictionary Properties { get; set; } + + public string Url { get; set; } } } diff --git a/Test/Notion.UnitTests/ApiTestBase.cs b/Test/Notion.UnitTests/ApiTestBase.cs new file mode 100644 index 00000000..71f54063 --- /dev/null +++ b/Test/Notion.UnitTests/ApiTestBase.cs @@ -0,0 +1,52 @@ +using System; +using System.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using Notion.Client; +using WireMock.Matchers; +using WireMock.RequestBuilders; +using WireMock.Server; + +namespace Notion.UnitTests +{ + public class ApiTestBase : IDisposable + { + protected readonly WireMockServer Server; + + protected static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings() + { + Formatting = Newtonsoft.Json.Formatting.Indented, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new CamelCaseNamingStrategy() + } + }; + + protected readonly ClientOptions ClientOptions; + + protected ApiTestBase() + { + Server = WireMockServer.Start(); + ClientOptions = new ClientOptions() + { + BaseUrl = Server.Urls.First(), + AuthToken = "" + }; + } + + public void Dispose() + { + Server.Stop(); + Server.Dispose(); + } + + protected IRequestBuilder CreateGetRequestBuilder(string path) + { + return Request.Create() + .WithPath(path) + .UsingGet() + .WithHeader("Authorization", $"Bearer {ClientOptions.AuthToken}", MatchBehaviour.AcceptOnMatch) + .WithHeader("Notion-Version", Constants.DEFAULT_NOTION_VERSION, MatchBehaviour.AcceptOnMatch); + } + } +} diff --git a/Test/Notion.UnitTests/Notion.UnitTests.csproj b/Test/Notion.UnitTests/Notion.UnitTests.csproj index 010c0084..9bb4be89 100644 --- a/Test/Notion.UnitTests/Notion.UnitTests.csproj +++ b/Test/Notion.UnitTests/Notion.UnitTests.csproj @@ -8,6 +8,8 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -23,4 +25,10 @@ + + + Always + + + diff --git a/Test/Notion.UnitTests/PagesClientTests.cs b/Test/Notion.UnitTests/PagesClientTests.cs new file mode 100644 index 00000000..a3a5b83d --- /dev/null +++ b/Test/Notion.UnitTests/PagesClientTests.cs @@ -0,0 +1,37 @@ +using System.IO; +using System.Threading.Tasks; +using Notion.Client; +using WireMock.ResponseBuilders; +using Xunit; + +namespace Notion.UnitTests +{ + public class PagesClientTests : ApiTestBase + { + private readonly IPagesClient _client; + + public PagesClientTests() + { + _client = new PagesClient(new RestClient(ClientOptions)); + } + + [Fact] + public async Task PageObjectShouldHaveUrlProperty() + { + var pageId = "251d2b5f-268c-4de2-afe9-c71ff92ca95c"; + var path = ApiEndpoints.PagesApiUrls.Retrieve(pageId); + var jsonData = await File.ReadAllTextAsync("data/pages/PageObjectShouldHaveUrlPropertyResponse.json"); + + Server.Given(CreateGetRequestBuilder(path)) + .RespondWith( + Response.Create() + .WithStatusCode(200) + .WithBody(jsonData) + ); + + var page = await _client.RetrieveAsync(pageId); + + Assert.Equal("https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", page.Url); + } + } +} diff --git a/Test/Notion.UnitTests/data/pages/PageObjectShouldHaveUrlPropertyResponse.json b/Test/Notion.UnitTests/data/pages/PageObjectShouldHaveUrlPropertyResponse.json new file mode 100644 index 00000000..8f9d45ab --- /dev/null +++ b/Test/Notion.UnitTests/data/pages/PageObjectShouldHaveUrlPropertyResponse.json @@ -0,0 +1,37 @@ +{ + "object": "page", + "id": "251d2b5f-268c-4de2-afe9-c71ff92ca95c", + "created_time": "2020-03-17T19:10:04.968Z", + "last_edited_time": "2020-03-17T21:49:37.913Z", + "parent": { + "type": "database_id", + "database_id": "48f8fee9-cd79-4180-bc2f-ec0398253067" + }, + "archived": false, + "url": "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", + "properties": { + "Name": { + "id": "title", + "type": "title", + "title": [ + { + "type": "text", + "text": { + "content": "Avocado", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Avocado", + "href": null + } + ] + } + } +}