Пример #1
0
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)
		}
	})
}
Пример #2
0
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)
		}
	})
}
Пример #3
0
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)
}
Пример #4
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)
}