-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b21c9d2
commit 634402b
Showing
6 changed files
with
248 additions
and
14 deletions.
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,80 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConfig_ValidateConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid listen address", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg := DefaultConfig() | ||
cfg.ListenAddress = "gno.land" // doesn't follow the format | ||
|
||
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidListenAddress) | ||
}) | ||
|
||
t.Run("invalid chain ID", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg := DefaultConfig() | ||
cfg.ChainID = "" // empty | ||
|
||
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidChainID) | ||
}) | ||
|
||
t.Run("invalid send amount", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg := DefaultConfig() | ||
cfg.SendAmount = "1000goo" // invalid denom | ||
|
||
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidSendAmount) | ||
}) | ||
|
||
t.Run("invalid gas fee", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg := DefaultConfig() | ||
cfg.GasFee = "1000goo" // invalid denom | ||
|
||
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidGasFee) | ||
}) | ||
|
||
t.Run("invalid gas wanted", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg := DefaultConfig() | ||
cfg.GasWanted = "totally a number" // invalid number | ||
|
||
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidGasWanted) | ||
}) | ||
|
||
t.Run("invalid mnemonic", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg := DefaultConfig() | ||
cfg.Mnemonic = "maybe valid mnemonic" // invalid mnemonic | ||
|
||
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidMnemonic) | ||
}) | ||
|
||
t.Run("invalid num accounts", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg := DefaultConfig() | ||
cfg.NumAccounts = 0 // invalid number of accounts | ||
|
||
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidNumAccounts) | ||
}) | ||
|
||
t.Run("valid configuration", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.NoError(t, ValidateConfig(DefaultConfig())) | ||
}) | ||
} |
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,141 @@ | ||
package faucet | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/gnolang/faucet/config" | ||
"github.com/gnolang/faucet/log/noop" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFaucet_NewFaucet(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("valid config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
f, err := NewFaucet( | ||
&mockEstimator{}, | ||
&mockClient{}, | ||
WithConfig(config.DefaultConfig()), | ||
) | ||
|
||
assert.NotNil(t, f) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("invalid config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create an invalid configuration | ||
invalidCfg := config.DefaultConfig() | ||
invalidCfg.NumAccounts = 0 | ||
|
||
// Create a faucet instance with the invalid configuration | ||
f, err := NewFaucet( | ||
&mockEstimator{}, | ||
&mockClient{}, | ||
WithConfig(invalidCfg), | ||
) | ||
|
||
// Make sure the error was caught | ||
assert.Nil(t, f) | ||
assert.ErrorIs(t, err, config.ErrInvalidNumAccounts) | ||
}) | ||
|
||
t.Run("with CORS config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Example CORS config | ||
corsConfig := config.DefaultCORSConfig() | ||
corsConfig.AllowedOrigins = []string{"gno.land"} | ||
|
||
validCfg := config.DefaultConfig() | ||
validCfg.CORSConfig = corsConfig | ||
|
||
// Create a valid faucet instance | ||
// with a valid CORS configuration | ||
f, err := NewFaucet( | ||
&mockEstimator{}, | ||
&mockClient{}, | ||
WithConfig(validCfg), | ||
) | ||
|
||
assert.NotNil(t, f) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("with middlewares", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
middlewares := []Middleware{ | ||
func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Example empty middleware | ||
next.ServeHTTP(w, r) | ||
}) | ||
}, | ||
} | ||
|
||
cfg := config.DefaultConfig() | ||
cfg.CORSConfig = nil // disable CORS middleware | ||
|
||
f, err := NewFaucet( | ||
&mockEstimator{}, | ||
&mockClient{}, | ||
WithConfig(cfg), | ||
WithMiddlewares(middlewares), | ||
) | ||
|
||
require.NotNil(t, f) | ||
assert.NoError(t, err) | ||
|
||
// Make sure the middleware was set | ||
assert.Len(t, f.mux.Middlewares(), len(middlewares)) | ||
}) | ||
|
||
t.Run("with handlers", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
handlers := []Handler{ | ||
{ | ||
Pattern: "/hello", | ||
HandlerFunc: func(_ http.ResponseWriter, _ *http.Request) { | ||
// Empty handler | ||
}, | ||
}, | ||
} | ||
|
||
f, err := NewFaucet( | ||
&mockEstimator{}, | ||
&mockClient{}, | ||
WithConfig(config.DefaultConfig()), | ||
WithHandlers(handlers), | ||
) | ||
|
||
require.NotNil(t, f) | ||
assert.NoError(t, err) | ||
|
||
// Make sure the handler was set | ||
routes := f.mux.Routes() | ||
require.Len(t, routes, len(handlers)+1) // base "/" handler as well | ||
|
||
assert.Equal(t, handlers[0].Pattern, routes[1].Pattern) | ||
}) | ||
|
||
t.Run("with logger", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
f, err := NewFaucet( | ||
&mockEstimator{}, | ||
&mockClient{}, | ||
WithConfig(config.DefaultConfig()), | ||
WithLogger(noop.New()), | ||
) | ||
|
||
assert.NotNil(t, f) | ||
assert.NoError(t, err) | ||
}) | ||
} |
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