Commit 13525b17 authored by JB Sazon's avatar JB Sazon Committed by Sander van Harmelen

Fix issue 414 for TransferGroup. Add tests for Groups (#415)

parent e519e706
...@@ -142,12 +142,19 @@ func (s *GroupsService) CreateGroup(opt *CreateGroupOptions, options ...OptionFu ...@@ -142,12 +142,19 @@ func (s *GroupsService) CreateGroup(opt *CreateGroupOptions, options ...OptionFu
// //
// GitLab API docs: // GitLab API docs:
// https://docs.gitlab.com/ce/api/groups.html#transfer-project-to-group // https://docs.gitlab.com/ce/api/groups.html#transfer-project-to-group
func (s *GroupsService) TransferGroup(gid interface{}, project int, options ...OptionFunc) (*Group, *Response, error) { func (s *GroupsService) TransferGroup(gid interface{}, pid interface{}, options ...OptionFunc) (*Group, *Response, error) {
group, err := parseID(gid) group, err := parseID(gid)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
u := fmt.Sprintf("groups/%s/projects/%d", url.QueryEscape(group), project)
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("groups/%s/projects/%s", url.QueryEscape(group),
url.QueryEscape(project))
req, err := s.client.NewRequest("POST", u, nil, options) req, err := s.client.NewRequest("POST", u, nil, options)
if err != nil { if err != nil {
......
...@@ -7,27 +7,173 @@ import ( ...@@ -7,27 +7,173 @@ import (
"testing" "testing"
) )
func TestListGroupProjects(t *testing.T) { func TestListGroups(t *testing.T) {
mux, server, client := setup() mux, server, client := setup()
defer teardown(server) defer teardown(server)
mux.HandleFunc("/api/v4/groups/22/projects", mux.HandleFunc("/api/v4/groups",
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET") testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`) fmt.Fprint(w, `[{"id":1},{"id":2}]`)
}) })
opt := &ListGroupProjectsOptions{ groups, _, err := client.Groups.ListGroups(&ListGroupsOptions{})
ListOptions: ListOptions{2, 3}, if err != nil {
Archived: Bool(true), t.Errorf("Groups.ListGroups returned error: %v", err)
OrderBy: String("name"), }
Sort: String("asc"),
Search: String("query"), want := []*Group{{ID: 1}, {ID: 2}}
Simple: Bool(true), if !reflect.DeepEqual(want, groups) {
Visibility: Visibility(PublicVisibility), t.Errorf("Groups.ListGroups returned %+v, want %+v", groups, want)
}
}
func TestGetGroup(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/groups/g",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id": 1, "name": "g"}`)
})
group, _, err := client.Groups.GetGroup("g")
if err != nil {
t.Errorf("Groups.GetGroup returned error: %v", err)
}
want := &Group{ID: 1, Name: "g"}
if !reflect.DeepEqual(want, group) {
t.Errorf("Groups.GetGroup returned %+v, want %+v", group, want)
}
}
func TestCreateGroup(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/groups",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"id": 1, "name": "g", "path": "g"}`)
})
opt := &CreateGroupOptions{
Name: String("g"),
Path: String("g"),
}
group, _, err := client.Groups.CreateGroup(opt, nil)
if err != nil {
t.Errorf("Groups.CreateGroup returned error: %v", err)
}
want := &Group{ID: 1, Name: "g", Path: "g"}
if !reflect.DeepEqual(want, group) {
t.Errorf("Groups.CreateGroup returned %+v, want %+v", group, want)
}
}
func TestTransferGroup(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1/projects/2",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprintf(w, `{"id": 1}`)
})
group, _, err := client.Groups.TransferGroup(1, 2)
if err != nil {
t.Errorf("Groups.TransferGroup returned error: %v", err)
}
want := &Group{ID: 1}
if !reflect.DeepEqual(group, want) {
t.Errorf("Groups.TransferGroup returned %+v, want %+v", group, want)
}
}
func TestDeleteGroup(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusAccepted)
})
resp, err := client.Groups.DeleteGroup(1)
if err != nil {
t.Errorf("Groups.DeleteGroup returned error: %v", err)
}
fmt.Println(resp.StatusCode)
want := http.StatusAccepted
got := resp.StatusCode
if got != want {
t.Errorf("Groups.DeleteGroup returned %d, want %d", got, want)
} }
}
func TestSearchGroup(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
projects, _, err := client.Groups.ListGroupProjects(22, opt) mux.HandleFunc("/api/v4/groups",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id": 1, "name": "Foobar Group"}]`)
})
groups, _, err := client.Groups.SearchGroup("foobar")
if err != nil {
t.Errorf("Groups.SearchGroup returned error: %v", err)
}
want := []*Group{{ID: 1, Name: "Foobar Group"}}
if !reflect.DeepEqual(want, groups) {
t.Errorf("Groups.SearchGroup returned +%v, want %+v", groups, want)
}
}
func TestUpdateGroup(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprint(w, `{"id": 1}`)
})
group, _, err := client.Groups.UpdateGroup(1, &UpdateGroupOptions{})
if err != nil {
t.Errorf("Groups.UpdateGroup returned error: %v", err)
}
want := &Group{ID: 1}
if !reflect.DeepEqual(want, group) {
t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", group, want)
}
}
func TestListGroupProjects(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/groups/22/projects",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
projects, _, err := client.Groups.ListGroupProjects(22,
&ListGroupProjectsOptions{})
if err != nil { if err != nil {
t.Errorf("Groups.ListGroupProjects returned error: %v", err) t.Errorf("Groups.ListGroupProjects returned error: %v", err)
} }
...@@ -37,3 +183,24 @@ func TestListGroupProjects(t *testing.T) { ...@@ -37,3 +183,24 @@ func TestListGroupProjects(t *testing.T) {
t.Errorf("Groups.ListGroupProjects returned %+v, want %+v", projects, want) t.Errorf("Groups.ListGroupProjects returned %+v, want %+v", projects, want)
} }
} }
func TestListSubgroups(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1/subgroups",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id": 1}, {"id": 2}]`)
})
groups, _, err := client.Groups.ListSubgroups(1, &ListSubgroupsOptions{})
if err != nil {
t.Errorf("Groups.ListSubgroups returned error: %v", err)
}
want := []*Group{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(want, groups) {
t.Errorf("Groups.ListSubgroups returned %+v, want %+v", groups, want)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment