-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pfs-117 moving logic from the api layer
- Loading branch information
1 parent
e192e40
commit 4eb01e6
Showing
9 changed files
with
150 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,36 +4,35 @@ import ( | |
"bytes" | ||
"encoding/json" | ||
"github.com/opg-sirius-finance-admin/internal/model" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUploadUrlSwitching(t *testing.T) { | ||
mockClient := &MockClient{} | ||
client, _ := NewClient(mockClient, "http://localhost:3000", "") | ||
|
||
// Write some content to the temp file | ||
content := strings.NewReader("something here") | ||
|
||
var capturedURL *url.URL | ||
uploadDate := model.NewDate("2025-06-15") | ||
content := []byte("file content") | ||
|
||
data := model.Upload{ | ||
ReportUploadType: "reportUploadType", | ||
UploadDate: &uploadDate, | ||
Email: "[email protected]", | ||
File: content, | ||
} | ||
|
||
GetDoFunc = func(req *http.Request) (*http.Response, error) { | ||
capturedURL = req.URL | ||
return &http.Response{ | ||
StatusCode: http.StatusCreated, | ||
Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
}, nil | ||
} | ||
|
||
err := client.Upload(getContext(nil), "", "", "", content) | ||
err := client.Upload(getContext(nil), data) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "/uploads", capturedURL.String()) | ||
} | ||
|
||
func TestSubmitUploadUnauthorised(t *testing.T) { | ||
|
@@ -44,7 +43,7 @@ func TestSubmitUploadUnauthorised(t *testing.T) { | |
|
||
client, _ := NewClient(http.DefaultClient, svr.URL, svr.URL) | ||
|
||
err := client.Upload(getContext(nil), "", "", "", nil) | ||
err := client.Upload(getContext(nil), model.Upload{}) | ||
|
||
assert.Equal(t, ErrUnauthorized.Error(), err.Error()) | ||
} | ||
|
@@ -57,7 +56,7 @@ func TestSubmitUploadReturns500Error(t *testing.T) { | |
|
||
client, _ := NewClient(http.DefaultClient, svr.URL, svr.URL) | ||
|
||
err := client.Upload(getContext(nil), "", "", "", nil) | ||
err := client.Upload(getContext(nil), model.Upload{}) | ||
|
||
assert.Equal(t, StatusError{ | ||
Code: http.StatusInternalServerError, | ||
|
@@ -83,7 +82,7 @@ func TestSubmitUploadReturnsBadRequestError(t *testing.T) { | |
}, nil | ||
} | ||
|
||
err := client.Upload(getContext(nil), "", "", "", nil) | ||
err := client.Upload(getContext(nil), model.Upload{}) | ||
|
||
expectedError := model.ValidationError{Message: "", Errors: model.ValidationErrors{"EndDate": map[string]string{"EndDate": "EndDate"}, "StartDate": map[string]string{"StartDate": "StartDate"}}} | ||
assert.Equal(t, expectedError, err) | ||
|
@@ -107,7 +106,7 @@ func TestSubmitUploadReturnsValidationError(t *testing.T) { | |
|
||
client, _ := NewClient(http.DefaultClient, svr.URL, svr.URL) | ||
|
||
err := client.Upload(getContext(nil), "", "", "", nil) | ||
err := client.Upload(getContext(nil), model.Upload{}) | ||
expectedError := model.ValidationError{Message: "", Errors: model.ValidationErrors{"ReportUploadType": map[string]string{"required": "Please select a report type"}}} | ||
assert.Equal(t, expectedError, err.(model.ValidationError)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
package model | ||
|
||
import "io" | ||
|
||
type Upload struct { | ||
ReportUploadType string `json:"reportUploadType"` | ||
UploadDate *Date `json:"uploadDate"` | ||
Email string `json:"email"` | ||
File []byte `json:"file"` | ||
} | ||
|
||
func NewUpload(reportUploadType string, uploadDate string, email string, file io.Reader) (Upload, error) { | ||
fileTransformed, err := io.ReadAll(file) | ||
if err != nil { | ||
return Upload{}, err | ||
} | ||
|
||
upload := Upload{ | ||
ReportUploadType: reportUploadType, | ||
Email: email, | ||
File: fileTransformed, | ||
} | ||
|
||
if uploadDate != "" { | ||
uploadDateFormatted := NewDate(uploadDate) | ||
upload.UploadDate = &uploadDateFormatted | ||
} | ||
|
||
return upload, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package model | ||
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"testing" | ||
) | ||
|
||
type errorReader struct{} | ||
|
||
func (e *errorReader) Read(_ []byte) (int, error) { | ||
return 0, io.ErrUnexpectedEOF | ||
} | ||
|
||
func TestNewUploadReturnsCorrectly(t *testing.T) { | ||
reportUploadType := "SomeReportType" | ||
email := "[email protected]" | ||
uploadDate := "11/05/2024" | ||
fileContent := []byte("file content") | ||
fileReader := bytes.NewReader(fileContent) | ||
|
||
// Expected result | ||
expectedDate := NewDate(uploadDate) | ||
expectedUpload := Upload{ | ||
ReportUploadType: reportUploadType, | ||
Email: email, | ||
File: fileContent, | ||
UploadDate: &expectedDate, | ||
} | ||
|
||
upload, err := NewUpload(reportUploadType, uploadDate, email, fileReader) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expectedUpload, upload) | ||
} | ||
|
||
func TestNewUploadWithNoFileReturnsCorrectly(t *testing.T) { | ||
reportUploadType := "SomeReportType" | ||
email := "[email protected]" | ||
uploadDate := "" | ||
fileContent := []byte("file content") | ||
fileReader := bytes.NewReader(fileContent) | ||
|
||
expectedUpload := Upload{ | ||
ReportUploadType: reportUploadType, | ||
Email: email, | ||
File: fileContent, | ||
UploadDate: nil, | ||
} | ||
|
||
upload, err := NewUpload(reportUploadType, uploadDate, email, fileReader) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expectedUpload, upload) | ||
} | ||
|
||
func TestNewUpload_ReturnsError(t *testing.T) { | ||
// Prepare input with an errorReader that always fails | ||
reportUploadType := "SomeReportType" | ||
email := "[email protected]" | ||
uploadDate := "11/05/2024" | ||
reader := &errorReader{} | ||
|
||
upload, err := NewUpload(reportUploadType, uploadDate, email, reader) | ||
|
||
assert.ErrorIs(t, err, io.ErrUnexpectedEOF) | ||
assert.Empty(t, upload.File) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters