Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
canercidam committed May 15, 2023
1 parent eeafe95 commit 22532c5
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 102 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mocks:
test:
go test -v -count=1 ./... -coverprofile=coverage.out

.PHONY: coverage
coverage:
go tool cover -func=coverage.out | grep total | awk '{print substr($$3, 1, length($$3)-1)}'

Expand Down
13 changes: 0 additions & 13 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ publishes alerts about them`,
},
}

cmdFortaBatchDecode = &cobra.Command{
Use: "decode",
Short: "download a batch from IPFS and decode data",
RunE: handleFortaBatchDecode,
}

cmdFortaStatus = &cobra.Command{
Use: "status",
Short: "display statuses of node services",
Expand Down Expand Up @@ -148,7 +142,6 @@ func init() {
cmdForta.AddCommand(cmdFortaVersion)

cmdForta.AddCommand(cmdFortaBatch)
cmdFortaBatch.AddCommand(cmdFortaBatchDecode)

cmdForta.AddCommand(cmdFortaStatus)

Expand Down Expand Up @@ -176,12 +169,6 @@ func init() {
// forta run
cmdFortaRun.Flags().BoolVar(&parsedArgs.NoCheck, "no-check", false, "disable scanner registry check and just run")

// forta batch decode
cmdFortaBatchDecode.Flags().String("cid", "", "batch IPFS CID (content ID)")
cmdFortaBatchDecode.MarkFlagRequired("cid")
cmdFortaBatchDecode.Flags().String("o", "alert-batch.json", "output file name (default: alert-batch.json)")
cmdFortaBatchDecode.Flags().Bool("stdout", false, "print to stdout instead of writing to a file")

// forta status
cmdFortaStatus.Flags().String("format", StatusFormatPretty, "output formatting/encoding: pretty (default), oneline, json, csv")
cmdFortaStatus.Flags().Bool("no-color", false, "disable colors")
Expand Down
8 changes: 4 additions & 4 deletions cmd/cmd_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func handleFortaAccountAddress(cmd *cobra.Command, args []string) error {
accounts := ks.Accounts()
if len(accounts) > 1 {
redBold("You have multiple accounts. Please import your scanner account again with 'forta account import'.")
cmd.Println("Your current account addresses:")
fmt.Println("Your current account addresses:")
for _, account := range accounts {
cmd.Println(account.Address.Hex())
fmt.Println(account.Address.Hex())
}
return errors.New("multiple accounts")
}
Expand All @@ -29,7 +29,7 @@ func handleFortaAccountAddress(cmd *cobra.Command, args []string) error {
return errors.New("no accounts")
}

cmd.Println(accounts[0].Address.Hex())
fmt.Println(accounts[0].Address.Hex())
return nil
}

Expand Down Expand Up @@ -59,6 +59,6 @@ func handleFortaAccountImport(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("failed to import: %v", err)
}
cmd.Println(account.Address.Hex())
fmt.Println(account.Address.Hex())
return nil
}
47 changes: 47 additions & 0 deletions cmd/cmd_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/stretchr/testify/require"
)

func TestShowAccounts(t *testing.T) {
r := require.New(t)

dir := t.TempDir()
cfg.KeyDirPath = dir
scannerKeyStore := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

_, err := scannerKeyStore.NewAccount("Forta123")
r.NoError(err)

r.NoError(handleFortaAccountAddress(nil, nil))
}

func TestShowAccounts_NoAccounts(t *testing.T) {
r := require.New(t)

dir := t.TempDir()
cfg.KeyDirPath = dir
keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

r.Error(handleFortaAccountAddress(nil, nil))
}

func TestShowAccounts_MultipleAccounts(t *testing.T) {
r := require.New(t)

dir := t.TempDir()
cfg.KeyDirPath = dir
scannerKeyStore := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

_, err := scannerKeyStore.NewAccount("Forta123")
r.NoError(err)

_, err = scannerKeyStore.NewAccount("Forta456")
r.NoError(err)

r.Error(handleFortaAccountAddress(nil, nil))
}
82 changes: 0 additions & 82 deletions cmd/cmd_batch.go

This file was deleted.

10 changes: 10 additions & 0 deletions cmd/cmd_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"time"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/fatih/color"
"github.com/forta-network/forta-core-go/registry"
Expand Down Expand Up @@ -47,6 +48,15 @@ func handleFortaAuthorizePool(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("failed to create registry client: %v", err)
}

return authorizePoolWithRegistry(regClient, scannerKey, poolID, polygonscan, force, clean)
}

func authorizePoolWithRegistry(
regClient registry.Client,
scannerKey *keystore.Key,
poolID int64, polygonscan, force, clean bool,
) error {
regClient.SetRegistryChainID(cfg.Registry.ChainID)

scanner, err := regClient.GetPoolScanner(scannerKey.Address.Hex())
Expand Down
135 changes: 135 additions & 0 deletions cmd/cmd_tx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package cmd

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/forta-network/forta-core-go/registry"
mock_registry "github.com/forta-network/forta-core-go/registry/mocks"
"github.com/forta-network/forta-core-go/security"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

const (
testPoolID = int64(123)
)

func TestPoolAuthorization(t *testing.T) {
r := require.New(t)

ctrl := gomock.NewController(t)
regClient := mock_registry.NewMockClient(ctrl)

dir := t.TempDir()
scannerKeyStore := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

_, err := scannerKeyStore.NewAccount("Forta123")
r.NoError(err)

scannerKey, err := security.LoadKeyWithPassphrase(dir, "Forta123")
r.NoError(err)

regClient.EXPECT().SetRegistryChainID(cfg.Registry.ChainID)
regClient.EXPECT().GetPoolScanner(scannerKey.Address.Hex()).Return(nil, nil)
regClient.EXPECT().WillNewScannerShutdownPool(big.NewInt(testPoolID)).Return(false, nil)
regClient.EXPECT().GenerateScannerRegistrationSignature(gomock.Any()).Return(&registry.ScannerRegistrationInfo{}, nil)

err = authorizePoolWithRegistry(regClient, scannerKey, testPoolID, false, false, false)
r.NoError(err)
}

func TestPoolAuthorization_ScannerExists(t *testing.T) {
r := require.New(t)

ctrl := gomock.NewController(t)
regClient := mock_registry.NewMockClient(ctrl)

dir := t.TempDir()
scannerKeyStore := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

_, err := scannerKeyStore.NewAccount("Forta123")
r.NoError(err)

scannerKey, err := security.LoadKeyWithPassphrase(dir, "Forta123")
r.NoError(err)

regClient.EXPECT().SetRegistryChainID(cfg.Registry.ChainID)
regClient.EXPECT().GetPoolScanner(scannerKey.Address.Hex()).Return(&registry.Scanner{}, nil)

err = authorizePoolWithRegistry(regClient, scannerKey, testPoolID, false, false, false)
r.NoError(err)
}

func TestPoolAuthorization_Polygonscan(t *testing.T) {
r := require.New(t)

ctrl := gomock.NewController(t)
regClient := mock_registry.NewMockClient(ctrl)

dir := t.TempDir()
scannerKeyStore := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

_, err := scannerKeyStore.NewAccount("Forta123")
r.NoError(err)

scannerKey, err := security.LoadKeyWithPassphrase(dir, "Forta123")
r.NoError(err)

regClient.EXPECT().SetRegistryChainID(cfg.Registry.ChainID)
regClient.EXPECT().GetPoolScanner(scannerKey.Address.Hex()).Return(nil, nil)
regClient.EXPECT().WillNewScannerShutdownPool(big.NewInt(testPoolID)).Return(false, nil)
regClient.EXPECT().GenerateScannerRegistrationSignature(gomock.Any()).Return(&registry.ScannerRegistrationInfo{}, nil)

err = authorizePoolWithRegistry(regClient, scannerKey, testPoolID, true, false, false)
r.NoError(err)
}

func TestPoolAuthorization_GenerateForRegisteredScanner(t *testing.T) {
r := require.New(t)

ctrl := gomock.NewController(t)
regClient := mock_registry.NewMockClient(ctrl)

dir := t.TempDir()
scannerKeyStore := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

_, err := scannerKeyStore.NewAccount("Forta123")
r.NoError(err)

scannerKey, err := security.LoadKeyWithPassphrase(dir, "Forta123")
r.NoError(err)

regClient.EXPECT().SetRegistryChainID(cfg.Registry.ChainID)
regClient.EXPECT().GetPoolScanner(scannerKey.Address.Hex()).Return(&registry.Scanner{}, nil)
regClient.EXPECT().WillNewScannerShutdownPool(big.NewInt(testPoolID)).Return(false, nil)
regClient.EXPECT().GenerateScannerRegistrationSignature(gomock.Any()).Return(&registry.ScannerRegistrationInfo{}, nil)

err = authorizePoolWithRegistry(regClient, scannerKey, testPoolID, false, true, false)
r.NoError(err)
}

func TestPoolAuthorization_CleanOutput(t *testing.T) {
r := require.New(t)

ctrl := gomock.NewController(t)
regClient := mock_registry.NewMockClient(ctrl)

dir := t.TempDir()
scannerKeyStore := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)

_, err := scannerKeyStore.NewAccount("Forta123")
r.NoError(err)

scannerKey, err := security.LoadKeyWithPassphrase(dir, "Forta123")
r.NoError(err)

regClient.EXPECT().SetRegistryChainID(cfg.Registry.ChainID)
regClient.EXPECT().GetPoolScanner(scannerKey.Address.Hex()).Return(nil, nil)
regClient.EXPECT().WillNewScannerShutdownPool(big.NewInt(testPoolID)).Return(false, nil)
regClient.EXPECT().GenerateScannerRegistrationSignature(gomock.Any()).Return(&registry.ScannerRegistrationInfo{}, nil)

err = authorizePoolWithRegistry(regClient, scannerKey, testPoolID, false, false, true)
r.NoError(err)
}
3 changes: 3 additions & 0 deletions cmd/cmd_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func handleFortaVersion(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("failed to create the docker client: %v", err)
}
return handleFortaVersionWithDockerClient(dockerClient)
}

func handleFortaVersionWithDockerClient(dockerClient clients.DockerClient) error {
output, err := makeFortaVersionOutput(dockerClient)
if err != nil {
return nil
Expand Down
12 changes: 12 additions & 0 deletions cmd/cmd_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ func TestMakeFortaVersionOutput_OnlyCLI(t *testing.T) {
r.NoError(err)
r.Equal(testCliVersionOnlyOutput, output)
}

func TestHandleFortaVersion(t *testing.T) {
r := require.New(t)

ctrl := gomock.NewController(t)
dockerClient := mock_clients.NewMockDockerClient(ctrl)

dockerClient.EXPECT().GetContainerByName(gomock.Any(), config.DockerScannerContainerName).Return(nil, errors.New("some error"))

err := handleFortaVersionWithDockerClient(dockerClient)
r.NoError(err)
}
Loading

0 comments on commit 22532c5

Please sign in to comment.