Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Ensuring corrected envelopes are validated
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Jul 5, 2023
1 parent ca1676c commit 3ebcd02
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 27 deletions.
8 changes: 3 additions & 5 deletions internal/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ type KeygenResponse struct {

// CorrectRequest is the payload used to generate a corrected document.
type CorrectRequest struct {
Data []byte `json:"data"`
Credit bool `json:"credit"`
Debit bool `json:"debit"`
Data []byte `json:"data"`
Options []byte `json:"options"`
}

// Bulk processes a stream of bulk requests.
Expand Down Expand Up @@ -229,8 +228,7 @@ func processRequest(ctx context.Context, req BulkRequest, seq int64, bulkOpts *B
ParseOptions: &ParseOptions{
Input: bytes.NewReader(bld.Data),
},
Credit: bld.Credit,
Debit: bld.Debit,
Data: bld.Options,
}
env, err := Correct(ctx, opts)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ func TestBulk(t *testing.T) {
"action": "correct",
"req_id": "asdf",
"payload": map[string]interface{}{
"data": base64.StdEncoding.EncodeToString(payload),
"data": base64.StdEncoding.EncodeToString(payload),
"options": []byte(`{"credit":true,"correction_method":"complete","corrections":["line"]}`),
},
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/correct.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Correct(ctx context.Context, opts *CorrectOptions) (interface{}, error) {
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
if err = env.Validate(); err != nil {
if err = e2.Validate(); err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
return e2, nil
Expand Down
31 changes: 29 additions & 2 deletions internal/correct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,42 @@ func TestCorrect(t *testing.T) {
Input: testFileReader(t, "testdata/success.json"),
},
Date: cal.MakeDate(2023, 4, 17),
Data: []byte(`{"credit":true,"correction_method":"complete","corrections":["line"]}`),
},
}
})
tests.Add("success with data", func(t *testing.T) interface{} {
tests.Add("error missing data", func(t *testing.T) interface{} {
return tt{
opts: &CorrectOptions{
ParseOptions: &ParseOptions{
Input: testFileReader(t, "testdata/success.json"),
},
Data: []byte(`{"issue_date":"2023-04-17","credit":true}`),
},
err: "preceding: (0: (correction_method: cannot be blank; corrections: cannot be blank.).).",
}
})

tests.Add("success just invoice", func(t *testing.T) interface{} {
return tt{
opts: &CorrectOptions{
ParseOptions: &ParseOptions{
Input: testFileReader(t, "testdata/invoice.json"),
},
Date: cal.MakeDate(2023, 4, 17),
Data: []byte(`{"credit":true,"correction_method":"complete","corrections":["line"]}`),
},
}
})
tests.Add("error just invoice", func(t *testing.T) interface{} {
return tt{
opts: &CorrectOptions{
ParseOptions: &ParseOptions{
Input: testFileReader(t, "testdata/invoice.json"),
},
Date: cal.MakeDate(2023, 4, 17),
},
err: "preceding: (0: (correction_method: cannot be blank; corrections: cannot be blank.).).",
}
})

Expand All @@ -47,7 +72,9 @@ func TestCorrect(t *testing.T) {
if tt.err == "" {
assert.Nil(t, err)
} else {
assert.EqualError(t, err, tt.err)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), tt.err)
}
}
if err != nil {
return
Expand Down
40 changes: 22 additions & 18 deletions internal/testdata/TestCorrect_success
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"discounts": [
{
"amount": "180.00",
"amount": "-180.00",
"percent": "10%",
"reason": "Special discount"
}
Expand All @@ -27,8 +27,8 @@
"price": "90.00",
"unit": "h"
},
"quantity": "20",
"sum": "1800.00",
"quantity": "-20",
"sum": "-1800.00",
"taxes": [
{
"cat": "VAT",
Expand All @@ -40,7 +40,7 @@
"percent": "15.0%"
}
],
"total": "1620.00"
"total": "-1620.00"
}
],
"payment": {
Expand All @@ -60,6 +60,10 @@
"preceding": [
{
"code": "SAMPLE-001",
"correction_method": "complete",
"corrections": [
"line"
],
"issue_date": "2022-02-01"
}
],
Expand Down Expand Up @@ -95,49 +99,49 @@
}
},
"totals": {
"payable": "1717.20",
"sum": "1620.00",
"tax": "97.20",
"payable": "-1717.20",
"sum": "-1620.00",
"tax": "-97.20",
"taxes": {
"categories": [
{
"amount": "340.20",
"amount": "-340.20",
"code": "VAT",
"rates": [
{
"amount": "340.20",
"base": "1620.00",
"amount": "-340.20",
"base": "-1620.00",
"key": "standard",
"percent": "21.0%"
}
]
},
{
"amount": "243.00",
"amount": "-243.00",
"code": "IRPF",
"rates": [
{
"amount": "243.00",
"base": "1620.00",
"amount": "-243.00",
"base": "-1620.00",
"percent": "15.0%"
}
],
"retained": true
}
],
"sum": "97.20"
"sum": "-97.20"
},
"total": "1620.00",
"total_with_tax": "1717.20"
"total": "-1620.00",
"total_with_tax": "-1717.20"
},
"type": "corrective"
},
"head": {
"dig": {
"alg": "sha256",
"val": "df3d5393b2dbcfb6cc67dfafe7aec9d6dec4135c78ca9cf2c1c6f2c19536336c"
"val": "b10481852757289928a9e28046e0866cc04fa63f83591f066bf63321a1f35819"
},
"draft": true,
"uuid": "c544855a-19ba-11ee-a150-3e7e00ce5635"
"uuid": "e5be59dc-1b17-11ee-9228-3e7e00ce5635"
}
}
136 changes: 136 additions & 0 deletions internal/testdata/TestCorrect_success_just_invoice
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"$schema": "https://gobl.org/draft-0/bill/invoice",
"code": "",
"currency": "EUR",
"customer": {
"name": "Sample Consumer",
"tax_id": {
"code": "54387763P",
"country": "ES"
}
},
"issue_date": "2023-04-17",
"lines": [
{
"discounts": [
{
"amount": "-180.00",
"percent": "10%",
"reason": "Special discount"
}
],
"i": 1,
"item": {
"name": "Development services",
"price": "90.00",
"unit": "h"
},
"quantity": "-20",
"sum": "-1800.00",
"taxes": [
{
"cat": "VAT",
"percent": "21.0%",
"rate": "standard"
},
{
"cat": "IRPF",
"percent": "15.0%"
}
],
"total": "-1620.00"
}
],
"payment": {
"instructions": {
"credit_transfer": [
{
"iban": "ES06 0128 0011 3901 0008 1391",
"name": "Bankinter"
}
],
"key": "credit-transfer"
},
"terms": {
"key": "instant"
}
},
"preceding": [
{
"code": "SAMPLE-001",
"correction_method": "complete",
"corrections": [
"line"
],
"issue_date": "2022-02-01"
}
],
"supplier": {
"addresses": [
{
"code": "23480",
"country": "ES",
"locality": "CANENA",
"num": "9",
"region": "JAÉN",
"street": "CAMÍ MADRID"
}
],
"emails": [
{
"addr": "billing@example.com"
}
],
"name": "MªF. Services",
"people": [
{
"name": {
"given": "MARIA FRANCISCA",
"surname": "MONTERO",
"surname2": "ESTEBAN"
}
}
],
"tax_id": {
"code": "58384285G",
"country": "ES"
}
},
"totals": {
"payable": "-1717.20",
"sum": "-1620.00",
"tax": "-97.20",
"taxes": {
"categories": [
{
"amount": "-340.20",
"code": "VAT",
"rates": [
{
"amount": "-340.20",
"base": "-1620.00",
"key": "standard",
"percent": "21.0%"
}
]
},
{
"amount": "-243.00",
"code": "IRPF",
"rates": [
{
"amount": "-243.00",
"base": "-1620.00",
"percent": "15.0%"
}
],
"retained": true
}
],
"sum": "-97.20"
},
"total": "-1620.00",
"total_with_tax": "-1717.20"
},
"type": "corrective"
}

0 comments on commit 3ebcd02

Please sign in to comment.