Add Filtering, HealthCheck, Better Tests (#350)
feat: add filtering when choosing merge requests (#346) feat: Add healthcheck (#345) refactor: Move to gomock (#349) feat: Makes the remote of the plugin configurable (#348) This is a #MINOR release.
This commit is contained in:
committed by
GitHub
parent
aa5d3c1f52
commit
4ae623cd65
@@ -1,52 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
mock_main "gitlab.com/harrisoncramer/gitlab.nvim/cmd/mocks"
|
||||
)
|
||||
|
||||
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) {
|
||||
client := mock_main.NewMockClient(t)
|
||||
mock_main.WithMr(t, client)
|
||||
client.EXPECT().ApproveMergeRequest("", mock_main.MergeId, nil, nil).Return(&gitlab.MergeRequestApprovals{}, makeResponse(http.StatusOK), nil)
|
||||
|
||||
request := makeRequest(t, http.MethodPost, "/mr/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequest})
|
||||
server, _ := CreateRouterAndApi(client)
|
||||
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) {
|
||||
client := mock_main.NewMockClient(t)
|
||||
mock_main.WithMr(t, client)
|
||||
client.EXPECT().ApproveMergeRequest("", mock_main.MergeId, nil, nil).Return(&gitlab.MergeRequestApprovals{}, makeResponse(http.StatusOK), nil)
|
||||
|
||||
request := makeRequest(t, http.MethodPut, "/mr/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequest})
|
||||
server, _ := CreateRouterAndApi(client)
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkBadMethod(t, *data, http.MethodPost)
|
||||
})
|
||||
|
||||
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
|
||||
client := mock_main.NewMockClient(t)
|
||||
mock_main.WithMr(t, client)
|
||||
client.EXPECT().ApproveMergeRequest("", mock_main.MergeId, nil, nil).Return(nil, nil, errorFromGitlab)
|
||||
|
||||
request := makeRequest(t, http.MethodPost, "/mr/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequestErr})
|
||||
server, _ := CreateRouterAndApi(client)
|
||||
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) {
|
||||
client := mock_main.NewMockClient(t)
|
||||
mock_main.WithMr(t, client)
|
||||
client.EXPECT().ApproveMergeRequest("", mock_main.MergeId, nil, nil).Return(nil, makeResponse(http.StatusSeeOther), nil)
|
||||
|
||||
request := makeRequest(t, http.MethodPost, "/mr/approve", nil)
|
||||
server, _ := createRouterAndApi(fakeClient{approveMergeRequest: approveMergeRequestNon200})
|
||||
server, _ := CreateRouterAndApi(client)
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
|
||||
checkNon200(t, *data, "Could not approve merge request", "/mr/approve")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user