func TestPostSaveDuplicationWithID(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() testutil.ShouldExistFile(t, 0) func() { post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post.ID = "abcdefghijklmnopqrst" err := post.Save(nil) if err != nil { t.Fatal(err) } }() func() { post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post.ID = "abcdefghijklmnopqrst" err := post.Save(nil) if err != nil { t.Fatal(err) } }() testutil.ShouldExistFile(t, 1) }
func TestPostCreateWithWrongResponseBody(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() mux := http.NewServeMux() mux.HandleFunc("/api/v2/items", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Non JSON format") }) server := httptest.NewServer(mux) defer server.Close() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { t.Fatal(err) } client := api.NewClient(func(subDomain, path string) (url string) { url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path) return }, inf) post := model.NewPost("Example Title", nil, nil) err = post.Create(client, model.CreationOptions{}) if err == nil { t.Fatal("error should occur") } }
func TestPostDeleteWithNoServer(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() mux := http.NewServeMux() server := httptest.NewServer(mux) defer server.Close() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { t.Fatal(err) } client := api.NewClient(func(subDomain, path string) (url string) { url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path) return }, inf) testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.ID = "abcdefghijklmnopqrst" err = post.Delete(client) if err == nil { t.Fatal("error should occur") } testutil.ShouldExistFile(t, 0) }
func TestPostEncodeWithNewPost(t *testing.T) { post := model.NewPost("Example title", &model.Time{time.Date(2016, 2, 2, 6, 30, 46, 0, time.UTC)}, nil) post.ID = "4bd431809afb1bb99e4f" post.URL = "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f" buf := bytes.NewBuffer([]byte{}) err := post.Encode(buf) if err != nil { t.Fatal(err) } actual := string(buf.Bytes()) expected := `<!-- id: 4bd431809afb1bb99e4f url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f created_at: 2016-02-02T15:30:46+09:00 updated_at: 2016-02-02T15:30:46+09:00 private: false coediting: false tags: [] team: null --> # Example title ` if expected != actual { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }
func TestPostCreateWithNoServer(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() mux := http.NewServeMux() server := httptest.NewServer(mux) defer server.Close() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { t.Fatal(err) } client := api.NewClient(func(subDomain, path string) (url string) { url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path) return }, inf) testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", nil, nil) err = post.Create(client, model.CreationOptions{}) if err == nil { t.Fatal("error should occur") } testutil.ShouldExistFile(t, 0) }
func TestPostDeleteWithWrongResponseBody(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() mux := http.NewServeMux() mux.HandleFunc("/api/v2/items/abcdefghijklmnopqrst", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Non JSON format") }) server := httptest.NewServer(mux) defer server.Close() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { t.Fatal(err) } client := api.NewClient(func(subDomain, path string) (url string) { url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path) return }, inf) post := model.NewPost("Example Title", nil, nil) post.ID = "abcdefghijklmnopqrst" err = post.Delete(client) // Don't parse response body from DELETE method if err != nil { t.Fatal(err) } }
func TestNewPost(t *testing.T) { post := model.NewPost("Example Title", nil, nil) if post.Title != "Example Title" { t.Errorf("wrong Title: %s", post.Title) } if !post.CreatedAt.Equal(post.UpdatedAt.Time) { t.Errorf("wrong CreatedAt: %s", post.CreatedAt) } }
func BenchmarkPostsSave(b *testing.B) { testutil.CleanUp() defer testutil.CleanUp() var posts model.Posts for i := 0; i < b.N; i++ { posts = append(posts, model.NewPost(fmt.Sprintf("Example Title %d", i), nil, nil)) } b.ResetTimer() err := posts.Save() if err != nil { b.Fatal(err) } }
// GenerateFile generates markdown file at current working directory. func (r GenerateFileRunner) Run(c api.Client, o GlobalOptions, w io.Writer) (err error) { var team *model.Team if *r.Team != "" { team = &model.Team{ ID: *r.Team, } } post := model.NewPost(*r.Title, nil, team) err = post.Save(nil) if err != nil { return } _, err = fmt.Fprintf(w, "%s\n", post.Path) return }
func TestPostSaveWithPath(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post.ID = "abcdefghijklmnopqrst" post.Path = "foo/bar.md" err := post.Save(nil) if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 1) func() { a, err := ioutil.ReadFile("foo/bar.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }
func TestPostUpdateWithEmptyID(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { log.Fatal(err) } client := api.NewClient(nil, inf) testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", nil, nil) err = post.Update(client) err, ok := err.(model.EmptyIDError) if !ok { t.Fatal("empty ID error should occur") } testutil.ShouldExistFile(t, 0) }
func TestPostsSave(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() post0 := model.NewPost("Example Title 0", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post1 := model.NewPost("Example Title 1", &model.Time{time.Date(2016, 2, 1, 5, 21, 49, 0, time.UTC)}, nil) posts := model.Posts{post0, post1} err := posts.Save() if err != nil { t.Fatal(err) } func() { a, err := ioutil.ReadFile("mine/2015/11/28/Example Title 0.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: "" url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title 0 ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() func() { a, err := ioutil.ReadFile("mine/2016/02/01/Example Title 1.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: "" url: "" created_at: 2016-02-01T14:21:49+09:00 updated_at: 2016-02-01T14:21:49+09:00 private: false coediting: false tags: [] team: null --> # Example Title 1 ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }
func TestPostUpdateInTeam(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() mux := http.NewServeMux() mux.HandleFunc("/api/v2/items/abcdefghijklmnopqrst", func(w http.ResponseWriter, r *http.Request) { if r.Method != "PATCH" { w.WriteHeader(405) b, _ := json.Marshal(api.ResponseError{"method_not_allowed", "Method Not Allowed"}) w.Write(b) return } defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) if err != nil { testutil.ResponseError(w, 500, err) return } if string(b) == "" { testutil.ResponseAPIError(w, 500, api.ResponseError{ Type: "fatal", Message: "empty body", }) return } var post model.Post err = json.Unmarshal(b, &post) if err != nil { testutil.ResponseError(w, 500, err) return } post.UpdatedAt = model.Time{Time: time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)} b, err = json.Marshal(post) if err != nil { testutil.ResponseError(w, 500, err) return } _, err = w.Write(b) if err != nil { testutil.ResponseError(w, 500, err) return } }) server := httptest.NewServer(mux) defer server.Close() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { t.Fatal(err) } client := api.NewClient(func(subDomain, path string) (url string) { if subDomain != "increments" { t.Fatalf("wrong sub domain: %s", subDomain) return } url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path) return }, inf) testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, &model.Team{Active: true, ID: "increments", Name: "Increments Inc."}) post.ID = "abcdefghijklmnopqrst" prevPath := post.Path if err != nil { t.Fatal(err) } err = post.Update(client) if err != nil { t.Fatal(err) } postPath := post.Path if err != nil { t.Fatal(err) } if postPath != prevPath { t.Errorf("wrong path: expected %s, but actual %s", prevPath, postPath) } if !post.UpdatedAt.Equal(time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)) { t.Errorf("wrong UpdatedAt: %s", post.UpdatedAt) } testutil.ShouldExistFile(t, 0) }
func TestPostCreateWithTweetAndGist(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() mux := http.NewServeMux() mux.HandleFunc("/api/v2/items", func(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { w.WriteHeader(405) b, _ := json.Marshal(api.ResponseError{"method_not_allowed", "Method Not Allowed"}) w.Write(b) return } defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) if err != nil { testutil.ResponseError(w, 500, err) return } if string(b) == "" { testutil.ResponseAPIError(w, 500, api.ResponseError{ Type: "fatal", Message: "empty body", }) return } var post model.CreationPost err = json.Unmarshal(b, &post) if err != nil { testutil.ResponseError(w, 500, err) return } if !post.Tweet || !post.Gist { testutil.ResponseError(w, 500, errors.New("tweet and gist should be true")) return } post.CreatedAt = model.Time{Time: time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)} post.UpdatedAt = post.CreatedAt b, err = json.Marshal(post) if err != nil { testutil.ResponseError(w, 500, err) return } _, err = w.Write(b) if err != nil { testutil.ResponseError(w, 500, err) return } }) server := httptest.NewServer(mux) defer server.Close() err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") if err != nil { t.Fatal(err) } client := api.NewClient(func(subDomain, path string) (url string) { url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path) return }, inf) testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) prevPath := post.Path if err != nil { t.Fatal(err) } err = post.Create(client, model.CreationOptions{true, true}) if err != nil { t.Fatal(err) } postPath := post.Path if err != nil { t.Fatal(err) } if postPath != prevPath { t.Errorf("wrong path: expected %s, but actual %s", prevPath, postPath) } if !post.CreatedAt.Equal(time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)) { t.Errorf("wrong CreatedAt: %s", post.CreatedAt) } if !post.UpdatedAt.Equal(time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)) { t.Errorf("wrong UpdatedAt: %s", post.UpdatedAt) } testutil.ShouldExistFile(t, 0) }
func TestPostValidateWithTeam(t *testing.T) { func() { post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Body = "Example Body" post.Team = &model.Team{ Active: true, ID: "increments", Name: "Increments Inc.", } err := post.Validate() if err != nil { t.Error("should be valid") } }() func() { post := model.NewPost("", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Body = "Example Body" post.Team = &model.Team{ Active: true, ID: "increments", Name: "Increments Inc.", } err := post.Validate() if err == nil { t.Error("should be invalid") } for name, status := range err { switch name { case "title": if !status.Required { t.Errorf("%s should be required", name) } default: t.Errorf("%s should be valid: %s", name, status) } } }() func() { post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Team = &model.Team{ Active: true, ID: "increments", Name: "Increments Inc.", } err := post.Validate() if err == nil { t.Error("should be invalid") } for name, status := range err { switch name { case "body": if !status.Required { t.Errorf("%s should be required", name) } default: t.Errorf("%s should be valid: %s", name, status) } } }() func() { post := model.NewPost("", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Team = &model.Team{ Active: true, ID: "increments", Name: "Increments Inc.", } err := post.Validate() if err == nil { t.Error("should be invalid") } for name, status := range err { switch name { case "title": if !status.Required { t.Errorf("%s should be required", name) } case "body": if !status.Required { t.Errorf("%s should be required", name) } default: t.Errorf("%s should be valid: %s", name, status) } } }() }
func TestPostValidateWithoutTeam(t *testing.T) { func() { post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Body = "Example Body" post.Tags = model.Tags{ model.Tag{ Name: "Go", }, } err := post.Validate() if err != nil { t.Error("should be valid") } }() func() { post := model.NewPost("", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Body = "Example Body" post.Tags = model.Tags{ model.Tag{ Name: "Go", }, } err := post.Validate() if err == nil { t.Error("should be invalid") } for name, status := range err { switch name { case "title": if !status.Required { t.Errorf("%s should be required", name) } default: t.Errorf("%s should be valid: %s", name, status) } } }() func() { post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Tags = model.Tags{ model.Tag{ Name: "Go", }, } err := post.Validate() if err == nil { t.Error("should be invalid") } for name, status := range err { switch name { case "body": if !status.Required { t.Errorf("%s should be required", name) } default: t.Errorf("%s should be valid: %s", name, status) } } }() func() { post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) post.Body = "Example Body" err := post.Validate() if err == nil { t.Error("should be invalid") } for name, status := range err { switch name { case "tags": if !status.Required { t.Errorf("%s should be required", name) } default: t.Errorf("%s should be valid: %s", name, status) } } }() func() { post := model.NewPost("", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil) err := post.Validate() if err == nil { t.Error("should be invalid") } for name, status := range err { switch name { case "title": if !status.Required { t.Errorf("%s should be required", name) } case "body": if !status.Required { t.Errorf("%s should be required", name) } case "tags": if !status.Required { t.Errorf("%s should be required", name) } default: t.Errorf("%s should be valid: %s", name, status) } } }() }
func TestPostSaveWithTeam(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() testutil.ShouldExistFile(t, 0) post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, &model.Team{Active: true, ID: "increments", Name: "Increments Inc."}) post.ID = "abcdefghijklmnopqrst" err := post.Save(nil) if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 1) func() { a, err := ioutil.ReadFile("increments/2015/11/28/Example Title.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: active: true id: increments name: Increments Inc. --> # Example Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() post.Title = "Example Edited Title" post.CreatedAt = model.Time{time.Date(2015, 12, 28, 13, 2, 37, 0, time.UTC)} post.UpdatedAt = post.CreatedAt err = post.Save(nil) if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 1) func() { _, err := os.Stat("increments/2015/12/28/Example Edited Title.md") if err == nil { t.Errorf("filename based on edited post shouldn't exist: %s", "mine/2015/12/28/Example Edited Title.md") } }() func() { a, err := ioutil.ReadFile("increments/2015/11/28/Example Title.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-12-28T22:02:37+09:00 updated_at: 2015-12-28T22:02:37+09:00 private: false coediting: false tags: [] team: active: true id: increments name: Increments Inc. --> # Example Edited Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }
func TestPostSaveWithInvalidMarkdown(t *testing.T) { testutil.CleanUp() defer testutil.CleanUp() err := os.MkdirAll("foo", 0755) if err != nil { t.Fatal(err) } err = ioutil.WriteFile("foo/wrong-format.md", []byte{}, 0644) if err != nil { t.Fatal(err) } err = ioutil.WriteFile("foo/no-id.md", []byte(`<!-- id: "" url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title `), 0644) if err != nil { t.Fatal(err) } if err != nil { t.Fatal(err) } testutil.ShouldExistFile(t, 2) func() { post := model.NewPost("Example Title", &model.Time{time.Date(2015, 11, 28, 13, 2, 37, 0, time.UTC)}, nil) post.ID = "abcdefghijklmnopqrst" post.Path = "foo/bar.md" err := post.Save(nil) if err != nil { t.Fatal(err) } }() testutil.ShouldExistFile(t, 3) func() { a, err := ioutil.ReadFile("foo/bar.md") if err != nil { t.Fatal(err) } actual := string(a) expected := `<!-- id: abcdefghijklmnopqrst url: "" created_at: 2015-11-28T22:02:37+09:00 updated_at: 2015-11-28T22:02:37+09:00 private: false coediting: false tags: [] team: null --> # Example Title ` if actual != expected { t.Errorf("wrong content:\n%s", testutil.Diff(expected, actual)) } }() }