Skip to content

Commit

Permalink
Remove faulty title param from UpdatePageParams
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
dstotijn committed Mar 21, 2022
1 parent 5af1e9a commit 1bd13b0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 94 deletions.
136 changes: 71 additions & 65 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ func TestQueryDatabase(t *testing.T) {
})
}
}

func TestCreateDatabase(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2116,10 +2117,14 @@ func TestUpdatePage(t *testing.T) {
{
name: "page props, successful response",
params: notion.UpdatePageParams{
Title: []notion.RichText{
{
Text: &notion.Text{
Content: "Foobar",
DatabasePageProperties: notion.DatabasePageProperties{
"Name": notion.DatabasePageProperty{
Title: []notion.RichText{
{
Text: &notion.Text{
Content: "Foobar",
},
},
},
},
},
Expand Down Expand Up @@ -2168,10 +2173,12 @@ func TestUpdatePage(t *testing.T) {
respStatusCode: http.StatusOK,
expPostBody: map[string]interface{}{
"properties": map[string]interface{}{
"title": []interface{}{
map[string]interface{}{
"text": map[string]interface{}{
"content": "Foobar",
"Name": map[string]interface{}{
"title": []interface{}{
map[string]interface{}{
"text": map[string]interface{}{
"content": "Foobar",
},
},
},
},
Expand Down Expand Up @@ -2306,14 +2313,9 @@ func TestUpdatePage(t *testing.T) {
expError: nil,
},
{
name: "page cover, successful response",
name: "page archived, successful response",
params: notion.UpdatePageParams{
Cover: &notion.Cover{
Type: notion.FileTypeExternal,
External: &notion.FileExternal{
URL: "https://example.com/image.png",
},
},
Archived: notion.BoolPtr(true),
},
respBody: func(_ *http.Request) io.Reader {
return strings.NewReader(
Expand All @@ -2332,7 +2334,7 @@ func TestUpdatePage(t *testing.T) {
"url": "https://example.com/image.png"
}
},
"archived": false,
"archived": true,
"url": "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c",
"properties": {
"title": {
Expand Down Expand Up @@ -2364,12 +2366,7 @@ func TestUpdatePage(t *testing.T) {
},
respStatusCode: http.StatusOK,
expPostBody: map[string]interface{}{
"cover": map[string]interface{}{
"type": "external",
"external": map[string]interface{}{
"url": "https://example.com/image.png",
},
},
"archived": true,
},
expResponse: notion.Page{
ID: "cb261dc5-6c85-4767-8585-3852382fb466",
Expand All @@ -2380,6 +2377,7 @@ func TestUpdatePage(t *testing.T) {
Type: notion.ParentTypePage,
PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7",
},
Archived: true,
Cover: &notion.Cover{
Type: notion.FileTypeExternal,
External: &notion.FileExternal{
Expand All @@ -2406,34 +2404,36 @@ func TestUpdatePage(t *testing.T) {
expError: nil,
},
{
name: "database page props, successful response",
name: "page cover, successful response",
params: notion.UpdatePageParams{
DatabasePageProperties: &notion.DatabasePageProperties{
"Name": notion.DatabasePageProperty{
Title: []notion.RichText{
{
Text: &notion.Text{
Content: "Lorem ipsum",
},
},
},
Cover: &notion.Cover{
Type: notion.FileTypeExternal,
External: &notion.FileExternal{
URL: "https://example.com/image.png",
},
},
},
respBody: func(_ *http.Request) io.Reader {
return strings.NewReader(
`{
"object": "page",
"id": "e4f419a7-f01f-4d5b-af58-ff4786a429fe",
"created_time": "2021-05-17T17:56:00.000Z",
"last_edited_time": "2021-05-22T16:24:23.007Z",
"id": "cb261dc5-6c85-4767-8585-3852382fb466",
"created_time": "2021-05-14T09:15:46.796Z",
"last_edited_time": "2021-05-22T15:54:31.116Z",
"parent": {
"type": "database_id",
"database_id": "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8"
"type": "page_id",
"page_id": "b0668f48-8d66-4733-9bdb-2f82215707f7"
},
"cover": {
"type": "external",
"external": {
"url": "https://example.com/image.png"
}
},
"archived": false,
"url": "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c",
"properties": {
"Name": {
"title": {
"id": "title",
"type": "title",
"title": [
Expand Down Expand Up @@ -2462,30 +2462,30 @@ func TestUpdatePage(t *testing.T) {
},
respStatusCode: http.StatusOK,
expPostBody: map[string]interface{}{
"properties": map[string]interface{}{
"Name": map[string]interface{}{
"title": []interface{}{
map[string]interface{}{
"text": map[string]interface{}{
"content": "Lorem ipsum",
},
},
},
"cover": map[string]interface{}{
"type": "external",
"external": map[string]interface{}{
"url": "https://example.com/image.png",
},
},
},
expResponse: notion.Page{
ID: "e4f419a7-f01f-4d5b-af58-ff4786a429fe",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-17T17:56:00.000Z"),
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T16:24:23.007Z"),
ID: "cb261dc5-6c85-4767-8585-3852382fb466",
CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:46.796Z"),
LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T15:54:31.116Z"),
URL: "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c",
Parent: notion.Parent{
Type: notion.ParentTypeDatabase,
DatabaseID: "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8",
Type: notion.ParentTypePage,
PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7",
},
Properties: notion.DatabasePageProperties{
"Name": notion.DatabasePageProperty{
ID: "title",
Type: notion.DBPropTypeTitle,
Cover: &notion.Cover{
Type: notion.FileTypeExternal,
External: &notion.FileExternal{
URL: "https://example.com/image.png",
},
},
Properties: notion.PageProperties{
Title: notion.PageTitle{
Title: []notion.RichText{
{
Type: notion.RichTextTypeText,
Expand All @@ -2506,10 +2506,14 @@ func TestUpdatePage(t *testing.T) {
{
name: "error response",
params: notion.UpdatePageParams{
Title: []notion.RichText{
{
Text: &notion.Text{
Content: "Foobar",
DatabasePageProperties: notion.DatabasePageProperties{
"Name": notion.DatabasePageProperty{
Title: []notion.RichText{
{
Text: &notion.Text{
Content: "Foobar",
},
},
},
},
},
Expand All @@ -2527,10 +2531,12 @@ func TestUpdatePage(t *testing.T) {
respStatusCode: http.StatusBadRequest,
expPostBody: map[string]interface{}{
"properties": map[string]interface{}{
"title": []interface{}{
map[string]interface{}{
"text": map[string]interface{}{
"content": "Foobar",
"Name": map[string]interface{}{
"title": []interface{}{
map[string]interface{}{
"text": map[string]interface{}{
"content": "Foobar",
},
},
},
},
Expand All @@ -2543,7 +2549,7 @@ func TestUpdatePage(t *testing.T) {
name: "missing any params",
params: notion.UpdatePageParams{},
expResponse: notion.Page{},
expError: errors.New("notion: invalid page params: at least one of database page properties, title, icon or cover is required"),
expError: errors.New("notion: invalid page params: at least one of database page properties, archived, icon or cover is required"),
},
}

Expand Down
35 changes: 6 additions & 29 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ type CreatePageParams struct {
// UpdatePageParams is used for updating a page. At least one field should have
// a non-empty value.
type UpdatePageParams struct {
DatabasePageProperties *DatabasePageProperties
Title []RichText
Icon *Icon
Cover *Cover
DatabasePageProperties DatabasePageProperties `json:"properties,omitempty"`
Archived *bool `json:"archived,omitempty"`
Icon *Icon `json:"icon,omitempty"`
Cover *Cover `json:"cover,omitempty"`
}

// PagePropItem is used for a *single* property object value, e.g. for a `rich_text`
Expand Down Expand Up @@ -287,8 +287,8 @@ func (p *Page) UnmarshalJSON(b []byte) error {

func (p UpdatePageParams) Validate() error {
// At least one of the params must be set.
if p.DatabasePageProperties == nil && p.Title == nil && p.Icon == nil && p.Cover == nil {
return errors.New("at least one of database page properties, title, icon or cover is required")
if p.DatabasePageProperties == nil && p.Archived == nil && p.Icon == nil && p.Cover == nil {
return errors.New("at least one of database page properties, archived, icon or cover is required")
}
if p.Icon != nil {
if err := p.Icon.Validate(); err != nil {
Expand All @@ -297,26 +297,3 @@ func (p UpdatePageParams) Validate() error {
}
return nil
}

func (p UpdatePageParams) MarshalJSON() ([]byte, error) {
type UpdatePageParamsDTO struct {
Properties interface{} `json:"properties,omitempty"`
Icon *Icon `json:"icon,omitempty"`
Cover *Cover `json:"cover,omitempty"`
}

dto := UpdatePageParamsDTO{
Icon: p.Icon,
Cover: p.Cover,
}

if p.DatabasePageProperties != nil {
dto.Properties = p.DatabasePageProperties
} else if p.Title != nil {
dto.Properties = PageTitle{
Title: p.Title,
}
}

return json.Marshal(dto)
}

0 comments on commit 1bd13b0

Please sign in to comment.