Skip to content

Commit

Permalink
fix: Numscript: Parse monetary variables with negative amount (#415)
Browse files Browse the repository at this point in the history
fix: first
  • Loading branch information
Antoine Gelloz authored and flemzord committed Jan 25, 2023
1 parent 003f0d4 commit 3f88d83
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/buger/jsonparser v1.1.1
github.com/dgraph-io/ristretto v0.1.1
github.com/formancehq/go-libs v1.4.1
github.com/formancehq/machine v1.4.4
github.com/formancehq/machine v1.4.5
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/go-redis/redis/v8 v8.11.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/formancehq/go-libs v1.4.1 h1:rUKfUyZFq9aid+JUIqKN8Mk80Lx06Bx7AreHj+9vyTo=
github.com/formancehq/go-libs v1.4.1/go.mod h1:IK1zDIGRPi/o8sSApIc1W0VC1Y0DGdADzxvpbqlD8fk=
github.com/formancehq/machine v1.4.4 h1:CY0MOmNCOP0yO4Y1DA3ce5R/QMOLukDPfkVAwcMkPoU=
github.com/formancehq/machine v1.4.4/go.mod h1:VJgYOFY7rCzs/MyfWVOKASAf5bJHyX3v35ZmSIl2yic=
github.com/formancehq/machine v1.4.5 h1:tN7ftSmyW8WZFK0GStA/cyFyBRIX3EU02OLvYj45AJ4=
github.com/formancehq/machine v1.4.5/go.mod h1:VJgYOFY7rCzs/MyfWVOKASAf5bJHyX3v35ZmSIl2yic=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
Expand Down
35 changes: 31 additions & 4 deletions pkg/api/controllers/transaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestPostTransactions(t *testing.T) {
},
},
{
name: "script failure with invalid variable",
name: "script failure with invalid account variable",
payload: []controllers.PostTransaction{{
Script: core.Script{
Plain: `
Expand All @@ -424,10 +424,37 @@ func TestPostTransactions(t *testing.T) {
expectedStatusCode: http.StatusBadRequest,
expectedErr: sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrScriptCompilationFailed,
ErrorMessage: "[COMPILATION_FAILED] could not set variables: invalid json for variable acc of type account: value 'invalid-acc': accounts should respect pattern ^[a-zA-Z_]+[a-zA-Z0-9_:]*$",
Details: apierrors.EncodeLink("could not set variables: invalid json for variable acc of type account: value 'invalid-acc': accounts should respect pattern ^[a-zA-Z_]+[a-zA-Z0-9_:]*$"),
ErrorMessage: "[COMPILATION_FAILED] could not set variables: invalid JSON value for variable $acc of type account: value invalid-acc: accounts should respect pattern ^[a-zA-Z_]+[a-zA-Z0-9_:]*$",
Details: apierrors.EncodeLink("could not set variables: invalid JSON value for variable $acc of type account: value invalid-acc: accounts should respect pattern ^[a-zA-Z_]+[a-zA-Z0-9_:]*$"),
ErrorCodeDeprecated: apierrors.ErrScriptCompilationFailed,
ErrorMessageDeprecated: "[COMPILATION_FAILED] could not set variables: invalid json for variable acc of type account: value 'invalid-acc': accounts should respect pattern ^[a-zA-Z_]+[a-zA-Z0-9_:]*$",
ErrorMessageDeprecated: "[COMPILATION_FAILED] could not set variables: invalid JSON value for variable $acc of type account: value invalid-acc: accounts should respect pattern ^[a-zA-Z_]+[a-zA-Z0-9_:]*$",
},
},
{
name: "script failure with invalid monetary variable",
payload: []controllers.PostTransaction{{
Script: core.Script{
Plain: `
vars {
monetary $mon
}
send $mon (
source = @world
destination = @alice
)
`,
Vars: map[string]json.RawMessage{
"mon": json.RawMessage(`{"asset": "COIN","amount":-1}`),
},
},
}},
expectedStatusCode: http.StatusBadRequest,
expectedErr: sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrScriptCompilationFailed,
ErrorMessage: "[COMPILATION_FAILED] could not set variables: invalid JSON value for variable $mon of type monetary: value [COIN -1]: negative amount",
Details: apierrors.EncodeLink("could not set variables: invalid JSON value for variable $mon of type monetary: value [COIN -1]: negative amount"),
ErrorCodeDeprecated: apierrors.ErrScriptCompilationFailed,
ErrorMessageDeprecated: "[COMPILATION_FAILED] could not set variables: invalid JSON value for variable $mon of type monetary: value [COIN -1]: negative amount",
},
},
{
Expand Down

0 comments on commit 3f88d83

Please sign in to comment.