Repository: nukosuke/go-zendesk (go)

Issue:
Add support for group
https://developer.zendesk.com/rest_api/docs/support/groups

- [x] Create
- [x] Read
- [x] Update
- [x] Delete

Pull request description:
Group update & delete
close #74 

Patch:
diff --git a/zendesk/group.go b/zendesk/group.go
index b0f8ef5..1fda3c8 100644
--- a/zendesk/group.go
+++ b/zendesk/group.go
@@ -22,6 +22,8 @@ type GroupAPI interface {
 	GetGroups() ([]Group, Page, error)
 	GetGroup(groupID int64) (Group, error)
 	CreateGroup(group Group) (Group, error)
+	UpdateGroup(groupID int64, group Group) (Group, error)
+	DeleteGroup(groupID int64) error
 }
 
 // GetGroups fetches group list
@@ -84,3 +86,37 @@ func (z *Client) GetGroup(groupID int64) (Group, error) {
 
 	return result.Group, err
 }
+
+// UpdateGroup updates a group with the specified group
+// ref: https://developer.zendesk.com/rest_api/docs/support/groups#update-group
+func (z *Client) UpdateGroup(groupID int64, group Group) (Group, error) {
+	var result, data struct {
+		Group Group `json:"group"`
+	}
+	data.Group = group
+
+	body, err := z.Put(fmt.Sprintf("/groups/%d.json", groupID), data)
+
+	if err != nil {
+		return Group{}, err
+	}
+
+	err = json.Unmarshal(body, &result)
+	if err != nil {
+		return Group{}, err
+	}
+
+	return result.Group, err
+}
+
+// DeleteGroup deletes the specified group
+// ref: https://developer.zendesk.com/rest_api/docs/support/groups#delete-group
+func (z *Client) DeleteGroup(groupID int64) error {
+	err := z.Delete(fmt.Sprintf("/groups/%d.json", groupID))
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go
index 423ebda..4772661 100644
--- a/zendesk/mock/client.go
+++ b/zendesk/mock/client.go
@@ -123,6 +123,20 @@ func (mr *ClientMockRecorder) CreateUser(arg0 interface{}) *gomock.Call {
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUser", reflect.TypeOf((*Client)(nil).CreateUser), arg0)
 }
 
+// DeleteGroup mocks base method
+func (m *Client) DeleteGroup(arg0 int64) error {
+	m.ctrl.T.Helper()
+	ret := m.ctrl.Call(m, "DeleteGroup", arg0)
+	ret0, _ := ret[0].(error)
+	return ret0
+}
+
+// DeleteGroup indicates an expected call of DeleteGroup
+func (mr *ClientMockRecorder) DeleteGroup(arg0 interface{}) *gomock.Call {
+	mr.mock.ctrl.T.Helper()
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteGroup", reflect.TypeOf((*Client)(nil).DeleteGroup), arg0)
+}
+
 // DeleteTicketField mocks base method
 func (m *Client) DeleteTicketField(arg0 int64) error {
 	m.ctrl.T.Helper()
@@ -262,6 +276,21 @@ func (mr *ClientMockRecorder) GetUsers() *gomock.Call {
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsers", reflect.TypeOf((*Client)(nil).GetUsers))
 }
 
+// UpdateGroup mocks base method
+func (m *Client) UpdateGroup(arg0 int64, arg1 zendesk.Group) (zendesk.Group, error) {
+	m.ctrl.T.Helper()
+	ret := m.ctrl.Call(m, "UpdateGroup", arg0, arg1)
+	ret0, _ := ret[0].(zendesk.Group)
+	ret1, _ := ret[1].(error)
+	return ret0, ret1
+}
+
+// UpdateGroup indicates an expected call of UpdateGroup
+func (mr *ClientMockRecorder) UpdateGroup(arg0, arg1 interface{}) *gomock.Call {
+	mr.mock.ctrl.T.Helper()
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateGroup", reflect.TypeOf((*Client)(nil).UpdateGroup), arg0, arg1)
+}
+
 // UpdateTicketField mocks base method
 func (m *Client) UpdateTicketField(arg0 int64, arg1 zendesk.TicketField) (zendesk.TicketField, error) {
 	m.ctrl.T.Helper()


Test patch:
diff --git a/zendesk/group_test.go b/zendesk/group_test.go
index f67e15f..bef915e 100644
--- a/zendesk/group_test.go
+++ b/zendesk/group_test.go
@@ -2,6 +2,7 @@ package zendesk
 
 import (
 	"net/http"
+	"net/http/httptest"
 	"testing"
 )
 
@@ -46,3 +47,32 @@ func TestGetGroup(t *testing.T) {
 		t.Fatalf("Returned group does not have the expected ID %d. Group ID is %d", expectedID, group.ID)
 	}
 }
+
+func TestUpdateGroup(t *testing.T) {
+	mockAPI := newMockAPIWithStatus(http.MethodPut, "groups.json", http.StatusOK)
+	client := newTestClient(mockAPI)
+	defer mockAPI.Close()
+
+	updatedGroup, err := client.UpdateGroup(int64(1234), Group{})
+	if err != nil {
+		t.Fatalf("Failed to send request to create group: %s", err)
+	}
+
+	expectedID := int64(360002440594)
+	if updatedGroup.ID != expectedID {
+		t.Fatalf("Updated group %v did not have expected id %d", updatedGroup, expectedID)
+	}
+}
+
+func TestDeleteGroup(t *testing.T) {
+	mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		w.WriteHeader(http.StatusNoContent)
+		w.Write(nil)
+	}))
+
+	c := newTestClient(mockAPI)
+	err := c.DeleteGroup(1234)
+	if err != nil {
+		t.Fatalf("Failed to delete group: %s", err)
+	}
+}