Files
gitlab.nvim/cmd/attachment_test.go
Harrison (Harry) Cramer 93fe3e8bd6 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.
2023-12-04 10:15:07 -05:00

67 lines
2.8 KiB
Go

package main
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
"github.com/xanzy/go-gitlab"
)
type MockAttachmentReader struct{}
func (mf MockAttachmentReader) ReadFile(path string) (io.Reader, error) {
return bytes.NewReader([]byte{}), nil
}
func uploadFile(pid interface{}, content io.Reader, filename string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectFile, *gitlab.Response, error) {
return &gitlab.ProjectFile{}, makeResponse(http.StatusOK), nil
}
func uploadFileNon200(pid interface{}, content io.Reader, filename string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectFile, *gitlab.Response, error) {
return &gitlab.ProjectFile{}, makeResponse(http.StatusSeeOther), nil
}
func uploadFileErr(pid interface{}, content io.Reader, filename string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectFile, *gitlab.Response, error) {
return nil, nil, errors.New("Some error from Gitlab")
}
func withMockFileReader(a *api) error {
reader := MockAttachmentReader{}
a.fileReader = reader
return nil
}
func TestAttachmentHandler(t *testing.T) {
t.Run("Returns 200-status response after upload", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/mr/attachment", AttachmentRequest{FilePath: "some_file_path", FileName: "some_file_name"})
router, _ := createRouterAndApi(fakeClient{uploadFile: uploadFile}, withMockFileReader)
data := serveRequest(t, router, request, AttachmentResponse{})
assert(t, data.SuccessResponse.Status, http.StatusOK)
assert(t, data.SuccessResponse.Message, "File uploaded successfully")
})
t.Run("Disallows non-POST method", func(t *testing.T) {
request := makeRequest(t, http.MethodPut, "/mr/attachment", AttachmentRequest{FilePath: "some_file_path", FileName: "some_file_name"})
router, _ := createRouterAndApi(fakeClient{uploadFile: uploadFile}, withMockFileReader)
data := serveRequest(t, router, request, ErrorResponse{})
checkBadMethod(t, *data, http.MethodPost)
})
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/mr/attachment", AttachmentRequest{FilePath: "some_file_path", FileName: "some_file_name"})
router, _ := createRouterAndApi(fakeClient{uploadFile: uploadFileErr}, withMockFileReader)
data := serveRequest(t, router, request, ErrorResponse{})
checkErrorFromGitlab(t, *data, "Could not upload some_file_name to Gitlab")
})
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/mr/attachment", AttachmentRequest{FilePath: "some_file_path", FileName: "some_file_name"})
router, _ := createRouterAndApi(fakeClient{uploadFile: uploadFileNon200}, withMockFileReader)
data := serveRequest(t, router, request, ErrorResponse{})
checkNon200(t, *data, "Could not upload some_file_name to Gitlab", "/mr/attachment")
})
}