Repository: nicklaw5/helix
Pull request #187 (2023-03-10 21:07:45)

Add channel remove and channel add moderator endpoints


Patch:
diff --git a/SUPPORTED_ENDPOINTS.md b/SUPPORTED_ENDPOINTS.md
index 92dabe0..756ef7d 100644
--- a/SUPPORTED_ENDPOINTS.md
+++ b/SUPPORTED_ENDPOINTS.md
@@ -78,8 +78,8 @@
 - [x] Remove Blocked Term
 - [x] Delete Chat Messages
 - [x] Get Moderators
-- [ ] Add Channel Moderator
-- [ ] Remove Channel Moderator
+- [x] Add Channel Moderator
+- [x] Remove Channel Moderator
 - [x] Get VIPs
 - [x] Add Channel VIP
 - [x] Remove Channel VIP
diff --git a/moderation.go b/moderation.go
index 607d6fd..1059a03 100644
--- a/moderation.go
+++ b/moderation.go
@@ -318,6 +318,24 @@ type ModeratorsResponse struct {
 	Data ManyModerators
 }
 
+type AddChannelModeratorParams struct {
+	UserID        string `query:"user_id"`
+	BroadcasterID string `query:"broadcaster_id"`
+}
+
+type AddChannelModeratorResponse struct {
+	ResponseCommon
+}
+
+type RemoveChannelModeratorParams struct {
+	UserID        string `query:"user_id"`
+	BroadcasterID string `query:"broadcaster_id"`
+}
+
+type RemoveChannelModeratorResponse struct {
+	ResponseCommon
+}
+
 // GetModerators Gets all users allowed to moderate the broadcaster’s chat room.
 // Required scope: moderation:read
 func (c *Client) GetModerators(params *GetModeratorsParams) (*ModeratorsResponse, error) {
@@ -337,3 +355,27 @@ func (c *Client) GetModerators(params *GetModeratorsParams) (*ModeratorsResponse
 
 	return moderators, nil
 }
+
+func (c *Client) AddChannelModerator(params *AddChannelModeratorParams) (*AddChannelModeratorResponse, error) {
+	resp, err := c.post("/moderation/moderators", nil, params)
+	if err != nil {
+		return nil, err
+	}
+
+	moderators := &AddChannelModeratorResponse{}
+	resp.HydrateResponseCommon(&moderators.ResponseCommon)
+
+	return moderators, nil
+}
+
+func (c *Client) RemoveChannelModerator(params *RemoveChannelModeratorParams) (*RemoveChannelModeratorResponse, error) {
+	resp, err := c.delete("/moderation/moderators", nil, params)
+	if err != nil {
+		return nil, err
+	}
+
+	moderators := &RemoveChannelModeratorResponse{}
+	resp.HydrateResponseCommon(&moderators.ResponseCommon)
+
+	return moderators, nil
+}


Test patch:
diff --git a/moderation_test.go b/moderation_test.go
index 0d7def5..3168f3b 100644
--- a/moderation_test.go
+++ b/moderation_test.go
@@ -809,3 +809,143 @@ func TestGetModerators(t *testing.T) {
 		t.Error("expected error does match return error")
 	}
 }
+
+func TestAddChannelModerator(t *testing.T) {
+	t.Parallel()
+
+	testCases := []struct {
+		statusCode int
+		options    *Options
+		params     *AddChannelModeratorParams
+		respBody   string
+	}{
+		{
+			http.StatusOK,
+			&Options{ClientID: "my-client-id"},
+			&AddChannelModeratorParams{
+				BroadcasterID: "123",
+				UserID:        "456",
+			},
+			``,
+		},
+		{
+			http.StatusBadRequest,
+			&Options{ClientID: "my-client-id"},
+			&AddChannelModeratorParams{},
+			`{"error":"Bad Request","status":400,"message":"Missing required parameter \"broadcaster_id\""}`,
+		},
+	}
+
+	for _, testCase := range testCases {
+		c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
+
+		resp, err := c.AddChannelModerator(testCase.params)
+		if err != nil {
+			t.Error(err)
+		}
+
+		// Test Bad Request Responses
+		if resp.StatusCode == http.StatusBadRequest {
+			firstErrStr := "Missing required parameter \"broadcaster_id\""
+			if resp.ErrorMessage != firstErrStr {
+				t.Errorf("expected error message to be \"%s\", got \"%s\"", firstErrStr, resp.ErrorMessage)
+			}
+			continue
+		}
+
+		if resp.StatusCode != testCase.statusCode {
+			t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
+		}
+	}
+
+	// Test with HTTP Failure
+	options := &Options{
+		ClientID: "my-client-id",
+		HTTPClient: &badMockHTTPClient{
+			newMockHandler(0, "", nil),
+		},
+	}
+	c := &Client{
+		opts: options,
+		ctx:  context.Background(),
+	}
+
+	_, err := c.AddChannelModerator(&AddChannelModeratorParams{})
+	if err == nil {
+		t.Error("expected error but got nil")
+	}
+
+	if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
+		t.Error("expected error does match return error")
+	}
+}
+
+func TestRemoveChannelModerator(t *testing.T) {
+	t.Parallel()
+
+	testCases := []struct {
+		statusCode int
+		options    *Options
+		params     *RemoveChannelModeratorParams
+		respBody   string
+	}{
+		{
+			http.StatusOK,
+			&Options{ClientID: "my-client-id"},
+			&RemoveChannelModeratorParams{
+				BroadcasterID: "123",
+				UserID:        "456",
+			},
+			``,
+		},
+		{
+			http.StatusBadRequest,
+			&Options{ClientID: "my-client-id"},
+			&RemoveChannelModeratorParams{},
+			`{"error":"Bad Request","status":400,"message":"Missing required parameter \"broadcaster_id\""}`,
+		},
+	}
+
+	for _, testCase := range testCases {
+		c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
+
+		resp, err := c.RemoveChannelModerator(testCase.params)
+		if err != nil {
+			t.Error(err)
+		}
+
+		// Test Bad Request Responses
+		if resp.StatusCode == http.StatusBadRequest {
+			firstErrStr := "Missing required parameter \"broadcaster_id\""
+			if resp.ErrorMessage != firstErrStr {
+				t.Errorf("expected error message to be \"%s\", got \"%s\"", firstErrStr, resp.ErrorMessage)
+			}
+			continue
+		}
+
+		if resp.StatusCode != testCase.statusCode {
+			t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
+		}
+	}
+
+	// Test with HTTP Failure
+	options := &Options{
+		ClientID: "my-client-id",
+		HTTPClient: &badMockHTTPClient{
+			newMockHandler(0, "", nil),
+		},
+	}
+	c := &Client{
+		opts: options,
+		ctx:  context.Background(),
+	}
+
+	_, err := c.RemoveChannelModerator(&RemoveChannelModeratorParams{})
+	if err == nil {
+		t.Error("expected error but got nil")
+	}
+
+	if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
+		t.Error("expected error does match return error")
+	}
+}