コード例 #1
0
ファイル: post_test.go プロジェクト: minodisk/qiitactl
func handleAuthenticatedUserItems(mux *http.ServeMux) {
	mux.HandleFunc("/api/v2/authenticated_user/items", func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			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))
		default:
			w.WriteHeader(405)
		}
	})
}
コード例 #2
0
ファイル: post_test.go プロジェクト: minodisk/qiitactl
func handleItems(mux *http.ServeMux) {
	mux.HandleFunc("/api/v2/items", func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "POST":
			defer r.Body.Close()

			b, err := ioutil.ReadAll(r.Body)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}
			if len(b) == 0 {
				testutil.ResponseAPIError(w, 500, api.ResponseError{
					Type:    "fatal",
					Message: "empty body",
				})
				return
			}

			type Options struct {
				Tweet *bool `json:"tweet"`
				Gist  *bool `json:"gist"`
			}
			var options Options
			err = json.Unmarshal(b, &options)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}
			if options.Tweet == nil || options.Gist == nil {
				testutil.ResponseError(w, 500, errors.New("tweet or gist is required"))
				return
			}

			var post model.Post
			err = json.Unmarshal(b, &post)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}
			post.ID = "4bd431809afb1bb99e4f"
			post.URL = "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f"
			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
			}

		default:
			w.WriteHeader(405)
		}
	})
}
コード例 #3
0
ファイル: posts_test.go プロジェクト: minodisk/qiitactl
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))
	}
}
コード例 #4
0
ファイル: posts_test.go プロジェクト: minodisk/qiitactl
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])
	}
}
コード例 #5
0
ファイル: client_test.go プロジェクト: minodisk/qiitactl
func TestMain(m *testing.M) {
	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/echo", func(w http.ResponseWriter, r *http.Request) {
		auth := r.Header.Get("Authorization")
		if auth != "Bearer XXXXXXXXXXXX" {
			b, _ := json.Marshal(api.ResponseError{
				Type:    "unauthorized",
				Message: "Unauthorized",
			})
			w.WriteHeader(401)
			w.Write(b)
			return
		}

		ua := r.Header.Get("User-Agent")
		if !rUserAgent.MatchString(ua) {
			b, _ := json.Marshal(api.ResponseError{
				Type:    "bad_request",
				Message: "Bad Request",
			})
			w.WriteHeader(400)
			w.Write(b)
			return
		}

		defer r.Body.Close()
		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			testutil.ResponseError(w, 500, err)
			return
		}

		if len(b) == 0 {
			fmt.Fprintf(w, "%s %s is accepted", r.Method, r.URL)
			return
		}

		contentType := r.Header.Get("Content-Type")
		if contentType != "application/json" {
			testutil.ResponseError(w, 400, api.ResponseError{
				Type:    "bad_request",
				Message: "Bad Request",
			})
			return
		}

		var v interface{}
		err = json.Unmarshal(b, &v)
		if err != nil {
			testutil.ResponseError(w, 500, err)
			return
		}
		b, err = json.Marshal(v)
		if err != nil {
			testutil.ResponseError(w, 500, err)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write(b)
	})

	mux.HandleFunc("/api/v2/errors/response", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
		b, _ := json.Marshal(api.ResponseError{"internal_server_error", "Internal Server Error"})
		w.Write(b)
	})

	mux.HandleFunc("/api/v2/errors/status", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
	})

	server = httptest.NewServer(mux)
	defer server.Close()

	code := m.Run()

	// clean up
	testutil.CleanUp()

	os.Exit(code)
}
コード例 #6
0
ファイル: post_test.go プロジェクト: minodisk/qiitactl
func handleItem(mux *http.ServeMux) {
	mux.HandleFunc("/api/v2/items/4bd431809afb1bb99e4f", func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			w.Write([]byte(`{
					"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"
					}
				}`))

		case "PATCH":
			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
			}

		case "DELETE":
			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
			}
			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
			}

		default:
			w.WriteHeader(405)
		}
	})
}
コード例 #7
0
ファイル: post_test.go プロジェクト: minodisk/qiitactl
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)
}
コード例 #8
0
ファイル: post_test.go プロジェクト: minodisk/qiitactl
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)
}