Esempio n. 1
0
func createRepo(t *testing.T, record bool) string {
	// create repo
	repo := TestPrefix + faker.Internet().UserName()
	req := Client.NewRequest(t, "POST", "/user/repos", jsons.Parse(`{"name": %q}`, repo))
	if record {
		req.EnableRecording("repo_create.apib")
	}
	j := Client.Do(t, req, 201).JSON(t)
	assert.Equal(t, jsons.Parse(`{"name": %q, "full_name": %q}`, repo, Login+"/"+repo), j.KeepFields("name", "full_name"))
	assert.Equal(t, jsons.Parse(`{"login": %q}`, Login), j.Get("/owner").KeepFields("login"))
	return repo
}
Esempio n. 2
0
func TestRequestResponseBody(t *testing.T) {
	u, err := url.Parse("http://jsonplaceholder.typicode.com")
	require.Nil(t, err)
	client := NewClient(*u)

	j := jsons.Parse(`{"userId": 1, "id": 101, "title": "title", "body": "body"}`)
	req := client.NewRequest(t, "POST", "/posts", j)
	assert.Nil(t, req.Body)
	assert.NotNil(t, req.Request.Body)

	resp := client.Do(t, req, 201)
	assert.Equal(t, []byte(j.String()), req.Body)
	assert.IsType(t, errorReadCloser{}, req.Request.Body)
	assert.Equal(t, jsons.Parse(`{"id": 101}`), jsons.ParseBytes(resp.Body))
	assert.IsType(t, errorReadCloser{}, resp.Response.Body)
}
Esempio n. 3
0
func TestRepoCreateDestroy(t *testing.T) {
	t.Parallel()

	repo := createRepo(t, true)
	defer destroyRepo(t, repo)

	// check repo exists
	j := Client.Get(t, "/repos/"+Login+"/"+repo, 200).JSON(t)
	assert.Equal(t, jsons.Parse(`{"login": %q}`, Login), j.Get("/owner").KeepFields("login"))

	// try to create repo with the same name again
	req := Client.NewRequest(t, "POST", "/user/repos", jsons.Parse(`{"name": %q}`, repo)).EnableRecording("repo_create_exist.apib")
	j = Client.Do(t, req, 422).JSON(t)
	assert.Equal(t, jsons.Parse(`{"message": "Validation Failed"}`), j.KeepFields("message"))
	assert.Equal(t, jsons.Parse(`{"code": "custom", "field": "name"}`), j.Get("/errors/0").KeepFields("code", "field"))
}
Esempio n. 4
0
func TestListOrgs(t *testing.T) {
	t.Parallel()

	j := Client.Get(t, "/user/orgs", 200).JSON(t)

	var found bool
	v := j.KeepFields("login")
	expect := jsons.Parse(`{"login": "******"}`).String()
	for _, e := range v.(jsons.Array) {
		if jsons.Cast(e).String() == expect {
			found = true
			break
		}
	}

	assert.True(t, found, "current user doesn't belong to go-gophers organization")
}
Esempio n. 5
0
func TestRepoList(t *testing.T) {
	t.Parallel()

	repo := createRepo(t, false)
	defer destroyRepo(t, repo)

	j := Client.Get(t, "/user/repos?visibility=public&affiliation=owner&sort=created", 200).JSON(t)

	var found bool
	v := j.KeepFields("name")
	expect := jsons.Parse(`{"name": %q}`, repo).String()
	for _, e := range v.(jsons.Array) {
		if jsons.Cast(e).String() == expect {
			found = true
			break
		}
	}

	assert.True(t, found, "created repository not found in list")
}