Skip to content

Commit

Permalink
Add Support for Deleting a File's Comments (#259)
Browse files Browse the repository at this point in the history
Add support for deleting file comments
  • Loading branch information
Abdullah Obaied authored and james-lawrence committed Feb 10, 2018
1 parent db1988b commit 8ab4d0b
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
23 changes: 23 additions & 0 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,29 @@ func (api *Client) UploadFileContext(ctx context.Context, params FileUploadParam
return &response.File, nil
}

// DeleteFileComment deletes a file's comment
func (api *Client) DeleteFileComment(commentID, fileID string) error {
return api.DeleteFileCommentContext(context.Background(), fileID, commentID)
}

// DeleteFileCommentContext deletes a file's comment with a custom context
func (api *Client) DeleteFileCommentContext(ctx context.Context, fileID, commentID string) (err error) {
if fileID == "" || commentID == "" {
return errors.New("received empty parameters")
}

values := url.Values{
"token": {api.token},
"file": {fileID},
"id": {commentID},
}
if _, err = fileRequest(ctx, api.httpclient, "files.comments.delete", values, api.debug); err != nil {
return err
}

return nil
}

// DeleteFile deletes a file
func (api *Client) DeleteFile(fileID string) error {
return api.DeleteFileContext(context.Background(), fileID)
Expand Down
106 changes: 106 additions & 0 deletions files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package slack

import (
"log"
"net/http"
"net/url"
"reflect"
"testing"
)

type fileCommentHandler struct {
gotParams map[string]string
}

func newFileCommentHandler() *fileCommentHandler {
return &fileCommentHandler{
gotParams: make(map[string]string),
}
}

func (h *fileCommentHandler) accumulateFormValue(k string, r *http.Request) {
if v := r.FormValue(k); v != "" {
h.gotParams[k] = v
}
}

func (h *fileCommentHandler) handler(w http.ResponseWriter, r *http.Request) {
h.accumulateFormValue("token", r)
h.accumulateFormValue("file", r)
h.accumulateFormValue("id", r)

w.Header().Set("Content-Type", "application/json")
if h.gotParams["id"] == "trigger-error" {
w.Write([]byte(`{ "ok": false, "error": "errored" }`))
} else {
w.Write([]byte(`{ "ok": true }`))
}
}

func TestSlack_DeleteFileComment(t *testing.T) {
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
tests := []struct {
title string
body url.Values
wantParams map[string]string
expectError bool
}{
{
title: "Testing with proper body",
body: url.Values{
"file": {"file12345"},
"id": {"id12345"},
},
wantParams: map[string]string{
"token": "testing-token",
"file": "file12345",
"id": "id12345",
},
expectError: false,
},
{
title: "Testing with false body",
body: url.Values{
"file": {""},
"id": {""},
},
wantParams: map[string]string{},
expectError: true,
},
{
title: "Testing with error",
body: url.Values{
"file": {"file12345"},
"id": {"trigger-error"},
},
wantParams: map[string]string{
"token": "testing-token",
"file": "file12345",
"id": "trigger-error",
},
expectError: true,
},
}

var fch *fileCommentHandler
http.HandleFunc("/files.comments.delete", func(w http.ResponseWriter, r *http.Request) {
fch.handler(w, r)
})

for _, test := range tests {
fch = newFileCommentHandler()
err := api.DeleteFileComment(test.body["id"][0], test.body["file"][0])

if test.expectError == false && err != nil {
log.Fatalf("Unexpected error: %s in test", err, test.title)
} else if test.expectError == true && err == nil {
log.Fatalf("Expected error but got none")
}

if !reflect.DeepEqual(fch.gotParams, test.wantParams) {
log.Fatalf("%s: Got params [%#v]\nBut received [%#v]\n", test.title, fch.gotParams, test.wantParams)
}
}
}

0 comments on commit 8ab4d0b

Please sign in to comment.