Skip to content

Commit 91e189f

Browse files
committed
add various tests for the comment endpoint
- listing comments - creating comments
1 parent 6ddc29e commit 91e189f

7 files changed

+245
-0
lines changed

tests/EndpointCommentsTest.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
use Carbon\Carbon;
4+
use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection;
5+
use FiveamCode\LaravelNotionApi\Entities\Comment;
6+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
7+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
8+
use Illuminate\Support\Facades\Http;
9+
10+
it('should throw correct exception if comment access not allowed by api when listing comments', function () {
11+
// not_found /v1/comments
12+
Http::fake([
13+
'https://api.notion.com/v1/comments?block_id=cbf6b0af-6eaa-45ca-9715-9fa147ef6b17*' => Http::response(
14+
json_decode(file_get_contents('tests/stubs/endpoints/comments/response_list_comments_403.json'), true),
15+
403,
16+
['Headers']
17+
),
18+
]);
19+
20+
$this->expectException(NotionException::class);
21+
$this->expectExceptionMessage('Insufficient permissions for this endpoint.');
22+
$this->expectExceptionCode(403);
23+
24+
\Notion::comments()->ofBlock('cbf6b0af-6eaa-45ca-9715-9fa147ef6b17')->list();
25+
});
26+
27+
it('should throw correct exception if block_id has not been found when listing comments', function () {
28+
// not_found /v1/comments
29+
Http::fake([
30+
'https://api.notion.com/v1/comments?block_id=cbf6b0af-6eaa-45ca-9715-9fa147ef6b17*' => Http::response(
31+
json_decode(file_get_contents('tests/stubs/endpoints/comments/response_list_comments_404.json'), true),
32+
404,
33+
['Headers']
34+
),
35+
]);
36+
37+
$this->expectException(NotionException::class);
38+
$this->expectExceptionMessage('Not Found');
39+
$this->expectExceptionCode(404);
40+
41+
\Notion::comments()->ofBlock('cbf6b0af-6eaa-45ca-9715-9fa147ef6b17')->list();
42+
});
43+
44+
45+
it('should fetch list of comments with an accurate representation of attributes', function () {
46+
47+
// successfull /v1/comments
48+
Http::fake([
49+
'https://api.notion.com/v1/comments?block_id=abf6b0af-6eaa-45ca-9715-9fa147ef6b17*' => Http::response(
50+
json_decode(file_get_contents('tests/stubs/endpoints/comments/response_list_comments_200.json'), true),
51+
200,
52+
['Headers']
53+
),
54+
]);
55+
56+
$commentCollection = \Notion::comments()->ofBlock('abf6b0af-6eaa-45ca-9715-9fa147ef6b17');
57+
58+
$collection = $commentCollection->asCollection();
59+
$json = $commentCollection->asJson();
60+
61+
expect($commentCollection)->toBeInstanceOf(CommentCollection::class);
62+
expect($collection)->toBeInstanceOf(\Illuminate\Support\Collection::class);
63+
expect($json)->toBeString();
64+
65+
expect($collection->count())->toBe(1);
66+
expect($collection->first())->toBeInstanceOf(Comment::class);
67+
expect($collection->first()->getObjectType())->toBe('comment');
68+
expect($collection->first()->getId())->toBe('94cc56ab-9f02-409d-9f99-1037e9fe502f');
69+
expect($collection->first()->getCreatedTime())->toEqual(Carbon::parse('2022-07-15T16:52:00.000Z')->toDateTime());
70+
expect($collection->first()->getLastEditedTime())->toEqual(Carbon::parse('2022-07-15T19:16:00.000Z')->toDateTime());
71+
expect($collection->first()->getCreatedBy()->getId())->toBe('9b15170a-9941-4297-8ee6-83fa7649a87a');
72+
expect($collection->first()->getText())->toBe('Single comment');
73+
expect($collection->first()->getRichText()->getPlainText())->toBe('Single comment');
74+
expect($collection->first()->getRichText())->toBeInstanceOf(RichText::class);
75+
expect($collection->first()->getParentId())->toBe('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d');
76+
expect($collection->first()->getParentType())->toBe('page_id');
77+
expect($collection->first()->getDiscussionId())->toBe('f1407351-36f5-4c49-a13c-49f8ba11776d');
78+
79+
expect($json)->toBeJson();
80+
});
81+
82+
83+
it('should throw correct exception if comment access not allowed by api when creating a comment', function () {
84+
// not_found /v1/comments
85+
Http::fake([
86+
'https://api.notion.com/v1/comments*' => Http::response(
87+
json_decode(file_get_contents('tests/stubs/endpoints/comments/response_create_comment_403.json'), true),
88+
403,
89+
['Headers']
90+
),
91+
]);
92+
93+
$this->expectException(NotionException::class);
94+
$this->expectExceptionMessage('Insufficient permissions for this endpoint.');
95+
$this->expectExceptionCode(403);
96+
97+
\Notion::comments()->onPage('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d')->create(Comment::create('Hello world'));
98+
});
99+
100+
it('should throw correct exception if discussion is not found with discussion_id when creating a comment', function () {
101+
// not_found (post) /v1/comments
102+
Http::fake([
103+
'https://api.notion.com/v1/comments*' => Http::response(
104+
json_decode(file_get_contents('tests/stubs/endpoints/comments/response_create_comment_404.json'), true),
105+
404,
106+
['Headers']
107+
),
108+
]);
109+
110+
$this->expectException(NotionException::class);
111+
$this->expectExceptionMessage('Could not find discussion with ID: 141216d8-bbc5-4c24-9d37-3c45d3bc15cc.');
112+
$this->expectExceptionCode(404);
113+
114+
\Notion::comments()->onDiscussion('141216d8-bbc5-4c24-9d37-3c45d3bc15cc')->create(Comment::create('Hello world'));
115+
});
116+
117+
it('successfully creates a comment within a page', function(){
118+
119+
// successfull (post) /v1/comments
120+
Http::fake([
121+
'https://api.notion.com/v1/comments*' => Http::response(
122+
json_decode(file_get_contents('tests/stubs/endpoints/comments/response_create_comment_200.json'), true),
123+
200,
124+
['Headers']
125+
),
126+
]);
127+
128+
$comment = \Notion::comments()->onPage('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d')->create(Comment::create('Hello world'));
129+
130+
expect($comment)->toBeInstanceOf(Comment::class);
131+
expect($comment->getObjectType())->toBe('comment');
132+
expect($comment->getId())->toBe('b52b8ed6-e029-4707-a671-832549c09de3');
133+
expect($comment->getCreatedTime())->toEqual(Carbon::parse('2022-07-15T20:53:00.000Z')->toDateTime());
134+
expect($comment->getLastEditedTime())->toEqual(Carbon::parse('2022-07-15T20:53:00.000Z')->toDateTime());
135+
expect($comment->getCreatedBy()->getId())->toBe('067dee40-6ebd-496f-b446-093c715fb5ec');
136+
expect($comment->getText())->toBe('Hello world');
137+
expect($comment->getRichText()->getPlainText())->toBe('Hello world');
138+
expect($comment->getRichText())->toBeInstanceOf(RichText::class);
139+
expect($comment->getParentId())->toBe('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d');
140+
expect($comment->getParentType())->toBe('page_id');
141+
expect($comment->getDiscussionId())->toBe('f1407351-36f5-4c49-a13c-49f8ba11776d');
142+
});
143+
144+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"object": "comment",
3+
"id": "b52b8ed6-e029-4707-a671-832549c09de3",
4+
"parent": {
5+
"type": "page_id",
6+
"page_id": "5c6a2821-6bb1-4a7e-b6e1-c50111515c3d"
7+
},
8+
"discussion_id": "f1407351-36f5-4c49-a13c-49f8ba11776d",
9+
"created_time": "2022-07-15T20:53:00.000Z",
10+
"last_edited_time": "2022-07-15T20:53:00.000Z",
11+
"created_by": {
12+
"object": "user",
13+
"id": "067dee40-6ebd-496f-b446-093c715fb5ec"
14+
},
15+
"rich_text": [
16+
{
17+
"type": "text",
18+
"text": {
19+
"content": "Hello world",
20+
"link": null
21+
},
22+
"annotations": {
23+
"bold": false,
24+
"italic": false,
25+
"strikethrough": false,
26+
"underline": false,
27+
"code": false,
28+
"color": "default"
29+
},
30+
"plain_text": "Hello world",
31+
"href": null
32+
}
33+
]
34+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"object": "error",
3+
"status": 403,
4+
"code": "restricted_resource",
5+
"message": "Insufficient permissions for this endpoint."
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"object": "error",
3+
"status": 404,
4+
"code": "object_not_found",
5+
"message": "Could not find discussion with ID: 141216d8-bbc5-4c24-9d37-3c45d3bc15cc. Make sure the relevant pages and databases are shared with your integration."
6+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "comment",
6+
"id": "94cc56ab-9f02-409d-9f99-1037e9fe502f",
7+
"parent": {
8+
"type": "page_id",
9+
"page_id": "5c6a2821-6bb1-4a7e-b6e1-c50111515c3d"
10+
},
11+
"discussion_id": "f1407351-36f5-4c49-a13c-49f8ba11776d",
12+
"created_time": "2022-07-15T16:52:00.000Z",
13+
"last_edited_time": "2022-07-15T19:16:00.000Z",
14+
"created_by": {
15+
"object": "user",
16+
"id": "9b15170a-9941-4297-8ee6-83fa7649a87a"
17+
},
18+
"rich_text": [
19+
{
20+
"type": "text",
21+
"text": {
22+
"content": "Single comment",
23+
"link": null
24+
},
25+
"annotations": {
26+
"bold": false,
27+
"italic": false,
28+
"strikethrough": false,
29+
"underline": false,
30+
"code": false,
31+
"color": "default"
32+
},
33+
"plain_text": "Single comment",
34+
"href": null
35+
}
36+
]
37+
}
38+
],
39+
"next_cursor": null,
40+
"has_more": false,
41+
"type": "comment",
42+
"comment": {}
43+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"object": "error",
3+
"status": 403,
4+
"code": "restricted_resource",
5+
"message": "Insufficient permissions for this endpoint."
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"object": "error",
3+
"status": 404,
4+
"code": "object_not_found",
5+
"message": "Could not find block with ID: cbf6b0af-6eaa-45ca-9715-9fa147ef6b17. Make sure the relevant pages and databases are shared with your integration."
6+
}

0 commit comments

Comments
 (0)