Skip to content

Commit

Permalink
fix: article cannot be updated from public to private
Browse files Browse the repository at this point in the history
  • Loading branch information
d-tsuji committed Apr 26, 2020
1 parent 0b700ab commit 314bd6d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,13 @@ func (b *Broker) patchArticle(body *PostItem) error {
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
}
Logf("post", "fresh article ---> %s", body.URL)

var item Item
if err := json.NewDecoder(resp.Body).Decode(&item); err != nil {
return err
}

Logf("post", "fresh article ---> %s", item.URL)
return nil
}

Expand All @@ -365,10 +371,14 @@ func (b *Broker) UploadFresh(a *Article) (bool, error) {
}

if a.Item.UpdatedAt.After(ra.Item.UpdatedAt) == false {
Logf("", "Article is not updated. remote=%s > local=%s", ra.Item.UpdatedAt, a.Item.UpdatedAt)
Logf("", "article is not updated. remote=%s > local=%s", ra.Item.UpdatedAt, a.Item.UpdatedAt)
return false, nil
}

if a.Private && !ra.Private {
return false, errors.New("once an article has been published, it cannot be privately published")
}

body := &PostItem{
Body: a.Item.Body,
Private: a.Private,
Expand Down
46 changes: 46 additions & 0 deletions broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,34 @@ func TestPatchArticleNoID(t *testing.T) {
}
}

func TestPatchInvalidResponse(t *testing.T) {
b, mux, _, teardown := setup()
t.Cleanup(func() { teardown() })

mux.HandleFunc("/api/v2/items/c686397e4a0f4f11683d", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `[{}]`)
})

err := b.patchArticle(&PostItem{
Body: "# Example",
Private: false,
Tags: []*Tag{
{
Name: "Ruby",
Versions: []string{"0.0.1"},
},
},
Title: "Example title",
ID: "c686397e4a0f4f11683d",
})
if err == nil {
t.Errorf("expected error occured if cannot decode json")
return
}
}

func Test_fetchLocalArticles(t *testing.T) {
updateAt := time.Date(2020, 4, 22, 16, 59, 59, 0, time.UTC)

Expand Down Expand Up @@ -959,6 +987,24 @@ func TestUploadFresh(t *testing.T) {
want: false,
wantErr: false,
},
{
name: "cannot_private",
localArticle: &Article{
ArticleHeader: &ArticleHeader{
ID: "c686397e4a0f4f11683d",
Title: "Update title",
Tags: "Go:1.14",
Author: "d-tsuji",
Private: true,
},
Item: &Item{
Body: "# Update Example",
UpdatedAt: time.Date(2020, 4, 23, 05, 41, 36, 0, time.UTC),
},
},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 314bd6d

Please sign in to comment.