Simplify Go Endpoints + Add Tests (#120)

This MR represents a major refactor of the Go codebase, as well as introducing tests for the handlers. The MR also introduces an endpoint to shutdown or restart the Go server, which may be useful for clients who want to refresh the state of the plugin after checking out branches. Finally, this MR adds a contributing document for users who want to make feature changes.
This commit is contained in:
Harrison (Harry) Cramer
2023-12-04 10:15:07 -05:00
committed by GitHub
parent 10b0b596ae
commit 93fe3e8bd6
41 changed files with 1745 additions and 728 deletions

52
cmd/reply_test.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import (
"errors"
"net/http"
"testing"
"github.com/xanzy/go-gitlab"
)
func addMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
return &gitlab.Note{}, makeResponse(http.StatusOK), nil
}
func addMergeRequestDiscussionNoteErr(pid interface{}, mergeRequest int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
return nil, nil, errors.New("Some error from Gitlab")
}
func addMergeRequestDiscussionNoteNon200(pid interface{}, mergeRequest int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
return nil, makeResponse(http.StatusSeeOther), nil
}
func TestReplyHandler(t *testing.T) {
t.Run("Sends a reply", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/reply", ReplyRequest{})
server, _ := createRouterAndApi(fakeClient{addMergeRequestDiscussionNote: addMergeRequestDiscussionNote})
data := serveRequest(t, server, request, ReplyResponse{})
assert(t, data.SuccessResponse.Message, "Replied to comment")
assert(t, data.SuccessResponse.Status, http.StatusOK)
})
t.Run("Disallows non-POST methods", func(t *testing.T) {
request := makeRequest(t, http.MethodGet, "/reply", ReplyRequest{})
server, _ := createRouterAndApi(fakeClient{addMergeRequestDiscussionNote: addMergeRequestDiscussionNote})
data := serveRequest(t, server, request, ErrorResponse{})
checkBadMethod(t, *data, http.MethodPost)
})
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/reply", ReplyRequest{})
server, _ := createRouterAndApi(fakeClient{addMergeRequestDiscussionNote: addMergeRequestDiscussionNoteErr})
data := serveRequest(t, server, request, ErrorResponse{})
checkErrorFromGitlab(t, *data, "Could not leave reply")
})
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/reply", ReplyRequest{})
server, _ := createRouterAndApi(fakeClient{addMergeRequestDiscussionNote: addMergeRequestDiscussionNoteNon200})
data := serveRequest(t, server, request, ErrorResponse{})
checkNon200(t, *data, "Could not leave reply", "/reply")
})
}