-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pfs-116 FE only for downloading a report #3
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9f111f2
pfs-116 FE only for downloading a report
Nicholassully d5c3010
pfs-116 changing post to a get
Nicholassully 87a6803
pfs-116 renaming fields
Nicholassully cfdf923
pfs-116 pre feedback
Nicholassully b830fe6
Merge branch 'main' into pfs-116-fe-report-form
Nicholassully b3e7362
pfs-116 moved logic out of the api layer
Nicholassully a5ea7ec
pfs-116 removed creating of a date model and tidy up validation errors
Nicholassully 1cc81bd
pfs-116 adding marshal and unmarshal funcs;
Nicholassully File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockClient struct { | ||
} | ||
|
||
var ( | ||
GetDoFunc func(req *http.Request) (*http.Response, error) | ||
) | ||
|
||
func (m *MockClient) Do(req *http.Request) (*http.Response, error) { | ||
return GetDoFunc(req) | ||
} | ||
|
||
func getContext(cookies []*http.Cookie) Context { | ||
return Context{ | ||
Context: context.Background(), | ||
Cookies: cookies, | ||
XSRFToken: "abcde", | ||
} | ||
} | ||
|
||
func TestClientError(t *testing.T) { | ||
assert.Equal(t, "message", ClientError("message").Error()) | ||
} | ||
|
||
func TestStatusError(t *testing.T) { | ||
req, _ := http.NewRequest(http.MethodPost, "/some/url", nil) | ||
|
||
resp := &http.Response{ | ||
StatusCode: http.StatusTeapot, | ||
Request: req, | ||
} | ||
|
||
err := newStatusError(resp) | ||
|
||
assert.Equal(t, "POST /some/url returned 418", err.Error()) | ||
assert.Equal(t, err, err.Data()) | ||
} |
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,59 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/opg-sirius-finance-admin/internal/model" | ||
"net/http" | ||
) | ||
|
||
func (c *Client) Download(ctx Context, data model.Download) error { | ||
var body bytes.Buffer | ||
|
||
err := json.NewEncoder(&body).Encode(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := c.newBackendRequest(ctx, http.MethodGet, "/downloads", &body) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := c.http.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
switch resp.StatusCode { | ||
case http.StatusCreated: | ||
return nil | ||
|
||
case http.StatusUnauthorized: | ||
return ErrUnauthorized | ||
|
||
case http.StatusUnprocessableEntity: | ||
var v model.ValidationError | ||
if err := json.NewDecoder(resp.Body).Decode(&v); err == nil && len(v.Errors) > 0 { | ||
return model.ValidationError{Errors: v.Errors} | ||
} | ||
|
||
case http.StatusBadRequest: | ||
var badRequests model.BadRequests | ||
if err := json.NewDecoder(resp.Body).Decode(&badRequests); err != nil { | ||
return err | ||
} | ||
|
||
validationErrors := model.ValidationErrors{} | ||
for _, reason := range badRequests.Reasons { | ||
validationErrors[reason] = map[string]string{reason: reason} | ||
} | ||
|
||
return model.ValidationError{Errors: validationErrors} | ||
} | ||
|
||
return newStatusError(resp) | ||
} |
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,138 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/opg-sirius-finance-admin/internal/model" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestSubmitDownload(t *testing.T) { | ||
mockClient := &MockClient{} | ||
client, _ := NewClient(mockClient, "http://localhost:3000", "") | ||
dateOfTransaction := model.NewDate("2024-05-11") | ||
dateTo := model.NewDate("2025-06-15") | ||
dateFrom := model.NewDate("2022-07-21") | ||
|
||
data := model.Download{ | ||
ReportType: "reportType", | ||
ReportJournalType: "reportJournalType", | ||
ReportScheduleType: "reportScheduleType", | ||
ReportAccountType: "reportAccountType", | ||
ReportDebtType: "reportDebtType", | ||
DateOfTransaction: &dateOfTransaction, | ||
ToDateField: &dateTo, | ||
FromDateField: &dateFrom, | ||
Email: "[email protected]", | ||
} | ||
|
||
GetDoFunc = func(*http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: http.StatusCreated, | ||
Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
}, nil | ||
} | ||
|
||
err := client.Download(getContext(nil), data) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestSubmitDownloadUnauthorised(t *testing.T) { | ||
mockClient := &MockClient{} | ||
client, _ := NewClient(mockClient, "http://localhost:3000", "") | ||
|
||
data := model.Download{ | ||
ReportType: "reportType", | ||
ReportJournalType: "reportJournalType", | ||
ReportScheduleType: "reportScheduleType", | ||
ReportAccountType: "reportAccountType", | ||
ReportDebtType: "reportDebtType", | ||
DateOfTransaction: nil, | ||
ToDateField: nil, | ||
FromDateField: nil, | ||
Email: "[email protected]", | ||
} | ||
|
||
GetDoFunc = func(*http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: http.StatusUnauthorized, | ||
Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
}, nil | ||
} | ||
|
||
err := client.Download(getContext(nil), data) | ||
|
||
assert.Equal(t, ErrUnauthorized.Error(), err.Error()) | ||
} | ||
|
||
func TestSubmitDownloadReturnsBadRequestError(t *testing.T) { | ||
mockClient := &MockClient{} | ||
client, _ := NewClient(mockClient, "http://localhost:3000", "") | ||
|
||
data := model.Download{ | ||
ReportType: "reportType", | ||
ReportJournalType: "reportJournalType", | ||
ReportScheduleType: "reportScheduleType", | ||
ReportAccountType: "reportAccountType", | ||
ReportDebtType: "reportDebtType", | ||
DateOfTransaction: nil, | ||
ToDateField: nil, | ||
FromDateField: nil, | ||
Email: "[email protected]", | ||
} | ||
|
||
json := `{"reasons":["StartDate","EndDate"]}` | ||
|
||
r := io.NopCloser(bytes.NewReader([]byte(json))) | ||
|
||
GetDoFunc = func(*http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: http.StatusBadRequest, | ||
Body: r, | ||
}, nil | ||
} | ||
|
||
err := client.Download(getContext(nil), data) | ||
|
||
expectedError := model.ValidationError{Message: "", Errors: model.ValidationErrors{"EndDate": map[string]string{"EndDate": "EndDate"}, "StartDate": map[string]string{"StartDate": "StartDate"}}} | ||
assert.Equal(t, expectedError, err) | ||
} | ||
|
||
func TestSubmitDownloadReturnsValidationError(t *testing.T) { | ||
data := model.Download{ | ||
ReportType: "", | ||
ReportJournalType: "reportJournalType", | ||
ReportScheduleType: "reportScheduleType", | ||
ReportAccountType: "reportAccountType", | ||
ReportDebtType: "reportDebtType", | ||
DateOfTransaction: nil, | ||
ToDateField: nil, | ||
FromDateField: nil, | ||
Email: "[email protected]", | ||
} | ||
|
||
validationErrors := model.ValidationError{ | ||
Message: "Validation failed", | ||
Errors: map[string]map[string]string{ | ||
"ReportType": { | ||
"required": "Please select a report type", | ||
}, | ||
}, | ||
} | ||
responseBody, _ := json.Marshal(validationErrors) | ||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusUnprocessableEntity) | ||
_, _ = w.Write(responseBody) | ||
})) | ||
defer svr.Close() | ||
|
||
client, _ := NewClient(http.DefaultClient, svr.URL, svr.URL) | ||
|
||
err := client.Download(getContext(nil), data) | ||
expectedError := model.ValidationError{Message: "", Errors: model.ValidationErrors{"ReportType": 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package model | ||
|
||
var ReportAccountTypes = []ReportAccountType{ | ||
ReportAccountTypeAgedDebt, | ||
ReportAccountTypeUnappliedReceipts, | ||
ReportAccountTypeCustomerAgeingBuckets, | ||
ReportAccountTypeARPaidInvoiceReport, | ||
ReportAccountTypePaidInvoiceTransactionLines, | ||
ReportAccountTypeTotalReceiptsReport, | ||
ReportAccountTypeBadDebtWriteOffReport, | ||
ReportAccountTypeFeeAccrual, | ||
} | ||
|
||
type ReportAccountType int | ||
|
||
const ( | ||
ReportAccountTypeUnknown ReportAccountType = iota | ||
ReportAccountTypeAgedDebt | ||
ReportAccountTypeUnappliedReceipts | ||
ReportAccountTypeCustomerAgeingBuckets | ||
ReportAccountTypeARPaidInvoiceReport | ||
ReportAccountTypePaidInvoiceTransactionLines | ||
ReportAccountTypeTotalReceiptsReport | ||
ReportAccountTypeBadDebtWriteOffReport | ||
ReportAccountTypeFeeAccrual | ||
) | ||
|
||
func (i ReportAccountType) String() string { | ||
return i.Key() | ||
} | ||
|
||
func (i ReportAccountType) Translation() string { | ||
switch i { | ||
case ReportAccountTypeAgedDebt: | ||
return "Aged Debt" | ||
case ReportAccountTypeUnappliedReceipts: | ||
return "Unapplied Receipts" | ||
case ReportAccountTypeCustomerAgeingBuckets: | ||
return "Customer Ageing Buckets" | ||
case ReportAccountTypeARPaidInvoiceReport: | ||
return "AR Paid Invoice Report" | ||
case ReportAccountTypePaidInvoiceTransactionLines: | ||
return "Paid Invoice Transaction Lines" | ||
case ReportAccountTypeTotalReceiptsReport: | ||
return "Total Receipts Report" | ||
case ReportAccountTypeBadDebtWriteOffReport: | ||
return "Bad Debt Write-off Report" | ||
case ReportAccountTypeFeeAccrual: | ||
return "Fee Accrual" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func (i ReportAccountType) Key() string { | ||
switch i { | ||
case ReportAccountTypeAgedDebt: | ||
return "AgedDebt" | ||
case ReportAccountTypeUnappliedReceipts: | ||
return "UnappliedReceipts" | ||
case ReportAccountTypeCustomerAgeingBuckets: | ||
return "CustomerAgeingBuckets" | ||
case ReportAccountTypeARPaidInvoiceReport: | ||
return "ARPaidInvoiceReport" | ||
case ReportAccountTypePaidInvoiceTransactionLines: | ||
return "PaidInvoiceTransactionLines" | ||
case ReportAccountTypeTotalReceiptsReport: | ||
return "TotalReceiptsReport" | ||
case ReportAccountTypeBadDebtWriteOffReport: | ||
return "BadDebtWriteOffReport" | ||
case ReportAccountTypeFeeAccrual: | ||
return "FeeAccrual" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func (i ReportAccountType) Valid() bool { | ||
return i != ReportAccountTypeUnknown | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you haven't already, get https://pkg.go.dev/golang.org/x/tools/cmd/goimports for formatting. Otherwise maybe it needs updating?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you look into this?