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 dffbd3e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 23 deletions.
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ nfpms:
formats:
- deb
- rpm
dependencies:
- rpm
vendor: "d-tsuji"
homepage: "https://github.com/d-tsuji/qiisync"
maintainer: "Tsuji Daishiro"
Expand Down
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
49 changes: 30 additions & 19 deletions cmd/qiisync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -72,31 +71,48 @@ var commandPost = &cli.Command{

// Receives parameters from the stdin.
sc := bufio.NewScanner(os.Stdin)
var (
title string
tag string
private bool
)

fmt.Fprintln(os.Stdout, "")
fmt.Fprintln(os.Stdout, `Please enter the "title" of the Article you want to post.`)
_ = sc.Scan()
title := sc.Text()
if title == "" {
return fmt.Errorf("title is required")
if sc.Scan() {
title = sc.Text()
if title == "" {
return fmt.Errorf("title is required")
}
}
if err := sc.Err(); err != nil {
return fmt.Errorf("an unexpected error has occurred when scanning: %w", err)
}

fmt.Fprintln(os.Stdout, "")
fmt.Fprintln(os.Stdout, `Please enter the "tag" of the Article you want to post.`)
fmt.Fprintln(os.Stdout, `Tag is like "React,redux,TypeScript" or "Go" or "Python:3.7". To specify more than one, separate them with ",".`)
_ = sc.Scan()
tag := sc.Text()
if tag == "" {
return fmt.Errorf("more than one tag is required")
if sc.Scan() {
tag = sc.Text()
if tag == "" {
return fmt.Errorf("more than one tag is required")
}
}
if err := sc.Err(); err != nil {
return fmt.Errorf("an unexpected error has occurred when scanning: %w", err)
}

fmt.Fprintln(os.Stdout, "")
fmt.Fprintln(os.Stdout, `Do you make the Article you post private? "true" is private, "false" is public.`)
_ = sc.Scan()
text := sc.Text()
private, err := strconv.ParseBool(text)
if err != nil {
return fmt.Errorf("input string (%s) could not be parsed into bool", text)
if sc.Scan() {
text := sc.Text()
private, err = strconv.ParseBool(text)
if err != nil {
return fmt.Errorf("input string (%s) could not be parsed into bool", text)
}
}
if err := sc.Err(); err != nil {
return fmt.Errorf("an unexpected error has occurred when scanning: %w", err)
}

a, err := qiisync.ArticleFromFile(filename)
Expand Down Expand Up @@ -140,11 +156,6 @@ var commandUpdate = &cli.Command{
return err
}

if !a.Private {
return errors.New("Once published, an Article cannot be made a private publication.\n" +
"\tPlease check if the Private item in the header of the Article is set to false.")
}

b := qiisync.NewBroker(conf)
_, err = b.UploadFresh(a)
if err != nil {
Expand Down

0 comments on commit dffbd3e

Please sign in to comment.