Skip to content

Commit e62153e

Browse files
Merge pull request #67 from notion-dotnet/tests/66-add-tests-for-pages-endpoints
Add tests for pages endpoints ✅
2 parents 7ec09bc + 22f6294 commit e62153e

File tree

5 files changed

+190
-2
lines changed

5 files changed

+190
-2
lines changed

Test/Notion.UnitTests/ApiTestBase.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,23 @@ protected IRequestBuilder CreateGetRequestBuilder(string path)
4848
.WithHeader("Authorization", $"Bearer {ClientOptions.AuthToken}", MatchBehaviour.AcceptOnMatch)
4949
.WithHeader("Notion-Version", Constants.DEFAULT_NOTION_VERSION, MatchBehaviour.AcceptOnMatch);
5050
}
51+
52+
protected IRequestBuilder CreatePostRequestBuilder(string path)
53+
{
54+
return Request.Create()
55+
.WithPath(path)
56+
.UsingPost()
57+
.WithHeader("Authorization", $"Bearer {ClientOptions.AuthToken}", MatchBehaviour.AcceptOnMatch)
58+
.WithHeader("Notion-Version", Constants.DEFAULT_NOTION_VERSION, MatchBehaviour.AcceptOnMatch);
59+
}
60+
61+
protected IRequestBuilder CreatePatchRequestBuilder(string path)
62+
{
63+
return Request.Create()
64+
.WithPath(path)
65+
.UsingPatch()
66+
.WithHeader("Authorization", $"Bearer {ClientOptions.AuthToken}", MatchBehaviour.AcceptOnMatch)
67+
.WithHeader("Notion-Version", Constants.DEFAULT_NOTION_VERSION, MatchBehaviour.AcceptOnMatch);
68+
}
5169
}
5270
}

Test/Notion.UnitTests/Notion.UnitTests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="FluentAssertions" Version="5.10.3" />
1011
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
1112
<PackageReference Include="Microsoft.VisualStudio.VsixColorCompiler" Version="16.0.0" />
1213
<PackageReference Include="WireMock.Net" Version="1.4.19" />
@@ -26,9 +27,15 @@
2627
</ItemGroup>
2728

2829
<ItemGroup>
30+
<None Update="data\pages\CreatePageResponse.json">
31+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
32+
</None>
2933
<None Update="data\pages\PageObjectShouldHaveUrlPropertyResponse.json">
3034
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3135
</None>
36+
<None Update="data\pages\UpdatePagePropertiesResponse.json">
37+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
38+
</None>
3239
</ItemGroup>
3340

3441
</Project>

Test/Notion.UnitTests/PagesClientTests.cs

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
24
using System.Threading.Tasks;
5+
using FluentAssertions;
36
using Notion.Client;
47
using WireMock.ResponseBuilders;
58
using Xunit;
@@ -15,6 +18,95 @@ public PagesClientTests()
1518
_client = new PagesClient(new RestClient(ClientOptions));
1619
}
1720

21+
[Fact]
22+
public async Task RetrieveAsync()
23+
{
24+
var pageId = "251d2b5f-268c-4de2-afe9-c71ff92ca95c";
25+
var path = ApiEndpoints.PagesApiUrls.Retrieve(pageId);
26+
var jsonData = await File.ReadAllTextAsync("data/pages/PageObjectShouldHaveUrlPropertyResponse.json");
27+
28+
Server.Given(CreateGetRequestBuilder(path))
29+
.RespondWith(
30+
Response.Create()
31+
.WithStatusCode(200)
32+
.WithBody(jsonData)
33+
);
34+
35+
var page = await _client.RetrieveAsync(pageId);
36+
37+
page.Url.Should().Be("https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c");
38+
page.Id.Should().Be(pageId);
39+
page.Parent.Type.Should().Be(ParentType.DatabaseId);
40+
((DatabaseParent)page.Parent).DatabaseId.Should().Be("48f8fee9-cd79-4180-bc2f-ec0398253067");
41+
page.IsArchived.Should().BeFalse();
42+
}
43+
44+
[Fact]
45+
public async Task CreateAsync()
46+
{
47+
var path = ApiEndpoints.PagesApiUrls.Create();
48+
49+
var jsonData = await File.ReadAllTextAsync("data/pages/CreatePageResponse.json");
50+
51+
Server.Given(CreatePostRequestBuilder(path))
52+
.RespondWith(
53+
Response.Create()
54+
.WithStatusCode(200)
55+
.WithBody(jsonData)
56+
);
57+
58+
var newPage = new NewPage();
59+
newPage.AddProperty("Name", new TitlePropertyValue()
60+
{
61+
Title = new List<RichTextBase>()
62+
{
63+
new RichTextText()
64+
{
65+
Text = new Text
66+
{
67+
Content = "Test"
68+
}
69+
}
70+
}
71+
});
72+
73+
var page = await _client.CreateAsync(newPage);
74+
75+
page.Id.Should().NotBeNullOrEmpty();
76+
page.Url.Should().NotBeNullOrEmpty();
77+
page.Properties.Should().HaveCount(1);
78+
page.Properties.First().Key.Should().Be("Name");
79+
page.IsArchived.Should().BeFalse();
80+
}
81+
82+
[Fact]
83+
public async Task UpdatePropertiesAsync()
84+
{
85+
var pageId = "251d2b5f-268c-4de2-afe9-c71ff92ca95c";
86+
var path = ApiEndpoints.PagesApiUrls.UpdateProperties(pageId);
87+
88+
var jsonData = await File.ReadAllTextAsync("data/pages/UpdatePagePropertiesResponse.json");
89+
90+
Server.Given(CreatePatchRequestBuilder(path))
91+
.RespondWith(
92+
Response.Create()
93+
.WithStatusCode(200)
94+
.WithBody(jsonData)
95+
);
96+
97+
var updatedProperties = new Dictionary<string, PropertyValue>()
98+
{
99+
{ "In stock", new CheckboxPropertyValue() { Checkbox = true } }
100+
};
101+
102+
var page = await _client.UpdatePropertiesAsync(pageId, updatedProperties);
103+
104+
page.Id.Should().Be(pageId);
105+
page.Properties.Should().HaveCount(2);
106+
var updatedProperty = page.Properties.First(x => x.Key == "In stock");
107+
((CheckboxPropertyValue)updatedProperty.Value).Checkbox.Should().BeTrue();
108+
}
109+
18110
[Fact]
19111
public async Task PageObjectShouldHaveUrlProperty()
20112
{
@@ -31,7 +123,7 @@ public async Task PageObjectShouldHaveUrlProperty()
31123

32124
var page = await _client.RetrieveAsync(pageId);
33125

34-
Assert.Equal("https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", page.Url);
126+
page.Url.Should().Be("https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c");
35127
}
36128
}
37129
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"object": "page",
3+
"id": "251d2b5f-268c-4de2-afe9-c71ff92ca95c",
4+
"created_time": "2020-03-17T19:10:04.968Z",
5+
"last_edited_time": "2020-03-17T21:49:37.913Z",
6+
"archived": false,
7+
"url": "https://www.notion.so/Test-251d2b5f268c4de2afe9c71ff92ca95c",
8+
"properties": {
9+
"Name": {
10+
"id": "title",
11+
"type": "title",
12+
"title": [
13+
{
14+
"type": "text",
15+
"text": {
16+
"content": "Test",
17+
"link": null
18+
},
19+
"annotations": {
20+
"bold": false,
21+
"italic": false,
22+
"strikethrough": false,
23+
"underline": false,
24+
"code": false,
25+
"color": "default"
26+
},
27+
"plain_text": "Test",
28+
"href": null
29+
}
30+
]
31+
}
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"object": "page",
3+
"id": "251d2b5f-268c-4de2-afe9-c71ff92ca95c",
4+
"created_time": "2020-03-17T19:10:04.968Z",
5+
"last_edited_time": "2020-03-17T21:49:37.913Z",
6+
"archived": false,
7+
"url": "https://www.notion.so/Test-251d2b5f268c4de2afe9c71ff92ca95c",
8+
"properties": {
9+
"In stock": {
10+
"id": "{>U;",
11+
"type": "checkbox",
12+
"checkbox": true
13+
},
14+
"Name": {
15+
"id": "title",
16+
"type": "title",
17+
"title": [
18+
{
19+
"type": "text",
20+
"text": {
21+
"content": "Test",
22+
"link": null
23+
},
24+
"annotations": {
25+
"bold": false,
26+
"italic": false,
27+
"strikethrough": false,
28+
"underline": false,
29+
"code": false,
30+
"color": "default"
31+
},
32+
"plain_text": "Test",
33+
"href": null
34+
}
35+
]
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)