Skip to content

Commit

Permalink
Test adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
TevinAdams committed Jan 9, 2025
1 parent 1f98f2f commit f340761
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
5 changes: 4 additions & 1 deletion pkg/handlers/ghcapi/documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (suite *HandlerSuite) TestGetDocumentHandler() {
}
documentPayload := showResponse.Payload

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(userUpload.Upload.Filename)

// Validate outgoing payload
suite.NoError(documentPayload.Validate(strfmt.Default))

Expand All @@ -68,7 +71,7 @@ func (suite *HandlerSuite) TestGetDocumentHandler() {
uploadPayload := documentPayload.Uploads[0]
values := url.Values{}
values.Add("response-content-type", uploader.FileTypePDF)
values.Add("response-content-disposition", "attachment; filename="+userUpload.Upload.Filename)
values.Add("response-content-disposition", "attachment; filename="+quotedFilename)
values.Add("signed", "test")
expectedURL := fmt.Sprintf("https://example.com/dir/%s?", userUpload.Upload.StorageKey) + values.Encode()
if (uploadPayload.URL).String() != expectedURL {
Expand Down
6 changes: 5 additions & 1 deletion pkg/handlers/internalapi/documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/go-openapi/strfmt"
"github.com/gofrs/uuid"
Expand Down Expand Up @@ -91,6 +92,9 @@ func (suite *HandlerSuite) TestShowDocumentHandler() {
}
documentPayload := showResponse.Payload

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(userUpload.Upload.Filename)

responseDocumentUUID := documentPayload.ID.String()
if responseDocumentUUID != documentID.String() {
t.Errorf("wrong document uuid, expected %v, got %v", documentID, responseDocumentUUID)
Expand All @@ -103,7 +107,7 @@ func (suite *HandlerSuite) TestShowDocumentHandler() {
uploadPayload := documentPayload.Uploads[0]
values := url.Values{}
values.Add("response-content-type", uploader.FileTypePDF)
values.Add("response-content-disposition", "attachment; filename="+userUpload.Upload.Filename)
values.Add("response-content-disposition", "attachment; filename="+quotedFilename)
values.Add("signed", "test")
expectedURL := fmt.Sprintf("https://example.com/dir/%s?", userUpload.Upload.StorageKey) + values.Encode()
if (uploadPayload.URL).String() != expectedURL {
Expand Down
31 changes: 25 additions & 6 deletions pkg/handlers/internalapi/uploads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
Expand Down Expand Up @@ -461,6 +462,9 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
upload := models.Upload{}
err := suite.DB().Find(&upload, createdResponse.Payload.ID)

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(upload.Filename)

suite.NoError(err)
suite.Equal("V/Q6K9rVdEPVzgKbh5cn2x4Oci4XDaG4fcG04R41Iz4=", upload.Checksum)

Expand All @@ -470,7 +474,7 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(document.ServiceMember.UserID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(upload.ID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(uploader.FileTypeExcel))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+upload.Filename))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+quotedFilename))
})

suite.Run("uploads .xlsx file", func() {
Expand All @@ -486,6 +490,9 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
upload := models.Upload{}
err := suite.DB().Find(&upload, createdResponse.Payload.ID)

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(upload.Filename)

suite.NoError(err)
suite.Equal("eRZ1Cr3Ms0692k03ftoEdqXpvd/CHcbxmhEGEQBYVdY=", upload.Checksum)

Expand All @@ -495,7 +502,7 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(document.ServiceMember.UserID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(upload.ID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(uploader.FileTypeExcelXLSX))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+upload.Filename))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+quotedFilename))
})

suite.Run("uploads weight estimator .xlsx file (full weight)", func() {
Expand All @@ -513,14 +520,17 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {

suite.NoError(err)

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(upload.Filename)

// uploaded xlsx document should now be converted to a pdf so we check for pdf instead of xlsx
suite.NotEmpty(createdResponse.Payload.ID)
suite.Contains(createdResponse.Payload.Filename, WeightEstimatorPrefix)
suite.Equal(uploader.FileTypePDF, createdResponse.Payload.ContentType)
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(document.ServiceMember.UserID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(upload.ID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(uploader.FileTypePDF))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+upload.Filename))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+quotedFilename))
})

suite.Run("uploads file for a progear document", func() {
Expand All @@ -536,6 +546,9 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
upload := models.Upload{}
err := suite.DB().Find(&upload, createdResponse.Payload.ID)

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(upload.Filename)

suite.NoError(err)
suite.Equal("/io1MRhLi2BFk9eF+lH1Ax+hyH+bPhlEK7A9/bqWlPY=", upload.Checksum)

Expand All @@ -545,7 +558,7 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(document.ServiceMember.UserID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(upload.ID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(uploader.FileTypePNG))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+upload.Filename))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+quotedFilename))
})

suite.Run("uploads file for an expense document", func() {
Expand All @@ -561,6 +574,9 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
upload := models.Upload{}
err := suite.DB().Find(&upload, createdResponse.Payload.ID)

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(upload.Filename)

suite.NoError(err)
suite.Equal("ibKT78j4CJecDXC6CbGISkqWFG5eSjCjlZJHlaFRho4=", upload.Checksum)

Expand All @@ -570,7 +586,7 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(document.ServiceMember.UserID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(upload.ID.String()))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape(uploader.FileTypeJPEG))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+upload.Filename))
suite.Contains(createdResponse.Payload.URL, url.QueryEscape("attachment; filename="+quotedFilename))
})

suite.Run("uploads file with filename characters not supported by ISO8859_1", func() {
Expand All @@ -586,8 +602,11 @@ func (suite *HandlerSuite) TestCreatePPMUploadsHandlerSuccess() {
upload := models.Upload{}
err := suite.DB().Find(&upload, createdResponse.Payload.ID)

// Double quote the filename to be able to handle filenames with commas in them
quotedFilename := strconv.Quote(upload.Filename)

filenameBuffer := make([]byte, 0)
for _, r := range upload.Filename {
for _, r := range quotedFilename {
if encodedRune, ok := charmap.ISO8859_1.EncodeRune(r); ok {
filenameBuffer = append(filenameBuffer, encodedRune)
}
Expand Down

0 comments on commit f340761

Please sign in to comment.