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 25, 2020
1 parent 0b700ab commit bba3457
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ $ brew install qiisync
### CentOS

```
$ sudo rpm -ivh https://github.com/d-tsuji/qiisync/releases/download/v0.0.1/qiisync_0.0.1_Tux-64-bit.rpm
$ sudo rpm -ivh https://github.com/d-tsuji/qiisync/releases/download/v0.0.3/qiisync_0.0.3_Tux-64-bit.rpm
```

### Debian, Ubuntu

```
$ wget https://github.com/d-tsuji/qiisync/releases/download/v0.0.1/qiisync_0.0.1_Tux-64-bit.deb
$ sudo dpkg -i qiisync_0.0.1_Tux-64-bit.deb
$ wget https://github.com/d-tsuji/qiisync/releases/download/v0.0.3/qiisync_0.0.3_Tux-64-bit.deb
$ sudo dpkg -i qiisync_0.0.3_Tux-64-bit.deb
```

### Golang
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
51 changes: 51 additions & 0 deletions broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,39 @@ func TestPatchArticleNoID(t *testing.T) {
}
}

func TestPatchInvalidResponse(t *testing.T) {
b, mux, _, teardown := setup()
t.Cleanup(func() {
teardown()
if err := os.RemoveAll(b.baseDir()); err != nil {
t.Errorf("remove tempDir: %v", err)
}
})

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 +992,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
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package qiisync

// Version is the version to be displayed in the command line help message.
const Version = "0.0.2"
const Version = "0.0.3"

0 comments on commit bba3457

Please sign in to comment.