Skip to content

Commit

Permalink
fixed api handler lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvodi committed Aug 31, 2024
1 parent 980ba7c commit fc5350f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 77 deletions.
49 changes: 29 additions & 20 deletions internal/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/tomvodi/limepipes-plugin-api/plugin/v1/file_type"
"github.com/tomvodi/limepipes/internal/apigen/apimodel"
api_interfaces "github.com/tomvodi/limepipes/internal/apigen/interfaces"
"github.com/tomvodi/limepipes/internal/common"
"github.com/tomvodi/limepipes/internal/interfaces"
"io"
"mime/multipart"
"net/http"
"path/filepath"
)
Expand Down Expand Up @@ -62,28 +64,12 @@ func (a *apiHandler) ImportFile(c *gin.Context) {
return
}

fileReader, err := iFile.Open()
if err != nil {
c.JSON(http.StatusBadRequest,
apimodel.Error{
Message: fmt.Sprintf("failed open file %s for reading", iFile.Filename),
},
)
return
}
defer fileReader.Close()

fileData, err := io.ReadAll(fileReader)
if err != nil {
c.JSON(http.StatusInternalServerError,
fmt.Sprintf("failed reading file %s: %s", iFile.Filename, err.Error()))
return
}

fInfo, err := common.NewImportFileInfo(iFile.Filename, fType, fileData)
fInfo, err := a.createImportFileInfo(iFile, fType)
if err != nil {
c.JSON(http.StatusInternalServerError,
fmt.Sprintf("failed creating import file info for file %s: %s", iFile.Filename, err.Error()))
apimodel.Error{
Message: err.Error(),
})
return
}
_, err = a.service.GetImportFileByHash(fInfo.Hash)
Expand All @@ -109,6 +95,29 @@ func (a *apiHandler) ImportFile(c *gin.Context) {
c.JSON(http.StatusOK, importResponse)
}

func (a *apiHandler) createImportFileInfo(
iFile *multipart.FileHeader,
fType file_type.Type,
) (*common.ImportFileInfo, error) {
fileReader, err := iFile.Open()
if err != nil {
return nil, fmt.Errorf("failed open file %s for reading", iFile.Filename)
}
defer fileReader.Close()

fileData, err := io.ReadAll(fileReader)
if err != nil {
return nil, fmt.Errorf("failed reading file %s: %s", iFile.Filename, err.Error())
}

fInfo, err := common.NewImportFileInfo(iFile.Filename, fType, fileData)
if err != nil {
return nil, fmt.Errorf("failed creating import file info for file %s: %s", iFile.Filename, err.Error())
}

return fInfo, nil
}

func (a *apiHandler) importFile(
fInfo *common.ImportFileInfo,
fileExt string,
Expand Down
118 changes: 61 additions & 57 deletions internal/api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import (
"net/http/httptest"
)

type multipartRequest struct {
Fieldname string
Filename string
Content []byte
Endpoint string
HTTPMethod string
}

var _ = Describe("Api handler", func() {
utils.SetupConsoleLogger()
var c *gin.Context
Expand Down Expand Up @@ -155,13 +163,13 @@ var _ = Describe("Api handler", func() {

When("fieldname is wrong", func() {
BeforeEach(func() {
c.Request = multipartRequestForFile(
"wrongfieldname",
"test.bww",
[]byte("test file content"),
"/imports",
http.MethodPost,
)
c.Request = multipartRequestForFile(multipartRequest{
Fieldname: "wrongfieldname",
Filename: "test.bww",
Content: []byte("test file content"),
Endpoint: "/imports",
HTTPMethod: http.MethodPost,
})
})

It("should return BadRequest", func() {
Expand All @@ -172,13 +180,13 @@ var _ = Describe("Api handler", func() {

When("file has no extension", func() {
BeforeEach(func() {
c.Request = multipartRequestForFile(
"file",
"test",
[]byte("test file content"),
"/imports",
http.MethodPost,
)
c.Request = multipartRequestForFile(multipartRequest{
Fieldname: "file",
Filename: "test",
Content: []byte("test file content"),
Endpoint: "/imports",
HTTPMethod: http.MethodPost,
})
})

It("should return BadRequest", func() {
Expand All @@ -189,13 +197,13 @@ var _ = Describe("Api handler", func() {

When("file extension is not supported", func() {
BeforeEach(func() {
c.Request = multipartRequestForFile(
"file",
"test.abc",
[]byte("test file content"),
"/imports",
http.MethodPost,
)
c.Request = multipartRequestForFile(multipartRequest{
Fieldname: "file",
Filename: "test.abc",
Content: []byte("test file content"),
Endpoint: "/imports",
HTTPMethod: http.MethodPost,
})
pluginLoader.EXPECT().FileTypeForFileExtension(".abc").
Return(file_type.Type_Unknown, fmt.Errorf("file extension .abc is not supported"))
})
Expand All @@ -208,13 +216,13 @@ var _ = Describe("Api handler", func() {

When("file was already imported", func() {
BeforeEach(func() {
c.Request = multipartRequestForFile(
"file",
"test.bww",
[]byte("test file content"),
"/imports",
http.MethodPost,
)
c.Request = multipartRequestForFile(multipartRequest{
Fieldname: "file",
Filename: "test.bww",
Content: []byte("test file content"),
Endpoint: "/imports",
HTTPMethod: http.MethodPost,
})
pluginLoader.EXPECT().FileTypeForFileExtension(".bww").
Return(file_type.Type_BWW, nil)
dataService.EXPECT().GetImportFileByHash("60f5237ed4049f0382661ef009d2bc42e48c3ceb3edb6600f7024e7ab3b838f3").
Expand All @@ -229,13 +237,13 @@ var _ = Describe("Api handler", func() {

When("there is no plugin for the file extension", func() {
BeforeEach(func() {
c.Request = multipartRequestForFile(
"file",
"test.bww",
[]byte("test file content"),
"/imports",
http.MethodPost,
)
c.Request = multipartRequestForFile(multipartRequest{
Fieldname: "file",
Filename: "test.bww",
Content: []byte("test file content"),
Endpoint: "/imports",
HTTPMethod: http.MethodPost,
})
pluginLoader.EXPECT().FileTypeForFileExtension(".bww").
Return(file_type.Type_BWW, nil)
dataService.EXPECT().GetImportFileByHash("60f5237ed4049f0382661ef009d2bc42e48c3ceb3edb6600f7024e7ab3b838f3").
Expand All @@ -252,13 +260,13 @@ var _ = Describe("Api handler", func() {

When("plugin fails parsing the file", func() {
BeforeEach(func() {
c.Request = multipartRequestForFile(
"file",
"test.bww",
[]byte("test file content"),
"/imports",
http.MethodPost,
)
c.Request = multipartRequestForFile(multipartRequest{
Fieldname: "file",
Filename: "test.bww",
Content: []byte("test file content"),
Endpoint: "/imports",
HTTPMethod: http.MethodPost,
})
pluginLoader.EXPECT().FileTypeForFileExtension(".bww").
Return(file_type.Type_BWW, nil)
dataService.EXPECT().GetImportFileByHash("60f5237ed4049f0382661ef009d2bc42e48c3ceb3edb6600f7024e7ab3b838f3").
Expand All @@ -277,13 +285,13 @@ var _ = Describe("Api handler", func() {

When("plugin successfully parses the file", func() {
BeforeEach(func() {
c.Request = multipartRequestForFile(
"file",
"test.bww",
[]byte("test file content"),
"/imports",
http.MethodPost,
)
c.Request = multipartRequestForFile(multipartRequest{
Fieldname: "file",
Filename: "test.bww",
Content: []byte("test file content"),
Endpoint: "/imports",
HTTPMethod: http.MethodPost,
})
pluginLoader.EXPECT().FileTypeForFileExtension(".bww").
Return(file_type.Type_BWW, nil)
dataService.EXPECT().GetImportFileByHash("60f5237ed4049f0382661ef009d2bc42e48c3ceb3edb6600f7024e7ab3b838f3").
Expand Down Expand Up @@ -327,23 +335,19 @@ var _ = Describe("Api handler", func() {
})

func multipartRequestForFile(
fieldname string,
filename string,
content []byte,
endpoint string,
httpMethod string,
req multipartRequest,
) *http.Request {
body := new(bytes.Buffer)
mulWriter := multipart.NewWriter(body)
dataPart, err := mulWriter.CreateFormFile(fieldname, filename)
dataPart, err := mulWriter.CreateFormFile(req.Fieldname, req.Filename)
Expect(err).NotTo(HaveOccurred())

_, err = dataPart.Write(content)
_, err = dataPart.Write(req.Content)
Expect(err).NotTo(HaveOccurred())
err = mulWriter.Close()
Expect(err).NotTo(HaveOccurred())

r, err := http.NewRequest(httpMethod, endpoint, body)
r, err := http.NewRequest(req.HTTPMethod, req.Endpoint, body)
Expect(err).NotTo(HaveOccurred())
r.Header.Set("Content-Type", mulWriter.FormDataContentType())

Expand Down

0 comments on commit fc5350f

Please sign in to comment.