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:
committed by
GitHub
parent
10b0b596ae
commit
93fe3e8bd6
52
cmd/approve_test.go
Normal file
52
cmd/approve_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
func approveMergeRequest(pid interface{}, mr int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error) {
|
||||
return &gitlab.MergeRequestApprovals{}, makeResponse(http.StatusOK), nil
|
||||
}
|
||||
|
||||
func approveMergeRequestNon200(pid interface{}, mr int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error) {
|
||||
return &gitlab.MergeRequestApprovals{}, makeResponse(http.StatusSeeOther), nil
|
||||
}
|
||||
|
||||
func approveMergeRequestErr(pid interface{}, mr int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error) {
|
||||
return &gitlab.MergeRequestApprovals{}, nil, errors.New("Some error from Gitlab")
|
||||
}
|
||||
|
||||
func TestApproveHandler(t *testing.T) {
|
||||
t.Run("Approves merge request", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequest})
|
||||
data := serveRequest(t, server, request, SuccessResponse{})
|
||||
assert(t, data.Message, "Approved MR")
|
||||
assert(t, data.Status, http.StatusOK)
|
||||
})
|
||||
|
||||
t.Run("Disallows non-POST method", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPut, "/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequest})
|
||||
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, "/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequestErr})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkErrorFromGitlab(t, *data, "Could not approve merge request")
|
||||
})
|
||||
|
||||
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequestNon200})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkNon200(t, *data, "Could not approve merge request", "/approve")
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user