Exemple #1
0
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)
}
Exemple #2
0
func TestFetchPostsWithResponseStatusError(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/authenticated_user/items", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
	})

	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)

	_, err = model.FetchPosts(client, nil)
	if err == nil {
		t.Fatal("error should occur")
	}
	_, ok := err.(api.StatusError)
	if !ok {
		t.Fatalf("wrong type error: %s", reflect.TypeOf(err))
	}
}
Exemple #3
0
func TestPostDecodeWithWrongTitle(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	var post model.Post
	err := post.Decode([]byte(`<!--
id: abcdefghijklmnopqrst
url: http://example.com/mypost
created_at: 2013-12-10T12:29:14+09:00
updated_at: 2015-02-25T09:26:30+09:00
private: true
coediting: false
tags:
- TypeScript
- Docker:
  - 1.9
- Go:
  - 1.4.3
  - 1.5.3
team: null
-->

## Sub title
# Main title

Paragraph
`))
	if err == nil {
		t.Errorf("should return error with non-object element in tags")
	}
}
Exemple #4
0
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")
	}
}
Exemple #5
0
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)
}
Exemple #6
0
func TestPostDecodeWithMultiComment(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	var post model.Post
	err := post.Decode([]byte(`<!--
id: ""
url: ""
created_at: 2016-02-17T13:08:30+09:00
updated_at: 2016-02-17T13:08:30+09:00
private: false
coediting: false
tags:
- Qiita
- Go
team: null
-->

# Example Title

<!--
This is not meta
-->

# This is not title of this post
`))
	if err != nil {
		t.Fatal(err)
	}
}
Exemple #7
0
func TestFetchPostsWitWrongResponseBody(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/authenticated_user/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)

	_, err = model.FetchPosts(client, nil)
	if err == nil {
		t.Fatal("error should occur")
	}
}
Exemple #8
0
func TestGenerateUniqueFile(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)

	app := command.New(inf, client, os.Stdout, os.Stderr)
	app.Run([]string{"qiitactl", "generate", "file", "Example Title"})
	app.Run([]string{"qiitactl", "generate", "file", "Example Title"})

	testutil.ShouldExistFile(t, 2)

	path := fmt.Sprintf("mine/%s/Example Title.md", time.Now().Format("2006/01/02"))
	_, err = os.Stat(path)
	if err != nil {
		t.Errorf("file should exist at %s", path)
	}
	path = fmt.Sprintf("mine/%s/Example Title-.md", time.Now().Format("2006/01/02"))
	_, err = os.Stat(path)
	if err != nil {
		t.Errorf("file should exist at %s", path)
	}
}
Exemple #9
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)
	}
}
Exemple #10
0
func TestClientPost(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
		return
	}, inf)
	client.DebugMode(true)

	body, _, err := client.Post("", "/echo", "data")
	if err != nil {
		t.Fatal(err)
	}

	var b string
	err = json.Unmarshal(body, &b)
	if err != nil {
		t.Fatal(err)
	}
	if b != "data" {
		t.Errorf("wrong body: %s", b)
	}
}
Exemple #11
0
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)
}
Exemple #12
0
func TestFetchPostWithID(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	handleItem(mux)
	serverMine := httptest.NewServer(mux)
	defer serverMine.Close()
	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		switch subDomain {
		case "":
			url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
		default:
			log.Fatalf("wrong sub domain \"%s\"", subDomain)
		}
		return
	}, inf)

	testutil.ShouldExistFile(t, 0)

	errBuf := bytes.NewBuffer([]byte{})
	app := command.New(inf, client, os.Stdout, errBuf)
	app.Run([]string{"qiitactl", "fetch", "post", "-i", "4bd431809afb1bb99e4f"})
	e := errBuf.Bytes()
	if len(e) != 0 {
		t.Fatal(string(e))
	}

	testutil.ShouldExistFile(t, 1)

	b, err := ioutil.ReadFile("mine/2000/01/01/Example Title.md")
	if err != nil {
		t.Fatal(err)
	}
	actual := string(b)
	expected := `<!--
id: 4bd431809afb1bb99e4f
url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f
created_at: 2000-01-01T09:00:00+09:00
updated_at: 2000-01-01T09:00:00+09:00
private: false
coediting: false
tags:
- Ruby:
  - 0.0.1
team: null
-->

# Example Title

## Example body`
	if actual != expected {
		t.Errorf("wrong body:\n%s", testutil.Diff(expected, actual))
	}
}
Exemple #13
0
func TestNewPostWithFileWithNoFile(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	_, err := model.NewPostWithFile("mine/2013/12/10-main-title.md")
	if err == nil {
		t.Fatal("error should occur")
	}
}
Exemple #14
0
func TestResponseError(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	err := api.ResponseError{
		Type:    "not_found",
		Message: "Not found",
	}
	if err.Error() != "Not found" {
		t.Errorf("wrong Error: %s", err.Error())
	}
}
Exemple #15
0
func TestStatusError(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	err := api.StatusError{
		Code:    404,
		Message: "404 Not Found",
	}
	if err.Error() != "404 Not Found" {
		t.Errorf("wrong Error: %s", err.Error())
	}
}
Exemple #16
0
func TestShowPosts(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	handleAuthenticatedUserItems(mux)
	handleTeams(mux)
	serverMine := httptest.NewServer(mux)
	defer serverMine.Close()
	mux = http.NewServeMux()
	handleAuthenticatedUserItemsWithTeam(mux)
	serverTeam := httptest.NewServer(mux)
	defer serverTeam.Close()
	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		switch subDomain {
		case "":
			url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
		case "increments":
			url = fmt.Sprintf("%s%s%s", serverTeam.URL, "/api/v2", path)
		default:
			log.Fatalf("wrong sub domain \"%s\"", subDomain)
		}
		return
	}, inf)

	testutil.ShouldExistFile(t, 0)

	buf := bytes.NewBuffer([]byte{})
	errBuf := bytes.NewBuffer([]byte{})
	app := command.New(inf, client, buf, errBuf)
	app.Run([]string{"qiitactl", "show", "posts"})
	e := errBuf.Bytes()
	if len(e) != 0 {
		t.Fatal(string(e))
	}

	testutil.ShouldExistFile(t, 0)

	if string(buf.Bytes()) != `Posts in Qiita:
4bd431809afb1bb99e4f 2000/01/01 Example Title
Posts in Qiita:Team (Increments Inc.):
4bd431809afb1bb99e4t 2015/09/25 Example Title in team
` {
		t.Errorf("written text is wrong: %s", buf.Bytes())
	}
}
Exemple #17
0
func TestClientProcessWithEmptyToken(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	client := api.NewClient(func(subDomain, path string) (url string) {
		url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
		return
	}, inf)

	_, _, err := client.Options("", "/echo", nil)
	_, ok := err.(api.EmptyTokenError)
	if !ok {
		t.Fatal("empty token error should occur")
	}
}
Exemple #18
0
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)
	}
}
Exemple #19
0
func TestClientProcessWithStatusError(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
		return
	}, inf)

	_, _, err = client.Options("", "/errors/status", nil)
	_, ok := err.(api.StatusError)
	if !ok {
		t.Fatal("status error should occur")
	}
}
Exemple #20
0
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))
		}
	}()
}
Exemple #21
0
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)
}
Exemple #22
0
func TestFetchPostWithResponseError(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/items/4bd431809afb1bb99e4f", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(400)
		w.Write([]byte(`{
  "message": "Not found",
  "type": "not_found"
}`))
	})

	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)

	_, err = model.FetchPost(client, nil, "4bd431809afb1bb99e4f")
	if err == nil {
		t.Fatal("error should occur")
	}
	_, ok := err.(api.ResponseError)
	if !ok {
		t.Fatalf("wrong type error: %s", reflect.TypeOf(err))
	}

	testutil.ShouldExistFile(t, 0)
}
Exemple #23
0
func TestClientDelete(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
		return
	}, inf)

	body, _, err := client.Delete("", "/echo", nil)
	if err != nil {
		t.Fatal(err)
	}

	if string(body) != fmt.Sprintf("%s /api/v2%s is accepted", "DELETE", "/echo") {
		t.Errorf("wrong body: %s", body)
	}
}
Exemple #24
0
func TestNewPostWithFileWithWrongFormattedFile(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	err := os.MkdirAll("mine/2013/12", 0755)
	if err != nil {
		t.Fatal(err)
	}
	err = ioutil.WriteFile("mine/2013/12/10-main-title.md", []byte(`<!--
id: abcdefghijklmnopqrst
url: http://example.com/mypost
created_at: 2013-12-10T12:29:14+09:00
updated_at: 2015-02-25T09:26:30+09:00
private: true
coediting: false
tags:
- TypeScript
- Docker:
  - 1.9
- Go:
  - 1.4.3
  - 1.5.3
team: null
-->

## Sub title
Paragraph
`), 0644)
	if err != nil {
		t.Fatal(err)
	}

	_, err = model.NewPostWithFile("mine/2013/12/10-main-title.md")
	if err == nil {
		t.Fatal("error should occur")
	}
}
Exemple #25
0
func TestFetchPostsWithWrongTotalCount(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/authenticated_user/items", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			w.WriteHeader(405)
			b, _ := json.Marshal(api.ResponseError{"method_not_allowed", "Method Not Allowed"})
			w.Write(b)
			return
		}

		var posts []model.Post
		b, _ := json.Marshal(posts)
		w.Header().Set("Total-Count", "dummy")
		w.Write(b)
	})

	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)

	_, err = model.FetchPosts(client, nil)
	if err == nil {
		t.Fatal("error should occur")
	}
}
Exemple #26
0
func TestFetchPostWithWrongID(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	handleItem(mux)
	serverMine := httptest.NewServer(mux)
	defer serverMine.Close()
	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		log.Fatal(err)
	}
	client := api.NewClient(func(subDomain, path string) (url string) {
		switch subDomain {
		case "":
			url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
		default:
			log.Fatalf("wrong sub domain \"%s\"", subDomain)
		}
		return
	}, inf)

	testutil.ShouldExistFile(t, 0)

	buf := bytes.NewBuffer([]byte{})
	errBuf := bytes.NewBuffer([]byte{})
	app := command.New(inf, client, buf, errBuf)
	app.Run([]string{"qiitactl", "fetch", "post", "-i", "XXXXXXXXXXXXXXXXXXXX"})
	e := errBuf.Bytes()
	actual := string(e)
	expected := "404 Not Found\n"
	if actual != expected {
		t.Fatalf("error should occur when fetches post with wrong ID: %s", actual)
	}

	testutil.ShouldExistFile(t, 0)
}
Exemple #27
0
func TestMain(m *testing.M) {
	code := m.Run()
	testutil.CleanUp()
	os.Exit(code)
}
Exemple #28
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))
		}
	}()
}
Exemple #29
0
func TestFetchPostsWithTotalCount(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/authenticated_user/items", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			w.WriteHeader(405)
			b, _ := json.Marshal(api.ResponseError{"method_not_allowed", "Method Not Allowed"})
			w.Write(b)
			return
		}
		total := 1422
		q := r.URL.Query()
		perPage, err := strconv.Atoi(q.Get("per_page"))
		if err != nil {
			perPage = 10
		}
		page, err := strconv.Atoi(q.Get("page"))
		if err != nil {
			page = 1
		}

		var posts []model.Post

		from := perPage*(page-1) + 1
		if from <= total {
			to := perPage * page
			if to > total {
				to = total
			}
			for i := from; i <= to; i++ {
				post := model.Post{Meta: model.Meta{ID: fmt.Sprint(i)}}
				posts = append(posts, post)
			}
		} else {
			testutil.ResponseError(w, 500, fmt.Errorf("shouldn't access over total count"))
			return
		}

		b, _ := json.Marshal(posts)
		w.Header().Set("Total-Count", fmt.Sprint(total))
		w.Write(b)
	})

	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)

	team := model.Team{
		Active: true,
		ID:     "increments",
		Name:   "Increments Inc",
	}
	posts, err := model.FetchPosts(client, &team)
	if err != nil {
		t.Fatal(err)
	}

	if len(posts) != 1422 {
		t.Errorf("wrong posts length: %d", len(posts))
	}
}
Exemple #30
0
func TestFetchPosts(t *testing.T) {
	testutil.CleanUp()
	defer testutil.CleanUp()

	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/authenticated_user/items", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			w.WriteHeader(405)
			b, _ := json.Marshal(api.ResponseError{"method_not_allowed", "Method Not Allowed"})
			w.Write(b)
			return
		}
		var body string
		if r.URL.Query().Get("page") == "1" {
			body = `[
				{
					"rendered_body": "<h2>Example body</h2>",
					"body": "## Example body",
					"coediting": false,
					"created_at": "2000-01-01T00:00:00+00:00",
					"id": "4bd431809afb1bb99e4f",
					"private": false,
					"tags": [
						{
							"name": "Ruby",
							"versions": [
								"0.0.1"
							]
						}
					],
					"title": "Example title",
					"updated_at": "2000-01-01T00:00:00+00:00",
					"url": "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f",
					"user": {
						"description": "Hello, world.",
						"facebook_id": "yaotti",
						"followees_count": 100,
						"followers_count": 200,
						"github_login_name": "yaotti",
						"id": "yaotti",
						"items_count": 300,
						"linkedin_id": "yaotti",
						"location": "Tokyo, Japan",
						"name": "Hiroshige Umino",
						"organization": "Increments Inc",
						"permanent_id": 1,
						"profile_image_url": "https://si0.twimg.com/profile_images/2309761038/1ijg13pfs0dg84sk2y0h_normal.jpeg",
						"twitter_screen_name": "yaotti",
						"website_url": "http://yaotti.hatenablog.com"
					}
				}
			]`
		} else {
			testutil.ResponseError(w, 500, fmt.Errorf("shouldn't access over total count"))
			return
		}
		w.Header().Set("Total-Count", fmt.Sprint(1))
		w.Write([]byte(body))
	})

	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)

	team := model.Team{
		Active: true,
		ID:     "increments",
		Name:   "Increments Inc",
	}
	posts, err := model.FetchPosts(client, &team)
	if err != nil {
		t.Fatal(err)
	}

	if len(posts) != 1 {
		t.Fatalf("wrong length: expected %d, but actual %d", 1, len(posts))
	}

	post := posts[0]
	if post.RenderedBody != "<h2>Example body</h2>" {
		t.Errorf("wrong RenderedBody: %s", post.RenderedBody)
	}
	if post.Body != "## Example body" {
		t.Errorf("wrong Body: %s", post.Body)
	}
	if post.Coediting != false {
		t.Errorf("wrong Coediting: %b", post.Coediting)
	}
	if !post.CreatedAt.Equal(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) {
		t.Errorf("wrong CreatedAt: %s", post.CreatedAt)
	}
	if post.ID != "4bd431809afb1bb99e4f" {
		t.Errorf("wrong ID: %s", post.ID)
	}
	if post.Private != false {
		t.Errorf("wrong Private: %b", post.Private)
	}
	if post.Title != "Example title" {
		t.Errorf("wrong Title: %s", post.Title)
	}
	if !post.UpdatedAt.Equal(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) {
		t.Errorf("wrong UpdatedAt: %s", post.UpdatedAt)
	}
	if post.URL != "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f" {
		t.Errorf("wrong URL: %s", post.URL)
	}
	if post.User.Description != "Hello, world." {
		t.Errorf("wrong Description: %s", post.User.Description)
	}
	if post.User.FacebookID != "yaotti" {
		t.Errorf("wrong FacebookId: %s", post.User.FacebookID)
	}
	if post.User.FolloweesCount != 100 {
		t.Errorf("wrong FolloweesCount: %s", post.User.FolloweesCount)
	}
	if post.User.FollowersCount != 200 {
		t.Errorf("wrong FollowersCount: %s", post.User.FollowersCount)
	}
	if post.User.GithubLoginName != "yaotti" {
		t.Errorf("wrong GithubLoginName: %s", post.User.GithubLoginName)
	}
	if post.User.ID != "yaotti" {
		t.Errorf("wrong Id: %s", post.User.ID)
	}
	if post.User.ItemsCount != 300 {
		t.Errorf("wrong ItemsCount: %d", post.User.ItemsCount)
	}
	if post.User.LinkedinID != "yaotti" {
		t.Errorf("wrong LinkedinId: %s", post.User.LinkedinID)
	}
	if post.User.Location != "Tokyo, Japan" {
		t.Errorf("wrong Location: %s", post.User.Location)
	}
	if post.User.Name != "Hiroshige Umino" {
		t.Errorf("wrong Name: %s", post.User.Name)
	}
	if post.User.Organization != "Increments Inc" {
		t.Errorf("wrong Organization: %s", post.User.Organization)
	}
	if post.User.PermanentID != 1 {
		t.Errorf("wrong PermanentId: %d", post.User.PermanentID)
	}
	if post.User.ProfileImageURL != "https://si0.twimg.com/profile_images/2309761038/1ijg13pfs0dg84sk2y0h_normal.jpeg" {
		t.Errorf("wrong ProfileImageUrl: %s", post.User.ProfileImageURL)
	}
	if post.User.TwitterScreenName != "yaotti" {
		t.Errorf("wrong TwitterScreenName: %s", post.User.TwitterScreenName)
	}
	if post.User.WebsiteURL != "http://yaotti.hatenablog.com" {
		t.Errorf("wrong WebsiteUrl: %s", post.User.WebsiteURL)
	}
	if len(post.Tags) != 1 {
		t.Fatalf("wrong Tags length: %d", len(post.Tags))
	}
	if post.Tags[0].Name != "Ruby" {
		t.Errorf("wrong tag Name: %s", post.Tags[0].Name)
	}
	if len(post.Tags[0].Versions) != 1 {
		t.Fatalf("wrong tag Versions length: %d", len(post.Tags[0].Versions))
	}
	if post.Tags[0].Versions[0] != "0.0.1" {
		t.Errorf("wrong tag Versions: %s", post.Tags[0].Versions[0])
	}
}