Skip to content
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

Fixed token setup error handling #249

Merged
merged 5 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions tests/tokens_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package tests

import (
"context"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"

"github.com/flow-hydraulics/flow-wallet-api/jobs"
"github.com/flow-hydraulics/flow-wallet-api/tests/internal/test"
"github.com/flow-hydraulics/flow-wallet-api/transactions"
)

func Test_TokensSetup(t *testing.T) {
cfg := test.LoadConfig(t, testConfigPath)
svc := test.GetServices(t, cfg).GetTokens()

_, testAccount, err := test.GetServices(t, cfg).GetAccounts().Create(context.Background(), true)
if err != nil {
t.Fatal(err)
}

type input struct {
sync bool
tokenName string
address string
}
type expect struct {
job *jobs.Job
tx *transactions.Transaction
error bool
}
testCases := []struct {
name string
input input
expect expect
}{
{
name: "success",
input: input{
sync: false,
tokenName: "flowToken",
nanuuki marked this conversation as resolved.
Show resolved Hide resolved
address: testAccount.Address,
},
expect: expect{
job: &jobs.Job{
Type: transactions.TransactionJobType,
State: jobs.Init,
nanuuki marked this conversation as resolved.
Show resolved Hide resolved
Error: "",
Result: "",
ExecCount: int(1),
ShouldSendNotification: true,
},
tx: &transactions.Transaction{
TransactionType: transactions.FtSetup,
ProposerAddress: testAccount.Address,
},
error: false,
},
},
{
name: "fail address not found",
input: input{
sync: false,
tokenName: "flowToken",
nanuuki marked this conversation as resolved.
Show resolved Hide resolved
address: "0x0ae53cb6e3f42a79",
},
expect: expect{
job: nil,
tx: nil,
error: true,
nanuuki marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
job, tx, err := svc.Setup(context.Background(), tc.input.sync, tc.input.tokenName, tc.input.address)
jobOpts := []cmp.Option{
nanuuki marked this conversation as resolved.
Show resolved Hide resolved
cmpopts.IgnoreFields(jobs.Job{}, "TransactionID", "ExecCount"),
cmpopts.IgnoreTypes(uuid.UUID{}, time.Time{}),
}
if diff := cmp.Diff(tc.expect.job, job, jobOpts...); diff != "" {
t.Fatalf("\n\n%s\n", diff)
}
txOpts := []cmp.Option{
cmpopts.IgnoreFields(transactions.Transaction{}, "TransactionId", "FlowTransaction"),
cmpopts.IgnoreTypes(time.Time{}),
}
if diff := cmp.Diff(tc.expect.tx, tx, txOpts...); diff != "" {
t.Fatalf("\n\n%s\n", diff)
}
if (err != nil) != tc.expect.error {
t.Fatal("error mismatch")
}
})
}
}
3 changes: 3 additions & 0 deletions tokens/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (s *Service) Setup(ctx context.Context, sync bool, tokenName, address strin
}

job, tx, err := s.transactions.Create(ctx, sync, address, token.Setup, nil, txType)
if err != nil && !strings.Contains(err.Error(), "vault exists") {
return nil, nil, err
}

if err == nil || strings.Contains(err.Error(), "vault exists") {
// Handle adding token to account in database
Expand Down