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")) }
func createRepo(t *testing.T, record bool) string { // Create new Faker instance since it's not thread-safe // https://github.com/manveru/faker/issues/6 faker, err := faker.New("en") require.Nil(t, err) // create repo repo := TestPrefix + faker.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 }
// JSON returns reponse body as JSON structure. // In case of error if fails test. func (r *Response) JSON(t TestingTB) (j jsons.Struct) { defer func() { if p := recover(); p != nil { j = nil t.Fatalf("panic: %v", p) } }() b, err := ioutil.ReadAll(r.Body) if err != nil { t.Fatalf("can't read body: %s", err) } j = jsons.Parse(string(b)) return }
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 gophergala2016 organization") }
func dump(b []byte, te []string) (status, headers, body []byte, err error) { p := bytes.SplitN(b, []byte("\r\n\r\n"), 2) headers, body = p[0], p[1] p = bytes.SplitN(headers, []byte("\r\n"), 2) status, headers = p[0], p[1] if len(body) > 0 && isChunked(te) { r := httputil.NewChunkedReader(bytes.NewReader(body)) body, err = ioutil.ReadAll(r) if err != nil { return } } if len(body) > 0 { body = []byte(jsons.Parse(string(body)).Indent()) } return }
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") }